Refactoring directory path for storage

This commit is contained in:
bao-qian 2016-01-07 02:31:17 +00:00
parent b1ed51e72c
commit c8a932b6b3
15 changed files with 29 additions and 131 deletions

View File

@ -17,15 +17,7 @@ namespace Wox.Plugin.CMD
[JsonProperty] [JsonProperty]
public Dictionary<string, int> CMDHistory = new Dictionary<string, int>(); public Dictionary<string, int> CMDHistory = new Dictionary<string, int>();
protected override string ConfigFolder protected override string FileName { get; } = "CMDHistory";
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override string ConfigName
{
get { return "CMDHistory"; }
}
protected override CMDStorage LoadDefault() protected override CMDStorage LoadDefault()
{ {

View File

@ -15,15 +15,7 @@ namespace Wox.Plugin.Everything
public IPublicAPI API { get; set; } public IPublicAPI API { get; set; }
protected override string ConfigName protected override string FileName { get; } = "EverythingContextMenu";
{
get { return "EverythingContextMenu"; }
}
protected override string ConfigFolder
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override void OnAfterLoad(ContextMenuStorage obj) protected override void OnAfterLoad(ContextMenuStorage obj)
{ {

View File

@ -10,14 +10,7 @@ namespace Wox.Plugin.Folder
{ {
[JsonProperty] [JsonProperty]
public List<FolderLink> FolderLinks { get; set; } public List<FolderLink> FolderLinks { get; set; }
protected override string ConfigFolder
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override string ConfigName protected override string FileName { get; } = "settings_folder_plugin";
{
get { return "setting"; }
}
} }
} }

View File

@ -11,14 +11,6 @@ namespace Wox.Plugin.Program
{ {
public List<Program> Programs = new List<Program>(); public List<Program> Programs = new List<Program>();
protected override string ConfigFolder protected override string FileName { get; } = "ProgramIndexCache";
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override string ConfigName
{
get { return "ProgramIndexCache"; }
}
} }
} }

View File

@ -23,11 +23,6 @@ namespace Wox.Plugin.Program
[DefaultValue(true)] [DefaultValue(true)]
public bool EnableRegistrySource { get; set; } public bool EnableRegistrySource { get; set; }
protected override string ConfigFolder
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override ProgramStorage LoadDefault() protected override ProgramStorage LoadDefault()
{ {
ProgramSources = new List<ProgramSource>(); ProgramSources = new List<ProgramSource>();
@ -44,9 +39,6 @@ namespace Wox.Plugin.Program
} }
} }
protected override string ConfigName protected override string FileName { get; } = "settings_plugin_program";
{
get { return "setting"; }
}
} }
} }

View File

@ -17,14 +17,6 @@ namespace Wox.Plugin.WebSearch
[JsonProperty] [JsonProperty]
public string WebSearchSuggestionSource { get; set; } public string WebSearchSuggestionSource { get; set; }
protected override string ConfigFolder protected override string FileName { get; } = "settings_plugin_websearch";
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override string ConfigName
{
get { return "setting"; }
}
} }
} }

View File

