2020-04-08 15:19:00 +08:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
2020-10-22 03:32:53 +08:00
using System.IO ;
2020-11-03 01:33:43 +08:00
using System.IO.Abstractions ;
2020-03-25 10:55:02 +08:00
using System.Text.Json ;
2020-10-23 00:45:48 +08:00
using Microsoft.PowerToys.Settings.UI.Library.Interfaces ;
using Microsoft.PowerToys.Settings.UI.Library.Utilities ;
2020-03-25 10:55:02 +08:00
2020-10-23 00:45:48 +08:00
namespace Microsoft.PowerToys.Settings.UI.Library
2020-03-25 10:55:02 +08:00
{
2020-09-22 01:14:44 +08:00
public class SettingsUtils : ISettingsUtils
2020-03-25 10:55:02 +08:00
{
2020-04-18 06:25:08 +08:00
private const string DefaultFileName = "settings.json" ;
2020-04-20 21:03:26 +08:00
private const string DefaultModuleName = "" ;
2020-11-03 01:33:43 +08:00
private readonly IFile _file ;
private readonly ISettingsPath _settingsPath ;
2020-04-18 06:25:08 +08:00
2020-11-03 01:33:43 +08:00
public SettingsUtils ( )
: this ( new FileSystem ( ) )
2020-09-09 01:04:17 +08:00
{
}
2020-11-03 01:33:43 +08:00
public SettingsUtils ( IFileSystem fileSystem )
: this ( fileSystem ? . File , new SettingPath ( fileSystem ? . Directory , fileSystem ? . Path ) )
2020-04-04 10:02:38 +08:00
{
}
2020-11-03 01:33:43 +08:00
public SettingsUtils ( IFile file , ISettingsPath settingPath )
2020-04-04 10:02:38 +08:00
{
2020-11-03 01:33:43 +08:00
_file = file ? ? throw new ArgumentNullException ( nameof ( file ) ) ;
_settingsPath = settingPath ;
2020-09-22 01:14:44 +08:00
}
2020-11-03 01:33:43 +08:00
public bool SettingsExists ( string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-09-22 01:14:44 +08:00
{
2020-11-03 01:33:43 +08:00
var settingsPath = _settingsPath . GetSettingsPath ( powertoy , fileName ) ;
return _file . Exists ( settingsPath ) ;
2020-04-04 10:02:38 +08:00
}
2020-11-03 01:33:43 +08:00
public void DeleteSettings ( string powertoy = "" )
2020-04-04 10:02:38 +08:00
{
2020-11-03 01:33:43 +08:00
_settingsPath . DeleteSettings ( powertoy ) ;
2020-04-04 10:02:38 +08:00
}
2021-01-14 20:14:29 +08:00
public T GetSettings < T > ( string powertoy = DefaultModuleName , string fileName = DefaultFileName )
where T : ISettingsConfig , new ( )
{
if ( ! SettingsExists ( powertoy , fileName ) )
{
throw new FileNotFoundException ( ) ;
}
2021-03-10 20:16:46 +08:00
// Given the file already exists, to deserialize the file and read its content.
2021-01-14 20:14:29 +08:00
T deserializedSettings = GetFile < T > ( powertoy , fileName ) ;
// If the file needs to be modified, to save the new configurations accordingly.
if ( deserializedSettings . UpgradeSettingsConfiguration ( ) )
{
SaveSettings ( deserializedSettings . ToJsonString ( ) , powertoy , fileName ) ;
}
return deserializedSettings ;
}
2020-04-04 10:02:38 +08:00
/// <summary>
/// Get a Deserialized object of the json settings string.
2020-09-24 04:20:32 +08:00
/// This function creates a file in the powertoy folder if it does not exist and returns an object with default properties.
2020-04-04 10:02:38 +08:00
/// </summary>
/// <returns>Deserialized json settings object.</returns>
2021-01-14 20:14:29 +08:00
public T GetSettingsOrDefault < T > ( string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-09-24 04:20:32 +08:00
where T : ISettingsConfig , new ( )
{
2021-01-14 20:14:29 +08:00
try
2020-09-24 04:20:32 +08:00
{
2021-01-14 20:14:29 +08:00
return GetSettings < T > ( powertoy , fileName ) ;
}
2020-09-24 04:20:32 +08:00
2021-01-14 20:14:29 +08:00
// Catch json deserialization exceptions when the file is corrupt and has an invalid json.
// If there are any deserialization issues like in https://github.com/microsoft/PowerToys/issues/7500, log the error and create a new settings.json file.
// This is different from the case where we have trailing zeros following a valid json file, which we have handled by trimming the trailing zeros.
catch ( JsonException ex )
{
Logger . LogError ( $"Exception encountered while loading {powertoy} settings." , ex ) ;
}
catch ( FileNotFoundException )
{
Logger . LogInfo ( $"Settings file {fileName} for {powertoy} was not found." ) ;
2020-09-24 04:20:32 +08:00
}
2020-10-27 02:13:53 +08:00
// If the settings file does not exist or if the file is corrupt, to create a new object with default parameters and save it to a newly created settings file.
T newSettingsItem = new T ( ) ;
SaveSettings ( newSettingsItem . ToJsonString ( ) , powertoy , fileName ) ;
return newSettingsItem ;
2020-09-24 04:20:32 +08:00
}
// Given the powerToy folder name and filename to be accessed, this function deserializes and returns the file.
private T GetFile < T > ( string powertoyFolderName = DefaultModuleName , string fileName = DefaultFileName )
2020-03-25 10:55:02 +08:00
{
2020-09-12 07:15:18 +08:00
// Adding Trim('\0') to overcome possible NTFS file corruption.
// Look at issue https://github.com/microsoft/PowerToys/issues/6413 you'll see the file has a large sum of \0 to fill up a 4096 byte buffer for writing to disk
// This, while not totally ideal, does work around the problem by trimming the end.
// The file itself did write the content correctly but something is off with the actual end of the file, hence the 0x00 bug
2020-11-03 01:33:43 +08:00
var jsonSettingsString = _file . ReadAllText ( _settingsPath . GetSettingsPath ( powertoyFolderName , fileName ) ) . Trim ( '\0' ) ;
2020-03-25 10:55:02 +08:00
return JsonSerializer . Deserialize < T > ( jsonSettingsString ) ;
}
2020-04-08 01:19:14 +08:00
// Save settings to a json file.
2020-09-22 01:14:44 +08:00
public void SaveSettings ( string jsonSettings , string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-03-25 10:55:02 +08:00
{
2020-04-18 06:25:08 +08:00
try
2020-04-04 10:02:38 +08:00
{
2020-04-18 06:25:08 +08:00
if ( jsonSettings ! = null )
2020-04-04 10:02:38 +08:00
{
2020-11-03 01:33:43 +08:00
if ( ! _settingsPath . SettingsFolderExists ( powertoy ) )
2020-04-18 06:25:08 +08:00
{
2020-11-03 01:33:43 +08:00
_settingsPath . CreateSettingsFolder ( powertoy ) ;
2020-04-18 06:25:08 +08:00
}
2020-04-08 15:19:00 +08:00
2020-11-03 01:33:43 +08:00
_file . WriteAllText ( _settingsPath . GetSettingsPath ( powertoy , fileName ) , jsonSettings ) ;
2020-04-18 06:25:08 +08:00
}
}
2020-10-22 03:32:53 +08:00
catch ( Exception e )
2020-04-18 06:25:08 +08:00
{
2020-10-22 03:32:53 +08:00
Logger . LogError ( $"Exception encountered while saving {powertoy} settings." , e ) ;
#if DEBUG
if ( e is ArgumentException | | e is ArgumentNullException | | e is PathTooLongException )
{
2020-10-23 00:45:48 +08:00
throw ;
2020-10-22 03:32:53 +08:00
}
#endif
2020-04-04 10:02:38 +08:00
}
2020-03-25 10:55:02 +08:00
}
Espresso (#11303)
* Update README.md
* Espresso (#11245)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Update Product.wxs
* Update Shortcut.cpp
* Update with more logging (#11332)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Fix minor issue in the module branch (#11340)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Update Product.wxs
* Update Program.cs
* fixing typo
* removing a unneeded removal
* [Espresso] More minor tweaks to logging (#11341)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
* Remove unused handle codes
* Update log file name for the Espresso C++ code.
* Update the project configuration to fix build issue
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Update patterns.txt
* Fix binding issues (#11368)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
* Remove unused handle codes
* Update log file name for the Espresso C++ code.
* Update the project configuration to fix build issue
* Fix binding issue with the time settings
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Introduce the off mode and fix binding issues (#11385)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
* Remove unused handle codes
* Update log file name for the Espresso C++ code.
* Update the project configuration to fix build issue
* Fix binding issue with the time settings
* Proper Espresso behavior for binding
* Fix settings UI
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Update with missing strings. (#11386)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
* Remove unused handle codes
* Update log file name for the Espresso C++ code.
* Update the project configuration to fix build issue
* Fix binding issue with the time settings
* Proper Espresso behavior for binding
* Fix settings UI
* Re-add missing strings
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Fix whitespace issue (#11387)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
* Remove unused handle codes
* Update log file name for the Espresso C++ code.
* Update the project configuration to fix build issue
* Fix binding issue with the time settings
* Proper Espresso behavior for binding
* Fix settings UI
* Re-add missing strings
* Fix whitespace issue
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Fix default (#11388)
* Revert "Merge branch 'microsoft:master' into master"
This reverts commit b080908712557fce0e93a98e8d3bcb58fbd111fc, reversing
changes made to 8463c95a43a737532ae21dfa9aee1894a3a37dde.
* Fix conversion of settings in the UX
* Update terminology
* Updating logging configuration
* Set up how tray and setting configuration works
* Adding hero images
* Fix how binding works
* Update OOBE string
* Fix spelling error
* fixing dep to include espresso, adding in yml
* Update API components and fix display keep-awake bug
* Adding words that the spell check is yelling about
* tweak wsx
* Change default setting for Espresso
* Adding some extra logging
* Update Shortcut.cpp
* Fix log location coming from the runner
* More chatty logging for console allocation
* Installer config to add the missing assets
* Remove unused handle codes
* Update log file name for the Espresso C++ code.
* Update the project configuration to fix build issue
* Fix binding issue with the time settings
* Proper Espresso behavior for binding
* Fix settings UI
* Re-add missing strings
* Fix whitespace issue
* Fix the default mode of operation
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
* Update Microsoft.PowerToys.Settings.UI.csproj
* Localization improvements
* Replaced a computer with your pc
* Updated Espresso imagery
* Fixed inconsistent string
* Margin fix and updated images
* Removed unused code
Co-authored-by: Den Delimarsky <1389609+dend@users.noreply.github.com>
Co-authored-by: Niels Laute <niels9001@hotmail.com>
Co-authored-by: crutkas <crutkas@microsoft.com>
Co-authored-by: Enrico Giordani <enrico.giordani@gmail.com>
2021-05-26 01:13:04 +08:00
// Returns the file path to the settings file, that is exposed from the local ISettingsPath instance.
public string GetSettingsFilePath ( string powertoy = "" , string fileName = "settings.json" )
{
return _settingsPath . GetSettingsPath ( powertoy , fileName ) ;
}
2022-10-13 15:41:21 +08:00
/// <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 ) ;
}
2020-03-25 10:55:02 +08:00
}
2020-05-06 05:28:44 +08:00
}