PowerToys/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs

85 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
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;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public class FileSystemProgramSource : AbstractProgramSource
{
private string _baseDirectory;
private int _maxDepth;
private string[] _suffixes;
public FileSystemProgramSource(string baseDirectory, int maxDepth, string[] suffixes)
{
_baseDirectory = baseDirectory;
_maxDepth = maxDepth;
_suffixes = suffixes;
}
public FileSystemProgramSource(string baseDirectory, string[] suffixes)
: this(baseDirectory, -1, suffixes) {}
2015-02-02 23:28:40 +08:00
public FileSystemProgramSource(ProgramSource source)
: this(source.Location, source.MaxDepth, source.Suffixes)
{
2016-01-07 05:34:42 +08:00
BonusPoints = source.BonusPoints;
}
public override List<Program> LoadPrograms()
{
List<Program> list = new List<Program>();
if (Directory.Exists(_baseDirectory))
{
GetAppFromDirectory(_baseDirectory, list);
FileChangeWatcher.AddWatch(_baseDirectory, _suffixes);
}
return list;
}
private void GetAppFromDirectory(string path, List<Program> list)
{
GetAppFromDirectory(path, list, 0);
}
private void GetAppFromDirectory(string path, List<Program> list, int depth)
{
if(_maxDepth != -1 && depth > _maxDepth)
{
return;
}
try
{
foreach (string file in Directory.GetFiles(path))
{
if (_suffixes.Any(o => file.EndsWith("." + o)))
{
Program p = CreateEntry(file);
p.Source = this;
list.Add(p);
}
}
foreach (var subDirectory in Directory.GetDirectories(path))
{
GetAppFromDirectory(subDirectory, list, depth + 1);
}
}
2015-02-02 23:28:40 +08:00
catch (Exception e)
{
var woxPluginException = new WoxPluginException("Program", $"GetAppFromDirectory failed: {path}", e);
Log.Error(woxPluginException);
}
}
2014-03-19 20:16:20 +08:00
public override string ToString()
{
return typeof(FileSystemProgramSource).Name + ":" + _baseDirectory;
2014-03-19 20:16:20 +08:00
}
}
}