Exemplo n.º 1
0
        private async Task ExportSettings()
        {
            FileSavePicker filePicker = new FileSavePicker();

            filePicker.FileTypeChoices.Add("Zip File", new[] { ".zip" });
            filePicker.SuggestedFileName = $"Files_{App.AppVersion}";

            StorageFile file = await filePicker.PickSaveFileAsync();

            if (file != null)
            {
                try
                {
                    var zipFolder = await ZipStorageFolder.FromStorageFileAsync(file);

                    if (zipFolder == null)
                    {
                        return;
                    }
                    var localFolderPath = ApplicationData.Current.LocalFolder.Path;
                    // Export user settings
                    var userSettings = await zipFolder.CreateFileAsync(Constants.LocalSettings.UserSettingsFileName, CreationCollisionOption.ReplaceExisting);

                    string exportSettings = (string)UserSettingsService.ExportSettings();
                    await userSettings.WriteTextAsync(exportSettings);

                    // Export bundles
                    var bundles = await zipFolder.CreateFileAsync(Constants.LocalSettings.BundlesSettingsFileName, CreationCollisionOption.ReplaceExisting);

                    string exportBundles = (string)BundlesSettingsService.ExportSettings();
                    await bundles.WriteTextAsync(exportBundles);

                    // Export pinned items
                    var pinnedItems = await BaseStorageFile.GetFileFromPathAsync(Path.Combine(localFolderPath, Constants.LocalSettings.SettingsFolderName, App.SidebarPinnedController.JsonFileName));

                    await pinnedItems.CopyAsync(zipFolder, pinnedItems.Name, NameCollisionOption.ReplaceExisting);

                    // Export terminals config
                    var terminals = await BaseStorageFile.GetFileFromPathAsync(Path.Combine(localFolderPath, Constants.LocalSettings.SettingsFolderName, App.TerminalController.JsonFileName));

                    await terminals.CopyAsync(zipFolder, terminals.Name, NameCollisionOption.ReplaceExisting);

                    // Export file tags list and DB
                    var fileTagsList = await zipFolder.CreateFileAsync(Constants.LocalSettings.FileTagSettingsFileName, CreationCollisionOption.ReplaceExisting);

                    string exportTags = (string)FileTagsSettingsService.ExportSettings();
                    await fileTagsList.WriteTextAsync(exportTags);

                    var fileTagsDB = await zipFolder.CreateFileAsync(Path.GetFileName(FileTagsHelper.FileTagsDbPath), CreationCollisionOption.ReplaceExisting);

                    string exportTagsDB = FileTagsHelper.DbInstance.Export();
                    await fileTagsDB.WriteTextAsync(exportTagsDB);
                }
                catch (Exception ex)
                {
                    App.Logger.Warn(ex, "Error exporting settings");
                }
            }
        }
Exemplo n.º 2
0
        private async Task ImportSettings()
        {
            FileOpenPicker filePicker = new FileOpenPicker();

            filePicker.FileTypeFilter.Add(".zip");

            StorageFile file = await filePicker.PickSingleFileAsync();

            if (file != null)
            {
                try
                {
                    var zipFolder = await ZipStorageFolder.FromStorageFileAsync(file);

                    if (zipFolder == null)
                    {
                        return;
                    }
                    var localFolderPath = ApplicationData.Current.LocalFolder.Path;
                    var settingsFolder  = await StorageFolder.GetFolderFromPathAsync(Path.Combine(localFolderPath, Constants.LocalSettings.SettingsFolderName));

                    // Import user settings
                    var userSettingsFile = await zipFolder.GetFileAsync(Constants.LocalSettings.UserSettingsFileName);

                    string importSettings = await userSettingsFile.ReadTextAsync();

                    UserSettingsService.ImportSettings(importSettings);
                    // Import bundles
                    var bundles = await zipFolder.GetFileAsync(Constants.LocalSettings.BundlesSettingsFileName);

                    string importBundles = await bundles.ReadTextAsync();

                    BundlesSettingsService.ImportSettings(importBundles);
                    // Import pinned items
                    var pinnedItems = await zipFolder.GetFileAsync(App.SidebarPinnedController.JsonFileName);

                    await pinnedItems.CopyAsync(settingsFolder, pinnedItems.Name, NameCollisionOption.ReplaceExisting);

                    await App.SidebarPinnedController.ReloadAsync();

                    // Import terminals config
                    var terminals = await zipFolder.GetFileAsync(App.TerminalController.JsonFileName);

                    await terminals.CopyAsync(settingsFolder, terminals.Name, NameCollisionOption.ReplaceExisting);

                    // Import file tags list and DB
                    var fileTagsList = await zipFolder.GetFileAsync(Constants.LocalSettings.FileTagSettingsFileName);

                    string importTags = await fileTagsList.ReadTextAsync();

                    FileTagsSettingsService.ImportSettings(importTags);
                    var fileTagsDB = await zipFolder.GetFileAsync(Path.GetFileName(FileTagsHelper.FileTagsDbPath));

                    string importTagsDB = await fileTagsDB.ReadTextAsync();

                    FileTagsHelper.DbInstance.Import(importTagsDB);
                    // Import layout preferences and DB
                    var layoutPrefsDB = await zipFolder.GetFileAsync(Path.GetFileName(FolderSettingsViewModel.LayoutSettingsDbPath));

                    string importPrefsDB = await layoutPrefsDB.ReadTextAsync();

                    FolderSettingsViewModel.DbInstance.Import(importPrefsDB);
                }
                catch (Exception ex)
                {
                    App.Logger.Warn(ex, "Error importing settings");
                    UIHelpers.CloseAllDialogs();
                    await DialogDisplayHelper.ShowDialogAsync("SettingsImportErrorTitle".GetLocalized(), "SettingsImportErrorDescription".GetLocalized());
                }
            }
        }