mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
close #48 Refactor setting storage.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Wox.Plugin.System/CMD/CMDStorage.cs
Normal file
33
Wox.Plugin.System/CMD/CMDStorage.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user