This commit is contained in:
qianlifeng 2014-01-24 20:03:45 +08:00
commit 63072205d6
17 changed files with 122 additions and 151 deletions

View file

@ -31,7 +31,7 @@ namespace WinAlfred.Plugin.Fanyi
private string translateURL = "http://openapi.baidu.com/public/2.0/bmt/translate";
private string baiduKey = "SnPcDY3iH5jDbklRewkG2D2v";
static public string AssemblyDirectory
private static string AssemblyDirectory
{
get
{
@ -89,23 +89,6 @@ namespace WinAlfred.Plugin.Fanyi
return results;
}
public static string GetHtmlStr(string url)
{
try
{
WebRequest rGet = WebRequest.Create(url);
WebResponse rSet = rGet.GetResponse();
Stream s = rSet.GetResponseStream();
StreamReader reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
catch (WebException)
{
//连接失败
return null;
}
}
public void Init(PluginInitContext context)
{
this.context = context;

View file

@ -22,3 +22,8 @@ Create workflow
=========
Currently, WinAlfred support using C# and Python to write your workflows. Please refer to [Create-workflows](https://github.com/qianlifeng/WinAlfred/wiki/Create-workflows) page for more infomation.
Share workflow
=========
Share your workflows through <a href="http://winalfred.scottqian.com/">WinAlfredWorkflows</a>. After you upload your workflow, other uses can download or search your workflow through `wf` command (this is a workflow for workflow management , which is one of the default workflows inside winalfred).

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinAlfred.Plugin.System
{
public abstract class BaseSystemPlugin :ISystemPlugin
{
protected abstract List<Result> QueryInternal(Query query);
protected abstract void InitInternal(PluginInitContext context);
public List<Result> Query(Query query)
{
return QueryInternal(query);
}
public void Init(PluginInitContext context)
{
InitInternal(context);
}
public string Name
{
get
{
return "System workflow";
}
}
public string Description
{
get
{
return "System workflow";
}
}
}
}

View file

@ -12,7 +12,7 @@ using WinAlfred.Plugin.System.Common;
namespace WinAlfred.Plugin.System
{
public class BrowserBookmarks : ISystemPlugin
public class BrowserBookmarks : BaseSystemPlugin
{
private List<Bookmark> bookmarks = new List<Bookmark>();
@ -21,7 +21,7 @@ namespace WinAlfred.Plugin.System
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_LOCAL_APPDATA = 0x001c;
public List<Result> Query(Query query)
protected override List<Result> QueryInternal(Query query)
{
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
@ -55,7 +55,7 @@ namespace WinAlfred.Plugin.System
return false;
}
public void Init(PluginInitContext context)
protected override void InitInternal(PluginInitContext context)
{
LoadChromeBookmarks();
}
@ -114,22 +114,6 @@ namespace WinAlfred.Plugin.System
Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
return reg.Replace(dataStr, m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString());
}
public string Name
{
get
{
return "BrowserBookmark";
}
}
public string Description
{
get
{
return "BrowserBookmark";
}
}
}
public class Bookmark

View file

@ -7,9 +7,9 @@ using System.Windows.Forms;
namespace WinAlfred.Plugin.System
{
public class CMD : ISystemPlugin
public class CMD : BaseSystemPlugin
{
public List<Result> Query(Query query)
protected override List<Result> QueryInternal(Query query)
{
List<Result> results = new List<Result>();
if (query.RawQuery.StartsWith(">") && query.RawQuery.Length > 1)
@ -38,24 +38,9 @@ namespace WinAlfred.Plugin.System
return results;
}
public void Init(PluginInitContext context)
protected override void InitInternal(PluginInitContext context)
{
}
public string Name
{
get
{
return "CMD";
}
}
public string Description
{
get
{
return "Execute shell commands.";
}
}
}
}

View file

@ -7,14 +7,14 @@ using System.Text;
namespace WinAlfred.Plugin.System
{
public class DirectoryIndicator : ISystemPlugin
public class DirectoryIndicator : BaseSystemPlugin
{
public List<Result> Query(Query query)
protected override List<Result> QueryInternal(Query query)
{
List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.RawQuery)) return results;
if (CheckIfDirectory(query.RawQuery))
if (Directory.Exists(query.RawQuery))
{
Result result = new Result
{
@ -30,29 +30,9 @@ namespace WinAlfred.Plugin.System
return results;
}
private bool CheckIfDirectory(string path)
{
return Directory.Exists(path);
}
public void Init(PluginInitContext context)
protected override void InitInternal(PluginInitContext context)
{
}
public string Name
{
get
{
return "DirectoryIndicator";
}
}
public string Description
{
get
{
return "DirectoryIndicator";
}
}
}
}

View file

@ -19,7 +19,7 @@ namespace WinAlfred.Plugin.System
public int Score { get; set; }
}
public class Programs : ISystemPlugin
public class Programs : BaseSystemPlugin
{
private List<string> indexDirectory = new List<string>();
private List<string> indexPostfix = new List<string> { "lnk", "exe" };
@ -32,7 +32,7 @@ namespace WinAlfred.Plugin.System
const int CSIDL_COMMON_STARTMENU = 0x16; // \Windows\Start Menu\Programs
const int CSIDL_COMMON_PROGRAMS = 0x17;
public List<Result> Query(Query query)
protected override List<Result> QueryInternal(Query query)
{
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
@ -66,7 +66,7 @@ namespace WinAlfred.Plugin.System
return false;
}
public void Init(PluginInitContext context)
protected override void InitInternal(PluginInitContext context)
{
indexDirectory.Add(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
@ -126,21 +126,5 @@ namespace WinAlfred.Plugin.System
string name = temp.Substring(0, temp.LastIndexOf('.'));
return name;
}
public string Name
{
get
{
return "Programs";
}
}
public string Description
{
get
{
return "get system programs";
}
}
}
}

View file

@ -8,7 +8,7 @@ using System.Windows.Forms;
namespace WinAlfred.Plugin.System
{
public class Sys : ISystemPlugin
public class Sys : BaseSystemPlugin
{
List<Result> availableResults = new List<Result>();
@ -23,7 +23,7 @@ namespace WinAlfred.Plugin.System
[DllImport("user32")]
public static extern void LockWorkStation();
public List<Result> Query(Query query)
protected override List<Result> QueryInternal(Query query)
{
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
@ -39,7 +39,7 @@ namespace WinAlfred.Plugin.System
return results;
}
public void Init(PluginInitContext context)
protected override void InitInternal(PluginInitContext context)
{
availableResults.Add(new Result
{
@ -74,21 +74,5 @@ namespace WinAlfred.Plugin.System
Action = () => context.CloseApp()
});
}
public string Name
{
get
{
return "sys";
}
}
public string Description
{
get
{
return "provide system commands";
}
}
}
}

View file

@ -5,12 +5,12 @@ using System.Text;
namespace WinAlfred.Plugin.System
{
public class ThirdpartyPluginIndicator : ISystemPlugin
public class ThirdpartyPluginIndicator : BaseSystemPlugin
{
private List<PluginPair> allPlugins = new List<PluginPair>();
private Action<string> changeQuery;
public List<Result> Query(Query query)
protected override List<Result> QueryInternal(Query query)
{
List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.RawQuery)) return results;
@ -27,7 +27,7 @@ namespace WinAlfred.Plugin.System
Score = 50,
IcoPath = "Images/work.png",
Action = () => changeQuery(metadataCopy.ActionKeyword + " "),
DontHideWinAlfredAfterAction = true
DontHideWinAlfredAfterSelect = true
};
results.Add(result);
}
@ -35,27 +35,12 @@ namespace WinAlfred.Plugin.System
return results;
}
public void Init(PluginInitContext context)
protected override void InitInternal(PluginInitContext context)
{
allPlugins = context.Plugins;
changeQuery = context.ChangeQuery;
}
public string Name {
get
{
return "ThirdpartyPluginIndicator";
}
}
public string Description
{
get
{
return "ThirdpartyPluginIndicator";
}
}
}
}

