Remove instance logic for BinaryStorage and JsonStorage, part 1

1. part of #389
2. huge refactoring
This commit is contained in:
bao-qian
2016-04-21 01:53:21 +01:00
parent 0bcb76fa81
commit 8d10c9aa41
52 changed files with 502 additions and 584 deletions

View File

@@ -25,7 +25,7 @@ namespace Wox.Core.Plugin
/// </summary>
private static readonly List<string> PluginDirectories = new List<string>();
public static IEnumerable<PluginPair> AllPlugins { get; private set; }
public static List<PluginPair> AllPlugins { get; private set; }
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
@@ -69,10 +69,8 @@ namespace Wox.Core.Plugin
SetupPluginDirectories();
var metadatas = PluginConfig.Parse(PluginDirectories);
AllPlugins = (new CSharpPluginLoader().LoadPlugin(metadatas)).
Concat(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(metadatas));
AllPlugins = new CSharpPluginLoader().LoadPlugin(metadatas).Concat(
new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(metadatas)).ToList();
}
public static void InitializePlugins(IPublicAPI api)
@@ -164,6 +162,42 @@ namespace Wox.Core.Plugin
return GlobalPlugins;
}
}
//happlebao todo prevent plugin initial when plugin is disabled
public static void DisablePlugin(PluginPair plugin)
{
var actionKeywords = plugin.Metadata.ActionKeywords;
if (actionKeywords == null || actionKeywords.Count == 0 || actionKeywords[0] == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Remove(plugin);
}
else
{
foreach (var actionkeyword in plugin.Metadata.ActionKeywords)
{
NonGlobalPlugins.Remove(actionkeyword);
}
}
AllPlugins.Remove(plugin);
}
public static void EnablePlugin(PluginPair plugin)
{
var actionKeywords = plugin.Metadata.ActionKeywords;
if (actionKeywords == null || actionKeywords.Count == 0 || actionKeywords[0] == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Add(plugin);
}
else
{
foreach (var actionkeyword in plugin.Metadata.ActionKeywords)
{
NonGlobalPlugins[actionkeyword] = plugin;
}
}
AllPlugins.Add(plugin);
}
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
{
var results = new List<Result>();
@@ -184,7 +218,7 @@ namespace Wox.Core.Plugin
}
catch (Exception e)
{
throw new WoxPluginException(pair.Metadata.Name, $"QueryForPlugin failed", e);
throw new WoxPluginException(pair.Metadata.Name, "QueryForPlugin failed", e);
}
return results;
}

View File

