From 2b4965246896bcdd8352ed1707da7dd13512e759 Mon Sep 17 00:00:00 2001 From: BornToBeRoot Date: Tue, 17 Apr 2018 23:06:47 +0200 Subject: [PATCH 1/3] overwrite true/false added --- Source/NETworkManager/App.xaml.cs | 6 ++++- .../Models/Settings/SettingsManager.cs | 25 ++++++++----------- .../ViewModels/SettingsSettingsViewModel.cs | 7 ++++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Source/NETworkManager/App.xaml.cs b/Source/NETworkManager/App.xaml.cs index 2cb3dcd66a..266c622cb6 100644 --- a/Source/NETworkManager/App.xaml.cs +++ b/Source/NETworkManager/App.xaml.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading; using System.Windows; +using NETworkManager.Properties; namespace NETworkManager { @@ -90,10 +91,13 @@ protected override void OnSessionEnding(SessionEndingCancelEventArgs e) } private void Application_Exit(object sender, ExitEventArgs e) - { + { // Save settings, when the application is normally closed if (!_singleInstanceClose && !ImportExportManager.ForceRestart && !CommandLineManager.Current.Help) { + // Save local settings (custom settings path in AppData/Local) + Settings.Default.Save(); + if (SettingsManager.Current.SettingsChanged) // This will also create the "Settings" folder, if it does not exist SettingsManager.Save(); diff --git a/Source/NETworkManager/Models/Settings/SettingsManager.cs b/Source/NETworkManager/Models/Settings/SettingsManager.cs index 31fb394d04..57d611f545 100644 --- a/Source/NETworkManager/Models/Settings/SettingsManager.cs +++ b/Source/NETworkManager/Models/Settings/SettingsManager.cs @@ -74,12 +74,7 @@ public static string GetSettingsLocation() if (GetIsPortable()) return GetPortableSettingsLocation(); - string settingsLocation = GetCustomSettingsLocation(); - - if (!string.IsNullOrEmpty(settingsLocation) && Directory.Exists(settingsLocation)) - return settingsLocation; - - return GetDefaultSettingsLocation(); + return GetSettingsLocationNotPortable(); } public static string GetSettingsLocationNotPortable() @@ -133,12 +128,12 @@ public static void Save() Current.SettingsChanged = false; } - public static Task MoveSettingsAsync(string sourceLocation, string targedLocation) + public static Task MoveSettingsAsync(string sourceLocation, string targedLocation, bool overwrite) { - return Task.Run(() => MoveSettings(sourceLocation, targedLocation)); + return Task.Run(() => MoveSettings(sourceLocation, targedLocation, overwrite)); } - private static void MoveSettings(string sourceLocation, string targedLocation) + private static void MoveSettings(string sourceLocation, string targedLocation, bool overwrite) { string[] sourceFiles = Directory.GetFiles(sourceLocation); @@ -146,7 +141,7 @@ private static void MoveSettings(string sourceLocation, string targedLocation) Directory.CreateDirectory(targedLocation); foreach (string file in sourceFiles) - File.Copy(file, Path.Combine(targedLocation, Path.GetFileName(file)), true); + File.Copy(file, Path.Combine(targedLocation, Path.GetFileName(file)), overwrite); // Delete the old files foreach (string file in sourceFiles) @@ -157,23 +152,23 @@ private static void MoveSettings(string sourceLocation, string targedLocation) Directory.Delete(sourceLocation); } - public static Task MakePortableAsync(bool isPortable) + public static Task MakePortableAsync(bool isPortable, bool overwrite) { - return Task.Run(() => MakePortable(isPortable)); + return Task.Run(() => MakePortable(isPortable, overwrite)); } - public static void MakePortable(bool isPortable) + public static void MakePortable(bool isPortable, bool overwrite) { if (isPortable) { - MoveSettings(GetSettingsLocationNotPortable(), GetPortableSettingsLocation()); + MoveSettings(GetSettingsLocationNotPortable(), GetPortableSettingsLocation(), overwrite); // After moving the files, set the indicator that the settings are now portable File.Create(GetIsPortableFilePath()); } else { - MoveSettings(GetPortableSettingsLocation(), GetSettingsLocationNotPortable()); + MoveSettings(GetPortableSettingsLocation(), GetSettingsLocationNotPortable(), overwrite); // Remove the indicator after moving the files... File.Delete(GetIsPortableFilePath()); diff --git a/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs b/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs index b4c3c3c53e..9c4ce719ab 100644 --- a/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs +++ b/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs @@ -333,10 +333,13 @@ private async void ChangeSettingsAction() { MovingFiles = true; + // Check if there are any file... + //string[] filesInNewFolder = Directory.GetFiles(LocationSelectedPath); + // Try moving files (permissions, file is in use...) try { - await SettingsManager.MoveSettingsAsync(SettingsManager.GetSettingsLocation(), LocationSelectedPath); + await SettingsManager.MoveSettingsAsync(SettingsManager.GetSettingsLocation(), LocationSelectedPath, true); Properties.Settings.Default.Settings_CustomSettingsLocation = LocationSelectedPath; @@ -441,7 +444,7 @@ private async void MakePortable(bool isPortable) // Try moving files (permissions, file is in use...) try { - await SettingsManager.MakePortableAsync(isPortable); + await SettingsManager.MakePortableAsync(isPortable, true); Properties.Settings.Default.Settings_CustomSettingsLocation = string.Empty; LocationSelectedPath = SettingsManager.GetSettingsLocationNotPortable(); From bcd2bd193dde7440fa188b7bc16900ac53c17952 Mon Sep 17 00:00:00 2001 From: BornToBeRoot Date: Sun, 22 Apr 2018 05:19:15 +0200 Subject: [PATCH 2/3] Dialog added, ask to overwrite, move and restart or cancel --- Source/NETworkManager/MainWindow.xaml | 2 +- .../Models/Settings/SettingsManager.cs | 6 ++ .../Localization/Resources.de-DE.xaml | 6 ++ .../Localization/Resources.en-US.xaml | 8 +- Source/NETworkManager/SettingsViewManager.cs | 2 +- .../ViewModels/SettingsSettingsViewModel.cs | 84 ++++++++++++++++--- .../Views/SettingsImportExportView.xaml | 38 ++++----- 7 files changed, 114 insertions(+), 32 deletions(-) diff --git a/Source/NETworkManager/MainWindow.xaml b/Source/NETworkManager/MainWindow.xaml index c1b1e2e48a..549241181f 100644 --- a/Source/NETworkManager/MainWindow.xaml +++ b/Source/NETworkManager/MainWindow.xaml @@ -626,7 +626,7 @@ - + diff --git a/Source/NETworkManager/Models/Settings/SettingsManager.cs b/Source/NETworkManager/Models/Settings/SettingsManager.cs index 57d611f545..bdc9094bf5 100644 --- a/Source/NETworkManager/Models/Settings/SettingsManager.cs +++ b/Source/NETworkManager/Models/Settings/SettingsManager.cs @@ -141,7 +141,13 @@ private static void MoveSettings(string sourceLocation, string targedLocation, b Directory.CreateDirectory(targedLocation); foreach (string file in sourceFiles) + { + // Skip if file exists and user don't want to overwrite it + if (!overwrite && File.Exists(file)) + continue; + File.Copy(file, Path.Combine(targedLocation, Path.GetFileName(file)), overwrite); + } // Delete the old files foreach (string file in sourceFiles) diff --git a/Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml b/Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml index ae4f0ed175..d80b8d4a22 100644 --- a/Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml +++ b/Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml @@ -104,6 +104,7 @@ IP-Scanner PuTTY Gruppe bearbeiten + Überschreiben? NETworkManager @@ -391,6 +392,7 @@ Neue Sitzung Gitter GitHub + Einstellungen im Zielordner überschreiben?\n\nWenn Sie auf "Verschieben & neu starten" klicken, werden die restlichen Dateien kopiert und die Anwendung danach mit den neuen Einstellungen neu gestartet! Wie installiere ich RDP 8.1 unter Windows 7 / Server 2008 R2? @@ -452,6 +454,10 @@ Auf Updates prüfen PuTTY konfigurieren DNS-Cache leeren + Ja + Nein + Überschreiben + Verschieben & neu starten Ausschneiden diff --git a/Source/NETworkManager/Resources/Localization/Resources.en-US.xaml b/Source/NETworkManager/Resources/Localization/Resources.en-US.xaml index e72b070370..7139cbc936 100644 --- a/Source/NETworkManager/Resources/Localization/Resources.en-US.xaml +++ b/Source/NETworkManager/Resources/Localization/Resources.en-US.xaml @@ -104,7 +104,8 @@ IP Scanner PuTTY Edit group - + Overwrite? + NETworkManager A powerful tool for managing networks and troubleshoot network problems! @@ -391,6 +392,7 @@ New session Gitter GitHub + Overwrite settings in the destination folder?\n\nIf you click "Move & Restart", the remaining files will be copied and the application will be restarted with the new settings! How to install RDP 8.1 on Windows 7/Server 2008 R2 @@ -452,6 +454,10 @@ Check for updates Configure PuTTY Flush DNS cache + Yes + No + Overwrite + Move & Restart Cut diff --git a/Source/NETworkManager/SettingsViewManager.cs b/Source/NETworkManager/SettingsViewManager.cs index a502552296..f1304578b8 100644 --- a/Source/NETworkManager/SettingsViewManager.cs +++ b/Source/NETworkManager/SettingsViewManager.cs @@ -20,7 +20,7 @@ public static List List new SettingsViewInfo(Name.Language, new PackIconMaterial() { Kind = PackIconMaterialKind.Flag}, Group.General), new SettingsViewInfo(Name.HotKeys, new PackIconOcticons() { Kind = PackIconOcticonsKind.Keyboard }, Group.General), new SettingsViewInfo(Name.Autostart, new PackIconMaterial() { Kind = PackIconMaterialKind.Power }, Group.General), - new SettingsViewInfo(Name.Settings, new PackIconOcticons() { Kind = PackIconOcticonsKind.Settings }, Group.General), + new SettingsViewInfo(Name.Settings, new PackIconModern() { Kind = PackIconModernKind.Settings }, Group.General), new SettingsViewInfo(Name.Update, new PackIconMaterial() {Kind = PackIconMaterialKind.Download }, Group.General), new SettingsViewInfo(Name.ImportExport, new PackIconMaterial() { Kind = PackIconMaterialKind.Import}, Group.General), diff --git a/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs b/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs index 9c4ce719ab..9735680ecb 100644 --- a/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs +++ b/Source/NETworkManager/ViewModels/SettingsSettingsViewModel.cs @@ -329,17 +329,75 @@ public ICommand ChangeSettingsCommand get { return new RelayCommand(p => ChangeSettingsAction()); } } + // Check if a file(name) is a settings file + private bool FilesContainsSettingsFiles(string[] files) + { + foreach (string file in files) + { + string fileName = Path.GetFileName(file); + + if (SettingsManager.GetSettingsFileName() == fileName) + return true; + + if (NetworkInterfaceProfileManager.ProfilesFileName == fileName) + return true; + + if (IPScannerProfileManager.ProfilesFileName == fileName) + return true; + + if (PortScannerProfileManager.ProfilesFileName == fileName) + return true; + + if (RemoteDesktopSessionManager.SessionsFileName == fileName) + return true; + + if (PuTTYSessionManager.SessionsFileName == fileName) + return true; + + if (WakeOnLANClientManager.ClientsFileName == fileName) + return true; + } + + return false; + } + private async void ChangeSettingsAction() { MovingFiles = true; + bool overwrite = false; + bool forceRestart = false; + + // Check if there are any settings files in the folder... + if (FilesContainsSettingsFiles(Directory.GetFiles(LocationSelectedPath))) + { + MetroDialogSettings settings = AppearanceManager.MetroDialog; + + settings.AffirmativeButtonText = LocalizationManager.GetStringByKey("String_Button_Overwrite"); + settings.NegativeButtonText = LocalizationManager.GetStringByKey("String_Button_Cancel"); + settings.FirstAuxiliaryButtonText = LocalizationManager.GetStringByKey("String_Button_MoveAndRestart"); + settings.DefaultButtonFocus = MessageDialogResult.FirstAuxiliary; - // Check if there are any file... - //string[] filesInNewFolder = Directory.GetFiles(LocationSelectedPath); + MessageDialogResult result = await dialogCoordinator.ShowMessageAsync(this, LocalizationManager.GetStringByKey("String_Header_Overwrite"),LocalizationManager.GetStringByKey("String_OverwriteSettingsInTheDestinationFolder"), MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, AppearanceManager.MetroDialog); + + if (result == MessageDialogResult.Negative) + { + MovingFiles = false; + return; + } + else if (result == MessageDialogResult.Affirmative) + { + overwrite = true; + } + else if (result == MessageDialogResult.FirstAuxiliary) + { + forceRestart = true; + } + } // Try moving files (permissions, file is in use...) try { - await SettingsManager.MoveSettingsAsync(SettingsManager.GetSettingsLocation(), LocationSelectedPath, true); + await SettingsManager.MoveSettingsAsync(SettingsManager.GetSettingsLocation(), LocationSelectedPath, overwrite); Properties.Settings.Default.Settings_CustomSettingsLocation = LocationSelectedPath; @@ -348,12 +406,18 @@ private async void ChangeSettingsAction() } catch (Exception ex) { - await dialogCoordinator.ShowMessageAsync(this, Application.Current.Resources["String_Header_Error"] as string, ex.Message, MessageDialogStyle.Affirmative, AppearanceManager.MetroDialog); + await dialogCoordinator.ShowMessageAsync(this, LocalizationManager.GetStringByKey("String_Header_Error") as string, ex.Message, MessageDialogStyle.Affirmative, AppearanceManager.MetroDialog); } LocationSelectedPath = string.Empty; LocationSelectedPath = Properties.Settings.Default.Settings_CustomSettingsLocation; + if (forceRestart) + { + SettingsManager.ForceRestart = true; + CloseAction(); + } + MovingFiles = false; } @@ -376,20 +440,20 @@ public async void ResetSettingsAction() { MetroDialogSettings settings = AppearanceManager.MetroDialog; - settings.AffirmativeButtonText = Application.Current.Resources["String_Button_Continue"] as string; - settings.NegativeButtonText = Application.Current.Resources["String_Button_Cancel"] as string; + settings.AffirmativeButtonText = LocalizationManager.GetStringByKey("String_Button_Continue"); + settings.NegativeButtonText = LocalizationManager.GetStringByKey("String_Button_Cancel"); settings.DefaultButtonFocus = MessageDialogResult.Affirmative; - string message = Application.Current.Resources["String_SelectedSettingsAreReset"] as string; + string message = LocalizationManager.GetStringByKey("String_SelectedSettingsAreReset"); if (ResetEverything || ResetApplicationSettings) { - message += Environment.NewLine + Environment.NewLine + string.Format("* {0}", Application.Current.Resources["String_TheSettingsLocationIsNotAffected"] as string); - message += Environment.NewLine + string.Format("* {0}", Application.Current.Resources["String_ApplicationIsRestartedAfterwards"] as string); + message += Environment.NewLine + Environment.NewLine + string.Format("* {0}", LocalizationManager.GetStringByKey("String_TheSettingsLocationIsNotAffected")); + message += Environment.NewLine + string.Format("* {0}", LocalizationManager.GetStringByKey("String_ApplicationIsRestartedAfterwards")); } - if (await dialogCoordinator.ShowMessageAsync(this, Application.Current.Resources["String_Header_AreYouSure"] as string, message, MessageDialogStyle.AffirmativeAndNegative, settings) != MessageDialogResult.Affirmative) + if (await dialogCoordinator.ShowMessageAsync(this, LocalizationManager.GetStringByKey("String_Header_AreYouSure"), message, MessageDialogStyle.AffirmativeAndNegative, settings) != MessageDialogResult.Affirmative) return; bool forceRestart = false; diff --git a/Source/NETworkManager/Views/SettingsImportExportView.xaml b/Source/NETworkManager/Views/SettingsImportExportView.xaml index 0d6daf2ff5..3544be979e 100644 --- a/Source/NETworkManager/Views/SettingsImportExportView.xaml +++ b/Source/NETworkManager/Views/SettingsImportExportView.xaml @@ -96,19 +96,9 @@ - - - - - - - - - - - - - + + + @@ -117,8 +107,8 @@ - - + + @@ -127,8 +117,8 @@ - - + + @@ -137,6 +127,16 @@ + + + + + + + + + +