mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
close #48 Refactor setting storage.
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Wox.Infrastructure/Storage/UserSettings/ProgramSource.cs
Normal file
20
Wox.Infrastructure/Storage/UserSettings/ProgramSource.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
[Serializable]
|
||||
public class ProgramSource
|
||||
{
|
||||
public string Location { get; set; }
|
||||
public string Type { get; set; }
|
||||
public int BonusPoints { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
public Dictionary<string, string> Meta { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return (this.Type ?? "") + ":" + this.Location ?? "";
|
||||
}
|
||||
}
|
||||
}
|
||||
111
Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs
Normal file
111
Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
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 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()
|
||||
{
|
||||
List<WebSearch> webSearches = new List<WebSearch>();
|
||||
|
||||
WebSearch googleWebSearch = new WebSearch()
|
||||
{
|
||||
Title = "Google",
|
||||
ActionWord = "g",
|
||||
IconPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\Images\websearch\google.png",
|
||||
Url = "https://www.google.com/search?q={q}",
|
||||
Enabled = true
|
||||
};
|
||||
webSearches.Add(googleWebSearch);
|
||||
|
||||
|
||||
WebSearch wikiWebSearch = new WebSearch()
|
||||
{
|
||||
Title = "Wikipedia",
|
||||
ActionWord = "wiki",
|
||||
IconPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\Images\websearch\wiki.png",
|
||||
Url = "http://en.wikipedia.org/wiki/{q}",
|
||||
Enabled = true
|
||||
};
|
||||
webSearches.Add(wikiWebSearch);
|
||||
|
||||
return webSearches;
|
||||
}
|
||||
|
||||
public List<ProgramSource> LoadDefaultProgramSources()
|
||||
{
|
||||
var list = new List<ProgramSource>();
|
||||
list.Add(new ProgramSource()
|
||||
{
|
||||
BonusPoints = 0,
|
||||
Enabled = true,
|
||||
Type = "CommonStartMenuProgramSource"
|
||||
});
|
||||
list.Add(new ProgramSource()
|
||||
{
|
||||
BonusPoints = 0,
|
||||
Enabled = true,
|
||||
Type = "UserStartMenuProgramSource"
|
||||
});
|
||||
list.Add(new ProgramSource()
|
||||
{
|
||||
BonusPoints = -10,
|
||||
Enabled = true,
|
||||
Type = "AppPathsProgramSource"
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
protected override string ConfigName
|
||||
{
|
||||
get { return "config"; }
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Wox.Infrastructure/Storage/UserSettings/WebSearch.cs
Normal file
11
Wox.Infrastructure/Storage/UserSettings/WebSearch.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
public class WebSearch
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string ActionWord { get; set; }
|
||||
public string IconPath { get; set; }
|
||||
public string Url { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user