mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
close #48 Refactor setting storage.
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
{
|
||||
[Serializable]
|
||||
public class CommonStorage
|
||||
{
|
||||
private static string configPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\config.json";
|
||||
private static object locker = new object();
|
||||
private static CommonStorage storage;
|
||||
|
||||
public UserSetting UserSetting { get; set; }
|
||||
public UserSelectedRecords UserSelectedRecords { get; set; }
|
||||
|
||||
private CommonStorage()
|
||||
{
|
||||
UserSetting = new UserSetting();
|
||||
UserSelectedRecords = new UserSelectedRecords();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
//json is a good choise, readable and flexiable
|
||||
string json = JsonConvert.SerializeObject(storage, Formatting.Indented);
|
||||
File.WriteAllText(configPath, json);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Load()
|
||||
{
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
File.Create(configPath).Close();
|
||||
}
|
||||
string json = File.ReadAllText(configPath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
storage = JsonConvert.DeserializeObject<CommonStorage>(json);
|
||||
ValidateConfigs();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
LoadDefaultUserSetting();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadDefaultUserSetting();
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateConfigs()
|
||||
{
|
||||
try
|
||||
{
|
||||
new FontFamily(storage.UserSetting.QueryBoxFont);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
storage.UserSetting.QueryBoxFont = FontFamily.GenericSansSerif.Name;
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
new FontFamily(storage.UserSetting.ResultItemFont);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
storage.UserSetting.ResultItemFont = FontFamily.GenericSansSerif.Name;
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadDefaultUserSetting()
|
||||
{
|
||||
//default setting
|
||||
Instance.UserSetting.Theme = "Dark";
|
||||
Instance.UserSetting.ReplaceWinR = true;
|
||||
Instance.UserSetting.WebSearches = Instance.UserSetting.LoadDefaultWebSearches();
|
||||
Instance.UserSetting.ProgramSources = Instance.UserSetting.LoadDefaultProgramSources();
|
||||
Instance.UserSetting.Hotkey = "Win + W";
|
||||
Instance.UserSetting.QueryBoxFont = FontFamily.GenericSansSerif.Name;
|
||||
Instance.UserSetting.ResultItemFont = FontFamily.GenericSansSerif.Name;
|
||||
}
|
||||
|
||||
public static CommonStorage Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (storage == null)
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (storage == null)
|
||||
{
|
||||
storage = new CommonStorage();
|
||||
Load();
|
||||
}
|
||||
}
|
||||
}
|
||||
return storage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
76
Wox.Infrastructure/Storage/BaseStorage.cs
Normal file
76
Wox.Infrastructure/Storage/BaseStorage.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public abstract class BaseStorage<T> where T : class, new()
|
||||
{
|
||||
private string configFolder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config");
|
||||
private string fileSuffix = ".json";
|
||||
private static object locker = new object();
|
||||
private static T storage;
|
||||
|
||||
protected abstract string ConfigName { get; }
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (storage == null)
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (storage == null)
|
||||
{
|
||||
storage = new T();
|
||||
(storage as BaseStorage<T>).Load();
|
||||
}
|
||||
}
|
||||
}
|
||||
return storage;
|
||||
}
|
||||
}
|
||||
|
||||
private void Load()
|
||||
{
|
||||
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(configFolder);
|
||||
if (!fileInfo.Directory.Exists)
|
||||
{
|
||||
fileInfo.Directory.Create();
|
||||
}
|
||||
File.Create(configPath).Close();
|
||||
}
|
||||
string json = File.ReadAllText(configPath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
storage = JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//no-op
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
//json is a good choise, readable and flexiable
|
||||
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
|
||||
string json = JsonConvert.SerializeObject(storage, Formatting.Indented);
|
||||
File.WriteAllText(configPath, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs
Normal file
43
Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public class UserSelectedRecordStorage : BaseStorage<UserSelectedRecordStorage>
|
||||
{
|
||||
[JsonProperty]
|
||||
private Dictionary<string, int> records = new Dictionary<string, int>();
|
||||
|
||||
protected override string ConfigName
|
||||
{
|
||||
get { return "UserSelectedRecords"; }
|
||||
}
|
||||
|
||||
public void Add(Result result)
|
||||
{
|
||||
if (records.ContainsKey(result.ToString()))
|
||||
{
|
||||
records[result.ToString()] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
records.Add(result.ToString(), 1);
|
||||
}
|
||||
Save();
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
{
|
||||
if (records.ContainsKey(result.ToString()))
|
||||
{
|
||||
return records[result.ToString()];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Infrastructure.UserSettings
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
[Serializable]
|
||||
public class ProgramSource
|
||||
@@ -1,25 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.UserSettings
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
public class UserSetting
|
||||
public class UserSettingStorage : BaseStorage<UserSettingStorage>
|
||||
{
|
||||
[JsonProperty]
|
||||
public string Hotkey { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public string Theme { get; set; }
|
||||
|
||||
|
||||
[JsonProperty]
|
||||
public string QueryBoxFont { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public string ResultItemFont { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public bool ReplaceWinR { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public List<WebSearch> WebSearches { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public List<ProgramSource> ProgramSources { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public bool StartWoxOnSystemStartup { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public bool EnablePythonPlugins { get; set; }
|
||||
|
||||
public UserSetting()
|
||||
public UserSettingStorage()
|
||||
{
|
||||
EnablePythonPlugins = true;
|
||||
Theme = "Dark";
|
||||
ReplaceWinR = true;
|
||||
WebSearches = LoadDefaultWebSearches();
|
||||
ProgramSources = LoadDefaultProgramSources();
|
||||
Hotkey = "Alt + Space";
|
||||
QueryBoxFont = FontFamily.GenericSansSerif.Name;
|
||||
ResultItemFont = FontFamily.GenericSansSerif.Name;
|
||||
}
|
||||
|
||||
public List<WebSearch> LoadDefaultWebSearches()
|
||||
@@ -36,7 +65,7 @@ namespace Wox.Infrastructure.UserSettings
|
||||
};
|
||||
webSearches.Add(googleWebSearch);
|
||||
|
||||
|
||||
|
||||
WebSearch wikiWebSearch = new WebSearch()
|
||||
{
|
||||
Title = "Wikipedia",
|
||||
@@ -73,5 +102,10 @@ namespace Wox.Infrastructure.UserSettings
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
protected override string ConfigName
|
||||
{
|
||||
get { return "config"; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Wox.Infrastructure.UserSettings
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
public class WebSearch
|
||||
{
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Infrastructure.UserSettings
|
||||
{
|
||||
public class CustomPluginHotkey
|
||||
{
|
||||
public string Hotkey { get; set; }
|
||||
public string ActionKeyword { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Infrastructure.UserSettings
|
||||
{
|
||||
[Serializable]
|
||||
public class UserSelectedRecords
|
||||
{
|
||||
// private static int hasAddedCount = 0;
|
||||
|
||||
public Dictionary<string,int> Records = new Dictionary<string, int>();
|
||||
|
||||
public void Add(Result result)
|
||||
{
|
||||
if (Records.ContainsKey(result.ToString()))
|
||||
{
|
||||
Records[result.ToString()] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Records.Add(result.ToString(), 1);
|
||||
}
|
||||
|
||||
//hasAddedCount++;
|
||||
//if (hasAddedCount == 10)
|
||||
//{
|
||||
// hasAddedCount = 0;
|
||||
//}
|
||||
CommonStorage.Instance.Save();
|
||||
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
{
|
||||
if (Records.ContainsKey(result.ToString()))
|
||||
{
|
||||
return Records[result.ToString()];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,17 +49,17 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ChineseToPinYin.cs" />
|
||||
<Compile Include="CommonStorage.cs" />
|
||||
<Compile Include="Storage\BaseStorage.cs" />
|
||||
<Compile Include="FuzzyMatcher.cs" />
|
||||
<Compile Include="GlobalHotkey.cs" />
|
||||
<Compile Include="HotkeyModel.cs" />
|
||||
<Compile Include="IniParser.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="UserSettings\PluginHotkey.cs" />
|
||||
<Compile Include="UserSettings\ProgramSource.cs" />
|
||||
<Compile Include="UserSettings\UserSelectedRecords.cs" />
|
||||
<Compile Include="UserSettings\UserSetting.cs" />
|
||||
<Compile Include="UserSettings\WebSearch.cs" />
|
||||
<Compile Include="Storage\UserSelectedRecordStorage.cs" />
|
||||
<Compile Include="Storage\UserSettings\UserSettingStorage.cs" />
|
||||
<Compile Include="Storage\UserSettings\PluginHotkey.cs" />
|
||||
<Compile Include="Storage\UserSettings\ProgramSource.cs" />
|
||||
<Compile Include="Storage\UserSettings\WebSearch.cs" />
|
||||
<Compile Include="WindowsShellRun.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user