@@ -12,7 +12,7 @@ namespace Wox.Core.Resource
{
public class Internationalization : Resource
{
public UserSettingStorage Settings { get; set; }
public UserSettings.Settings Settings { get; set; }
public Internationalization()
{
@@ -67,7 +67,6 @@ namespace Wox.Core.Resource
}
Settings.Language = language.LanguageCode;
Settings.Save();
ResourceMerger.UpdateResource(this);
}

View File

@@ -15,7 +15,7 @@ namespace Wox.Core.Resource
public class Theme : Resource
{
private static List<string> themeDirectories = new List<string>();
public UserSettingStorage Settings { get; set; }
public UserSettings.Settings Settings { get; set; }
public Theme()
{
@@ -55,7 +55,6 @@ namespace Wox.Core.Resource
}
Settings.Theme = themeName;
Settings.Save();
ResourceMerger.UpdateResource(this);
// Exception of FindResource can't be cathed if global exception handle is set

View File

@@ -22,7 +22,7 @@ namespace Wox.Core.Updater
private const string UpdateFeedURL = "http://upgrade.getwox.com/update.xml";
//private const string UpdateFeedURL = "http://127.0.0.1:8888/update.xml";
private static SemanticVersion currentVersion;
private UserSettingStorage _settings;
public UserSettings.Settings Settings { get; set; }
public event EventHandler PrepareUpdateReady;
public event EventHandler UpdateError;
@@ -44,7 +44,6 @@ namespace Wox.Core.Updater
private UpdaterManager()
{
UpdateManager.Instance.UpdateSource = GetUpdateSource();
_settings = UserSettingStorage.Instance;
}
public SemanticVersion CurrentVersion
@@ -89,7 +88,7 @@ namespace Wox.Core.Updater
try
{
NewRelease = JsonConvert.DeserializeObject<Release>(json);
if (IsNewerThanCurrent(NewRelease) && !_settings.DontPromptUpdateMsg)
if (IsNewerThanCurrent(NewRelease) && !Settings.DontPromptUpdateMsg)
{
StartUpdate();
}
@@ -148,7 +147,7 @@ namespace Wox.Core.Updater
// get out of the way so the console window isn't obstructed
try
{
UpdateManager.Instance.ApplyUpdates(true, _settings.EnableUpdateLog, false);
UpdateManager.Instance.ApplyUpdates(true, Settings.EnableUpdateLog, false);
}
catch (Exception e)
{

View File

@@ -3,7 +3,7 @@
namespace Wox.Core.UserSettings
{
public class CustomizedPluginConfig
public class PluginSetting
{
public string ID { get; set; }

View File

@@ -5,7 +5,7 @@ namespace Wox.Core.UserSettings
public class HttpProxy : IHttpProxy
{
private static readonly HttpProxy instance = new HttpProxy();
public UserSettingStorage Settings { get; set; }
public Settings Settings { get; set; }
public static HttpProxy Instance => instance;
public bool Enabled => Settings.ProxyEnabled;

View File

@@ -2,7 +2,6 @@
namespace Wox.Core.UserSettings
{
[Serializable]
public class CustomPluginHotkey
{
public string Hotkey { get; set; }

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using Wox.Core.Plugin;
@@ -9,119 +10,66 @@ using Newtonsoft.Json;
namespace Wox.Core.UserSettings
{
public class UserSettingStorage : JsonStrorage<UserSettingStorage>
public class Settings
{
public bool DontPromptUpdateMsg { get; set; }
public int ActivateTimes { get; set; }
public bool EnableUpdateLog { get; set; }
public string Hotkey { get; set; }
public string Language { get; set; }
public string Theme { get; set; }
public string QueryBoxFont { get; set; }
public string Hotkey { get; set; } = "Alt + Space";
public string Language { get; set; } = "en";
public string Theme { get; set; } = "Dark";
public string QueryBoxFont { get; set; } = FontFamily.GenericSansSerif.Name;
public string QueryBoxFontStyle { get; set; }
public string QueryBoxFontWeight { get; set; }
public string QueryBoxFontStretch { get; set; }
public string ResultFont { get; set; }
public string ResultFont { get; set; } = FontFamily.GenericSansSerif.Name;
public string ResultFontStyle { get; set; }
public string ResultFontWeight { get; set; }
public string ResultFontStretch { get; set; }
public double WindowLeft { get; set; }
public double WindowTop { get; set; }
public int MaxResultsToShow { get; set; } = 6;
public int ActivateTimes { get; set; }
// Order defaults to 0 or -1, so 1 will let this property appear last
[JsonProperty(Order = 1)]
public Dictionary<string, CustomizedPluginConfig> CustomizedPluginConfigs { get; set; }
public Dictionary<string, PluginSetting> PluginSettings { get; set; } = new Dictionary<string, PluginSetting>();
public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; } = new List<CustomPluginHotkey>();
public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; }
[Obsolete]
public double Opacity { get; set; } = 1;
[Obsolete]
public OpacityMode OpacityMode { get; set; } = OpacityMode.Normal;
public bool DontPromptUpdateMsg { get; set; }
public bool EnableUpdateLog { get; set; }
public bool StartWoxOnSystemStartup { get; set; }
[Obsolete]
public double Opacity { get; set; }
[Obsolete]
public OpacityMode OpacityMode { get; set; }
public bool LeaveCmdOpen { get; set; }
public bool HideWhenDeactive { get; set; }
public bool RememberLastLaunchLocation { get; set; }
public bool IgnoreHotkeysOnFullscreen { get; set; }
public string ProxyServer { get; set; }
public bool ProxyEnabled { get; set; }
public int ProxyPort { get; set; }
public string ProxyUserName { get; set; }
public string ProxyPassword { get; set; }
public int MaxResultsToShow { get; set; }
protected override string FileName { get; } = "Settings";
public void IncreaseActivateTimes()
{
ActivateTimes++;
if (ActivateTimes % 15 == 0)
{
Save();
}
}
protected override UserSettingStorage LoadDefault()
{
DontPromptUpdateMsg = false;
Theme = "Dark";
Language = "en";
CustomizedPluginConfigs = new Dictionary<string, CustomizedPluginConfig>();
Hotkey = "Alt + Space";
QueryBoxFont = FontFamily.GenericSansSerif.Name;
ResultFont = FontFamily.GenericSansSerif.Name;
Opacity = 1;
OpacityMode = OpacityMode.Normal;
LeaveCmdOpen = false;
HideWhenDeactive = false;
CustomPluginHotkeys = new List<CustomPluginHotkey>();
RememberLastLaunchLocation = false;
MaxResultsToShow = 6;
return this;
}
protected override void OnAfterLoad(UserSettingStorage storage)
public void UpdatePluginSettings()
{
var metadatas = PluginManager.AllPlugins.Select(p => p.Metadata);
if (storage.CustomizedPluginConfigs == null)
if (PluginSettings == null)
{
var configs = new Dictionary<string, CustomizedPluginConfig>();
var configs = new Dictionary<string, PluginSetting>();
foreach (var metadata in metadatas)
{
addPluginMetadata(configs, metadata);
}
storage.CustomizedPluginConfigs = configs;
PluginSettings = configs;
}
else
{
var configs = storage.CustomizedPluginConfigs;
var configs = PluginSettings;
foreach (var metadata in metadatas)
{
if (configs.ContainsKey(metadata.ID))
@@ -139,26 +87,12 @@ namespace Wox.Core.UserSettings
}
}
}
if (storage.QueryBoxFont == null)
{
storage.QueryBoxFont = FontFamily.GenericSansSerif.Name;
}
if (storage.ResultFont == null)
{
storage.ResultFont = FontFamily.GenericSansSerif.Name;
}
if (storage.Language == null)
{
storage.Language = "en";
}
}
private void addPluginMetadata(Dictionary<string, CustomizedPluginConfig> configs, PluginMetadata metadata)
private void addPluginMetadata(Dictionary<string, PluginSetting> configs, PluginMetadata metadata)
{
configs[metadata.ID] = new CustomizedPluginConfig
configs[metadata.ID] = new PluginSetting
{
ID = metadata.ID,
Name = metadata.Name,
@@ -169,10 +103,10 @@ namespace Wox.Core.UserSettings
public void UpdateActionKeyword(PluginMetadata metadata)
{
var config = CustomizedPluginConfigs[metadata.ID];
var config = PluginSettings[metadata.ID];
config.ActionKeywords = metadata.ActionKeywords;
Save();
}
}
public enum OpacityMode