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-03-25 10:55:02 +08:00
using System.IO ;
2020-09-12 07:15:18 +08:00
using System.Runtime.Serialization ;
2020-03-25 10:55:02 +08:00
using System.Text.Json ;
namespace Microsoft.PowerToys.Settings.UI.Lib
{
public static class SettingsUtils
{
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-04-18 06:25:08 +08:00
2020-09-09 01:04:17 +08:00
public static void DeleteSettings ( string powertoy , string fileName = DefaultFileName )
{
File . Delete ( GetSettingsPath ( powertoy , fileName ) ) ;
}
2020-04-04 10:02:38 +08:00
public static bool SettingsFolderExists ( string powertoy )
{
return Directory . Exists ( Path . Combine ( LocalApplicationDataFolder ( ) , $"Microsoft\\PowerToys\\{powertoy}" ) ) ;
}
public static void CreateSettingsFolder ( string powertoy )
{
Directory . CreateDirectory ( Path . Combine ( LocalApplicationDataFolder ( ) , $"Microsoft\\PowerToys\\{powertoy}" ) ) ;
}
/// <summary>
/// Get path to the json settings file.
/// </summary>
/// <returns>string path.</returns>
2020-04-18 06:25:08 +08:00
public static string GetSettingsPath ( string powertoy , string fileName = DefaultFileName )
2020-03-25 10:55:02 +08:00
{
2020-04-08 01:19:14 +08:00
if ( string . IsNullOrWhiteSpace ( powertoy ) )
2020-03-25 10:55:02 +08:00
{
return Path . Combine (
2020-04-04 10:02:38 +08:00
LocalApplicationDataFolder ( ) ,
2020-04-18 06:25:08 +08:00
$"Microsoft\\PowerToys\\{fileName}" ) ;
2020-03-25 10:55:02 +08:00
}
2020-04-08 01:19:14 +08:00
2020-03-25 10:55:02 +08:00
return Path . Combine (
2020-04-04 10:02:38 +08:00
LocalApplicationDataFolder ( ) ,
2020-04-18 06:25:08 +08:00
$"Microsoft\\PowerToys\\{powertoy}\\{fileName}" ) ;
2020-03-25 10:55:02 +08:00
}
2020-04-23 05:55:45 +08:00
public static bool SettingsExists ( string powertoy = DefaultModuleName , string fileName = DefaultFileName )
2020-04-04 10:02:38 +08:00
{
2020-04-18 06:25:08 +08:00
return File . Exists ( GetSettingsPath ( powertoy , fileName ) ) ;
2020-04-04 10:02:38 +08:00
}
/// <summary>
/// Get a Deserialized object of the json settings string.
/// </summary>
/// <returns>Deserialized json settings object.</returns>
2020-04-20 21:03:26 +08:00
public static T GetSettings < T > ( string powertoy = 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
var jsonSettingsString = File . ReadAllText ( GetSettingsPath ( powertoy , 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-04-20 21:03:26 +08:00
public static 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-04-18 06:25:08 +08:00
if ( ! SettingsFolderExists ( powertoy ) )
{
CreateSettingsFolder ( powertoy ) ;
}
2020-04-08 15:19:00 +08:00
2020-04-18 06:25:08 +08:00
File . WriteAllText ( GetSettingsPath ( powertoy , fileName ) , jsonSettings ) ;
}
}
catch
{
2020-04-04 10:02:38 +08:00
}
2020-03-25 10:55:02 +08:00
}
2020-04-08 15:19:00 +08:00
2020-04-18 06:25:08 +08:00
public static string LocalApplicationDataFolder ( )
2020-04-08 15:19:00 +08:00
{
return Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ;
}
2020-03-25 10:55:02 +08:00
}
2020-05-06 05:28:44 +08:00
}