close #48 Refactor setting storage.

This commit is contained in:
qianlifeng 2014-03-23 16:17:41 +08:00
parent fc07979966
commit 4ca0453cff
27 changed files with 319 additions and 321 deletions

View File

@ -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;
}
}
}
}

View 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);
}
}
}
}

View 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;
}
}
}

View File

@ -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

View File

@ -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"; }
}
}
}

View File

@ -1,4 +1,4 @@
namespace Wox.Infrastructure.UserSettings
namespace Wox.Infrastructure.Storage.UserSettings
{
public class WebSearch
{

View File

@ -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; }
}
}

View File

@ -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;
}
}
}

View File

@ -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>

View File

@ -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<string, int> cmdHistory = new Dictionary<string, int>();
private string filePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\CMDHistory.dat";
private PluginInitContext context;
protected override List<Result> QueryInternal(Query query)
@ -20,7 +16,7 @@ namespace Wox.Plugin.System
List<Result> results = new List<Result>();
if (query.RawQuery == ">")
{
IEnumerable<Result> history = cmdHistory.OrderByDescending(o => o.Value)
IEnumerable<Result> 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<Result> history = cmdHistory.Where(o => o.Key.Contains(cmd))
IEnumerable<Result> 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<string> 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<string, int>)b.Deserialize(fileStream);
fileStream.Close();
}
if (cmdHistory.Count > 1000)
{
List<string> 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();
}
}
}

View File

@ -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<CMDStorage>
{
[JsonProperty]
public Dictionary<string, int> CMDHistory = new Dictionary<string, int>();
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();
}
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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),

View File

@ -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)
{

View File

@ -51,6 +51,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CMD\CMDStorage.cs" />
<Compile Include="ProgramSources\AppPathsProgramSource.cs" />
<Compile Include="ProgramSources\CommonStartMenuProgramSource.cs" />
<Compile Include="ProgramSources\PortableAppsProgramSource.cs" />
@ -61,7 +62,7 @@
<Compile Include="ProgramSources\FileSystemProgramSource.cs" />
<Compile Include="ProgramSources\UserStartMenuProgramSource.cs" />
<Compile Include="WebSearchPlugin.cs" />
<Compile Include="CMD.cs" />
<Compile Include="CMD\CMD.cs" />
<Compile Include="DirectoryIndicator.cs" />
<Compile Include="ISystemPlugin.cs" />
<Compile Include="Programs.cs" />

View File

@ -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<CustomPluginHotkey>();
UserSettingStorage.Instance.CustomPluginHotkeys = new List<CustomPluginHotkey>();
}
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");

View File

@ -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);

View File

@ -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());
}

View File

@ -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();
}

View File

@ -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<Result>()
{
@ -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

View File

@ -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();
}

View File

@ -341,6 +341,14 @@
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<Resource Include="Images\websearch\google.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\websearch\wiki.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<PropertyGroup>