From 1c2de3b73c0d7a028bef0bc815231e430ae33c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Sat, 2 Mar 2024 00:13:38 +0100 Subject: [PATCH] Implement import/Export settings, reset WingetUI (fix #1423) --- .../Interface/Pages/SettingsPage.xaml.cs | 67 +++++++++++++------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/src/wingetui/Interface/Pages/SettingsPage.xaml.cs b/src/wingetui/Interface/Pages/SettingsPage.xaml.cs index 16aeff8db..0061dba2e 100644 --- a/src/wingetui/Interface/Pages/SettingsPage.xaml.cs +++ b/src/wingetui/Interface/Pages/SettingsPage.xaml.cs @@ -8,6 +8,7 @@ using ModernWindow.Interface.Widgets; using ModernWindow.PackageEngine.Classes; using ModernWindow.Structures; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; @@ -309,47 +310,71 @@ public int GetHwnd() private void OpenWelcomeWizard(object sender, Interface.Widgets.ButtonCardEventArgs e) { - // TODO: Implement } - private async void ImportSettings(object sender, Interface.Widgets.ButtonCardEventArgs e) + private void ImportSettings(object sender, Interface.Widgets.ButtonCardEventArgs e) { - FileOpenPicker openPicker = new(); + var picker = new Pickers.FileOpenPicker(Tools.App.MainWindow.GetWindowHandle()); + var file = picker.Show(new List { "*.json" }); - openPicker.ViewMode = PickerViewMode.List; - openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; - WinRT.Interop.InitializeWithWindow.Initialize(openPicker, (IntPtr)GetHwnd()); - - openPicker.FileTypeFilter.Add(".conf"); - - StorageFile file = await openPicker.PickSingleFileAsync(); - if (file != null) + if (file != string.Empty) { - // TODO: Import Settings + ResetWingetUI(sender, e); + var settings = JsonConvert.DeserializeObject>(File.ReadAllText(file)); + foreach(var entry in settings) + Tools.SetSettingsValue(entry.Key, entry.Value); + GeneralSettingsExpander.ShowRestartRequiredBanner(); } } private async void ExportSettings(object sender, Interface.Widgets.ButtonCardEventArgs e) { + try + { + var picker = new Pickers.FileSavePicker(Tools.App.MainWindow.GetWindowHandle()); + var file = picker.Show(new List { "*.json" }, "WingetUI Settings.json"); + + if (file != String.Empty) + { + Tools.App.MainWindow.ShowLoadingDialog(Tools.Translate("Please wait...")); + + var IgnoredSettings = new string[] { "OperationHistory", "CurrentSessionToken", "OldWindowGeometry" }; + + Dictionary settings = new(); + foreach (var path in Directory.EnumerateFiles(CoreData.WingetUIDataDirectory)) + { + if (Path.GetFileName(path).Contains('.') || IgnoredSettings.Contains(Path.GetFileName(path))) + continue; + settings.Add(Path.GetFileName(path), await File.ReadAllTextAsync(path)); + } - FileSavePicker savePicker = new(); - savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; - WinRT.Interop.InitializeWithWindow.Initialize(savePicker, (IntPtr)GetHwnd()); - savePicker.FileTypeChoices.Add(Tools.Translate("WingetUI Settings File"), new List() { ".conf" }); - savePicker.SuggestedFileName = Tools.Translate("Exported Settings"); + await File.WriteAllTextAsync(file, JsonConvert.SerializeObject(settings)); - StorageFile file = await savePicker.PickSaveFileAsync(); - if (file != null) + Tools.App.MainWindow.HideLoadingDialog(); + } + } + catch (Exception ex) { - // TODO: Export settings + Tools.App.MainWindow.HideLoadingDialog(); + AppTools.Log(ex); } } private void ResetWingetUI(object sender, Interface.Widgets.ButtonCardEventArgs e) { - // TODO: Reset Settings + try + { + foreach (var path in Directory.EnumerateFiles(CoreData.WingetUIDataDirectory)) + { + File.Delete(path); + } + } + catch (Exception ex) + { + AppTools.Log(ex); + } GeneralSettingsExpander.ShowRestartRequiredBanner(); }