Example #1
0
        private void AddKey_btn_Click(object sender, RoutedEventArgs e)
        {
            if (NewKeyName_tb.Text.Trim().Length == 0)
            {
                return;
            }

            try
            {
                modified = true;
                key_manager.add_key(NewKeyName_tb.Text);
                update_gui();
            }
            catch (Exception ex)
            {
                MyMessageBox.show(ex.Message, "Error");
            }
        }
Example #2
0
        private void AddBackup_btn_Click(object sender, RoutedEventArgs e)
        {
            // Backup Name has to be filled
            BackupName_tb.Text = BackupName_tb.Text.Trim();
            if (BackupName_tb.Text.Length == 0)
            {
                MyMessageBox.show("The backup name field must be filled.", "Error");
                return;
            }

            // Source directory has to exist
            string directory = Source_tb.Text.Trim();

            WindowsBackup_App.remove_ending_slash(ref directory);
            Source_tb.Text = directory;

            if (directory.Length == 0)
            {
                MyMessageBox.show("The source directory field is not filled out.", "Error");
                return;
            }

            if (Directory.Exists(directory) == false)
            {
                MyMessageBox.show("The backup source directory \"" + directory
                                  + "\" does not exist.", "Error");
                return;
            }

            // Destination, if it's a disk directory, has to exist. This directory
            // has to be unused by other backups.
            if (DestinationName_cb.SelectedIndex == 0)
            {
                // Clean up destination directory.
                directory = DestinationPath_tb.Text.Trim();
                WindowsBackup_App.remove_ending_slash(ref directory);
                DestinationPath_tb.Text = directory;

                // Make sure destination directory exists, and is not the same as source.
                if (directory.Length == 0)
                {
                    MyMessageBox.show("The backup destination directory field "
                                      + "is not filled out.", "Error");
                    return;
                }

                if (Directory.Exists(directory) == false)
                {
                    MyMessageBox.show("The backup destination directory \"" + directory
                                      + "\" does not exist.", "Error");
                    return;
                }

                if (DestinationPath_tb.Text.Equals(Source_tb.Text))
                {
                    MyMessageBox.show("The backup destination directory is the same "
                                      + "as the backup source directory.", "Error");
                    return;
                }

                // Make sure destination directory has not been used before.
                foreach (var backup in backup_manager.backups)
                {
                    if (backup is DiskBackup)
                    {
                        var disk_backup = (DiskBackup)backup;
                        if (disk_backup.destination_base.Equals(directory))
                        {
                            MyMessageBox.show("The backup destination directory \"" + directory
                                              + "\" is already being used by the backup \""
                                              + disk_backup.Name + "\" .", "Error");
                            return;
                        }
                    }
                    else if (backup is EncryptedBackup)
                    {
                        var encrypted_backup = (EncryptedBackup)backup;
                        if (encrypted_backup.destination_name.ToLower().Equals("disk") &&
                            encrypted_backup.destination_base.Equals(directory))
                        {
                            MyMessageBox.show("The backup destination directory \"" + directory
                                              + "\" is already being used by the encrypted backup \""
                                              + encrypted_backup.Name + "\" .", "Error");
                            return;
                        }
                    }
                }
            }

            // Destination, if it's a bucket, has to be readable.
            if (DestinationName_cb.SelectedIndex > 0)
            {
                string bucket = DestinationPath_tb.Text.Trim();
                DestinationPath_tb.Text = bucket;

                int index = DestinationName_cb.SelectedIndex - 1;

                // Check that the destination bucket field is filled out.
                if (bucket.Length == 0)
                {
                    MyMessageBox.show("The backup destination bucket field needs to "
                                      + "be filled out.", "Error");
                    return;
                }

                // Check that the destination bucket is readable.
                try
                {
                    cloud_backups[index].list_objects(bucket, 10);
                }
                catch
                {
                    MyMessageBox.show("Failed to read from cloud backup \""
                                      + cloud_backups[index].Name + "\\" + bucket + "\".", "Error");
                    return;
                }
            }

            // If encryption is used, check the embedded prefix.
            if (Encryption_cb.IsChecked == true)
            {
                // Make sure there is an embedded prefix.
                string prefix = EmbeddedPrefix_tb.Text.Trim();
                if (prefix.Length == 0)
                {
                    MyMessageBox.show("All encrypted backups must have an unique embedded prefix.",
                                      "Error");
                    return;
                }

                WindowsBackup_App.remove_ending_slash(ref prefix);
                EmbeddedPrefix_tb.Text = prefix;

                // Make sure the embedded prefix has no '\' character
                if (prefix.IndexOf('\\') >= 0)
                {
                    MyMessageBox.show("The embedded prefix \"" + prefix + "\" has a '\\', "
                                      + "which is not allowed.", "Error");
                    return;
                }

                // Make sure the embedded prefix is unique.
                foreach (var backup in backup_manager.backups)
                {
                    if (backup is EncryptedBackup)
                    {
                        var encrypted_backup = (EncryptedBackup)backup;
                        if (encrypted_backup.embedded_prefix.Equals(prefix))
                        {
                            MyMessageBox.show("The embedded prefix \"" + prefix
                                              + "\" is already being used by the encrypted backup \""
                                              + encrypted_backup.Name + "\".", "Error");
                            return;
                        }
                    }
                }
            }

            // Determine the key number. Generate new key if needed.
            UInt16 key_number = 0;

            if (Encryption_cb.IsChecked == true)
            {
                if (Keys_cb.SelectedIndex == 0)
                {
                    key_number = key_manager.add_key();
                }
                else
                {
                    string key_name = Keys_cb.Items[Keys_cb.SelectedIndex].ToString();
                    key_number = key_manager.get_key_number(key_name).Value;
                }
            }

            // Determine destination_name
            string destination_name = "disk"; // use lower case for storage

            if (DestinationName_cb.SelectedIndex > 0)
            {
                destination_name = cloud_backups[DestinationName_cb.SelectedIndex - 1].Name;
            }

            // Create backup
            Backup new_backup = null;

            if (Encryption_cb.IsChecked == true)
            {
                new_backup = new EncryptedBackup(Source_tb.Text, EmbeddedPrefix_tb.Text,
                                                 key_number, destination_name, DestinationPath_tb.Text, rule_lists,
                                                 BackupName_tb.Text);
            }
            else
            {
                new_backup = new DiskBackup(Source_tb.Text, DestinationPath_tb.Text,
                                            rule_lists, BackupName_tb.Text);
            }

            add_backup(new_backup);
        }