2020-04-08 00:19:00 -07: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-03-24 19:55:02 -07:00
using System.Text.Json ;
2020-09-21 10:14:44 -07:00
using Microsoft.PowerToys.Settings.UI.Lib.Utilities ;
2020-03-24 19:55:02 -07:00
namespace Microsoft.PowerToys.Settings.UI.Lib
{
2020-09-21 10:14:44 -07:00
public class SettingsUtils : ISettingsUtils
2020-03-24 19:55:02 -07:00
{
2020-04-17 15:25:08 -07:00
private const string DefaultFileName = "settings.json" ;
2020-04-20 06:03:26 -07:00
private const string DefaultModuleName = "" ;
2020-09-21 10:14:44 -07:00
private IIOProvider _ioProvider ;
2020-04-17 15:25:08 -07:00
2020-09-21 10:14:44 -07:00
public SettingsUtils ( IIOProvider ioProvider )
2020-09-08 10:04:17 -07:00
{
2020-09-21 10:14:44 -07:00
_ioProvider = ioProvider ? ? throw new ArgumentNullException ( nameof ( ioProvider ) ) ;
2020-09-08 10:04:17 -07:00
}
2020-09-21 10:14:44 -07:00
private bool SettingsFolderExists ( string powertoy )
2020-04-03 19:02:38 -07:00
{
2020-09-21 10:14:44 -07:00
return _ioProvider . DirectoryExists ( System . IO . Path . Combine ( LocalApplicationDataFolder ( ) , $"Microsoft\\PowerToys\\{powertoy}" ) ) ;
2020-04-03 19:02:38 -07:00
}
2020-09-21 10:14:44 -07:00
private void CreateSettingsFolder ( string powertoy )
2020-04-03 19:02:38 -07:00
{
2020-09-21 10:14:44 -07:00
_ioProvider . CreateDirectory ( System . IO . Path . Combine ( LocalApplicationDataFolder ( ) , $"Microsoft\\PowerToys\\{powertoy}" ) ) ;
}
public void DeleteSettings ( string powertoy = "" )
{
_ioProvider . DeleteDirectory ( System . IO . Path . Combine ( LocalApplicationDataFolder ( ) , $"Microsoft\\PowerToys\\{powertoy}" ) ) ;
2020-04-03 19:02:38 -07:00
}
/// <summary>
/// Get path to the json settings file.
/// </summary>
/// <returns>string path.</returns>
2020-04-17 15:25:08 -07:00
public static string GetSettingsPath ( string powertoy , string fileName = DefaultFileName )
2020-03-24 19:55:02 -07:00
{
2020-04-07 10:19:14 -07:00
if ( string . IsNullOrWhiteSpace ( powertoy ) )
2020-03-24 19:55:02 -07:00
{
2020-09-21 10:14:44 -07:00
return System . IO . Path . Combine (
2020-04-03 19:02:38 -07:00
LocalApplicationDataFolder ( ) ,
2020-04-17 15:25:08 -07:00
$"Microsoft\\PowerToys\\{fileName}" ) ;
2020-03-24 19:55:02 -07:00
}
2020-04-07 10:19:14 -07:00
2020-09-21 10:14:44 -07:00
return System . IO . Path . Combine (
2020-04-03 19:02:38 -07:00
LocalApplicationDataFolder ( ) ,
2020-04-17 15:25:08 -07:00
$"Microsoft\\PowerToys\\{powertoy}\\{fileName}" ) ;
2020-03-24 19:55:02 -07:00
}
2020-09-21 10:14:44 -07:00
public bool SettingsExists ( string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-04-03 19:02:38 -07:00
{
2020-09-21 10:14:44 -07:00
return _ioProvider . FileExists ( GetSettingsPath ( powertoy , fileName ) ) ;
2020-04-03 19:02:38 -07:00
}
/// <summary>
/// Get a Deserialized object of the json settings string.
/// </summary>
/// <returns>Deserialized json settings object.</returns>
2020-09-21 10:14:44 -07:00
public T GetSettings < T > ( string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-03-24 19:55:02 -07:00
{
2020-09-11 16:15:18 -07: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-09-21 10:14:44 -07:00
var jsonSettingsString = _ioProvider . ReadAllText ( GetSettingsPath ( powertoy , fileName ) ) . Trim ( '\0' ) ;
2020-09-11 16:15:18 -07:00
2020-03-24 19:55:02 -07:00
return JsonSerializer . Deserialize < T > ( jsonSettingsString ) ;
}
2020-04-07 10:19:14 -07:00
// Save settings to a json file.
2020-09-21 10:14:44 -07:00
public void SaveSettings ( string jsonSettings , string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-03-24 19:55:02 -07:00
{
2020-04-17 15:25:08 -07:00
try
2020-04-03 19:02:38 -07:00
{
2020-04-17 15:25:08 -07:00
if ( jsonSettings ! = null )
2020-04-03 19:02:38 -07:00
{
2020-04-17 15:25:08 -07:00
if ( ! SettingsFolderExists ( powertoy ) )
{
CreateSettingsFolder ( powertoy ) ;
}
2020-04-08 00:19:00 -07:00
2020-09-21 10:14:44 -07:00
_ioProvider . WriteAllText ( GetSettingsPath ( powertoy , fileName ) , jsonSettings ) ;
2020-04-17 15:25:08 -07:00
}
}
catch
{
2020-04-03 19:02:38 -07:00
}
2020-03-24 19:55:02 -07:00
}
2020-04-08 00:19:00 -07:00
2020-09-21 10:14:44 -07:00
private static string LocalApplicationDataFolder ( )
2020-04-08 00:19:00 -07:00
{
return Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ;
}
2020-03-24 19:55:02 -07:00
}
2020-05-05 14:28:44 -07:00
}