public static bool AppSettingsEmptyOrNull(AppSettings setting) { bool result = string.IsNullOrEmpty(setting.DuplicateFileAction) || string.IsNullOrEmpty(setting.ExePath) || string.IsNullOrEmpty(setting.FileExtensionFilter) || string.IsNullOrEmpty(setting.SourcePath) || string.IsNullOrEmpty(setting.TargetPath); return result; }
// Main Form Ctor... public MainForm(AppSettings appSettings) { InitializeComponent(); // Create settings group... // FormSettings contains all of the Window properties... // It does NOT contain the controls or the app configuration data... formSettings = new FormSettings(this); // Enable Auto-Save... formSettings.SaveOnClose = true; // Copy the appSettings argument into this forms appSettings property... this.appSettings = appSettings; // Set the default form properties and then update them with the last used properties... InitControls(); listViewColumnSorter = new ListViewColumnSorter(); this.lvSource.ListViewItemSorter = listViewColumnSorter; this.lvTarget.ListViewItemSorter = listViewColumnSorter; }
// Form Load... public void ConfigForm_Load(object sender, EventArgs e) { // Hide the Minimize and Maximize controls. // Disable the ability to resize the form... this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; MinimizeBox = false; MaximizeBox = false; // Hydrate the controls with the current settings... cbHideDupeFileMessage.Checked = appSettings.HideDupeMessage.GetValueOrDefault(false); tbSourceFolder.Text = appSettings.SourcePath; tbTargetFolder.Text = appSettings.TargetPath; tbFileExtensions.Text = appSettings.FileExtensionFilter; // Save the original values in the appSettings... originalAppSettings = new AppSettings { DuplicateFileAction = appSettings.DuplicateFileAction, ExePath = appSettings.ExePath, FileExtensionFilter = appSettings.FileExtensionFilter, HideDupeMessage = appSettings.HideDupeMessage, SourcePath = appSettings.SourcePath, TargetPath = appSettings.TargetPath }; // Check to see if the Duplicate File Action has ever been set... if (appSettings.DuplicateFileAction == null) { // If we get here, the dupe action has never been set... cbHideDupeFileMessage.Visible = false; grpDupeHandeling.Enabled = true; rbOverwrite.Enabled = rbRename.Enabled = rbSkip.Enabled = rbCancel.Enabled = true; rbOverwrite.Checked = rbRename.Checked = rbSkip.Checked = rbCancel.Checked = false; } else { switch (appSettings.DuplicateFileAction) { case "Overwrite": rbOverwrite.Checked = true; break; case "Rename": rbRename.Checked = true; break; case "Skip": rbSkip.Checked = true; break; case "Cancel": default: { rbCancel.Checked = true; break; } } // END_SWITCH } // Set the applications exe path... if (Helper.AppSettingsEmptyOrNull(appSettings)) { FileInfo exePath = new FileInfo("Push.exe"); appSettings.ExePath = exePath.DirectoryName; } // Force ConfigForm validation... // This causes all required controls to be 'marked' with a '!' icon... ValidateConfigForm(); }
// OK, Cancel & 'X' Event Handler... private void ConfigForm_FormClosing(Object sender, FormClosingEventArgs e) { if (DialogResult != DialogResult.Cancel) { //------------------------------------------------------------- // If we get here, the user did not select Cancel... // Warn user when "*.*" is used for the File Filter. // Provide an option to change the setting. // Use Message Box with 2x options... if (tbFileExtensions.Text.Contains("*.*")) { DialogResult dialogResult = MessageBox.Show("The *.* file extension is capable of doing severe damage to your PC,\n"+ "including DELETING System Files necessary to run Windows.\n\n"+ "Users are strongly encouraged to change this setting.\n\n"+ "Press OK to Continue.\n"+ "Press Cancel to Change Filter", "Warning: File Extension Setting", MessageBoxButtons.OKCancel); if (dialogResult == DialogResult.Cancel) { e.Cancel = true; return; } } appSettings.HideDupeMessage = cbHideDupeFileMessage.Checked; appSettings.ShowDetails = appSettings.ShowDetails.GetValueOrDefault(true); return; } //----------------------------------------------------------------- // If we get here, the user wants to discard all entered values... if (Helper.AppSettingsEmptyOrNull(originalAppSettings)) { // If we get here, we are aborting the first-run, settings configuration... DialogResult dialogResult = MessageBox.Show( this, "Select Cancel to abort Application Setup\n\n"+ "Select Retry to return to Application Setup", "Cancel Push Application Setup", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation); if (dialogResult == DialogResult.Cancel) { // If we get here, the user wants to exit the configuration... return; } else { // If we get here, the user wants to finish configuration... e.Cancel = true; return; } } // END_IF_RESULT = CANCEL //----------------------------------------------------------------- // If were get here, the user wants to discard the settings they just // made and restory the orginal settings... // Restore the original appSettings appSettings = originalAppSettings; return; }
// File Extensions... public static List<string> LoadFileExtensions(AppSettings appSettings) { /* NOTE: * The FOUR Linq queries below could be componded into one large query * but it would make the code harder to read and debug. * Performance is not dramatically affected. */ string[] delimiters = new string[] { ";", "|", ":", ",", " " }; // Split the FileExtensionFilter string into an array of strings. string[] fefArray = appSettings.FileExtensionFilter .Split(delimiters, StringSplitOptions.None) .ToArray(); // Trim all leading and trailing spaces in each element... fefArray = fefArray.Select(fef => fef.Trim()).ToArray(); // Scan for duplicate filters with Linq Query... fefArray = fefArray.Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); // Strip any empty strings... fefArray = fefArray.Where(r => !string.IsNullOrEmpty(r)).ToArray(); // Check for "*.*" filter. Discard all other filters since *.* rules... if (ContainsFilterStarDotStar(fefArray)) fefArray = new string[] { "*.*" }; return new List<string>(fefArray); }
private bool ValidateAppSettings(AppSettings appSettings) { if (Helper.AppSettingsEmptyOrNull(appSettings)) // Invalid return false; // If we get here, there is data in appSettings... // Validate the data using the existing ConfigForm validation code... // Verify the ExePath... if (!Directory.Exists(appSettings.ExePath)) { appSettings.ExePath = null; // Invalid return false; } // NOTE: We are not going to show the form. We are only going to use the // validation code. ConfigForm configFormDialog = new ConfigForm(); configFormDialog.appSettings = appSettings; configFormDialog.ConfigForm_Load(null, null); // ValidateConfigForm return: True = valid || False = invalid; bool isValid = configFormDialog.ValidateConfigForm(); // Make sure the form object is completely cleaned up... configFormDialog.Close(); configFormDialog.Dispose(); // true = invalid || false = valid return isValid; }
// Configuration Dialog... private void LoadConfigurationDialog() { string s = string.Empty; ConfigForm configDialog = new ConfigForm(); // Copy the current settings into the Configuration form... configDialog.appSettings = appSettings; configDialog.StartPosition = FormStartPosition.CenterParent; if (configDialog.ShowDialog(this) == DialogResult.OK) s = "OK"; else s = "Cancel"; // Copy settings... appSettings = configDialog.appSettings; configDialog.Dispose(); }
private void InitControls() { if (!ValidateAppSettings(appSettings)) { ConfigForm configFormDialog = new ConfigForm(); // Copy the current settings into the Configuration form... configFormDialog.appSettings = appSettings; configFormDialog.StartPosition = FormStartPosition.CenterParent; configFormDialog.Text = "Push Application Setup"; if (configFormDialog.ShowDialog(this) == DialogResult.Cancel) { // If we get here, exit the application imeadiatly... this.Close(); return; } // Copy settings... appSettings = configFormDialog.appSettings; configFormDialog.Dispose(); } // Build the Ignore File regular expression pattern... ignorePattern = BuildIgnorePattern(); // Init Controls... lblStatus1_1.Text = string.Empty; lblStatus1_2.Text = string.Empty; lblStatus2_2.Text = string.Empty; lblStatus1_1.Visible = false; lblStatus1_2.Visible = false; lblStatus2_2.Visible = false; toolStripProgressBar.Visible = false; toolStripLblProgress.Visible = false; // Update the form properties to the last used... UpdateControls(); }