/// <summary> /// Handler that execut /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnButtonNewPasswordClicked(object sender, RoutedEventArgs e) { // Asks the user for the new password var askWin = new TextinputWindow( Lang.config_newpw_title, Lang.config_newpw_title, OnSubmitPassword, null, Lang.config_newpw_yes, Lang.config_newpw_no ); // Opens the window askWin.ShowDialog(); // Executes once the user submits a new password void OnSubmitPassword(string password) { // Sets the new password this.password = password; // Enables the save button this.buttonSave.IsEnabled = true; } }
/// <summary> /// Opens a serias of windows that ask the user for a password to open a config file /// </summary> /// <param name="OnReceive">Callback that executes once the config got loaded successfull. Contains a bool that says if the config has been created new (true) or if the config has been loaded and therewas existed (false). Contains a string that represents the config password, used to de/encrypt the config.</param> /// <param name="OnCancel">Callback that executes once the user cancels opening of the config</param> /// <returns>Null if the user cancled the config-request; otherwise the loaded or newly created config</returns> public static void GetConfigFromUser(Action <bool, Config, string> OnReceive, Action OnCancel) { log.Debug("Starting to request the config (From the user)"); // Checks if a config file exists if (!File.Exists(PLCA.CONFIG_PATH)) { log.Debug("Config does not exist, asking to create a new one."); // Creates a new window to ask the user for further instructions var askNew = new YesNoWindow( Lang.config_notexisting_title, OnCreateNewConfig, OnCancel, Lang.config_notexisting_yes, Lang.config_notexisting_no, Lang.config_notexisting_title, Lang.config_notexisting_info ); // Opens the window askNew.ShowDialog(); return; } // Creates the window to ask for the config-password var askPass = new TextinputWindow( Lang.config_pass_title, Lang.config_pass_title, OnReceivePassword, OnCancel, Lang.config_pass_ok, Lang.config_pass_cancle); // Opens the window askPass.ShowDialog(); // Executes once the user has submitted a password void OnReceivePassword(string password) { try { log .Debug("Received password, creating new config.") .Critical("Password="******"The previous config could be decrypted, but is currupted") .Critical("Raw previous content=" + e.RawInput); // The config could be decrypted but is currupted // Creates a new window to ask the user for further instructions var askNew = new YesNoWindow( Lang.config_currupted_title, OnCreateNewConfig, OnCancel, Lang.config_currupted_yes, Lang.config_currupted_no, Lang.config_currupted_title, Lang.config_currupted_info, () => Clipboard.SetText(e.RawInput), Lang.config_currepted_copy ); // Opens the window askNew.ShowDialog(); } catch (ConfigInvalidKeyException) { log.Debug("User has specified an invalid password."); // Creates a window to inform the user var ackWin = new AcknowledgmentWindow( Lang.config_wrongpw_title, Lang.config_wrongpw_title, () => GetConfigFromUser(OnReceive, OnCancel), Lang.config_wrongpw_retry ); // Opens the window ackWin.ShowDialog(); } catch (Exception e) { log .Debug("Unknown error occurred (Maybe a file error with permissions?)") .Critical(e); // Creates a window to inform the user var ackWin = new AcknowledgmentWindow( Lang.config_loaderr_title, e.Message, OnCancel, Lang.config_loaderr_close ); // Opens the window ackWin.ShowDialog(); } } // Executes once the user wants to create a new config (Because the old one got destroyed) void OnCreateNewConfig() { log.Debug("Asking for the password of the new config"); // Asks for the new password var askPass2 = new TextinputWindow( Lang.config_new_pass_title, Lang.config_new_pass_text, OnSubmitNewPassword, OnCancel, Lang.config_pass_ok, Lang.config_pass_cancle ); // Shows the window askPass2.ShowDialog(); } // Executes once the user has decided the new password void OnSubmitNewPassword(string password) { // Creates a config with the default parameters Config cfg = new Config { Host = string.Empty, Port = 0, PrivateKey = new RSAParameters() { D = new byte[0], DP = new byte[0], DQ = new byte[0], Exponent = new byte[0], InverseQ = new byte[0], Modulus = new byte[0], P = new byte[0], Q = new byte[0] } }; try { log .Debug("Saving new config") .Critical("New Password="******"Successfully saved the new config"); // Returns the new config OnReceive(true, cfg, password); } catch (Exception e) { log .Debug("Error saving the new config. Maybe a file-error with permissions?") .Critical(e); // Creates the warning window var ackWin = new AcknowledgmentWindow( Lang.config_new_error, e.Message, OnCancel, Lang.config_new_error_button ); // Opens the new window ackWin.ShowDialog(); } } }