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

@@ -8,6 +8,7 @@ using WindowsInput;
using WindowsInput.Native;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Control = System.Windows.Controls.Control;
namespace Wox.Plugin.CMD
@@ -17,7 +18,20 @@ namespace Wox.Plugin.CMD
private PluginInitContext context;
private bool WinRStroked;
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
private readonly CMDStorage _settings = CMDStorage.Instance;
private readonly CMDHistory _settings;
private readonly PluginSettingsStorage<CMDHistory> _storage;
public CMD()
{
_storage = new PluginSettingsStorage<CMDHistory>();
_settings = _storage.Load();
}
~CMD()
{
_storage.Save();
}
public List<Result> Query(Query query)
{
@@ -81,7 +95,7 @@ namespace Wox.Plugin.CMD
private List<Result> GetHistoryCmds(string cmd, Result result)
{
IEnumerable<Result> history = _settings.CMDHistory.Where(o => o.Key.Contains(cmd))
IEnumerable<Result> history = _settings.Count.Where(o => o.Key.Contains(cmd))
.OrderByDescending(o => o.Value)
.Select(m =>
{
@@ -127,7 +141,7 @@ namespace Wox.Plugin.CMD
private List<Result> ResultsFromlHistory()
{
IEnumerable<Result> history = _settings.CMDHistory.OrderByDescending(o => o.Value)
IEnumerable<Result> history = _settings.Count.OrderByDescending(o => o.Value)
.Select(m => new Result
{
Title = m.Key,

View File

@@ -5,9 +5,9 @@ namespace Wox.Plugin.CMD
{
public partial class CMDSetting : UserControl
{
private readonly CMDStorage _settings;
private readonly CMDHistory _settings;
public CMDSetting(CMDStorage settings)
public CMDSetting(CMDHistory settings)
{
InitializeComponent();
_settings = settings;
@@ -21,24 +21,20 @@ namespace Wox.Plugin.CMD
cbLeaveCmdOpen.Checked += (o, e) =>
{
_settings.LeaveCmdOpen = true;
_settings.Save();
};
cbLeaveCmdOpen.Unchecked += (o, e) =>
{
_settings.LeaveCmdOpen = false;
_settings.Save();
};
cbReplaceWinR.Checked += (o, e) =>
{
_settings.ReplaceWinR = true;
_settings.Save();
};
cbReplaceWinR.Unchecked += (o, e) =>
{
_settings.ReplaceWinR = false;
_settings.Save();
};
}
}

View File

@@ -1,40 +1,26 @@
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage;
namespace Wox.Plugin.CMD
{
public class CMDStorage : JsonStrorage<CMDStorage>
public class CMDHistory
{
[JsonProperty]
public bool ReplaceWinR { get; set; }
[JsonProperty]
public bool ReplaceWinR { get; set; } = true;
public bool LeaveCmdOpen { get; set; }
[JsonProperty]
public Dictionary<string, int> CMDHistory = new Dictionary<string, int>();
protected override string FileName { get; } = "CMDHistory";
protected override CMDStorage LoadDefault()
{
ReplaceWinR = true;
return this;
}
public Dictionary<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName)
{
if (CMDHistory.ContainsKey(cmdName))
if (Count.ContainsKey(cmdName))
{
CMDHistory[cmdName] += 1;
Count[cmdName] += 1;
}
else
{
CMDHistory.Add(cmdName, 1);
Count.Add(cmdName, 1);
}
Save();
}
}
}

View File

@@ -1,44 +1,22 @@
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage;
using JsonProperty = Newtonsoft.Json.JsonPropertyAttribute;
namespace Wox.Plugin.Everything
{
public class ContextMenuStorage : JsonStrorage<ContextMenuStorage>
public class Settings
{
[JsonProperty] public List<ContextMenu> ContextMenus = new List<ContextMenu>();
public List<ContextMenu> ContextMenus = new List<ContextMenu>();
[JsonProperty]
public int MaxSearchCount { get; set; }
public IPublicAPI API { get; set; }
protected override string FileName { get; } = "EverythingContextMenu";
protected override void OnAfterLoad(ContextMenuStorage obj)
{
}
protected override ContextMenuStorage LoadDefault()
{
MaxSearchCount = 100;
return this;
}
public int MaxSearchCount { get; set; } = 100;
}
public class ContextMenu
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public string Command { get; set; }
[JsonProperty]
public string Argument { get; set; }
[JsonProperty]
public string ImagePath { get; set; }
}
}

View File

@@ -7,6 +7,8 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Windows;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure;
using Wox.Plugin.Everything.Everything;
namespace Wox.Plugin.Everything
@@ -16,11 +18,26 @@ namespace Wox.Plugin.Everything
private readonly EverythingAPI _api = new EverythingAPI();
private static readonly List<string> ImageExts = new List<string> { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
private static readonly List<string> ExecutableExts = new List<string> { ".exe" };
private const string PortableEverything = "PortableEverything";
private const string EverythingProcessName = "Everything";
private const string PortableEverything = "PortableEverything";
internal static string LibraryPath;
private PluginInitContext _context;
private ContextMenuStorage _settings = ContextMenuStorage.Instance;
private readonly Settings _settings;
private readonly PluginSettingsStorage<Settings> _storage;
public Main()
{
_storage = new PluginSettingsStorage<Settings>();
_settings = _storage.Load();
}
~Main()
{
_storage.Save();
}
public List<Result> Query(Query query)
{
@@ -31,7 +48,6 @@ namespace Wox.Plugin.Everything
if (_settings.MaxSearchCount <= 0)
{
_settings.MaxSearchCount = 50;
_settings.Save();
}
if (keyword == "uninstalleverything")
@@ -158,19 +174,22 @@ namespace Wox.Plugin.Everything
public void Init(PluginInitContext context)
{
_context = context;
_settings.API = context.API;
LoadLibrary(Path.Combine(context.CurrentPluginMetadata.PluginDirectory,
PortableEverything, GetCpuType(), "Everything.dll"));
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
var libraryDirectory = Path.Combine(pluginDirectory, PortableEverything, CpuType());
LibraryPath = Path.Combine(libraryDirectory, "Everything.dll");
LoadLibrary(LibraryPath);
//Helper.AddDLLDirectory(libraryDirectory);
StartEverything();
}
private string GetCpuType()
private string CpuType()
{
return (IntPtr.Size == 4) ? "x86" : "x64";
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
}
private void StartEverything()
{
if (!CheckEverythingServiceRunning())
@@ -268,9 +287,11 @@ namespace Wox.Plugin.Everything
private string GetEverythingPath()
{
string directory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory,
PortableEverything, GetCpuType(),
"Everything.exe");
string directory = Path.Combine(
_context.CurrentPluginMetadata.PluginDirectory,
PortableEverything, CpuType(),
"Everything.exe"
);
return directory;
}

View File

@@ -4,7 +4,7 @@ using Newtonsoft.Json;
namespace Wox.Plugin.Folder
{
[Serializable]
[JsonObject(MemberSerialization.OptIn)]
public class FolderLink
{
[JsonProperty]

View File

@@ -5,6 +5,7 @@ using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Wox.Infrastructure.Storage;
namespace Wox.Plugin.Folder
{
@@ -12,7 +13,20 @@ namespace Wox.Plugin.Folder
{
private static List<string> driverNames;
private PluginInitContext context;
private FolderStorage _settings = FolderStorage.Instance;
private readonly Settings _settings;
private readonly PluginSettingsStorage<Settings> _storage;
public FolderPlugin()
{
_storage = new PluginSettingsStorage<Settings>();
_settings = _storage.Load();
}
~FolderPlugin()
{
_storage.Save();
}
public Control CreateSettingPanel()
{
@@ -26,7 +40,6 @@ namespace Wox.Plugin.Folder
if (_settings.FolderLinks == null)
{
_settings.FolderLinks = new List<FolderLink>();
_settings.Save();
}
}

View File

@@ -14,9 +14,9 @@ namespace Wox.Plugin.Folder
public partial class FileSystemSettings
{
private IPublicAPI woxAPI;
private FolderStorage _settings;
private Settings _settings;
public FileSystemSettings(IPublicAPI woxAPI, FolderStorage settings)
public FileSystemSettings(IPublicAPI woxAPI, Settings settings)
{
this.woxAPI = woxAPI;
InitializeComponent();
@@ -35,7 +35,6 @@ namespace Wox.Plugin.Folder
{
_settings.FolderLinks.Remove(selectedFolder);
lbxFolders.Items.Refresh();
_settings.Save();
}
}
else
@@ -56,8 +55,6 @@ namespace Wox.Plugin.Folder
{
var link = _settings.FolderLinks.First(x => x.Path == selectedFolder.Path);
link.Path = folderBrowserDialog.SelectedPath;
_settings.Save();
}
lbxFolders.Items.Refresh();
@@ -85,7 +82,6 @@ namespace Wox.Plugin.Folder
}
_settings.FolderLinks.Add(newFolder);
_settings.Save();
}
lbxFolders.Items.Refresh();
@@ -112,7 +108,6 @@ namespace Wox.Plugin.Folder
};
_settings.FolderLinks.Add(newFolder);
_settings.Save();
}
lbxFolders.Items.Refresh();

View File

@@ -4,11 +4,10 @@ using Wox.Infrastructure.Storage;
namespace Wox.Plugin.Folder
{
public class FolderStorage : JsonStrorage<FolderStorage>
public class Settings
{
[JsonProperty]
public List<FolderLink> FolderLinks { get; set; }
protected override string FileName { get; } = "settings_folder_plugin";
}
}

View File

@@ -14,8 +14,6 @@ namespace Wox.Plugin.PluginIndicator
var results = from keyword in PluginManager.NonGlobalPlugins.Keys
where keyword.StartsWith(query.Terms[0])
let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata
let customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[metadata.ID]
where !customizedPluginConfig.Disabled
select new Result
{
Title = keyword,

View File

@@ -10,16 +10,16 @@ namespace Wox.Plugin.Program
public partial class AddProgramSource
{
private ProgramSource _editing;
private ProgramStorage _settings;
private Settings _settings;
public AddProgramSource(ProgramStorage settings)
public AddProgramSource(Settings settings)
{
_settings = settings;
InitializeComponent();
Suffixes.Text = string.Join(";", settings.ProgramSuffixes);
}
public AddProgramSource(ProgramSource edit, ProgramStorage settings)
public AddProgramSource(ProgramSource edit, Settings settings)
{
_editing = edit;
Directory.Text = _editing.Location;
@@ -65,7 +65,6 @@ namespace Wox.Plugin.Program
_editing.Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator);
}
_settings.Save();
DialogResult = true;
Close();
}

View File

@@ -5,10 +5,8 @@ using Wox.Infrastructure.Storage;
namespace Wox.Plugin.Program
{
[Serializable]
public class ProgramCacheStorage : BinaryStorage<ProgramCacheStorage>
public class ProgramIndexCache
{
public List<Program> Programs = new List<Program>();
protected override string FileName { get; } = "ProgramIndexCache";
}
}

View File

@@ -11,9 +11,9 @@ namespace Wox.Plugin.Program
public partial class ProgramSetting : UserControl
{
private PluginInitContext context;
private ProgramStorage _settings;
private Settings _settings;
public ProgramSetting(PluginInitContext context, ProgramStorage settings)
public ProgramSetting(PluginInitContext context, Settings settings)
{
this.context = context;
InitializeComponent();
@@ -58,7 +58,6 @@ namespace Wox.Plugin.Program
if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
_settings.ProgramSources.Remove(selectedProgramSource);
_settings.Save();
ReIndexing();
}
}
@@ -127,7 +126,6 @@ namespace Wox.Plugin.Program
Enabled = true
});
_settings.Save();
ReIndexing();
}
}
@@ -137,14 +135,12 @@ namespace Wox.Plugin.Program
private void StartMenuEnabled_Click(object sender, RoutedEventArgs e)
{
_settings.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false;
_settings.Save();
ReIndexing();
}
private void RegistryEnabled_Click(object sender, RoutedEventArgs e)
{
_settings.EnableRegistrySource = RegistryEnabled.IsChecked ?? false;
_settings.Save();
ReIndexing();
}
}

View File

@@ -1,45 +1,16 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage;
namespace Wox.Plugin.Program
{
[Serializable]
public class ProgramStorage : JsonStrorage<ProgramStorage>
public class Settings
{
[JsonProperty]
public List<ProgramSource> ProgramSources { get; set; }
public List<ProgramSource> ProgramSources { get; set; } = new List<ProgramSource>();
public string[] ProgramSuffixes { get; set; } = {"bat", "appref-ms", "exe", "lnk"};
public bool EnableStartMenuSource { get; set; } = true;
[JsonProperty]
public string[] ProgramSuffixes { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[DefaultValue(true)]
public bool EnableStartMenuSource { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[DefaultValue(true)]
public bool EnableRegistrySource { get; set; }
protected override ProgramStorage LoadDefault()
{
ProgramSources = new List<ProgramSource>();
EnableStartMenuSource = true;
EnableRegistrySource = true;
return this;
}
protected override void OnAfterLoad(ProgramStorage storage)
{
if (storage.ProgramSuffixes == null || storage.ProgramSuffixes.Length == 0)
{
storage.ProgramSuffixes = new[] {"bat", "appref-ms", "exe", "lnk"};
}
}
protected override string FileName { get; } = "settings_plugin_program";
public bool EnableRegistrySource { get; set; } = true;
}
}

View File

@@ -9,9 +9,9 @@ namespace Wox.Plugin.Program
public partial class ProgramSuffixes
{
private PluginInitContext context;
private ProgramStorage _settings;
private Settings _settings;
public ProgramSuffixes(PluginInitContext context, ProgramStorage settings)
public ProgramSuffixes(PluginInitContext context, Settings settings)
{
this.context = context;
InitializeComponent();

View File

@@ -5,10 +5,10 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin.Program.ProgramSources;
using Stopwatch = Wox.Infrastructure.Stopwatch;
@@ -27,8 +27,25 @@ namespace Wox.Plugin.Program
{"AppPathsProgramSource", typeof(AppPathsProgramSource)}
};
private PluginInitContext _context;
private static ProgramCacheStorage _cache = ProgramCacheStorage.Instance;
private static ProgramStorage _settings = ProgramStorage.Instance;
private static ProgramIndexCache _cache;
private static BinaryStorage<ProgramIndexCache> _cacheStorage;
private static Settings _settings ;
private readonly PluginSettingsStorage<Settings> _settingsStorage;
public Programs()
{
_settingsStorage = new PluginSettingsStorage<Settings>();
_settings = _settingsStorage.Load();
_cacheStorage = new BinaryStorage<ProgramIndexCache>();
_cache = _cacheStorage.Load();
}
~Programs()
{
_settingsStorage.Save();
_cacheStorage.Save();
}
public List<Result> Query(Query query)
{
@@ -62,15 +79,13 @@ namespace Wox.Plugin.Program
public void Init(PluginInitContext context)
{
this._context = context;
_context = context;
Stopwatch.Debug("Preload programs", () =>
{
programs = _cache.Programs;
});
Log.Info($"Preload {programs.Count} programs from cache");
// happlebao todo fix this
//Stopwatch.Debug("Program Index", IndexPrograms);
IndexPrograms();
Stopwatch.Debug("Program Index", IndexPrograms);
}
public static void IndexPrograms()
@@ -120,7 +135,6 @@ namespace Wox.Plugin.Program
.Select(g => g.First()).ToList();
_cache.Programs = programs;
_cache.Save();
}
}

View File

@@ -2,7 +2,6 @@
namespace Wox.Plugin.WebSearch
{
[Serializable]
public class WebSearch
{
public string Title { get; set; }

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Controls;
using Wox.Infrastructure.Storage;
using Wox.Plugin.WebSearch.Annotations;
using Wox.Plugin.WebSearch.SuggestionSources;
@@ -10,9 +11,22 @@ namespace Wox.Plugin.WebSearch
{
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IMultipleActionKeywords
{
private WebSearchStorage _settings = WebSearchStorage.Instance;
public PluginInitContext Context { get; private set; }
private readonly PluginSettingsStorage<Settings> _storage;
private readonly Settings _settings;
public WebSearchPlugin()
{
_storage = new PluginSettingsStorage<Settings>();
_settings = _storage.Load();
}
~WebSearchPlugin()
{
_storage.Save();
}
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();

View File

@@ -18,9 +18,9 @@ namespace Wox.Plugin.WebSearch
private WebSearch _updateWebSearch;
private readonly PluginInitContext _context;
private readonly WebSearchPlugin _plugin;
private WebSearchStorage _settings;
private Settings _settings;
public WebSearchSetting(WebSearchesSetting settingWidow, WebSearchStorage settings)
public WebSearchSetting(WebSearchesSetting settingWidow, Settings settings)
{
_plugin = settingWidow.Plugin;
_context = settingWidow.Context;
@@ -122,7 +122,6 @@ namespace Wox.Plugin.WebSearch
});
}
_settings.Save();
_settingWindow.ReloadWebSearchView();
Close();
}

View File

@@ -4,22 +4,9 @@ using Wox.Infrastructure.Storage;
namespace Wox.Plugin.WebSearch
{
public class WebSearchStorage : JsonStrorage<WebSearchStorage>
public class Settings
{
[JsonProperty]
public List<WebSearch> WebSearches { get; set; }
[JsonProperty]
public bool EnableWebSearchSuggestion { get; set; }
[JsonProperty]
public string WebSearchSuggestionSource { get; set; }
protected override string FileName { get; } = "settings_plugin_websearch";
protected override WebSearchStorage LoadDefault()
{
WebSearches = new List<WebSearch>(new List<WebSearch>()
public List<WebSearch> WebSearches { get; set; } = new List<WebSearch>
{
new WebSearch
{
@@ -173,9 +160,10 @@ namespace Wox.Plugin.WebSearch
Url = "http://www.search.yahoo.com/search?p={q}",
Enabled = true
}
});
};
return this;
}
public bool EnableWebSearchSuggestion { get; set; }
public string WebSearchSuggestionSource { get; set; }
}
}

View File

@@ -10,11 +10,11 @@ namespace Wox.Plugin.WebSearch
/// </summary>
public partial class WebSearchesSetting : UserControl
{
private WebSearchStorage _settings;
private Settings _settings;
public PluginInitContext Context { get; }
public WebSearchPlugin Plugin { get; }
public WebSearchesSetting(WebSearchPlugin plugin, WebSearchStorage settings)
public WebSearchesSetting(WebSearchPlugin plugin, Settings settings)
{
Context = plugin.Context;
Plugin = plugin;
@@ -97,14 +97,12 @@ namespace Wox.Plugin.WebSearch
{
comboBoxSuggestionSource.Visibility = Visibility.Visible;
_settings.EnableWebSearchSuggestion = true;
_settings.Save();
}
private void CbEnableWebSearchSuggestion_OnUnchecked(object sender, RoutedEventArgs e)
{
comboBoxSuggestionSource.Visibility = Visibility.Collapsed;
_settings.EnableWebSearchSuggestion = false;
_settings.Save();
}
private void ComboBoxSuggestionSource_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -112,7 +110,6 @@ namespace Wox.Plugin.WebSearch
if (e.AddedItems.Count > 0)
{
_settings.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
_settings.Save();
}
}
}