Use variable instead of global static method

1. introduce variable
2. part of #389
3. refactoring program suffix in program plugin
4. 全局变量一时爽,代码重构火葬场
This commit is contained in:
bao-qian
2016-03-28 03:09:57 +01:00
parent c596039453
commit b22a4501cc
36 changed files with 402 additions and 343 deletions

View File

@@ -1,4 +1,5 @@
using System.Windows;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
namespace Wox.Plugin.Program
@@ -9,18 +10,23 @@ namespace Wox.Plugin.Program
public partial class AddProgramSource
{
private ProgramSource _editing;
private ProgramStorage _settings;
public AddProgramSource()
public AddProgramSource(ProgramStorage settings)
{
_settings = settings;
InitializeComponent();
Suffixes.Text = string.Join(";", settings.ProgramSuffixes);
}
public AddProgramSource(ProgramSource edit) : this()
public AddProgramSource(ProgramSource edit, ProgramStorage settings)
{
_editing = edit;
Directory.Text = _editing.Location;
MaxDepth.Text = _editing.MaxDepth.ToString();
Suffixes.Text = _editing.Suffixes;
Suffixes.Text = string.Join(";", _editing.Suffixes);
_settings = settings;
InitializeComponent();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
@@ -43,11 +49,11 @@ namespace Wox.Plugin.Program
if(_editing == null)
{
ProgramStorage.Instance.ProgramSources.Add(new ProgramSource
_settings.ProgramSources.Add(new ProgramSource
{
Location = Directory.Text,
MaxDepth = max,
Suffixes = Suffixes.Text,
Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator),
Type = "FileSystemProgramSource",
Enabled = true
});
@@ -56,10 +62,10 @@ namespace Wox.Plugin.Program
{
_editing.Location = Directory.Text;
_editing.MaxDepth = max;
_editing.Suffixes = Suffixes.Text;
_editing.Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator);
}
ProgramStorage.Instance.Save();
_settings.Save();
DialogResult = true;
Close();
}

View File

@@ -10,7 +10,7 @@ namespace Wox.Plugin.Program
private static bool isIndexing;
private static List<string> watchedPath = new List<string>();
public static void AddWatch(string path, bool includingSubDirectory = true)
public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true)
{
if (watchedPath.Contains(path)) return;
if (!Directory.Exists(path))
@@ -20,7 +20,7 @@ namespace Wox.Plugin.Program
}
watchedPath.Add(path);
foreach (string fileType in ProgramStorage.Instance.ProgramSuffixes.Split(';'))
foreach (string fileType in programSuffixes)
{
FileSystemWatcher watcher = new FileSystemWatcher
{

View File

@@ -11,19 +11,21 @@ namespace Wox.Plugin.Program
public partial class ProgramSetting : UserControl
{
private PluginInitContext context;
private ProgramStorage _settings;
public ProgramSetting(PluginInitContext context)
public ProgramSetting(PluginInitContext context, ProgramStorage settings)
{
this.context = context;
InitializeComponent();
Loaded += Setting_Loaded;
_settings = settings;
}
private void Setting_Loaded(object sender, RoutedEventArgs e)
{
programSourceView.ItemsSource = ProgramStorage.Instance.ProgramSources;
StartMenuEnabled.IsChecked = ProgramStorage.Instance.EnableStartMenuSource;
RegistryEnabled.IsChecked = ProgramStorage.Instance.EnableRegistrySource;
programSourceView.ItemsSource = _settings.ProgramSources;
StartMenuEnabled.IsChecked = _settings.EnableStartMenuSource;
RegistryEnabled.IsChecked = _settings.EnableRegistrySource;
}
private void ReIndexing()
@@ -39,7 +41,7 @@ namespace Wox.Plugin.Program
private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e)
{
var add = new AddProgramSource();
var add = new AddProgramSource(_settings);
if(add.ShowDialog() ?? false)
{
ReIndexing();
@@ -55,8 +57,8 @@ namespace Wox.Plugin.Program
if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
ProgramStorage.Instance.ProgramSources.Remove(selectedProgramSource);
ProgramStorage.Instance.Save();
_settings.ProgramSources.Remove(selectedProgramSource);
_settings.Save();
ReIndexing();
}
}
@@ -72,7 +74,7 @@ namespace Wox.Plugin.Program
ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
if (selectedProgramSource != null)
{
var add = new AddProgramSource(selectedProgramSource);
var add = new AddProgramSource(selectedProgramSource, _settings);
if (add.ShowDialog() ?? false)
{
ReIndexing();
@@ -92,7 +94,7 @@ namespace Wox.Plugin.Program
private void BtnProgramSuffixes_OnClick(object sender, RoutedEventArgs e)
{
ProgramSuffixes p = new ProgramSuffixes(context);
ProgramSuffixes p = new ProgramSuffixes(context, _settings);
p.ShowDialog();
}
@@ -118,14 +120,14 @@ namespace Wox.Plugin.Program
{
if (Directory.Exists(s))
{
ProgramStorage.Instance.ProgramSources.Add(new ProgramSource
_settings.ProgramSources.Add(new ProgramSource
{
Location = s,
Type = "FileSystemProgramSource",
Enabled = true
});
ProgramStorage.Instance.Save();
_settings.Save();
ReIndexing();
}
}
@@ -134,15 +136,15 @@ namespace Wox.Plugin.Program
private void StartMenuEnabled_Click(object sender, RoutedEventArgs e)
{
ProgramStorage.Instance.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false;
ProgramStorage.Instance.Save();
_settings.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false;
_settings.Save();
ReIndexing();
}
private void RegistryEnabled_Click(object sender, RoutedEventArgs e)
{
ProgramStorage.Instance.EnableRegistrySource = RegistryEnabled.IsChecked ?? false;
ProgramStorage.Instance.Save();
_settings.EnableRegistrySource = RegistryEnabled.IsChecked ?? false;
_settings.Save();
ReIndexing();
}
}

View File

@@ -10,7 +10,8 @@ namespace Wox.Plugin.Program
public string Type { get; set; }
public int BonusPoints { get; set; }
public bool Enabled { get; set; }
public string Suffixes { get; set; }
public string[] Suffixes { get; set; }
public const char SuffixSeperator = ';';
public int MaxDepth { get; set; }
public Dictionary<string, string> Meta { get; set; }

View File

@@ -21,13 +21,13 @@ namespace Wox.Plugin.Program.ProgramSources
return commonStartMenuPath.ToString();
}
public CommonStartMenuProgramSource()
: base(getPath())
public CommonStartMenuProgramSource(string[] suffixes)
: base(getPath(), suffixes)
{
}
public CommonStartMenuProgramSource(ProgramSource source)
: this()
: this(source.Suffixes)
{
BonusPoints = source.BonusPoints;
}

View File

@@ -10,19 +10,19 @@ namespace Wox.Plugin.Program.ProgramSources
[Serializable]
public class FileSystemProgramSource : AbstractProgramSource
{
private string baseDirectory;
private int maxDepth;
private string suffixes;
private string _baseDirectory;
private int _maxDepth;
private string[] _suffixes;
public FileSystemProgramSource(string baseDirectory, int maxDepth, string suffixes)
public FileSystemProgramSource(string baseDirectory, int maxDepth, string[] suffixes)
{
this.baseDirectory = baseDirectory;
this.maxDepth = maxDepth;
this.suffixes = suffixes;
_baseDirectory = baseDirectory;
_maxDepth = maxDepth;
_suffixes = suffixes;
}
public FileSystemProgramSource(string baseDirectory)
: this(baseDirectory, -1, "") {}
public FileSystemProgramSource(string baseDirectory, string[] suffixes)
: this(baseDirectory, -1, suffixes) {}
public FileSystemProgramSource(ProgramSource source)
: this(source.Location, source.MaxDepth, source.Suffixes)
@@ -33,10 +33,10 @@ namespace Wox.Plugin.Program.ProgramSources
public override List<Program> LoadPrograms()
{
List<Program> list = new List<Program>();
if (Directory.Exists(baseDirectory))
if (Directory.Exists(_baseDirectory))
{
GetAppFromDirectory(baseDirectory, list);
FileChangeWatcher.AddWatch(baseDirectory);
GetAppFromDirectory(_baseDirectory, list);
FileChangeWatcher.AddWatch(_baseDirectory, _suffixes);
}
return list;
}
@@ -48,7 +48,7 @@ namespace Wox.Plugin.Program.ProgramSources
private void GetAppFromDirectory(string path, List<Program> list, int depth)
{
if(maxDepth != -1 && depth > maxDepth)
if(_maxDepth != -1 && depth > _maxDepth)
{
return;
}
@@ -56,8 +56,7 @@ namespace Wox.Plugin.Program.ProgramSources
{
foreach (string file in Directory.GetFiles(path))
{
if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)) ||
suffixes.Split(';').Any(o => file.EndsWith("." + o)))
if (_suffixes.Any(o => file.EndsWith("." + o)))
{
Program p = CreateEntry(file);
list.Add(p);
@@ -78,7 +77,7 @@ namespace Wox.Plugin.Program.ProgramSources
public override string ToString()
{
return typeof(FileSystemProgramSource).Name + ":" + baseDirectory;
return typeof(FileSystemProgramSource).Name + ":" + _baseDirectory;
}
}
}

View File

@@ -7,13 +7,13 @@ namespace Wox.Plugin.Program.ProgramSources
[Browsable(false)]
public class UserStartMenuProgramSource : FileSystemProgramSource
{
public UserStartMenuProgramSource()
: base(Environment.GetFolderPath(Environment.SpecialFolder.Programs))
public UserStartMenuProgramSource(string[] suffixes)
: base(Environment.GetFolderPath(Environment.SpecialFolder.Programs), suffixes)
{
}
public UserStartMenuProgramSource(ProgramSource source)
: this()
: this(source.Suffixes)
{
BonusPoints = source.BonusPoints;
}

View File

@@ -1,17 +1,20 @@
using System.Collections.Generic;
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>
{
[JsonProperty]
public List<ProgramSource> ProgramSources { get; set; }
[JsonProperty]
public string ProgramSuffixes { get; set; }
public string[] ProgramSuffixes { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[DefaultValue(true)]
@@ -31,9 +34,9 @@ namespace Wox.Plugin.Program
protected override void OnAfterLoad(ProgramStorage storage)
{
if (string.IsNullOrEmpty(storage.ProgramSuffixes))
if (storage.ProgramSuffixes == null || storage.ProgramSuffixes.Length == 0)
{
storage.ProgramSuffixes = "lnk;exe;appref-ms;bat";
storage.ProgramSuffixes = new[] {"bat", "appref-ms", "exe", "lnk"};
}
}

View File

@@ -1,4 +1,5 @@
using System.Windows;
using System.Linq;
namespace Wox.Plugin.Program
{
@@ -8,13 +9,14 @@ namespace Wox.Plugin.Program
public partial class ProgramSuffixes
{
private PluginInitContext context;
private ProgramStorage _settings;
public ProgramSuffixes(PluginInitContext context)
public ProgramSuffixes(PluginInitContext context, ProgramStorage settings)
{
this.context = context;
InitializeComponent();
tbSuffixes.Text = ProgramStorage.Instance.ProgramSuffixes;
_settings = settings;
tbSuffixes.Text = string.Join(ProgramSource.SuffixSeperator.ToString(), _settings.ProgramSuffixes);
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
@@ -26,7 +28,7 @@ namespace Wox.Plugin.Program
return;
}
ProgramStorage.Instance.ProgramSuffixes = tbSuffixes.Text;
_settings.ProgramSuffixes = tbSuffixes.Text.Split(ProgramSource.SuffixSeperator);
string msg = context.API.GetTranslation("wox_plugin_program_update_file_suffixes");
MessageBox.Show(msg);
}

View File

@@ -27,6 +27,8 @@ namespace Wox.Plugin.Program
{"AppPathsProgramSource", typeof(AppPathsProgramSource)}
};
private PluginInitContext _context;
private static ProgramCacheStorage _cache = ProgramCacheStorage.Instance;
private static ProgramStorage _settings = ProgramStorage.Instance;
public List<Result> Query(Query query)
{
@@ -63,10 +65,12 @@ namespace Wox.Plugin.Program
this._context = context;
Stopwatch.Debug("Preload programs", () =>
{
programs = ProgramCacheStorage.Instance.Programs;
programs = _cache.Programs;
});
Log.Info($"Preload {programs.Count} programs from cache");
Stopwatch.Debug("Program Index", IndexPrograms);
// happlebao todo fix this
//Stopwatch.Debug("Program Index", IndexPrograms);
IndexPrograms();
}
public static void IndexPrograms()
@@ -75,15 +79,18 @@ namespace Wox.Plugin.Program
{
List<ProgramSource> programSources = new List<ProgramSource>();
programSources.AddRange(LoadDeaultProgramSources());
if (ProgramStorage.Instance.ProgramSources != null &&
ProgramStorage.Instance.ProgramSources.Count(o => o.Enabled) > 0)
if (_settings.ProgramSources != null &&
_settings.ProgramSources.Count(o => o.Enabled) > 0)
{
programSources.AddRange(ProgramStorage.Instance.ProgramSources);
programSources.AddRange(_settings.ProgramSources);
}
sources.Clear();
foreach (var source in programSources.Where(o => o.Enabled))
{
// happlebao todo: temp hack for program suffixes
source.Suffixes = _settings.ProgramSuffixes;
Type sourceClass;
if (SourceTypes.TryGetValue(source.Type, out sourceClass))
{
@@ -112,8 +119,8 @@ namespace Wox.Plugin.Program
programs = tempPrograms.GroupBy(x => new { x.ExecutePath, x.ExecuteName })
.Select(g => g.First()).ToList();
ProgramCacheStorage.Instance.Programs = programs;
ProgramCacheStorage.Instance.Save();
_cache.Programs = programs;
_cache.Save();
}
}
@@ -126,19 +133,19 @@ namespace Wox.Plugin.Program
list.Add(new ProgramSource
{
BonusPoints = 0,
Enabled = ProgramStorage.Instance.EnableStartMenuSource,
Enabled = _settings.EnableStartMenuSource,
Type = "CommonStartMenuProgramSource"
});
list.Add(new ProgramSource
{
BonusPoints = 0,
Enabled = ProgramStorage.Instance.EnableStartMenuSource,
Enabled = _settings.EnableStartMenuSource,
Type = "UserStartMenuProgramSource"
});
list.Add(new ProgramSource
{
BonusPoints = -10,
Enabled = ProgramStorage.Instance.EnableRegistrySource,
Enabled = _settings.EnableRegistrySource,
Type = "AppPathsProgramSource"
});
return list;
@@ -163,7 +170,7 @@ namespace Wox.Plugin.Program
public Control CreateSettingPanel()
{
return new ProgramSetting(_context);
return new ProgramSetting(_context, _settings);
}
#endregion