From c8a932b6b36de2d74b9b09c9e9e3902ea9533141 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Thu, 7 Jan 2016 02:31:17 +0000 Subject: [PATCH] Refactoring directory path for storage --- Plugins/Wox.Plugin.CMD/CMDStorage.cs | 10 +------- .../ContextMenuStorage.cs | 10 +------- Plugins/Wox.Plugin.Folder/FolderStorage.cs | 9 +------- .../Wox.Plugin.Program/ProgramCacheStorage.cs | 10 +------- Plugins/Wox.Plugin.Program/ProgramStorage.cs | 10 +------- .../Wox.Plugin.WebSearch/WebSearchStorage.cs | 10 +------- Wox.Core/UserSettings/UserSettingStorage.cs | 12 +--------- Wox.Infrastructure/Storage/BaseStorage.cs | 23 ++++++++----------- Wox.Infrastructure/Storage/BinaryStorage.cs | 4 ++-- Wox.Infrastructure/Storage/JsonStorage.cs | 4 ++-- Wox.UpdateFeedGenerator/ConfigStorage.cs | 10 +------- Wox/ImageLoader/ImageCacheStroage.cs | 10 +------- Wox/Storage/QueryHistoryStorage.cs | 10 +------- Wox/Storage/TopMostRecordStorage.cs | 18 ++++----------- Wox/Storage/UserSelectedRecordStorage.cs | 10 +------- 15 files changed, 29 insertions(+), 131 deletions(-) diff --git a/Plugins/Wox.Plugin.CMD/CMDStorage.cs b/Plugins/Wox.Plugin.CMD/CMDStorage.cs index ece62a06ad..c87bf37b48 100644 --- a/Plugins/Wox.Plugin.CMD/CMDStorage.cs +++ b/Plugins/Wox.Plugin.CMD/CMDStorage.cs @@ -17,15 +17,7 @@ namespace Wox.Plugin.CMD [JsonProperty] public Dictionary CMDHistory = new Dictionary(); - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } - - protected override string ConfigName - { - get { return "CMDHistory"; } - } + protected override string FileName { get; } = "CMDHistory"; protected override CMDStorage LoadDefault() { diff --git a/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs b/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs index ac5c1bab1e..150f010145 100644 --- a/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs +++ b/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs @@ -15,15 +15,7 @@ namespace Wox.Plugin.Everything public IPublicAPI API { get; set; } - protected override string ConfigName - { - get { return "EverythingContextMenu"; } - } - - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } + protected override string FileName { get; } = "EverythingContextMenu"; protected override void OnAfterLoad(ContextMenuStorage obj) { diff --git a/Plugins/Wox.Plugin.Folder/FolderStorage.cs b/Plugins/Wox.Plugin.Folder/FolderStorage.cs index 815643c9d8..f23273774d 100644 --- a/Plugins/Wox.Plugin.Folder/FolderStorage.cs +++ b/Plugins/Wox.Plugin.Folder/FolderStorage.cs @@ -10,14 +10,7 @@ namespace Wox.Plugin.Folder { [JsonProperty] public List FolderLinks { get; set; } - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } - protected override string ConfigName - { - get { return "setting"; } - } + protected override string FileName { get; } = "settings_folder_plugin"; } } diff --git a/Plugins/Wox.Plugin.Program/ProgramCacheStorage.cs b/Plugins/Wox.Plugin.Program/ProgramCacheStorage.cs index f1b3a138cc..4f57910fb3 100644 --- a/Plugins/Wox.Plugin.Program/ProgramCacheStorage.cs +++ b/Plugins/Wox.Plugin.Program/ProgramCacheStorage.cs @@ -11,14 +11,6 @@ namespace Wox.Plugin.Program { public List Programs = new List(); - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } - - protected override string ConfigName - { - get { return "ProgramIndexCache"; } - } + protected override string FileName { get; } = "ProgramIndexCache"; } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramStorage.cs b/Plugins/Wox.Plugin.Program/ProgramStorage.cs index d533443f01..a17171f7cd 100644 --- a/Plugins/Wox.Plugin.Program/ProgramStorage.cs +++ b/Plugins/Wox.Plugin.Program/ProgramStorage.cs @@ -23,11 +23,6 @@ namespace Wox.Plugin.Program [DefaultValue(true)] public bool EnableRegistrySource { get; set; } - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } - protected override ProgramStorage LoadDefault() { ProgramSources = new List(); @@ -44,9 +39,6 @@ namespace Wox.Plugin.Program } } - protected override string ConfigName - { - get { return "setting"; } - } + protected override string FileName { get; } = "settings_plugin_program"; } } diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchStorage.cs b/Plugins/Wox.Plugin.WebSearch/WebSearchStorage.cs index 5a059a2385..82e1d61356 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchStorage.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchStorage.cs @@ -17,14 +17,6 @@ namespace Wox.Plugin.WebSearch [JsonProperty] public string WebSearchSuggestionSource { get; set; } - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } - - protected override string ConfigName - { - get { return "setting"; } - } + protected override string FileName { get; } = "settings_plugin_websearch"; } } diff --git a/Wox.Core/UserSettings/UserSettingStorage.cs b/Wox.Core/UserSettings/UserSettingStorage.cs index a52a90ba99..1cab071e82 100644 --- a/Wox.Core/UserSettings/UserSettingStorage.cs +++ b/Wox.Core/UserSettings/UserSettingStorage.cs @@ -107,17 +107,7 @@ namespace Wox.Core.UserSettings [JsonProperty] public int MaxResultsToShow { get; set; } - protected override string ConfigFolder - { - get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); } - } - - protected override string ConfigName - { - get { return "config"; } - } - - + protected override string FileName { get; } = "Settings"; public void IncreaseActivateTimes() { diff --git a/Wox.Infrastructure/Storage/BaseStorage.cs b/Wox.Infrastructure/Storage/BaseStorage.cs index 4ddee08ada..03e0311eb7 100644 --- a/Wox.Infrastructure/Storage/BaseStorage.cs +++ b/Wox.Infrastructure/Storage/BaseStorage.cs @@ -4,23 +4,18 @@ using System.IO; namespace Wox.Infrastructure.Storage { [Serializable] - public abstract class BaseStorage : IStorage where T : class,IStorage, new() + public abstract class BaseStorage : IStorage where T : class, IStorage, new() { - protected abstract string ConfigFolder { get; } + protected string DirectoryPath { get; } = Path.Combine(WoxDirectroy.Executable, "Config"); - protected string ConfigPath - { - get - { - return Path.Combine(ConfigFolder, ConfigName + FileSuffix); - } - } + protected string FilePath => Path.Combine(DirectoryPath, FileName + FileSuffix); protected abstract string FileSuffix { get; } - protected abstract string ConfigName { get; } + protected abstract string FileName { get; } private static object locker = new object(); + protected static T serializedObject; public event Action AfterLoad; @@ -64,13 +59,13 @@ namespace Wox.Infrastructure.Storage public void Load() { - if (!File.Exists(ConfigPath)) + if (!File.Exists(FilePath)) { - if (!Directory.Exists(ConfigFolder)) + if (!Directory.Exists(DirectoryPath)) { - Directory.CreateDirectory(ConfigFolder); + Directory.CreateDirectory(DirectoryPath); } - File.Create(ConfigPath).Close(); + File.Create(FilePath).Close(); } LoadInternal(); OnAfterLoad(serializedObject); diff --git a/Wox.Infrastructure/Storage/BinaryStorage.cs b/Wox.Infrastructure/Storage/BinaryStorage.cs index fc433b9848..75258df007 100644 --- a/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -28,7 +28,7 @@ namespace Wox.Infrastructure.Storage AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; try { - using (FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (fileStream.Length > 0) { @@ -93,7 +93,7 @@ namespace Wox.Infrastructure.Storage { try { - FileStream fileStream = new FileStream(ConfigPath, FileMode.Create); + FileStream fileStream = new FileStream(FilePath, FileMode.Create); BinaryFormatter binaryFormatter = new BinaryFormatter { AssemblyFormat = FormatterAssemblyStyle.Simple diff --git a/Wox.Infrastructure/Storage/JsonStorage.cs b/Wox.Infrastructure/Storage/JsonStorage.cs index 07f6329ef4..16f6278059 100644 --- a/Wox.Infrastructure/Storage/JsonStorage.cs +++ b/Wox.Infrastructure/Storage/JsonStorage.cs @@ -17,7 +17,7 @@ namespace Wox.Infrastructure.Storage protected override void LoadInternal() { - string json = File.ReadAllText(ConfigPath); + string json = File.ReadAllText(FilePath); if (!string.IsNullOrEmpty(json)) { try @@ -42,7 +42,7 @@ namespace Wox.Infrastructure.Storage lock (syncObject) { string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented); - File.WriteAllText(ConfigPath, json); + File.WriteAllText(FilePath, json); } }); } diff --git a/Wox.UpdateFeedGenerator/ConfigStorage.cs b/Wox.UpdateFeedGenerator/ConfigStorage.cs index 84bd970144..a7dcbc35cf 100644 --- a/Wox.UpdateFeedGenerator/ConfigStorage.cs +++ b/Wox.UpdateFeedGenerator/ConfigStorage.cs @@ -40,14 +40,6 @@ namespace Wox.UpdateFeedGenerator } } - protected override string ConfigFolder - { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } - } - - protected override string ConfigName - { - get { return "config"; } - } + protected override string FileName { get; } = "config"; } } diff --git a/Wox/ImageLoader/ImageCacheStroage.cs b/Wox/ImageLoader/ImageCacheStroage.cs index 22240cc0b8..18b441d9ec 100644 --- a/Wox/ImageLoader/ImageCacheStroage.cs +++ b/Wox/ImageLoader/ImageCacheStroage.cs @@ -14,15 +14,7 @@ namespace Wox.ImageLoader private const int maxCached = 200; public Dictionary TopUsedImages = new Dictionary(); - protected override string ConfigFolder - { - get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); } - } - - protected override string ConfigName - { - get { return "ImageCache"; } - } + protected override string FileName { get; } = "ImageCache"; public void Add(string path) { diff --git a/Wox/Storage/QueryHistoryStorage.cs b/Wox/Storage/QueryHistoryStorage.cs index d6b65931fd..6fec79789a 100644 --- a/Wox/Storage/QueryHistoryStorage.cs +++ b/Wox/Storage/QueryHistoryStorage.cs @@ -20,15 +20,7 @@ namespace Wox.Storage public static PluginMetadata MetaData { get; } = new PluginMetadata { ID = "Query history", Name = "Query history" }; - protected override string ConfigFolder - { - get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); } - } - - protected override string ConfigName - { - get { return "QueryHistory"; } - } + protected override string FileName { get; } = "QueryHistory"; public HistoryItem Previous() { diff --git a/Wox/Storage/TopMostRecordStorage.cs b/Wox/Storage/TopMostRecordStorage.cs index c313d567ef..2ccbe86e58 100644 --- a/Wox/Storage/TopMostRecordStorage.cs +++ b/Wox/Storage/TopMostRecordStorage.cs @@ -11,15 +11,7 @@ namespace Wox.Storage { public Dictionary records = new Dictionary(); - protected override string ConfigFolder - { - get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); } - } - - protected override string ConfigName - { - get { return "TopMostRecords"; } - } + protected override string FileName { get; } = "TopMostRecords"; internal bool IsTopMost(Result result) { @@ -50,10 +42,10 @@ namespace Wox.Storage { records.Add(result.OriginQuery.RawQuery, new TopMostRecord { - PluginID = result.PluginID, - Title = result.Title, - SubTitle = result.SubTitle - }); + PluginID = result.PluginID, + Title = result.Title, + SubTitle = result.SubTitle + }); } Save(); diff --git a/Wox/Storage/UserSelectedRecordStorage.cs b/Wox/Storage/UserSelectedRecordStorage.cs index 3f6ac51ca3..ea0289943e 100644 --- a/Wox/Storage/UserSelectedRecordStorage.cs +++ b/Wox/Storage/UserSelectedRecordStorage.cs @@ -12,15 +12,7 @@ namespace Wox.Storage [JsonProperty] private Dictionary records = new Dictionary(); - protected override string ConfigFolder - { - get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); } - } - - protected override string ConfigName - { - get { return "UserSelectedRecords"; } - } + protected override string FileName { get; } = "UserSelectedRecords"; public void Add(Result result) {