View file

@ -44,6 +44,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BaseSystemPlugin.cs" />
<Compile Include="BrowserBookmarks.cs" />
<Compile Include="CMD.cs" />
<Compile Include="Common\ChineseToPinYin.cs" />

View file

@ -14,5 +14,7 @@ namespace WinAlfred.Plugin
public Action HideApp { get; set; }
public Action ShowApp { get; set; }
public Action<string,string,string> ShowMsg { get; set; }
}
}

View file

@ -12,7 +12,12 @@ namespace WinAlfred.Plugin
public Action Action { get; set; }
public int Score { get; set; }
public bool DontHideWinAlfredAfterAction { get; set; }
public bool DontHideWinAlfredAfterSelect { get; set; }
/// <summary>
/// Auto add scores for MRU items
/// </summary>
public bool AutoAjustScore { get; set; }
//todo: this should be controlled by system, not visible to users
/// <summary>

View file

@ -30,6 +30,7 @@ namespace WinAlfred.Commands
{
result.PluginDirectory = pair1.Metadata.PluginDirecotry;
result.OriginQuery = query;
result.AutoAjustScore = true;
}
if(results.Count > 0 && updateView) UpdateResultView(results);
});

View file

@ -231,7 +231,7 @@ namespace WinAlfred
if (result != null)
{
selectedRecords.AddSelect(result);
if (!result.DontHideWinAlfredAfterAction)
if (!result.DontHideWinAlfredAfterSelect)
{
HideWinAlfred();
}
@ -247,13 +247,14 @@ namespace WinAlfred
progressBar.Dispatcher.Invoke(new Action(StopProgress));
if (list.Count > 0)
{
//todo:this used be opened to users, it's they choise use it or not in thier workflows
list.ForEach(o =>
{
o.Score += selectedRecords.GetSelectedCount(o);
if(o.AutoAjustScore) o.Score += selectedRecords.GetSelectedCount(o);
});
resultCtrl.Dispatcher.Invoke(new Action(() =>
{
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).OrderByDescending(o => o.Score).ToList();
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).ToList();
resultCtrl.AddResults(l);
}));
}

