Settings backup and restore (#20551)

* Merge and conflict resolution

* Messages good, backup/restore algo better.

* Start of "GetExportVerion"

* fixed spelling

* New backup/restore mode working.

* Rename a project

* Removed test project

* Switch to text.json

* Renamed BackupAndSync to BackupAndRestore

* Added IgnoredPTRunSettings and full merge

* Restored "fixed" settings that change for no reason

* Various UI updates.

* speling

* Some cleanup and zip support.

* Merge and clean

* code clean up

* code clean up

* code clean up

* Smarter settings compare and merge.

* config based file include/exclude

* Removed some "words"

* Code clean up

* cleanup

* cleanup

* cleanup

* cleanup

* fixed spelling.

* Fixed clean up 1

* more clean up

* Trying to add ptb as an OK word

* Some UI updates.

* UI tweaks and PR review items.

* UI tweaks

* Merge conflicts resolved.

* Added CurrentSettingMatchText

* PR review updates.

* Removed weird file.

* Review updates and fixes

* More UI tweaks.

* UI tweaks

* Set default backup location to "%USERPROFILE%\\Documents\\PowerToys\\Backup"

* settings ui tweaks

* Added ExpanderContentSettingStyle

* fix missing config file

* fix missing config file, part 2

* update ui, cleanup cope

* update ui, cleanup code - Part2

* update method comments

* code cleanup and adjust Backup message time

* fix changing backup location on empty Regsitry

* fix select location - part 2

* location input box min-width

* remove lastRestoreDate from ViewModel

* Code or backup timing, and error handling.

* Should fix file/folder name crash.

* Progress to instance class for backup/restore

* Persist backup status state, added refresh button.

* Better auto check for settings status

* Some UI/text updates.

* Clean up

* Added prefix for "General_Settings" to resources

* Code review updates.

* Code review changes.

* Changed to FolderPicker per review

* Fixed issue with early delete of cleanup.

* Testing issues with FolderPicker

* Removed WinForm req and fixed win10 issue.

* Review update.

* Review changes.

Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
This commit is contained in:
Jeff Lord
2022-10-13 03:41:21 -04:00
committed by GitHub
parent 79e037eef4
commit ee904ae1b1
17 changed files with 1981 additions and 53 deletions

View File

@@ -1,31 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\Version.props" />
<Import Project="..\..\Version.props" />
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<Version>$(Version).0</Version>
<Authors>Microsoft Corporation</Authors>
<Product>PowerToys</Product>
<Description>PowerToys Settings UI Library</Description>
<AssemblyName>PowerToys.Settings.UI.Lib</AssemblyName>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<Version>$(Version).0</Version>
<Authors>Microsoft Corporation</Authors>
<Product>PowerToys</Product>
<Description>PowerToys Settings UI Library</Description>
<AssemblyName>PowerToys.Settings.UI.Lib</AssemblyName>
<UseWindowsForms>false</UseWindowsForms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
<None Include="backup_restore_settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@@ -139,5 +139,30 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
return _settingsPath.GetSettingsPath(powertoy, fileName);
}
/// <summary>
/// Method <c>BackupSettings</c> Mostly a wrapper for SettingsBackupAndRestoreUtils.BackupSettings
/// </summary>
public static (bool success, string message, string severity, bool lastBackupExists) BackupSettings()
{
var settingsBackupAndRestoreUtilsX = SettingsBackupAndRestoreUtils.Instance;
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils._settingsPath.GetSettingsPath(string.Empty, string.Empty));
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtilsX.GetSettingsBackupAndRestoreDir();
return settingsBackupAndRestoreUtilsX.BackupSettings(appBasePath, settingsBackupAndRestoreDir, false);
}
/// <summary>
/// Method <c>RestoreSettings</c> Mostly a wrapper for SettingsBackupAndRestoreUtils.RestoreSettings
/// </summary>
public static (bool success, string message, string severity) RestoreSettings()
{
var settingsBackupAndRestoreUtilsX = SettingsBackupAndRestoreUtils.Instance;
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils._settingsPath.GetSettingsPath(string.Empty, string.Empty));
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtilsX.GetSettingsBackupAndRestoreDir();
return settingsBackupAndRestoreUtilsX.RestoreSettings(appBasePath, settingsBackupAndRestoreDir);
}
}
}

View File

