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 01:32:33 +00:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2015-10-30 23:17:34 +00: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-21 19:51:47 +01:00
|
|
|
|
public class FileSystemProgramSource : ProgramSource
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-07-21 19:51:47 +01:00
|
|
|
|
public string Location { get; set; } = "";
|
2016-08-19 20:34:20 +01:00
|
|
|
|
public int MaxDepth { get; set; } = -1;
|
|
|
|
|
|
internal string[] Suffixes { get; set; } = { "" };
|
2014-03-19 04:05:27 +08:00
|
|
|
|
|
2014-03-19 02:06:51 +08:00
|
|
|
|
public override List<Program> LoadPrograms()
|
|
|
|
|
|
{
|
2016-08-19 20:34:20 +01:00
|
|
|
|
if (Directory.Exists(Location) && MaxDepth >= -1)
|
2014-08-12 12:21:04 +08:00
|
|
|
|
{
|
2016-08-19 20:34:20 +01:00
|
|
|
|
var apps = new List<Program>();
|
|
|
|
|
|
GetAppFromDirectory(apps, Location, 0);
|
|
|
|
|
|
return apps;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return new List<Program>();
|
2014-08-12 12:21:04 +08:00
|
|
|
|
}
|
2015-04-20 20:32:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-19 20:34:20 +01:00
|
|
|
|
private void GetAppFromDirectory(List<Program> apps, string path, int depth)
|
2015-04-20 20:32:10 -04:00
|
|
|
|
{
|
2016-08-19 20:34:20 +01:00
|
|
|
|
if (MaxDepth != depth)
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-07-21 19:51:47 +01:00
|
|
|
|
foreach (var file in Directory.GetFiles(path))
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-07-21 19:51:47 +01:00
|
|
|
|
if (Suffixes.Any(o => file.EndsWith("." + o)))
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-08-19 20:34:20 +01:00
|
|
|
|
Program p;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
p = CreateEntry(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var woxPluginException = new WoxPluginException("Program",
|
|
|
|
|
|
$"GetAppFromDirectory failed: {path}", e);
|
|
|
|
|
|
Log.Exception(woxPluginException);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2016-04-22 23:03:32 +01:00
|
|
|
|
p.Source = this;
|
2016-08-19 20:34:20 +01:00
|
|
|
|
apps.Add(p);
|
2014-08-14 22:21:07 +08:00
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
2016-08-19 20:34:20 +01:00
|
|
|
|
foreach (var d in Directory.GetDirectories(path))
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-08-19 20:34:20 +01:00
|
|
|
|
GetAppFromDirectory(apps, d, depth + 1);
|
2014-08-14 22:21:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-03-19 20:16:20 +08:00
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
2016-07-21 19:51:47 +01:00
|
|
|
|
}
|