2014-08-14 14:21:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2014-03-18 18:06:51 +00:00
|
|
|
|
using System.IO;
|
2014-03-30 03:16:44 +00:00
|
|
|
|
using System.Linq;
|
2014-09-19 08:57:48 +00:00
|
|
|
|
using Log = Wox.Infrastructure.Logger.Log;
|
2014-03-18 18:06:51 +00:00
|
|
|
|
|
2015-01-03 07:20:34 +00:00
|
|
|
|
namespace Wox.Plugin.Program.ProgramSources
|
2014-03-18 18:06:51 +00:00
|
|
|
|
{
|
2014-12-15 14:58:49 +00:00
|
|
|
|
[Serializable]
|
2014-03-18 18:06:51 +00:00
|
|
|
|
public class FileSystemProgramSource : AbstractProgramSource
|
|
|
|
|
|
{
|
2014-08-14 14:21:07 +00:00
|
|
|
|
private string baseDirectory;
|
2014-03-18 18:06:51 +00:00
|
|
|
|
|
|
|
|
|
|
public FileSystemProgramSource(string baseDirectory)
|
|
|
|
|
|
{
|
2014-08-14 14:21:07 +00:00
|
|
|
|
this.baseDirectory = baseDirectory;
|
2014-03-18 18:06:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-14 14:21:07 +00:00
|
|
|
|
public FileSystemProgramSource(ProgramSource source):this(source.Location)
|
2014-03-18 20:05:27 +00:00
|
|
|
|
{
|
2014-03-19 12:16:20 +00:00
|
|
|
|
this.BonusPoints = source.BonusPoints;
|
2014-03-18 20:05:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-18 18:06:51 +00:00
|
|
|
|
public override List<Program> LoadPrograms()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Program> list = new List<Program>();
|
2014-08-14 14:21:07 +00:00
|
|
|
|
if (Directory.Exists(baseDirectory))
|
2014-08-12 04:21:04 +00:00
|
|
|
|
{
|
2014-08-14 14:21:07 +00:00
|
|
|
|
GetAppFromDirectory(baseDirectory, list);
|
|
|
|
|
|
FileChangeWatcher.AddWatch(baseDirectory);
|
2014-08-12 04:21:04 +00:00
|
|
|
|
}
|
2014-03-18 18:06:51 +00:00
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void GetAppFromDirectory(string path, List<Program> list)
|
|
|
|
|
|
{
|
2014-08-14 14:21:07 +00:00
|
|
|
|
try
|
2014-03-18 18:06:51 +00:00
|
|
|
|
{
|
2014-08-14 14:21:07 +00:00
|
|
|
|
foreach (string file in Directory.GetFiles(path))
|
2014-03-18 18:06:51 +00:00
|
|
|
|
{
|
2015-01-05 14:41:17 +00:00
|
|
|
|
if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)))
|
2014-08-14 14:21:07 +00:00
|
|
|
|
{
|
|
|
|
|
|
Program p = CreateEntry(file);
|
|
|
|
|
|
list.Add(p);
|
|
|
|
|
|
}
|
2014-03-18 18:06:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-14 14:21:07 +00:00
|
|
|
|
foreach (var subDirectory in Directory.GetDirectories(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
GetAppFromDirectory(subDirectory, list);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (UnauthorizedAccessException e)
|
2014-03-18 18:06:51 +00:00
|
|
|
|
{
|
2014-09-19 08:57:48 +00:00
|
|
|
|
Log.Warn(string.Format("Can't access to directory {0}", path));
|
2014-09-19 08:23:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (DirectoryNotFoundException e)
|
|
|
|
|
|
{
|
2014-09-19 08:57:48 +00:00
|
|
|
|
Log.Warn(string.Format("Directory {0} doesn't exist", path));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (PathTooLongException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Warn(string.Format("File path too long: {0}", e.Message));
|
2014-03-18 18:06:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-03-19 12:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2014-08-14 14:21:07 +00:00
|
|
|
|
return typeof(FileSystemProgramSource).Name + ":" + this.baseDirectory;
|
2014-03-19 12:16:20 +00:00
|
|
|
|
}
|
2014-03-18 18:06:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|