@ -107,17 +107,7 @@ namespace Wox.Core.UserSettings
[JsonProperty] [JsonProperty]
public int MaxResultsToShow { get; set; } public int MaxResultsToShow { get; set; }
protected override string ConfigFolder protected override string FileName { get; } = "Settings";
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); }
}
protected override string ConfigName
{
get { return "config"; }
}
public void IncreaseActivateTimes() public void IncreaseActivateTimes()
{ {

View File

@ -4,23 +4,18 @@ using System.IO;
namespace Wox.Infrastructure.Storage namespace Wox.Infrastructure.Storage
{ {
[Serializable] [Serializable]
public abstract class BaseStorage<T> : IStorage where T : class,IStorage, new() public abstract class BaseStorage<T> : IStorage where T : class, IStorage, new()
{ {
protected abstract string ConfigFolder { get; } protected string DirectoryPath { get; } = Path.Combine(WoxDirectroy.Executable, "Config");
protected string ConfigPath protected string FilePath => Path.Combine(DirectoryPath, FileName + FileSuffix);
{
get
{
return Path.Combine(ConfigFolder, ConfigName + FileSuffix);
}
}
protected abstract string FileSuffix { get; } protected abstract string FileSuffix { get; }
protected abstract string ConfigName { get; } protected abstract string FileName { get; }
private static object locker = new object(); private static object locker = new object();
protected static T serializedObject; protected static T serializedObject;
public event Action<T> AfterLoad; public event Action<T> AfterLoad;
@ -64,13 +59,13 @@ namespace Wox.Infrastructure.Storage
public void Load() 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(); LoadInternal();
OnAfterLoad(serializedObject); OnAfterLoad(serializedObject);

View File

@ -28,7 +28,7 @@ namespace Wox.Infrastructure.Storage
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
try 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) if (fileStream.Length > 0)
{ {
@ -93,7 +93,7 @@ namespace Wox.Infrastructure.Storage
{ {
try try
{ {
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create); FileStream fileStream = new FileStream(FilePath, FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter BinaryFormatter binaryFormatter = new BinaryFormatter
{ {
AssemblyFormat = FormatterAssemblyStyle.Simple AssemblyFormat = FormatterAssemblyStyle.Simple

View File

@ -17,7 +17,7 @@ namespace Wox.Infrastructure.Storage
protected override void LoadInternal() protected override void LoadInternal()
{ {
string json = File.ReadAllText(ConfigPath); string json = File.ReadAllText(FilePath);
if (!string.IsNullOrEmpty(json)) if (!string.IsNullOrEmpty(json))
{ {
try try
@ -42,7 +42,7 @@ namespace Wox.Infrastructure.Storage
lock (syncObject) lock (syncObject)
{ {
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented); string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
File.WriteAllText(ConfigPath, json); File.WriteAllText(FilePath, json);
} }
}); });
} }

View File

@ -40,14 +40,6 @@ namespace Wox.UpdateFeedGenerator
} }
} }
protected override string ConfigFolder protected override string FileName { get; } = "config";
{
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
}
protected override string ConfigName
{
get { return "config"; }
}
} }
} }

View File

@ -14,15 +14,7 @@ namespace Wox.ImageLoader
private const int maxCached = 200; private const int maxCached = 200;
public Dictionary<string, int> TopUsedImages = new Dictionary<string, int>(); public Dictionary<string, int> TopUsedImages = new Dictionary<string, int>();
protected override string ConfigFolder protected override string FileName { get; } = "ImageCache";
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); }
}
protected override string ConfigName
{
get { return "ImageCache"; }
}
public void Add(string path) public void Add(string path)
{ {

View File

@ -20,15 +20,7 @@ namespace Wox.Storage
public static PluginMetadata MetaData { get; } = new PluginMetadata public static PluginMetadata MetaData { get; } = new PluginMetadata
{ ID = "Query history", Name = "Query history" }; { ID = "Query history", Name = "Query history" };
protected override string ConfigFolder protected override string FileName { get; } = "QueryHistory";
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); }
}
protected override string ConfigName
{
get { return "QueryHistory"; }
}
public HistoryItem Previous() public HistoryItem Previous()
{ {

View File

@ -11,15 +11,7 @@ namespace Wox.Storage
{ {
public Dictionary<string, TopMostRecord> records = new Dictionary<string, TopMostRecord>(); public Dictionary<string, TopMostRecord> records = new Dictionary<string, TopMostRecord>();
protected override string ConfigFolder protected override string FileName { get; } = "TopMostRecords";
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); }
}
protected override string ConfigName
{
get { return "TopMostRecords"; }
}
internal bool IsTopMost(Result result) internal bool IsTopMost(Result result)
{ {
@ -50,10 +42,10 @@ namespace Wox.Storage
{ {
records.Add(result.OriginQuery.RawQuery, new TopMostRecord records.Add(result.OriginQuery.RawQuery, new TopMostRecord
{ {
PluginID = result.PluginID, PluginID = result.PluginID,
Title = result.Title, Title = result.Title,
SubTitle = result.SubTitle SubTitle = result.SubTitle
}); });
} }
Save(); Save();

View File

@ -12,15 +12,7 @@ namespace Wox.Storage
[JsonProperty] [JsonProperty]
private Dictionary<string, int> records = new Dictionary<string, int>(); private Dictionary<string, int> records = new Dictionary<string, int>();
protected override string ConfigFolder protected override string FileName { get; } = "UserSelectedRecords";
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config"); }
}
protected override string ConfigName
{
get { return "UserSelectedRecords"; }
}
public void Add(Result result) public void Add(Result result)
{ {