2014-08-14 22:21:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
using System.IO;
|
2014-03-30 11:16:44 +08:00
|
|
|
|
using System.Linq;
|
2014-09-19 16:57:48 +08:00
|
|
|
|
using log4net.Repository.Hierarchy;
|
2014-03-23 16:17:41 +08:00
|
|
|
|
using Wox.Infrastructure.Storage.UserSettings;
|
2014-09-19 16:57:48 +08:00
|
|
|
|
using Log = Wox.Infrastructure.Logger.Log;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
|
2014-03-30 11:16:44 +08:00
|
|
|
|
namespace Wox.Plugin.SystemPlugins.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;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
|
|
|
|
|
public FileSystemProgramSource(string baseDirectory)
|
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
this.baseDirectory = baseDirectory;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-14 22:21:07 +08:00
|
|
|
|
public FileSystemProgramSource(ProgramSource source):this(source.Location)
|
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)
|
|
|
|
|
{
|
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
|
|
|
|
{
|
2014-08-14 22:21:07 +08:00
|
|
|
|
if (UserSettingStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)))
|
|
|
|
|
{
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
GetAppFromDirectory(subDirectory, list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException e)
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2014-09-19 16:57:48 +08:00
|
|
|
|
Log.Warn(string.Format("Can't access to directory {0}", path));
|
2014-09-19 16:23:43 +08:00
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException e)
|
|
|
|
|
{
|
2014-09-19 16:57:48 +08: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-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
|
|
|
|
}
|
|
|
|
|
}
|