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-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]
|
2014-03-19 02:06:51 +08:00
|
|
|
|
public class FileSystemProgramSource : AbstractProgramSource
|
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
private string baseDirectory;
|
2015-04-21 08:32:10 +08:00
|
|
|
|
private int maxDepth;
|
|
|
|
|
private string suffixes;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
|
2015-04-21 08:32:10 +08:00
|
|
|
|
public FileSystemProgramSource(string baseDirectory, int maxDepth, string suffixes)
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
this.baseDirectory = baseDirectory;
|
2015-04-21 08:32:10 +08:00
|
|
|
|
this.maxDepth = maxDepth;
|
|
|
|
|
this.suffixes = suffixes;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-21 08:32:10 +08:00
|
|
|
|
public FileSystemProgramSource(string baseDirectory)
|
|
|
|
|
: this(baseDirectory, -1, "") {}
|
|
|
|
|
|
2015-02-02 23:28:40 +08:00
|
|
|
|
public FileSystemProgramSource(ProgramSource source)
|
2015-04-21 08:32:10 +08:00
|
|
|
|
: this(source.Location, source.MaxDepth, source.Suffixes)
|
2014-03-19 04:05:27 +08:00
|
|
|
|
{
|
2014-03-19 20:16:20 +08:00
|
|
|
|
this.BonusPoints = source.BonusPoints;
|
2014-03-19 04:05:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-19 02:06:51 +08:00
|
|
|
|
public override List<Program> LoadPrograms()
|
|
|
|
|
{
|
|
|
|
|
List<Program> list = new List<Program>();
|
2014-08-14 22:21:07 +08:00
|
|
|
|
if (Directory.Exists(baseDirectory))
|
2014-08-12 12:21:04 +08:00
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
GetAppFromDirectory(baseDirectory, list);
|
|
|
|
|
FileChangeWatcher.AddWatch(baseDirectory);
|
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)
|
|
|
|
|
{
|
|
|
|
|
if(maxDepth != -1 && depth > maxDepth)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-14 22:21:07 +08:00
|
|
|
|
try
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
foreach (string file in Directory.GetFiles(path))
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2015-04-21 08:32:10 +08:00
|
|
|
|
if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)) ||
|
|
|
|
|
suffixes.Split(';').Any(o => file.EndsWith("." + o)))
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
|
|
|
|
Program p = CreateEntry(file);
|
|
|
|
|
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-02-02 23:28:40 +08:00
|
|
|
|
Log.Warn(string.Format("GetAppFromDirectory failed: {0} - {1}", path, e.Message));
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-19 20:16:20 +08:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
return typeof(FileSystemProgramSource).Name + ":" + this.baseDirectory;
|
2014-03-19 20:16:20 +08:00
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|