View file

@ -20,9 +20,8 @@ namespace WinAlfred.PluginLoader
{
try
{
byte[] buffer = System.IO.File.ReadAllBytes(metadata.ExecuteFilePath);
Assembly asm = Assembly.Load(buffer);
List<Type> types = asm.GetTypes().Where(o => o.IsClass && o.GetInterfaces().Contains(typeof(IPlugin)) || o.GetInterfaces().Contains(typeof(ISystemPlugin))).ToList();
Assembly asm = Assembly.LoadFile(metadata.ExecuteFilePath);
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
if (types.Count == 0)
{
Log.Error(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
@ -58,7 +57,7 @@ namespace WinAlfred.PluginLoader
private void InitPlugin(List<PluginPair> plugins)
{
}
}
}

View file

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Documents;
using Newtonsoft.Json;
using Python.Runtime;
using WinAlfred.Helper;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
@ -12,17 +15,19 @@ namespace WinAlfred.PluginLoader
{
private PluginMetadata metadata;
private string moduleName;
public PythonPluginWrapper(PluginMetadata metadata)
{
this.metadata = metadata;
moduleName = metadata.ExecuteFileName.Replace(".py", "");
}
public List<Result> Query(Query query)
{
try
{
string jsonResult = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery);
string jsonResult = InvokeFunc("query", query.RawQuery);
if (string.IsNullOrEmpty(jsonResult))
{
return new List<Result>();
@ -35,7 +40,7 @@ namespace WinAlfred.PluginLoader
PythonResult ps = pythonResult;
if (!string.IsNullOrEmpty(ps.ActionName))
{
ps.Action = () => InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
ps.Action = () => InvokeFunc(ps.ActionName, ps.ActionPara);
}
r.Add(ps);
}
@ -49,16 +54,37 @@ namespace WinAlfred.PluginLoader
}
#endif
}
}
private string InvokeFunc(string path, string moduleName, string func, string para)
private string InvokeFunc(string func, params string[] para)
{
string json;
PyObject[] paras = { };
if (para != null && para.Length > 0)
{
paras = para.Select(o => new PyString(o)).ToArray();
}
IntPtr gs = PythonEngine.AcquireLock();
PyObject module = PythonEngine.ImportModule(moduleName);
PyObject res = module.InvokeMethod(func, new PyString(para));
string json = Runtime.GetManagedString(res.Handle);
if (module.HasAttr(func))
{
PyObject res = paras.Length > 0 ? module.InvokeMethod(func, paras) : module.InvokeMethod(func);
json = Runtime.GetManagedString(res.Handle);
}
else
{
string error = string.Format("Python Invoke failed: {0} doesn't has function {1}",
metadata.ExecuteFilePath, func);
Log.Error(error);
#if (DEBUG)
{
throw new ArgumentException(error);
}
#endif
}
PythonEngine.ReleaseLock(gs);

View file

@ -74,7 +74,14 @@ namespace WinAlfred
{
if ((currentScore >= next.Result.Score && currentScore <= prev.Result.Score))
{
location = index;
if (currentScore == next.Result.Score)
{
location = index + 1;
}
else
{
location = index;
}
}
}
}