@@ -69,6 +69,11 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
try
{
if (LastCheckedDate == null)
{
return string.Empty;
}
long seconds = long.Parse(LastCheckedDate, CultureInfo.CurrentCulture);
var date = DateTimeOffset.FromUnixTimeSeconds(seconds).UtcDateTime;
return date.ToLocalTime().ToString(CultureInfo.CurrentCulture);

View File

@@ -4,8 +4,13 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
@@ -21,6 +26,20 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
public ButtonClickCommand CheckForUpdatesEventHandler { get; set; }
public object ResourceLoader { get; set; }
private Action HideBackupAndRestoreMessageAreaAction { get; set; }
private Action<int> DoBackupAndRestoreDryRun { get; set; }
public ButtonClickCommand BackupConfigsEventHandler { get; set; }
public ButtonClickCommand RestoreConfigsEventHandler { get; set; }
public ButtonClickCommand RefreshBackupStatusEventHandler { get; set; }
public ButtonClickCommand SelectSettingBackupDirEventHandler { get; set; }
public ButtonClickCommand RestartElevatedButtonEventHandler { get; set; }
public ButtonClickCommand UpdateNowButtonEventHandler { get; set; }
@@ -41,11 +60,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private IFileSystemWatcher _fileWatcher;
public GeneralViewModel(ISettingsRepository<GeneralSettings> settingsRepository, string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func<string, int> updateTheme, Func<string, int> ipcMSGCallBackFunc, Func<string, int> ipcMSGRestartAsAdminMSGCallBackFunc, Func<string, int> ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "", Action dispatcherAction = null)
private Func<Task<string>> PickSingleFolderDialog { get; }
private SettingsBackupAndRestoreUtils settingsBackupAndRestoreUtils = SettingsBackupAndRestoreUtils.Instance;
public GeneralViewModel(ISettingsRepository<GeneralSettings> settingsRepository, string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func<string, int> updateTheme, Func<string, int> ipcMSGCallBackFunc, Func<string, int> ipcMSGRestartAsAdminMSGCallBackFunc, Func<string, int> ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "", Action dispatcherAction = null, Action hideBackupAndRestoreMessageAreaAction = null, Action<int> doBackupAndRestoreDryRun = null, Func<Task<string>> pickSingleFolderDialog = null, object resourceLoader = null)
{
CheckForUpdatesEventHandler = new ButtonClickCommand(CheckForUpdatesClick);
RestartElevatedButtonEventHandler = new ButtonClickCommand(RestartElevated);
UpdateNowButtonEventHandler = new ButtonClickCommand(UpdateNowClick);
BackupConfigsEventHandler = new ButtonClickCommand(BackupConfigsClick);
SelectSettingBackupDirEventHandler = new ButtonClickCommand(SelectSettingBackupDir);
RestoreConfigsEventHandler = new ButtonClickCommand(RestoreConfigsClick);
RefreshBackupStatusEventHandler = new ButtonClickCommand(RefreshBackupStatusEventHandlerClick);
HideBackupAndRestoreMessageAreaAction = hideBackupAndRestoreMessageAreaAction;
DoBackupAndRestoreDryRun = doBackupAndRestoreDryRun;
PickSingleFolderDialog = pickSingleFolderDialog;
ResourceLoader = resourceLoader;
// To obtain the general settings configuration of PowerToys if it exists, else to create a new file and return the default configurations.
if (settingsRepository == null)
@@ -92,6 +123,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_startup = GeneralSettingsConfig.Startup;
_autoDownloadUpdates = GeneralSettingsConfig.AutoDownloadUpdates;
_isElevated = isElevated;
_runElevated = GeneralSettingsConfig.RunElevated;
@@ -127,6 +159,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private bool _isNewVersionDownloading;
private bool _isNewVersionChecked;
private bool _settingsBackupRestoreMessageVisible;
private string _settingsBackupMessage;
private string _backupRestoreMessageSeverity;
// Gets or sets a value indicating whether run powertoys on start-up.
public bool Startup
{
@@ -253,6 +289,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public string SettingsBackupAndRestoreDir
{
get
{
return settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
}
set
{
if (settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir() != value)
{
SettingsBackupAndRestoreUtils.SetRegSettingsBackupAndRestoreItem("SettingsBackupAndRestoreDir", value);
NotifyPropertyChanged();
}
}
}
public int ThemeIndex
{
get
@@ -313,6 +366,155 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public string LastSettingsBackupDate
{
get
{
try
{
var manifest = settingsBackupAndRestoreUtils.GetLatestSettingsBackupManifest();
if (manifest != null)
{
if (manifest["CreateDateTime"] != null)
{
if (DateTime.TryParse(manifest["CreateDateTime"].ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var theDateTime))
{
return theDateTime.ToString("G", CultureInfo.CurrentCulture);
}
else
{
Logger.LogError("Failed to parse time from backup");
return GetResourceString("General_SettingsBackupAndRestore_FailedToParseTime");
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupTime");
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
}
catch (Exception e)
{
Logger.LogError("Error getting LastSettingsBackupDate", e);
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupTime");
}
}
}
public string CurrentSettingMatchText
{
get
{
try
{
var results = settingsBackupAndRestoreUtils.GetLastBackupSettingsResults();
var resultText = string.Empty;
if (!results.lastRan.HasValue)
{
// not ran since started.
return GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsNoChecked"); // "Current Settings Unknown";
}
else
{
if (results.success)
{
if (results.lastBackupExists)
{
// if true, it means a backup would have been made
resultText = GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsDiffer"); // "Current Settings Differ";
}
else
{
// would have done the backup, but there also was not an existing one there.
resultText = GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
}
else
{
if (results.hadError)
{
// if false and error we don't really know
resultText = GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsUnknown"); // "Current Settings Unknown";
}
else
{
// if false, it means a backup would not have been needed/made
resultText = GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsMatch"); // "Current Settings Match";
}
}
return $"{resultText} {GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsStatusAt")} {results.lastRan.Value.ToLocalTime().ToString("G", CultureInfo.CurrentCulture)}";
}
}
catch (Exception e)
{
Logger.LogError("Error getting CurrentSettingMatchText", e);
return string.Empty;
}
}
}
public string LastSettingsBackupSource
{
get
{
try
{
var manifest = settingsBackupAndRestoreUtils.GetLatestSettingsBackupManifest();
if (manifest != null)
{
if (manifest["BackupSource"] != null)
{
if (manifest["BackupSource"].ToString().Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase))
{
return GetResourceString("General_SettingsBackupAndRestore_ThisMachine");
}
else
{
return manifest["BackupSource"].ToString();
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupSource");
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
}
catch (Exception e)
{
Logger.LogError("Error getting LastSettingsBackupSource", e);
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupSource");
}
}
}
public string LastSettingsBackupFileName
{
get
{
try
{
var fileName = settingsBackupAndRestoreUtils.GetLatestBackupFileName();
return !string.IsNullOrEmpty(fileName) ? fileName : GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
catch (Exception e)
{
Logger.LogError("Error getting LastSettingsBackupFileName", e);
return string.Empty;
}
}
}
public UpdatingSettings.UpdatingState PowerToysUpdatingState
{
get
@@ -389,6 +591,30 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public bool SettingsBackupRestoreMessageVisible
{
get
{
return _settingsBackupRestoreMessageVisible;
}
}
public string BackupRestoreMessageSeverity
{
get
{
return _backupRestoreMessageSeverity;
}
}
public string SettingsBackupMessage
{
get
{
return _settingsBackupMessage;
}
}
public bool IsDownloadAllowed
{
get
@@ -397,13 +623,114 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null, bool reDoBackupDryRun = true)
{
// Notify UI of property change
OnPropertyChanged(propertyName);
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outsettings.ToString());
if (reDoBackupDryRun && DoBackupAndRestoreDryRun != null)
{
DoBackupAndRestoreDryRun(500);
}
}
/// <summary>
/// Method <c>SelectSettingBackupDir</c> opens folder browser to select a backup and retore location.
/// </summary>
private async void SelectSettingBackupDir()
{
var currentDir = settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
var newPath = await PickSingleFolderDialog();
if (!string.IsNullOrEmpty(newPath))
{
SettingsBackupAndRestoreDir = newPath;
NotifyAllBackupAndRestoreProperties();
}
}
private void RefreshBackupStatusEventHandlerClick()
{
DoBackupAndRestoreDryRun(0);
}
/// <summary>
/// Method <c>RestoreConfigsClick</c> starts the restore.
/// </summary>
private void RestoreConfigsClick()
{
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir))
{
SelectSettingBackupDir();
}
var results = SettingsUtils.RestoreSettings();
_backupRestoreMessageSeverity = results.severity;
if (!results.success)
{
_settingsBackupRestoreMessageVisible = true;
_settingsBackupMessage = GetResourceString(results.message);
NotifyAllBackupAndRestoreProperties();
HideBackupAndRestoreMessageAreaAction();
}
else
{
// make sure not to do NotifyPropertyChanged here, else it will persist the configs from memory and
// undo the settings restore.
SettingsBackupAndRestoreUtils.SetRegSettingsBackupAndRestoreItem("LastSettingsRestoreDate", DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture));
Restart();
}
}
/// <summary>
/// Method <c>BackupConfigsClick</c> starts the backup.
/// </summary>
private void BackupConfigsClick()
{
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir))
{
SelectSettingBackupDir();
}
var results = SettingsUtils.BackupSettings();
_settingsBackupRestoreMessageVisible = true;
_backupRestoreMessageSeverity = results.severity;
_settingsBackupMessage = GetResourceString(results.message);
// now we do a dry run to get the results for "setting match"
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils.GetSettingsFilePath());
settingsBackupAndRestoreUtils.BackupSettings(appBasePath, settingsBackupAndRestoreDir, true);
NotifyAllBackupAndRestoreProperties();
HideBackupAndRestoreMessageAreaAction();
}
public void NotifyAllBackupAndRestoreProperties()
{
NotifyPropertyChanged(nameof(LastSettingsBackupDate), false);
NotifyPropertyChanged(nameof(LastSettingsBackupSource), false);
NotifyPropertyChanged(nameof(LastSettingsBackupFileName), false);
NotifyPropertyChanged(nameof(CurrentSettingMatchText), false);
NotifyPropertyChanged(nameof(SettingsBackupMessage), false);
NotifyPropertyChanged(nameof(BackupRestoreMessageSeverity), false);
NotifyPropertyChanged(nameof(SettingsBackupRestoreMessageVisible), false);
}
// callback function to launch the URL to check for updates.
@@ -435,6 +762,36 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
Process.Start(new ProcessStartInfo(Helper.GetPowerToysInstallationFolder() + "\\PowerToys.exe") { Arguments = "powertoys://update_now/" });
}
/// <summary>
/// Class <c>GetResourceString</c> gets a localized text.
/// </summary>
/// <remarks>
/// To do: see if there is a betting way to do this, there should be. It does allow us to return missing localization in a way that makes it obvious they were missed.
/// </remarks>
public string GetResourceString(string resource)
{
if (ResourceLoader != null)
{
var type = ResourceLoader.GetType();
MethodInfo methodInfo = type.GetMethod("GetString");
object classInstance = Activator.CreateInstance(type, null);
object[] parametersArray = new object[] { resource };
var result = (string)methodInfo.Invoke(ResourceLoader, parametersArray);
if (string.IsNullOrEmpty(result))
{
return resource.ToUpperInvariant() + "!!!";
}
else
{
return result;
}
}
else
{
return resource;
}
}
public void RequestUpdateCheckedDate()
{
GeneralSettingsConfig.CustomActionName = "request_update_state_date";
@@ -455,6 +812,36 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
SendRestartAsAdminConfigMSG(customaction.ToString());
}
/// <summary>
/// Class <c>Restart</c> begin a restart and signal we want to maintain elevation
/// </summary>
/// <remarks>
/// Other restarts either raised or lowered elevation
/// </remarks>
public void Restart()
{
GeneralSettingsConfig.CustomActionName = "restart_maintain_elevation";
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfig);
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
var dataToSend = customaction.ToString();
dataToSend = JsonSerializer.Serialize(new { action = new { general = new { action_name = "restart_maintain_elevation" } } });
SendRestartAsAdminConfigMSG(dataToSend);
}
/// <summary>
/// Class <c>HideBackupAndRestoreMessageArea</c> hides the backup/restore message area
/// </summary>
/// <remarks>
/// We want to have it go away after a short period.
/// </remarks>
public void HideBackupAndRestoreMessageArea()
{
_settingsBackupRestoreMessageVisible = false;
NotifyAllBackupAndRestoreProperties();
}
public void RefreshUpdatingState()
{
object oLock = new object();

View File

@@ -0,0 +1,46 @@
{
"RestartAfterRestore": true,
"IncludeFiles": [
"*Keyboard Manager\\default.json",
"*settings.json",
"*FancyZones\\layout-hotkeys.json",
"*FancyZones\\layout-templates.json"
],
"IgnoreFiles": [
"*PowerToys\\log_settings.json",
"*PowerToys\\oobe_settings.json"
],
"IgnoredPTRunSettings": [
{
"Id": "525995402BEF4A8CA860D92F6D108092",
"Names": [
"IconPathDark",
"IconPathLight"
]
},
{
"Id": "5D69806A5A474115821C3E4C56B9C793",
"Names": [
"Description"
]
}
],
"IgnoredSettings": {
"backup-restore_settings.json": [
"RestartAfterRestore"
],
"settings.json": [
"powertoys_version"
],
"PowerToys Run\\Settings\\PowerToysRunSettings.json": [
"WindowLeft",
"WindowTop",
"ActivateTimes"
],
"PowerToys Run\\Settings\\Plugins\\Microsoft.Plugin.Program\\ProgramPluginSettings.json": [
"LastIndexTime",
"WindowTop",
"ActivateTimes"
]
}
}