/// <summary> /// What to do when the Check for Updates Now button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCheckForUpdates_Click(object sender, EventArgs e) { try { // Disable the Check for Updates button so it can't be clicked multiple times: btnCheckForUpdates.Enabled = false; // Launch the update checker. Since this time we're calling it on demand, the last update check and // update interval are set to values that ensure that we'll always get the update checker to run. updateChecker = new UpdateChecker.UpdateChecker(updateFeedUri, updateFeedAppName, version, this, DateTime.MinValue, 1, false); updateChecker.CheckForNewVersion(); } catch (Exception) { MessageBox.Show("I was unable to check for updates. Please try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); btnCheckForUpdates.Enabled = true; } }
/// <summary> /// What to do when the Check for Updates button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCheckForUpdates_Click(object sender, EventArgs e) { // Asbestos underpants: try { // Create a new instance of the update checker. Note that we create a new one // each time to make sure that things like the debug flag gets changed if the // checkbox is toggled. Also note that we'll hard code the last update check date // to sometime in the past (we'll subtract twice the update interval just to make // sure); this will force the update check to occur regardless of when the auto- // mattic check last occurred.. updateChecker = new UpdateChecker.UpdateChecker(updateFeedUri, updateFeedAppName, Assembly.GetExecutingAssembly().GetName().Version, this, DateTime.Now.AddDays(-(updateInterval * 2)), updateInterval, debug); // Set the text of the button to a "Please wait..." message, then disable it so // the user can't click it again: btnCheckForUpdates.Text = "Please wait..."; btnCheckForUpdates.Enabled = false; // Now initiate the update check: updateChecker.CheckForNewVersion(); } // If anything blew up, display an error message, then re-enable the Check for Updates // button: catch (Exception ex) { if (debug) { MessageBox.Show(ex.ToString(), "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("An error occurred while trying to perform the " + "update check. Please try another check later.", "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } btnCheckForUpdates.Text = btnCheckForUpdatesText; btnCheckForUpdates.Enabled = true; } }
/// <summary> /// Constructor /// </summary> public MainForm() { try { // Begin with the default initialization: InitializeComponent(); // Default to using all the available characters in the hash: cbCharTypes.SelectedIndex = 0; // Originally we bound the hash drop-down to the HashEngine Hashes enum, but that // no longer works with our cross-platform compatibility code. Instead, step // through the enumeration and convert its value to the "display" value, using // the handy method provided by HashEngine. foreach (Hashes item in Enum.GetValues(typeof(Hashes))) { cbHashes.Items.Add(HashEngine.HashEnumToDisplayHash(item)); } // Generate the hash lengths list. The idea here is to enforce the character // limit ranges depending on which hash algorithm has been selected. To do that, // we'll quickly generate each available hash and get the length of the output // string, then save all these to a Hashtable so we can quickly look it up later. hashLengths = new Hashtable(); foreach (Hashes item in Enum.GetValues(typeof(Hashes))) { string tmp = HashEngine.HashString(item, "null", 1); hashLengths.Add(item, tmp.Length); } // Default the hash selection to SHA-1. We probably ought to default this to // something stronger eventually. cbHashes.SelectedItem = HashEngine.HashEnumToDisplayHash(Hashes.SHA1); // Now put on our asbestos underpants, because now we're playing with // dynamite: try { // Attempt to open the Cryptnos registry key: if (CryptnosRegistryKeyOpen()) { // If that worked, get the state of the remember parameters value: chkRemember.Checked = (int)CryptnosSettings.GetValue("RememberParams", 1) == 1 ? true : false; // If we're remembering things, try to get any settings they've saved // from the registry so they're ready to go: if (chkRemember.Checked) { PopulateSitesDropdown(); GetLastSiteParams(); if (cbSites.Items.Count > 0) { btnForget.Enabled = true; btnForgetAll.Enabled = true; btnExport.Enabled = true; } else { btnForget.Enabled = false; btnForgetAll.Enabled = false; btnExport.Enabled = false; } chkLock.Enabled = true; } // Otherwise, set some sensible defaults and clear out the sites drop-down: else { cbSites.Items.Clear(); btnForget.Enabled = false; btnForgetAll.Enabled = false; btnExport.Enabled = false; chkLock.Enabled = false; } // If that worked, get the state of the lock parameters value: chkLock.Checked = (int)CryptnosSettings.GetValue("LockParams", 0) == 1 ? true : false; // Get the user's tooltip help preference: chkShowTooltips.Checked = (int)CryptnosSettings.GetValue("ToolTips", 1) == 1 ? true : false; // Get the user's copy to clipboard preference: chkCopyToClipboard.Checked = (int)CryptnosSettings.GetValue("CopyToClipboard", 0) == 1 ? true : false; // Get the last update check date. I'm not sure if the try/catch block is // really necessary, but I'm a belt-and-suspenders guy. Also note that // the default, whether the parse fails for the registry value isn't set, // is DateTime.MinValue, which pretty much guarantees an update check on // the first go-around. try { updateFeedLastCheck = DateTime.Parse((string)CryptnosSettings.GetValue("LastUpdateCheck", DateTime.MinValue.ToString())); } catch { updateFeedLastCheck = DateTime.MinValue; } // Get the "daily mode" flag: chkDailyMode.Checked = (int)CryptnosSettings.GetValue("DailyMode", 0) == 1 ? true : false; this.TopMost = (int)CryptnosSettings.GetValue("KeepOnTop", 0) == 1 ? true : false; // Get the disable update check flag. This used to be an "undocumented" // feature with no user interface option but I felt giving the user the // choice is always better than not. Note that the default is false, // meaning we will *not* disable the check by default. disableUpdateCheck = (int)CryptnosSettings.GetValue("DisableUpdateCheck", 0) == 1 ? true : false; // Turn debug mode on or off: debug = (int)CryptnosSettings.GetValue("DebugMode", 0) == 1 ? true : false; // Get the user's preference on whether or not the master passphrase should // be visible or not. It should be obscured by default. showMasterPassword = (int)CryptnosSettings.GetValue("ShowMasterPassword", 0) == 1 ? true : false; txtPassphrase.UseSystemPasswordChar = !showMasterPassword; // Get the user's preference of whether or not the master passphrase and // generated password should be cleared when Cryptnos loses focus: clearPasswordsOnFocusLoss = (int)CryptnosSettings.GetValue("ClearPasswordsOnFocusLoss", 0) == 1 ? true : false; // Get the encoding. Originally, Cryptnos defaulted to the system default // (Encoding.Default). However, this proved to be a problem with cross- // platform passwords. To fix the problem, we put in an elaborate check to // see if this was the very first time Cryptnos ever loaded; if it was, we // used the preferred cross-platform default (UTF-8), but if it wasn't we // used the system default. This has turned out to be rather pointless, so // we'll hard-code UTF-8 as the default from here on out. That's the best // choice for cross-platform work. If the user wants to change that, they // can do so in the settings. encoding = Encoding.GetEncoding((string)CryptnosSettings.GetValue("Encoding", Encoding.UTF8.WebName)); // Attempt to restore the window location. Since Cryptnos has a fixed size // window most of the time, we don't have to worry about size so much. But // we do want to make sure the window doesn't disappear off the screen. So // default to putting the window in the upper left quarter of the primary // screen. If the window's bottom or right edges appear off screen, adjust // the top left corner to make the entire window appear on screen. Also note // that we must set Form.StartPosition to Manual; otherwise, Windows will // put the window wherever it wants, ignoring our settings. StartPosition = FormStartPosition.Manual; int winTop = (int)CryptnosSettings.GetValue("WindowTop", Screen.PrimaryScreen.Bounds.Height / 4); int winLeft = (int)CryptnosSettings.GetValue("WindowLeft", Screen.PrimaryScreen.Bounds.Width / 4); if (winTop < 0) winTop = 0; if (winLeft < 0) winLeft = 0; if (winTop + Height > Screen.PrimaryScreen.Bounds.Height) winTop = Screen.PrimaryScreen.Bounds.Height - Height; if (winLeft + Width > Screen.PrimaryScreen.Bounds.Width) winLeft = Screen.PrimaryScreen.Bounds.Width - Width; Location = new Point(winLeft, winTop); } // If we were unable to open the registry, warn the user that something went wrong. // They can technically still use the program, but they won't be able to load or // save any parameters. else { MessageBox.Show("Cryptnos was unable to open the necessary registry keys to load and " + "save your settings for this session. You should be able to use Cryptnos if you " + "remember all of your password parameters, but you won't be able to use any saved " + "parameters and you won't be able to save any changes during this session. You " + "may need to check to see that you have the necessary permissions to modify " + "your personal registry settings with this login.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); SetDefaultSettings(); } } // If anything above blew up, set some sensible defaults: catch (Exception ex1) { if (debug) MessageBox.Show(ex1.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("An error occurred while attempting to load your Cryptnos settings from the " + "registry. You should be able to use Cryptnos if you " + "remember all of your password parameters, but you won't be able to use any saved " + "parameters and you won't be able to save any changes during this session. You " + "may need to check to see that you have the necessary permissions to modify " + "your personal registry settings with this login.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); SetDefaultSettings(); } // Turn on or off tooltip help depending on the user's save preference: toolTip1.Active = chkShowTooltips.Checked; // Set the window title to include the short version number: Text = versionShort; // Get our copyright information. It seems a bit silly to do it this way, // but this seems to be the only way to do it that I can find. We'll pull this // from the assembly so we only need to change it in one place, and it can be // automatically fetched from SVN. object[] obj = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (obj != null && obj.Length > 0) copyright = ((AssemblyCopyrightAttribute)obj[0]).Copyright; // Default the last import/export path to My Documents (or its equivalent) or, // if that fails, the current working directory: try { lastImportExportPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } catch { lastImportExportPath = Environment.CurrentDirectory; } // Test whether or not we're running under .NET or Mono: try { isMono = Type.GetType("Mono.Runtime") != null; } catch { } // Some Mono-specific setup logic: if (isMono) { // Our window setup is actually pretty lousy, and it really shows in Mono under // Linux. Enabling "daily mode" pretty much makes the app unusable, or at least // a lot more difficult. For our user's sanity, we'll disable the "daily mode" // check box for now to prevent them from accidentally enabling it. chkDailyMode.Checked = false; chkDailyMode.Enabled = false; chkDailyMode.Visible = false; } // Finally, initialize the update checker and set it to work. The update check // should occur in a separate thread, which will allow the main UI thread to // continue without any problems. The entire process *should* be transparent to // the user unless an update is actually found. if (!disableUpdateCheck) { try { updateChecker = new UpdateChecker.UpdateChecker(updateFeedUri, updateFeedAppName, Assembly.GetExecutingAssembly().GetName().Version, this, updateFeedLastCheck, updateInterval, debug); updateChecker.CheckForNewVersion(); } catch (Exception updateEx) { if (debug) MessageBox.Show(updateEx.ToString(), "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show("An error occurred while trying to perform the " + "update check. Please try another check later.", "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // If anything blew up, tell the user to restart Cryptnos: catch (Exception ex) { if (debug) MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show("Cryptnos encountered an error while trying to launch. " + "Please close the application and try to restart it.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// What to do when the Check for Updates button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCheckForUpdates_Click(object sender, EventArgs e) { // Asbestos underpants: try { // Create a new instance of the update checker. Note that we create a new one // each time to make sure that things like the debug flag gets changed if the // checkbox is toggled. Also note that we'll hard code the last update check date // to sometime in the past (we'll subtract twice the update interval just to make // sure); this will force the update check to occur regardless of when the auto- // mattic check last occurred.. updateChecker = new UpdateChecker.UpdateChecker(updateFeedUri, updateFeedAppName, Assembly.GetExecutingAssembly().GetName().Version, this, DateTime.Now.AddDays(-(updateInterval * 2)), updateInterval, debug); // Set the text of the button to a "Please wait..." message, then disable it so // the user can't click it again: btnCheckForUpdates.Text = "Please wait..."; btnCheckForUpdates.Enabled = false; // Now initiate the update check: updateChecker.CheckForNewVersion(); } // If anything blew up, display an error message, then re-enable the Check for Updates // button: catch (Exception ex) { if (debug) MessageBox.Show(ex.ToString(), "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show("An error occurred while trying to perform the " + "update check. Please try another check later.", "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error); btnCheckForUpdates.Text = btnCheckForUpdatesText; btnCheckForUpdates.Enabled = true; } }