2014-01-06 21:25:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Windows;
|
2015-10-31 07:17:34 +08:00
|
|
|
|
using System.Windows.Controls;
|
2014-01-06 21:25:24 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.Folder
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
2015-02-04 23:16:41 +08:00
|
|
|
|
public class FolderPlugin : IPlugin, ISettingProvider, IPluginI18n
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
|
|
|
|
private static List<string> driverNames;
|
|
|
|
|
private PluginInitContext context;
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
2014-07-19 10:12:11 +08:00
|
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
{
|
2015-02-04 23:16:41 +08:00
|
|
|
|
return new FileSystemSettings(context.API);
|
2014-07-19 10:12:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
public void Init(PluginInitContext context)
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.context.API.BackKeyDownEvent += ApiBackKeyDownEvent;
|
2015-02-03 18:32:16 +08:00
|
|
|
|
this.context.API.ResultItemDropEvent += API_ResultItemDropEvent;
|
2014-07-19 10:12:11 +08:00
|
|
|
|
InitialDriverList();
|
2015-01-05 22:41:17 +08:00
|
|
|
|
if (FolderStorage.Instance.FolderLinks == null)
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
2015-01-05 22:41:17 +08:00
|
|
|
|
FolderStorage.Instance.FolderLinks = new List<FolderLink>();
|
|
|
|
|
FolderStorage.Instance.Save();
|
2014-07-19 10:12:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 23:16:41 +08:00
|
|
|
|
void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e)
|
2015-02-03 18:32:16 +08:00
|
|
|
|
{
|
2015-02-04 23:16:41 +08:00
|
|
|
|
if (dropObject.GetDataPresent(DataFormats.FileDrop))
|
|
|
|
|
{
|
|
|
|
|
HanldeFilesDrop(result, dropObject);
|
|
|
|
|
}
|
2015-02-03 18:32:16 +08:00
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 23:16:41 +08:00
|
|
|
|
private void HanldeFilesDrop(Result targetResult, IDataObject dropObject)
|
|
|
|
|
{
|
|
|
|
|
List<string> files = ((string[])dropObject.GetData(DataFormats.FileDrop, false)).ToList();
|
|
|
|
|
context.API.ShowContextMenu(context.CurrentPluginMetadata, GetContextMenusForFileDrop(targetResult, files));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<Result> GetContextMenusForFileDrop(Result targetResult, List<string> files)
|
|
|
|
|
{
|
|
|
|
|
List<Result> contextMenus = new List<Result>();
|
|
|
|
|
string folderPath = ((FolderLink) targetResult.ContextData).Path;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
contextMenus.Add(new Result
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
|
|
|
|
Title = "Copy to this folder",
|
|
|
|
|
IcoPath = "Images/copy.png",
|
|
|
|
|
Action = _ =>
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Copy");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return contextMenus;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 18:21:48 +08:00
|
|
|
|
private void ApiBackKeyDownEvent(WoxKeyDownEventArgs e)
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
|
|
|
|
string query = e.Query;
|
|
|
|
|
if (Directory.Exists(query))
|
|
|
|
|
{
|
|
|
|
|
if (query.EndsWith("\\"))
|
|
|
|
|
{
|
|
|
|
|
query = query.Remove(query.Length - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.Contains("\\"))
|
|
|
|
|
{
|
|
|
|
|
int index = query.LastIndexOf("\\");
|
|
|
|
|
query = query.Remove(index) + "\\";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.API.ChangeQuery(query);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
public List<Result> Query(Query query)
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
|
string input = query.Search.ToLower();
|
2014-07-19 10:12:11 +08:00
|
|
|
|
|
2015-01-05 22:41:17 +08:00
|
|
|
|
List<FolderLink> userFolderLinks = FolderStorage.Instance.FolderLinks.Where(
|
2014-07-19 10:12:11 +08:00
|
|
|
|
x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase)).ToList();
|
|
|
|
|
List<Result> results =
|
|
|
|
|
userFolderLinks.Select(
|
|
|
|
|
item => new Result(item.Nickname, "Images/folder.png", "Ctrl + Enter to open the directory")
|
|
|
|
|
{
|
|
|
|
|
Action = c =>
|
|
|
|
|
{
|
|
|
|
|
if (c.SpecialKeyState.CtrlPressed)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process.Start(item.Path);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Could not start " + item.Path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-05 01:30:11 +08:00
|
|
|
|
context.API.ChangeQuery(item.Path + (item.Path.EndsWith("\\")? "": "\\"));
|
2014-07-19 10:12:11 +08:00
|
|
|
|
return false;
|
2015-02-04 23:16:41 +08:00
|
|
|
|
},
|
|
|
|
|
ContextData = item
|
2014-07-19 10:12:11 +08:00
|
|
|
|
}).ToList();
|
|
|
|
|
|
2015-01-08 22:49:42 +08:00
|
|
|
|
if (driverNames != null && !driverNames.Any(input.StartsWith))
|
2014-07-19 10:12:11 +08:00
|
|
|
|
return results;
|
|
|
|
|
|
2015-08-23 15:40:18 +08:00
|
|
|
|
//if (!input.EndsWith("\\"))
|
|
|
|
|
//{
|
|
|
|
|
// //"c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive"
|
|
|
|
|
// input = input + "\\";
|
|
|
|
|
//}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
results.AddRange(QueryInternal_Directory_Exists(input));
|
|
|
|
|
|
|
|
|
|
return results;
|
2015-02-04 23:16:41 +08:00
|
|
|
|
} private void InitialDriverList()
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
|
|
|
|
if (driverNames == null)
|
|
|
|
|
{
|
|
|
|
|
driverNames = new List<string>();
|
|
|
|
|
DriveInfo[] allDrives = DriveInfo.GetDrives();
|
|
|
|
|
foreach (DriveInfo driver in allDrives)
|
|
|
|
|
{
|
|
|
|
|
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Result> QueryInternal_Directory_Exists(string rawQuery)
|
|
|
|
|
{
|
|
|
|
|
var results = new List<Result>();
|
2015-08-23 15:40:18 +08:00
|
|
|
|
|
|
|
|
|
string incompleteName = "";
|
|
|
|
|
if (!Directory.Exists(rawQuery + "\\"))
|
|
|
|
|
{
|
|
|
|
|
//if the last component of the path is incomplete,
|
|
|
|
|
//then make auto complete for it.
|
|
|
|
|
int index = rawQuery.LastIndexOf('\\');
|
|
|
|
|
if (index > 0 && index < (rawQuery.Length - 1))
|
|
|
|
|
{
|
|
|
|
|
incompleteName = rawQuery.Substring(index + 1);
|
|
|
|
|
incompleteName = incompleteName.ToLower();
|
|
|
|
|
rawQuery = rawQuery.Substring(0, index + 1);
|
|
|
|
|
if (!Directory.Exists(rawQuery))
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!rawQuery.EndsWith("\\"))
|
|
|
|
|
rawQuery += "\\";
|
|
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
|
2015-08-23 15:40:18 +08:00
|
|
|
|
string firstResult = "Open current directory";
|
|
|
|
|
if (incompleteName.Length > 0)
|
|
|
|
|
firstResult = "Open " + rawQuery;
|
|
|
|
|
results.Add(new Result(firstResult, "Images/folder.png")
|
2014-07-19 10:12:11 +08:00
|
|
|
|
{
|
|
|
|
|
Score = 10000,
|
|
|
|
|
Action = c =>
|
|
|
|
|
{
|
|
|
|
|
Process.Start(rawQuery);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//Add children directories
|
|
|
|
|
DirectoryInfo[] dirs = new DirectoryInfo(rawQuery).GetDirectories();
|
|
|
|
|
foreach (DirectoryInfo dir in dirs)
|
|
|
|
|
{
|
|
|
|
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
|
|
|
|
|
2015-08-23 15:40:18 +08:00
|
|
|
|
if (incompleteName.Length != 0 && !dir.Name.ToLower().StartsWith(incompleteName))
|
|
|
|
|
continue;
|
2014-07-19 10:12:11 +08:00
|
|
|
|
DirectoryInfo dirCopy = dir;
|
|
|
|
|
var result = new Result(dir.Name, "Images/folder.png", "Ctrl + Enter to open the directory")
|
|
|
|
|
{
|
|
|
|
|
Action = c =>
|
|
|
|
|
{
|
|
|
|
|
if (c.SpecialKeyState.CtrlPressed)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process.Start(dirCopy.FullName);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Could not start " + dirCopy.FullName);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
context.API.ChangeQuery(dirCopy.FullName + "\\");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Add children files
|
|
|
|
|
FileInfo[] files = new DirectoryInfo(rawQuery).GetFiles();
|
|
|
|
|
foreach (FileInfo file in files)
|
|
|
|
|
{
|
|
|
|
|
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
2015-08-23 15:40:18 +08:00
|
|
|
|
if (incompleteName.Length != 0 && !file.Name.ToLower().StartsWith(incompleteName))
|
|
|
|
|
continue;
|
2014-07-19 10:12:11 +08:00
|
|
|
|
string filePath = file.FullName;
|
|
|
|
|
var result = new Result(Path.GetFileName(filePath), "Images/file.png")
|
|
|
|
|
{
|
|
|
|
|
Action = c =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process.Start(filePath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Could not start " + filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2015-01-07 18:59:55 +08:00
|
|
|
|
|
2015-02-07 20:17:49 +08:00
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
{
|
|
|
|
|
return context.API.GetTranslation("wox_plugin_folder_plugin_name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
{
|
|
|
|
|
return context.API.GetTranslation("wox_plugin_folder_plugin_description");
|
|
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
}
|
2014-05-10 09:24:16 +08:00
|
|
|
|
}
|