2014-08-14 22:21:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
using System.IO;
|
2014-03-30 11:16:44 +08:00
|
|
|
|
using System.Linq;
|
2015-11-09 09:32:33 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2015-10-31 07:17:34 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.Program.ProgramSources
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
[Serializable]
|
2016-07-22 02:51:47 +08:00
|
|
|
|
public class FileSystemProgramSource : ProgramSource
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
public string Location { get; set; } = "";
|
2014-03-19 04:05:27 +08:00
|
|
|
|
|
2014-03-19 02:06:51 +08:00
|
|
|
|
public override List<Program> LoadPrograms()
|
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
var list = new List<Program>();
|
|
|
|
|
if (Directory.Exists(Location))
|
2014-08-12 12:21:04 +08:00
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
GetAppFromDirectory(Location, list);
|
|
|
|
|
FileChangeWatcher.AddWatch(Location, Suffixes);
|
2014-08-12 12:21:04 +08:00
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetAppFromDirectory(string path, List<Program> list)
|
|
|
|
|
{
|
2015-04-21 08:32:10 +08:00
|
|
|
|
GetAppFromDirectory(path, list, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetAppFromDirectory(string path, List<Program> list, int depth)
|
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
if (MaxDepth != -1 && depth > MaxDepth)
|
2015-04-21 08:32:10 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-14 22:21:07 +08:00
|
|
|
|
try
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
foreach (var file in Directory.GetFiles(path))
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
if (Suffixes.Any(o => file.EndsWith("." + o)))
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
var p = CreateEntry(file);
|
2016-04-23 06:03:32 +08:00
|
|
|
|
p.Source = this;
|
2014-08-14 22:21:07 +08:00
|
|
|
|
list.Add(p);
|
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-14 22:21:07 +08:00
|
|
|
|
foreach (var subDirectory in Directory.GetDirectories(path))
|
|
|
|
|
{
|
2015-04-21 08:32:10 +08:00
|
|
|
|
GetAppFromDirectory(subDirectory, list, depth + 1);
|
2014-08-14 22:21:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-02 23:28:40 +08:00
|
|
|
|
catch (Exception e)
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
var woxPluginException = new WoxPluginException("Program", $"GetAppFromDirectory failed: {path}", e);
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(woxPluginException);
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-19 20:16:20 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2016-07-22 02:51:47 +08:00
|
|
|
|
var display = GetType().Name + Location;
|
|
|
|
|
return display;
|
2014-03-19 20:16:20 +08:00
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
2016-07-22 02:51:47 +08:00
|
|
|
|
}
|