2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-14 22:21:07 +08:00
|
|
|
|
using System.IO;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2015-11-08 01:32:58 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-08-20 03:34:20 +08:00
|
|
|
|
using Wox.Plugin.Program.ProgramSources;
|
2016-01-06 14:45:08 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.Program
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-07-21 07:02:13 +08:00
|
|
|
|
internal static class FileChangeWatcher
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-07-21 07:02:13 +08:00
|
|
|
|
private static readonly List<string> WatchedPath = new List<string>();
|
2016-08-20 03:34:20 +08:00
|
|
|
|
// todo remove previous watcher events
|
2016-08-20 06:24:21 +08:00
|
|
|
|
public static void AddAll(List<UnregisteredPrograms> sources, string[] suffixes)
|
2016-08-20 03:34:20 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var s in sources)
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(s.Location))
|
|
|
|
|
{
|
|
|
|
|
AddWatch(s.Location, suffixes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-14 22:21:07 +08:00
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true)
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-07-21 07:02:13 +08:00
|
|
|
|
if (WatchedPath.Contains(path)) return;
|
2014-08-14 22:21:07 +08:00
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Warn($"FileChangeWatcher: {path} doesn't exist");
|
2014-08-14 22:21:07 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 07:02:13 +08:00
|
|
|
|
WatchedPath.Add(path);
|
2016-03-28 10:09:57 +08:00
|
|
|
|
foreach (string fileType in programSuffixes)
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
|
|
|
|
FileSystemWatcher watcher = new FileSystemWatcher
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
IncludeSubdirectories = includingSubDirectory,
|
2016-07-21 07:02:13 +08:00
|
|
|
|
Filter = $"*.{fileType}",
|
2014-08-14 22:21:07 +08:00
|
|
|
|
EnableRaisingEvents = true
|
|
|
|
|
};
|
|
|
|
|
watcher.Changed += FileChanged;
|
|
|
|
|
watcher.Created += FileChanged;
|
|
|
|
|
watcher.Deleted += FileChanged;
|
|
|
|
|
watcher.Renamed += FileChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void FileChanged(object source, FileSystemEventArgs e)
|
|
|
|
|
{
|
2016-07-21 07:02:13 +08:00
|
|
|
|
Task.Run(() =>
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-07-21 07:02:13 +08:00
|
|
|
|
Main.IndexPrograms();
|
|
|
|
|
});
|
2014-08-14 22:21:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|