diff --git a/Wox.Infrastructure/CommonStorage.cs b/Wox.Infrastructure/CommonStorage.cs deleted file mode 100644 index a235192a38..0000000000 --- a/Wox.Infrastructure/CommonStorage.cs +++ /dev/null @@ -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(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; - } - } - } -} \ No newline at end of file diff --git a/Wox.Infrastructure/Storage/BaseStorage.cs b/Wox.Infrastructure/Storage/BaseStorage.cs new file mode 100644 index 0000000000..94fd9ecc63 --- /dev/null +++ b/Wox.Infrastructure/Storage/BaseStorage.cs @@ -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 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).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(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); + } + } + } +} diff --git a/Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs b/Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs new file mode 100644 index 0000000000..9c0c77d40b --- /dev/null +++ b/Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs @@ -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 + { + [JsonProperty] + private Dictionary records = new Dictionary(); + + 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; + } + } +} diff --git a/Wox.Infrastructure/UserSettings/ProgramSource.cs b/Wox.Infrastructure/Storage/UserSettings/ProgramSource.cs similarity index 85% rename from Wox.Infrastructure/UserSettings/ProgramSource.cs rename to Wox.Infrastructure/Storage/UserSettings/ProgramSource.cs index eb8223cba5..8e77dc9d10 100644 --- a/Wox.Infrastructure/UserSettings/ProgramSource.cs +++ b/Wox.Infrastructure/Storage/UserSettings/ProgramSource.cs @@ -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 diff --git a/Wox.Infrastructure/UserSettings/UserSetting.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs similarity index 73% rename from Wox.Infrastructure/UserSettings/UserSetting.cs rename to Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index dca235ee1a..5956855b93 100644 --- a/Wox.Infrastructure/UserSettings/UserSetting.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -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 { + [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 WebSearches { get; set; } + + [JsonProperty] public List ProgramSources { get; set; } + + [JsonProperty] public List 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 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"; } + } } } diff --git a/Wox.Infrastructure/UserSettings/WebSearch.cs b/Wox.Infrastructure/Storage/UserSettings/WebSearch.cs similarity index 82% rename from Wox.Infrastructure/UserSettings/WebSearch.cs rename to Wox.Infrastructure/Storage/UserSettings/WebSearch.cs index d276eddfb3..5b6eb0e5d2 100644 --- a/Wox.Infrastructure/UserSettings/WebSearch.cs +++ b/Wox.Infrastructure/Storage/UserSettings/WebSearch.cs @@ -1,4 +1,4 @@ -namespace Wox.Infrastructure.UserSettings +namespace Wox.Infrastructure.Storage.UserSettings { public class WebSearch { diff --git a/Wox.Infrastructure/UserSettings/PluginHotkey.cs b/Wox.Infrastructure/UserSettings/PluginHotkey.cs deleted file mode 100644 index d8dbb48e96..0000000000 --- a/Wox.Infrastructure/UserSettings/PluginHotkey.cs +++ /dev/null @@ -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; } - } -} diff --git a/Wox.Infrastructure/UserSettings/UserSelectedRecords.cs b/Wox.Infrastructure/UserSettings/UserSelectedRecords.cs deleted file mode 100644 index 598d1fa704..0000000000 --- a/Wox.Infrastructure/UserSettings/UserSelectedRecords.cs +++ /dev/null @@ -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 Records = new Dictionary(); - - 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; - } - } -} diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index a807ea39a8..e3c8ae0600 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -49,17 +49,17 @@ - + - - - - - + + + + + diff --git a/Wox.Plugin.System/CMD.cs b/Wox.Plugin.System/CMD/CMD.cs similarity index 71% rename from Wox.Plugin.System/CMD.cs rename to Wox.Plugin.System/CMD/CMD.cs index bb9914cae7..bdc3dfbd9d 100644 --- a/Wox.Plugin.System/CMD.cs +++ b/Wox.Plugin.System/CMD/CMD.cs @@ -1,18 +1,14 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; -using System.Text; using System.Windows.Forms; -namespace Wox.Plugin.System +namespace Wox.Plugin.System.CMD { public class CMD : BaseSystemPlugin { - private Dictionary cmdHistory = new Dictionary(); - private string filePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\CMDHistory.dat"; private PluginInitContext context; protected override List QueryInternal(Query query) @@ -20,7 +16,7 @@ namespace Wox.Plugin.System List results = new List(); if (query.RawQuery == ">") { - IEnumerable history = cmdHistory.OrderByDescending(o => o.Value) + IEnumerable history = CMDStorage.Instance.CMDHistory.OrderByDescending(o => o.Value) .Select(m => new Result { Title = m.Key, @@ -63,9 +59,10 @@ namespace Wox.Plugin.System results.Add(result); - IEnumerable history = cmdHistory.Where(o => o.Key.Contains(cmd)) + IEnumerable history = CMDStorage.Instance.CMDHistory.Where(o => o.Key.Contains(cmd)) .OrderByDescending(o => o.Value) - .Select(m => { + .Select(m => + { if (m.Key == cmd) { result.SubTitle = "this command has been executed " + m.Value + " times"; @@ -117,7 +114,8 @@ namespace Wox.Plugin.System { List autocomplete = Directory.GetFileSystemEntries(basedir).Select(o => dir + Path.GetFileName(o)).Where(o => o.StartsWith(cmd, StringComparison.OrdinalIgnoreCase) && !results.Any(p => o.Equals(p.Title, StringComparison.OrdinalIgnoreCase))).ToList(); autocomplete.Sort(); - results.AddRange(autocomplete.ConvertAll(m => new Result() { + results.AddRange(autocomplete.ConvertAll(m => new Result() + { Title = m, SubTitle = "", IcoPath = m, @@ -138,55 +136,13 @@ namespace Wox.Plugin.System private void ExecuteCmd(string cmd) { if (context.ShellRun(cmd)) - AddCmdHistory(cmd); + CMDStorage.Instance.AddCmdHistory(cmd); } protected override void InitInternal(PluginInitContext context) { this.context = context; - LoadCmdHistory(); } - //todo:we need provide a common data persist interface for user? - private void AddCmdHistory(string cmdName) - { - if (cmdHistory.ContainsKey(cmdName)) - { - cmdHistory[cmdName] += 1; - } - else - { - cmdHistory.Add(cmdName, 1); - } - PersistCmdHistory(); - } - - public void LoadCmdHistory() - { - if (File.Exists(filePath)) - { - FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); - BinaryFormatter b = new BinaryFormatter(); - cmdHistory = (Dictionary)b.Deserialize(fileStream); - fileStream.Close(); - } - - if (cmdHistory.Count > 1000) - { - List onlyOnceKeys = (from c in cmdHistory where c.Value == 1 select c.Key).ToList(); - foreach (string onlyOnceKey in onlyOnceKeys) - { - cmdHistory.Remove(onlyOnceKey); - } - } - } - - private void PersistCmdHistory() - { - FileStream fileStream = new FileStream(filePath, FileMode.Create); - BinaryFormatter b = new BinaryFormatter(); - b.Serialize(fileStream, cmdHistory); - fileStream.Close(); - } } } diff --git a/Wox.Plugin.System/CMD/CMDStorage.cs b/Wox.Plugin.System/CMD/CMDStorage.cs new file mode 100644 index 0000000000..10ccde0eb2 --- /dev/null +++ b/Wox.Plugin.System/CMD/CMDStorage.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Newtonsoft.Json; +using Wox.Infrastructure.Storage; + +namespace Wox.Plugin.System.CMD +{ + public class CMDStorage : BaseStorage + { + [JsonProperty] + public Dictionary CMDHistory = new Dictionary(); + + protected override string ConfigName + { + get { return "CMDHistory"; } + } + + public void AddCmdHistory(string cmdName) + { + if (CMDHistory.ContainsKey(cmdName)) + { + CMDHistory[cmdName] += 1; + } + else + { + CMDHistory.Add(cmdName, 1); + } + Save(); + } + } +} diff --git a/Wox.Plugin.System/ProgramSources/AppPathsProgramSource.cs b/Wox.Plugin.System/ProgramSources/AppPathsProgramSource.cs index 96f66c860b..d3436f1ac1 100644 --- a/Wox.Plugin.System/ProgramSources/AppPathsProgramSource.cs +++ b/Wox.Plugin.System/ProgramSources/AppPathsProgramSource.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.System.ProgramSources { @@ -13,7 +14,7 @@ namespace Wox.Plugin.System.ProgramSources this.BonusPoints = -10; } - public AppPathsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + public AppPathsProgramSource(ProgramSource source) : this() { this.BonusPoints = source.BonusPoints; diff --git a/Wox.Plugin.System/ProgramSources/CommonStartMenuProgramSource.cs b/Wox.Plugin.System/ProgramSources/CommonStartMenuProgramSource.cs index c0e8ca4dab..d23a7ebda4 100644 --- a/Wox.Plugin.System/ProgramSources/CommonStartMenuProgramSource.cs +++ b/Wox.Plugin.System/ProgramSources/CommonStartMenuProgramSource.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.System.ProgramSources { @@ -27,7 +28,7 @@ namespace Wox.Plugin.System.ProgramSources { } - public CommonStartMenuProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + public CommonStartMenuProgramSource(ProgramSource source) : this() { this.BonusPoints = source.BonusPoints; diff --git a/Wox.Plugin.System/ProgramSources/FileSystemProgramSource.cs b/Wox.Plugin.System/ProgramSources/FileSystemProgramSource.cs index 07b6d8a6a2..5792105683 100644 --- a/Wox.Plugin.System/ProgramSources/FileSystemProgramSource.cs +++ b/Wox.Plugin.System/ProgramSources/FileSystemProgramSource.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.System.ProgramSources { @@ -22,7 +23,7 @@ namespace Wox.Plugin.System.ProgramSources Suffixes = suffixes; } - public FileSystemProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + public FileSystemProgramSource(ProgramSource source) : this(source.Location) { this.BonusPoints = source.BonusPoints; diff --git a/Wox.Plugin.System/ProgramSources/PortableAppsProgramSource.cs b/Wox.Plugin.System/ProgramSources/PortableAppsProgramSource.cs index 41a6d7ce92..62d198d8da 100644 --- a/Wox.Plugin.System/ProgramSources/PortableAppsProgramSource.cs +++ b/Wox.Plugin.System/ProgramSources/PortableAppsProgramSource.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Text; using System.IO; using IniParser; +using Wox.Infrastructure.Storage.UserSettings; + namespace Wox.Plugin.System.ProgramSources { public class PortableAppsProgramSource : AbstractProgramSource @@ -15,7 +17,7 @@ namespace Wox.Plugin.System.ProgramSources BaseDirectory = baseDirectory; } - public PortableAppsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + public PortableAppsProgramSource(ProgramSource source) : this(source.Location) { this.BonusPoints = source.BonusPoints; diff --git a/Wox.Plugin.System/ProgramSources/UserStartMenuProgramSource.cs b/Wox.Plugin.System/ProgramSources/UserStartMenuProgramSource.cs index e4976797e7..0536c9d7ad 100644 --- a/Wox.Plugin.System/ProgramSources/UserStartMenuProgramSource.cs +++ b/Wox.Plugin.System/ProgramSources/UserStartMenuProgramSource.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.System.ProgramSources { @@ -13,7 +14,7 @@ namespace Wox.Plugin.System.ProgramSources { } - public UserStartMenuProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + public UserStartMenuProgramSource(ProgramSource source) : this() { this.BonusPoints = source.BonusPoints; diff --git a/Wox.Plugin.System/Programs.cs b/Wox.Plugin.System/Programs.cs index 0e657152c1..ca78c00a2b 100644 --- a/Wox.Plugin.System/Programs.cs +++ b/Wox.Plugin.System/Programs.cs @@ -9,6 +9,8 @@ using System.Text; using System.Windows.Forms; using Microsoft.Win32; using Wox.Infrastructure; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin.System.ProgramSources; namespace Wox.Plugin.System @@ -94,10 +96,10 @@ namespace Wox.Plugin.System { this.context = context; - if (CommonStorage.Instance.UserSetting.ProgramSources == null) - CommonStorage.Instance.UserSetting.ProgramSources = CommonStorage.Instance.UserSetting.LoadDefaultProgramSources(); + if (UserSettingStorage.Instance.ProgramSources == null) + UserSettingStorage.Instance.ProgramSources = UserSettingStorage.Instance.LoadDefaultProgramSources(); - CommonStorage.Instance.UserSetting.ProgramSources.ForEach(source => + UserSettingStorage.Instance.ProgramSources.ForEach(source => { if (source.Enabled) { @@ -105,7 +107,7 @@ namespace Wox.Plugin.System if (SourceTypes.TryGetValue(source.Type, out sourceClass)) { sources.Add(sourceClass.GetConstructor( - new Type[] { typeof(Wox.Infrastructure.UserSettings.ProgramSource) } + new Type[] { typeof(ProgramSource) } ).Invoke(new object[] { source }) as IProgramSource); } else diff --git a/Wox.Plugin.System/ThirdpartyPluginIndicator.cs b/Wox.Plugin.System/ThirdpartyPluginIndicator.cs index 5ded69a1f3..eee39ecafa 100644 --- a/Wox.Plugin.System/ThirdpartyPluginIndicator.cs +++ b/Wox.Plugin.System/ThirdpartyPluginIndicator.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Wox.Infrastructure; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.System { @@ -37,7 +39,7 @@ namespace Wox.Plugin.System } } - results.AddRange(CommonStorage.Instance.UserSetting.WebSearches.Where(o => o.ActionWord.StartsWith(query.RawQuery) && o.Enabled).Select(n => new Result() + results.AddRange(UserSettingStorage.Instance.WebSearches.Where(o => o.ActionWord.StartsWith(query.RawQuery) && o.Enabled).Select(n => new Result() { Title = n.ActionWord, SubTitle = string.Format("Activate {0} web search", n.ActionWord), diff --git a/Wox.Plugin.System/WebSearchPlugin.cs b/Wox.Plugin.System/WebSearchPlugin.cs index efb5faa4c9..ca35b46f6a 100644 --- a/Wox.Plugin.System/WebSearchPlugin.cs +++ b/Wox.Plugin.System/WebSearchPlugin.cs @@ -5,7 +5,8 @@ using System.IO; using System.Linq; using Newtonsoft.Json; using Wox.Infrastructure; -using Wox.Infrastructure.UserSettings; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.System { @@ -17,7 +18,7 @@ namespace Wox.Plugin.System if (string.IsNullOrEmpty(query.ActionName)) return results; WebSearch webSearch = - CommonStorage.Instance.UserSetting.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName && o.Enabled); + UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName && o.Enabled); if (webSearch != null) { diff --git a/Wox.Plugin.System/Wox.Plugin.System.csproj b/Wox.Plugin.System/Wox.Plugin.System.csproj index 34611c97bd..d44cae02a4 100644 --- a/Wox.Plugin.System/Wox.Plugin.System.csproj +++ b/Wox.Plugin.System/Wox.Plugin.System.csproj @@ -51,6 +51,7 @@ + @@ -61,7 +62,7 @@ - + diff --git a/Wox/CustomPluginHotkeySetting.xaml.cs b/Wox/CustomPluginHotkeySetting.xaml.cs index ca265ed59d..ae86627f6d 100644 --- a/Wox/CustomPluginHotkeySetting.xaml.cs +++ b/Wox/CustomPluginHotkeySetting.xaml.cs @@ -11,7 +11,8 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Wox.Infrastructure; -using Wox.Infrastructure.UserSettings; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; namespace Wox { @@ -43,9 +44,9 @@ namespace Wox return; } - if (CommonStorage.Instance.UserSetting.CustomPluginHotkeys == null) + if (UserSettingStorage.Instance.CustomPluginHotkeys == null) { - CommonStorage.Instance.UserSetting.CustomPluginHotkeys = new List(); + UserSettingStorage.Instance.CustomPluginHotkeys = new List(); } var pluginHotkey = new CustomPluginHotkey() @@ -53,7 +54,7 @@ namespace Wox Hotkey = ctlHotkey.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text }; - CommonStorage.Instance.UserSetting.CustomPluginHotkeys.Add(pluginHotkey); + UserSettingStorage.Instance.CustomPluginHotkeys.Add(pluginHotkey); settingWidow.MainWindow.SetHotkey(ctlHotkey.CurrentHotkey.ToString(), delegate { settingWidow.MainWindow.ChangeQuery(pluginHotkey.ActionKeyword); @@ -81,14 +82,14 @@ namespace Wox MessageBox.Show("Update successfully!"); } - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.Save(); settingWidow.ReloadCustomPluginHotkeyView(); Close(); } public void UpdateItem(CustomPluginHotkey item) { - updateCustomHotkey = CommonStorage.Instance.UserSetting.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey); + updateCustomHotkey = UserSettingStorage.Instance.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey); if (updateCustomHotkey == null) { MessageBox.Show("Invalid plugin hotkey"); diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index abafd9b3d3..136ded6cc8 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -17,7 +17,8 @@ using NHotkey.Wpf; using Wox.Commands; using Wox.Helper; using Wox.Infrastructure; -using Wox.Infrastructure.UserSettings; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; using Wox.PluginLoader; using Application = System.Windows.Application; @@ -60,14 +61,14 @@ namespace Wox ThreadPool.SetMaxThreads(30, 10); try { - SetTheme(CommonStorage.Instance.UserSetting.Theme); + SetTheme(UserSettingStorage.Instance.Theme); } catch (Exception) { - SetTheme(CommonStorage.Instance.UserSetting.Theme = "Dark"); + SetTheme(UserSettingStorage.Instance.Theme = "Dark"); } - SetHotkey(CommonStorage.Instance.UserSetting.Hotkey, OnHotkey); + SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey); SetCustomPluginHotkey(); globalHotkey.hookedKeyboardCallback += KListener_hookedKeyboardCallback; @@ -131,8 +132,8 @@ namespace Wox private void SetCustomPluginHotkey() { - if (CommonStorage.Instance.UserSetting.CustomPluginHotkeys == null) return; - foreach (CustomPluginHotkey hotkey in CommonStorage.Instance.UserSetting.CustomPluginHotkeys) + if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return; + foreach (CustomPluginHotkey hotkey in UserSettingStorage.Instance.CustomPluginHotkeys) { CustomPluginHotkey hotkey1 = hotkey; SetHotkey(hotkey.Hotkey, delegate @@ -285,7 +286,7 @@ namespace Wox private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state) { - if (CommonStorage.Instance.UserSetting.ReplaceWinR) + if (UserSettingStorage.Instance.ReplaceWinR) { //todo:need refatoring. move those codes to CMD file or expose events if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed) @@ -388,7 +389,8 @@ namespace Wox { HideWox(); } - CommonStorage.Instance.UserSelectedRecords.Add(result); + UserSelectedRecordStorage.Instance.Add(result); + UserSelectedRecordStorage.Instance.Add(result); } } } @@ -405,7 +407,7 @@ namespace Wox list.ForEach( o => { - if (o.AutoAjustScore) o.Score += CommonStorage.Instance.UserSelectedRecords.GetSelectedCount(o); + if (o.AutoAjustScore) o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o); }); lock (locker) { @@ -431,7 +433,7 @@ namespace Wox Style queryBoxStyle = dict["QueryBoxStyle"] as Style; if (queryBoxStyle != null) { - queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(CommonStorage.Instance.UserSetting.QueryBoxFont))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.QueryBoxFont))); } Style resultItemStyle = dict["ItemTitleStyle"] as Style; Style resultSubItemStyle = dict["ItemSubTitleStyle"] as Style; @@ -440,7 +442,7 @@ namespace Wox if (resultItemStyle != null && resultSubItemStyle != null && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null) { - Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(CommonStorage.Instance.UserSetting.ResultItemFont)); + Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultItemFont)); resultItemStyle.Setters.Add(fontFamily); resultSubItemStyle.Setters.Add(fontFamily); diff --git a/Wox/PluginLoader/Plugins.cs b/Wox/PluginLoader/Plugins.cs index fa5796275c..787c41641e 100644 --- a/Wox/PluginLoader/Plugins.cs +++ b/Wox/PluginLoader/Plugins.cs @@ -6,6 +6,8 @@ using System.Threading; using Microsoft.CSharp; using Wox.Helper; using Wox.Infrastructure; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; namespace Wox.PluginLoader @@ -20,7 +22,7 @@ namespace Wox.PluginLoader plugins.Clear(); BasePluginLoader.ParsePluginsConfig(); - if (CommonStorage.Instance.UserSetting.EnablePythonPlugins) + if (UserSettingStorage.Instance.EnablePythonPlugins) { plugins.AddRange(new PythonPluginLoader().LoadPlugin()); } diff --git a/Wox/ProgramSourceSetting.xaml.cs b/Wox/ProgramSourceSetting.xaml.cs index 8f0977872c..12a446c911 100644 --- a/Wox/ProgramSourceSetting.xaml.cs +++ b/Wox/ProgramSourceSetting.xaml.cs @@ -13,7 +13,8 @@ using System.Windows.Media.Imaging; using System.Windows.Shapes; using Wox.Helper; using Wox.Infrastructure; -using Wox.Infrastructure.UserSettings; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; using MessageBox = System.Windows.MessageBox; namespace Wox @@ -34,7 +35,7 @@ namespace Wox public void UpdateItem(ProgramSource programSource) { - updateProgramSource = CommonStorage.Instance.UserSetting.ProgramSources.FirstOrDefault(o => o == programSource); + updateProgramSource = UserSettingStorage.Instance.ProgramSources.FirstOrDefault(o => o == programSource); if (updateProgramSource == null) { MessageBox.Show("Invalid program source"); @@ -84,17 +85,17 @@ namespace Wox Type = type, BonusPoints = bonusPoint }; - if (CommonStorage.Instance.UserSetting.ProgramSources.Exists(o => o.ToString() == p.ToString() && o != p)) + if (UserSettingStorage.Instance.ProgramSources.Exists(o => o.ToString() == p.ToString() && o != p)) { MessageBox.Show("Program source already exists!"); return; } - CommonStorage.Instance.UserSetting.ProgramSources.Add(p); + UserSettingStorage.Instance.ProgramSources.Add(p); MessageBox.Show(string.Format("Add {0} program source successfully!", p.ToString())); } else { - if (CommonStorage.Instance.UserSetting.ProgramSources.Exists(o => o.ToString() == updateProgramSource.ToString() && o != updateProgramSource)) + if (UserSettingStorage.Instance.ProgramSources.Exists(o => o.ToString() == updateProgramSource.ToString() && o != updateProgramSource)) { MessageBox.Show("Program source already exists!"); return; @@ -105,7 +106,7 @@ namespace Wox updateProgramSource.BonusPoints = bonusPoint; MessageBox.Show(string.Format("Update {0} program source successfully!", updateProgramSource.ToString())); } - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.Save(); settingWindow.ReloadProgramSourceView(); Close(); } diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 177611091c..e251cb152b 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -7,8 +7,10 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Media; using IWshRuntimeLibrary; +using Microsoft.VisualBasic.ApplicationServices; using Wox.Infrastructure; -using Wox.Infrastructure.UserSettings; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; using Application = System.Windows.Forms.Application; using File = System.IO.File; @@ -36,40 +38,40 @@ namespace Wox private void Setting_Loaded(object sender, RoutedEventArgs ev) { ctlHotkey.OnHotkeyChanged += ctlHotkey_OnHotkeyChanged; - ctlHotkey.SetHotkey(CommonStorage.Instance.UserSetting.Hotkey, false); + ctlHotkey.SetHotkey(UserSettingStorage.Instance.Hotkey, false); cbReplaceWinR.Checked += (o, e) => { - CommonStorage.Instance.UserSetting.ReplaceWinR = true; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.ReplaceWinR = true; + UserSettingStorage.Instance.Save(); }; cbReplaceWinR.Unchecked += (o, e) => { - CommonStorage.Instance.UserSetting.ReplaceWinR = false; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.ReplaceWinR = false; + UserSettingStorage.Instance.Save(); }; cbEnablePythonPlugins.Checked += (o, e) => { - CommonStorage.Instance.UserSetting.EnablePythonPlugins = true; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.EnablePythonPlugins = true; + UserSettingStorage.Instance.Save(); }; cbEnablePythonPlugins.Unchecked += (o, e) => { - CommonStorage.Instance.UserSetting.EnablePythonPlugins = false; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.EnablePythonPlugins = false; + UserSettingStorage.Instance.Save(); }; #region Load Theme Data - if (!string.IsNullOrEmpty(CommonStorage.Instance.UserSetting.QueryBoxFont) && - Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(CommonStorage.Instance.UserSetting.QueryBoxFont)) > 0) + if (!string.IsNullOrEmpty(UserSettingStorage.Instance.QueryBoxFont) && + Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.QueryBoxFont)) > 0) { - cbQueryBoxFont.Text = CommonStorage.Instance.UserSetting.QueryBoxFont; + cbQueryBoxFont.Text = UserSettingStorage.Instance.QueryBoxFont; } - if (!string.IsNullOrEmpty(CommonStorage.Instance.UserSetting.ResultItemFont) && - Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(CommonStorage.Instance.UserSetting.ResultItemFont)) > 0) + if (!string.IsNullOrEmpty(UserSettingStorage.Instance.ResultItemFont) && + Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultItemFont)) > 0) { - cbResultItemFont.Text = CommonStorage.Instance.UserSetting.ResultItemFont; + cbResultItemFont.Text = UserSettingStorage.Instance.ResultItemFont; } resultPanelPreview.AddResults(new List() { @@ -118,12 +120,12 @@ namespace Wox themeComboBox.Items.Add(themeName); } - themeComboBox.SelectedItem = CommonStorage.Instance.UserSetting.Theme; - cbReplaceWinR.IsChecked = CommonStorage.Instance.UserSetting.ReplaceWinR; - webSearchView.ItemsSource = CommonStorage.Instance.UserSetting.WebSearches; - programSourceView.ItemsSource = CommonStorage.Instance.UserSetting.ProgramSources; - lvCustomHotkey.ItemsSource = CommonStorage.Instance.UserSetting.CustomPluginHotkeys; - cbEnablePythonPlugins.IsChecked = CommonStorage.Instance.UserSetting.EnablePythonPlugins; + themeComboBox.SelectedItem = UserSettingStorage.Instance.Theme; + cbReplaceWinR.IsChecked = UserSettingStorage.Instance.ReplaceWinR; + webSearchView.ItemsSource = UserSettingStorage.Instance.WebSearches; + programSourceView.ItemsSource = UserSettingStorage.Instance.ProgramSources; + lvCustomHotkey.ItemsSource = UserSettingStorage.Instance.CustomPluginHotkeys; + cbEnablePythonPlugins.IsChecked = UserSettingStorage.Instance.EnablePythonPlugins; cbStartWithWindows.IsChecked = File.Exists(woxLinkPath); } @@ -158,7 +160,7 @@ namespace Wox MessageBox.Show("Are your sure to delete " + seletedWebSearch.Title, "Delete WebSearch", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - CommonStorage.Instance.UserSetting.WebSearches.Remove(seletedWebSearch); + UserSettingStorage.Instance.WebSearches.Remove(seletedWebSearch); webSearchView.Items.Refresh(); } else @@ -195,7 +197,7 @@ namespace Wox MessageBox.Show("Are your sure to delete " + seletedProgramSource.ToString(), "Delete ProgramSource", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - CommonStorage.Instance.UserSetting.ProgramSources.Remove(seletedProgramSource); + UserSettingStorage.Instance.ProgramSources.Remove(seletedProgramSource); programSourceView.Items.Refresh(); } else @@ -222,8 +224,8 @@ namespace Wox private void CbStartWithWindows_OnChecked(object sender, RoutedEventArgs e) { CreateStartupFolderShortcut(); - CommonStorage.Instance.UserSetting.StartWoxOnSystemStartup = true; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.StartWoxOnSystemStartup = true; + UserSettingStorage.Instance.Save(); } private void CbStartWithWindows_OnUnchecked(object sender, RoutedEventArgs e) @@ -233,8 +235,8 @@ namespace Wox File.Delete(woxLinkPath); } - CommonStorage.Instance.UserSetting.StartWoxOnSystemStartup = false; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.StartWoxOnSystemStartup = false; + UserSettingStorage.Instance.Save(); } private void CreateStartupFolderShortcut() @@ -265,9 +267,9 @@ namespace Wox MainWindow.HideApp(); } }); - MainWindow.RemoveHotkey(CommonStorage.Instance.UserSetting.Hotkey); - CommonStorage.Instance.UserSetting.Hotkey = ctlHotkey.CurrentHotkey.ToString(); - CommonStorage.Instance.Save(); + MainWindow.RemoveHotkey(UserSettingStorage.Instance.Hotkey); + UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString(); + UserSettingStorage.Instance.Save(); } } @@ -280,9 +282,9 @@ namespace Wox MessageBox.Show("Are your sure to delete " + item.Hotkey + " plugin hotkey?", "Delete Custom Plugin Hotkey", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - CommonStorage.Instance.UserSetting.CustomPluginHotkeys.Remove(item); + UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item); lvCustomHotkey.Items.Refresh(); - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.Save(); MainWindow.RemoveHotkey(item.Hotkey); } else @@ -328,24 +330,24 @@ namespace Wox { string themeName = themeComboBox.SelectedItem.ToString(); MainWindow.SetTheme(themeName); - CommonStorage.Instance.UserSetting.Theme = themeName; - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.Theme = themeName; + UserSettingStorage.Instance.Save(); } private void CbQueryBoxFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { string queryBoxFontName = cbQueryBoxFont.SelectedItem.ToString(); - CommonStorage.Instance.UserSetting.QueryBoxFont = queryBoxFontName; - CommonStorage.Instance.Save(); - App.Window.SetTheme(CommonStorage.Instance.UserSetting.Theme); + UserSettingStorage.Instance.QueryBoxFont = queryBoxFontName; + UserSettingStorage.Instance.Save(); + App.Window.SetTheme(UserSettingStorage.Instance.Theme); } private void CbResultItemFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { string resultItemFont = cbResultItemFont.SelectedItem.ToString(); - CommonStorage.Instance.UserSetting.ResultItemFont = resultItemFont; - CommonStorage.Instance.Save(); - App.Window.SetTheme(CommonStorage.Instance.UserSetting.Theme); + UserSettingStorage.Instance.ResultItemFont = resultItemFont; + UserSettingStorage.Instance.Save(); + App.Window.SetTheme(UserSettingStorage.Instance.Theme); } #endregion diff --git a/Wox/WebSearchSetting.xaml.cs b/Wox/WebSearchSetting.xaml.cs index 19c07129d1..fd5bf997db 100644 --- a/Wox/WebSearchSetting.xaml.cs +++ b/Wox/WebSearchSetting.xaml.cs @@ -13,7 +13,8 @@ using System.Windows.Media.Imaging; using System.Windows.Shapes; using Wox.Helper; using Wox.Infrastructure; -using Wox.Infrastructure.UserSettings; +using Wox.Infrastructure.Storage; +using Wox.Infrastructure.Storage.UserSettings; using MessageBox = System.Windows.MessageBox; namespace Wox @@ -32,7 +33,7 @@ namespace Wox public void UpdateItem(WebSearch webSearch) { - updateWebSearch = CommonStorage.Instance.UserSetting.WebSearches.FirstOrDefault(o => o == webSearch); + updateWebSearch = UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch); if (updateWebSearch == null || string.IsNullOrEmpty(updateWebSearch.Url)) { MessageBox.Show("Invalid web search"); @@ -92,12 +93,12 @@ namespace Wox if (!update) { - if (CommonStorage.Instance.UserSetting.WebSearches.Exists(o => o.ActionWord == action)) + if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == action)) { MessageBox.Show("ActionWord has existed, please input a new one."); return; } - CommonStorage.Instance.UserSetting.WebSearches.Add(new WebSearch() + UserSettingStorage.Instance.WebSearches.Add(new WebSearch() { ActionWord = action, Enabled = cbEnable.IsChecked ?? false, @@ -109,7 +110,7 @@ namespace Wox } else { - if (CommonStorage.Instance.UserSetting.WebSearches.Exists(o => o.ActionWord == action && o != updateWebSearch)) + if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == action && o != updateWebSearch)) { MessageBox.Show("ActionWord has existed, please input a new one."); return; @@ -121,7 +122,7 @@ namespace Wox updateWebSearch.Title= title; MessageBox.Show(string.Format("Update {0} web search successfully!", title)); } - CommonStorage.Instance.Save(); + UserSettingStorage.Instance.Save(); settingWindow.ReloadWebSearchView(); Close(); } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index f3b1bc0a7c..d4ae1f6b26 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -341,6 +341,14 @@ True + + + PreserveNewest + + + PreserveNewest + +