From f25f4f7dc842d7e4bf925b06e67905a21e716344 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Fri, 20 Dec 2013 19:38:10 +0800 Subject: [PATCH] regular commit --- .../WinAlfred.Plugin.Everything.csproj | 2 +- WinAlfred.Plugin/AllowedLanguage.cs | 25 ++ WinAlfred.Plugin/PluginMetadata.cs | 16 ++ WinAlfred.Plugin/Query.cs | 13 +- WinAlfred.Plugin/Result.cs | 4 + WinAlfred.Plugin/WinAlfred.Plugin.csproj | 2 + WinAlfred/App.config | 20 ++ WinAlfred/FrmMain.Designer.cs | 20 +- WinAlfred/FrmMain.cs | 22 ++ WinAlfred/Helper/IniParser.cs | 217 ++++++++++++++++++ WinAlfred/Helper/Log.cs | 19 ++ WinAlfred/PluginLoader/BasePluginLoader.cs | 80 +++++++ WinAlfred/PluginLoader/CSharpPluginLoader.cs | 19 ++ WinAlfred/PluginLoader/PythonPluginLoader.cs | 19 ++ WinAlfred/Properties/AssemblyInfo.cs | 1 + WinAlfred/WinAlfred.csproj | 17 ++ WinAlfred/packages.config | 4 + 17 files changed, 489 insertions(+), 11 deletions(-) create mode 100644 WinAlfred.Plugin/AllowedLanguage.cs create mode 100644 WinAlfred.Plugin/PluginMetadata.cs create mode 100644 WinAlfred/App.config create mode 100644 WinAlfred/Helper/IniParser.cs create mode 100644 WinAlfred/Helper/Log.cs create mode 100644 WinAlfred/PluginLoader/BasePluginLoader.cs create mode 100644 WinAlfred/PluginLoader/CSharpPluginLoader.cs create mode 100644 WinAlfred/PluginLoader/PythonPluginLoader.cs create mode 100644 WinAlfred/packages.config diff --git a/Plugins/WinAlfred.Plugin.Everything/WinAlfred.Plugin.Everything.csproj b/Plugins/WinAlfred.Plugin.Everything/WinAlfred.Plugin.Everything.csproj index 615ecc8b7a..71739b3e00 100644 --- a/Plugins/WinAlfred.Plugin.Everything/WinAlfred.Plugin.Everything.csproj +++ b/Plugins/WinAlfred.Plugin.Everything/WinAlfred.Plugin.Everything.csproj @@ -16,7 +16,7 @@ true full false - bin\Debug\ + ..\..\WinAlfred\bin\Debug\Plugins\Everything\ DEBUG;TRACE prompt 4 diff --git a/WinAlfred.Plugin/AllowedLanguage.cs b/WinAlfred.Plugin/AllowedLanguage.cs new file mode 100644 index 0000000000..9067013c9b --- /dev/null +++ b/WinAlfred.Plugin/AllowedLanguage.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace WinAlfred.Plugin +{ + public static class AllowedLanguage + { + public static string Python + { + get { return "python"; } + } + + public static string CSharp + { + get { return "csharp"; } + } + + public static bool IsAllowed(string language) + { + return language.ToUpper() == Python.ToUpper() || language.ToUpper() == CSharp.ToUpper(); + } + } +} diff --git a/WinAlfred.Plugin/PluginMetadata.cs b/WinAlfred.Plugin/PluginMetadata.cs new file mode 100644 index 0000000000..0ce2b613b5 --- /dev/null +++ b/WinAlfred.Plugin/PluginMetadata.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace WinAlfred.Plugin +{ + public class PluginMetadata + { + public string Name { get; set; } + public string Author { get; set; } + public string Version { get; set; } + public string Language { get; set; } + public string Description { get; set; } + } +} diff --git a/WinAlfred.Plugin/Query.cs b/WinAlfred.Plugin/Query.cs index 9a8c046a59..24005b0727 100644 --- a/WinAlfred.Plugin/Query.cs +++ b/WinAlfred.Plugin/Query.cs @@ -11,12 +11,23 @@ namespace WinAlfred.Plugin public Query(string rawQuery) { RawQuery = rawQuery; + ActionParameters = new List(); ParseQuery(); } private void ParseQuery() { - + if (string.IsNullOrEmpty(RawQuery)) return; + + string[] strings = RawQuery.Split(' '); + ActionName = strings[0]; + if (strings.Length > 1) + { + for (int i = 1; i < strings.Length; i++) + { + ActionParameters.Add(strings[i]); + } + } } } } diff --git a/WinAlfred.Plugin/Result.cs b/WinAlfred.Plugin/Result.cs index f01ac2d1b8..75ea7b1941 100644 --- a/WinAlfred.Plugin/Result.cs +++ b/WinAlfred.Plugin/Result.cs @@ -1,11 +1,15 @@ using System; +using System.Collections.Generic; namespace WinAlfred.Plugin { public class Result { public string Title { get; set; } + public string SubTitle { get; set; } + public string IcoPath { get; set; } public Action Action { get; set; } public int Score { get; set; } + public List ContextResults { get; set; } } } \ No newline at end of file diff --git a/WinAlfred.Plugin/WinAlfred.Plugin.csproj b/WinAlfred.Plugin/WinAlfred.Plugin.csproj index eab3782330..85273995a3 100644 --- a/WinAlfred.Plugin/WinAlfred.Plugin.csproj +++ b/WinAlfred.Plugin/WinAlfred.Plugin.csproj @@ -38,7 +38,9 @@ + + diff --git a/WinAlfred/App.config b/WinAlfred/App.config new file mode 100644 index 0000000000..0383125086 --- /dev/null +++ b/WinAlfred/App.config @@ -0,0 +1,20 @@ + + + +
+ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WinAlfred/FrmMain.Designer.cs b/WinAlfred/FrmMain.Designer.cs index e3f672f95f..f0d93e2cc4 100644 --- a/WinAlfred/FrmMain.Designer.cs +++ b/WinAlfred/FrmMain.Designer.cs @@ -28,29 +28,31 @@ /// private void InitializeComponent() { - this.textBox1 = new System.Windows.Forms.TextBox(); + this.tbQuery = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // - // textBox1 + // tbQuery // - this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.textBox1.Location = new System.Drawing.Point(12, 12); - this.textBox1.Name = "textBox1"; - this.textBox1.Size = new System.Drawing.Size(471, 21); - this.textBox1.TabIndex = 0; + this.tbQuery.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.tbQuery.Location = new System.Drawing.Point(12, 12); + this.tbQuery.Name = "tbQuery"; + this.tbQuery.Size = new System.Drawing.Size(471, 21); + this.tbQuery.TabIndex = 0; + this.tbQuery.TextChanged += new System.EventHandler(this.TbQuery_TextChanged); // // FrmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(495, 45); - this.Controls.Add(this.textBox1); + this.Controls.Add(this.tbQuery); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FrmMain"; this.ShowIcon = false; this.Text = "Form1"; this.TopMost = true; + this.Load += new System.EventHandler(this.FrmMain_Load); this.ResumeLayout(false); this.PerformLayout(); @@ -58,7 +60,7 @@ #endregion - private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox tbQuery; } diff --git a/WinAlfred/FrmMain.cs b/WinAlfred/FrmMain.cs index 5aaad25f42..2c269ec258 100644 --- a/WinAlfred/FrmMain.cs +++ b/WinAlfred/FrmMain.cs @@ -6,14 +6,36 @@ using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; +using WinAlfred.Plugin; +using WinAlfred.PluginLoader; namespace WinAlfred { public partial class FrmMain : Form { + public List plugins = new List(); + private List results = new List(); + public FrmMain() { InitializeComponent(); } + + private void FrmMain_Load(object sender, EventArgs e) + { + plugins.AddRange(new PythonPluginLoader().LoadPlugin()); + plugins.AddRange(new CSharpPluginLoader().LoadPlugin()); + } + + private void TbQuery_TextChanged(object sender, EventArgs e) + { + results.Clear(); + foreach (IPlugin plugin in plugins) + { + results.AddRange(plugin.Query(new Query(tbQuery.Text))); + } + var s = results.OrderByDescending(o => o.Score); + } + } } diff --git a/WinAlfred/Helper/IniParser.cs b/WinAlfred/Helper/IniParser.cs new file mode 100644 index 0000000000..d72a38e191 --- /dev/null +++ b/WinAlfred/Helper/IniParser.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections; +using System.IO; + +namespace WinAlfred.Helper +{ + public class IniParser + { + private Hashtable keyPairs = new Hashtable(); + private String iniFilePath; + + private struct SectionPair + { + public String Section; + public String Key; + } + + /// + /// Opens the INI file at the given path and enumerates the values in the IniParser. + /// + /// Full path to INI file. + public IniParser(String iniPath) + { + TextReader iniFile = null; + String strLine = null; + String currentRoot = null; + String[] keyPair = null; + + iniFilePath = iniPath; + + if (File.Exists(iniPath)) + { + try + { + iniFile = new StreamReader(iniPath); + + strLine = iniFile.ReadLine(); + + while (strLine != null) + { + strLine = strLine.Trim(); + + if (strLine != "") + { + if (strLine.StartsWith("[") && strLine.EndsWith("]")) + { + currentRoot = strLine.Substring(1, strLine.Length - 2).ToUpper(); + } + else + { + keyPair = strLine.Split(new char[] { '=' }, 2); + + SectionPair sectionPair; + String value = null; + + if (currentRoot == null) + currentRoot = "ROOT"; + + sectionPair.Section = currentRoot; + sectionPair.Key = keyPair[0].ToUpper().Trim(); + + if (keyPair.Length > 1) + value = keyPair[1]; + + keyPairs.Add(sectionPair, value.Trim()); + } + } + + strLine = iniFile.ReadLine(); + } + + } + catch (Exception ex) + { + throw ex; + } + finally + { + if (iniFile != null) + iniFile.Close(); + } + } + else + throw new FileNotFoundException("Unable to locate " + iniPath); + } + + /// + /// Returns the value for the given section, key pair. + /// + /// Section name. + /// Key name. + public String GetSetting(String sectionName, String settingName) + { + SectionPair sectionPair; + sectionPair.Section = sectionName.ToUpper().Trim(); + sectionPair.Key = settingName.ToUpper().Trim(); + + return (String)keyPairs[sectionPair]; + } + + /// + /// Enumerates all lines for given section. + /// + /// Section to enum. + public String[] EnumSection(String sectionName) + { + ArrayList tmpArray = new ArrayList(); + + foreach (SectionPair pair in keyPairs.Keys) + { + if (pair.Section == sectionName.ToUpper()) + tmpArray.Add(pair.Key); + } + + return (String[])tmpArray.ToArray(typeof(String)); + } + + /// + /// Adds or replaces a setting to the table to be saved. + /// + /// Section to add under. + /// Key name to add. + /// Value of key. + public void AddSetting(String sectionName, String settingName, String settingValue) + { + SectionPair sectionPair; + sectionPair.Section = sectionName.ToUpper(); + sectionPair.Key = settingName.ToUpper(); + + if (keyPairs.ContainsKey(sectionPair)) + keyPairs.Remove(sectionPair); + + keyPairs.Add(sectionPair, settingValue); + } + + /// + /// Adds or replaces a setting to the table to be saved with a null value. + /// + /// Section to add under. + /// Key name to add. + public void AddSetting(String sectionName, String settingName) + { + AddSetting(sectionName, settingName, null); + } + + /// + /// Remove a setting. + /// + /// Section to add under. + /// Key name to add. + public void DeleteSetting(String sectionName, String settingName) + { + SectionPair sectionPair; + sectionPair.Section = sectionName.ToUpper(); + sectionPair.Key = settingName.ToUpper(); + + if (keyPairs.ContainsKey(sectionPair)) + keyPairs.Remove(sectionPair); + } + + /// + /// Save settings to new file. + /// + /// New file path. + public void SaveSettings(String newFilePath) + { + ArrayList sections = new ArrayList(); + String tmpValue = ""; + String strToSave = ""; + + foreach (SectionPair sectionPair in keyPairs.Keys) + { + if (!sections.Contains(sectionPair.Section)) + sections.Add(sectionPair.Section); + } + + foreach (String section in sections) + { + strToSave += ("[" + section + "]\r\n"); + + foreach (SectionPair sectionPair in keyPairs.Keys) + { + if (sectionPair.Section == section) + { + tmpValue = (String)keyPairs[sectionPair]; + + if (tmpValue != null) + tmpValue = "=" + tmpValue; + + strToSave += (sectionPair.Key + tmpValue + "\r\n"); + } + } + + strToSave += "\r\n"; + } + + try + { + TextWriter tw = new StreamWriter(newFilePath); + tw.Write(strToSave); + tw.Close(); + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// Save settings back to ini file. + /// + public void SaveSettings() + { + SaveSettings(iniFilePath); + } + } +} \ No newline at end of file diff --git a/WinAlfred/Helper/Log.cs b/WinAlfred/Helper/Log.cs new file mode 100644 index 0000000000..158245a088 --- /dev/null +++ b/WinAlfred/Helper/Log.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using log4net; + +namespace WinAlfred.Helper +{ + public class Log + { + private static ILog fileLogger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + public static ILog FileLogger + { + get { return fileLogger; } + } + } +} diff --git a/WinAlfred/PluginLoader/BasePluginLoader.cs b/WinAlfred/PluginLoader/BasePluginLoader.cs new file mode 100644 index 0000000000..f408342717 --- /dev/null +++ b/WinAlfred/PluginLoader/BasePluginLoader.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using WinAlfred.Helper; +using WinAlfred.Plugin; +using log4net; + +namespace WinAlfred.PluginLoader +{ + public abstract class BasePluginLoader + { + private static string PluginPath = "Plugins"; + private static string PluginConfigName = "plugin.ini"; + protected static List pluginMetadatas = new List(); + + public abstract List LoadPlugin(); + + static BasePluginLoader() + { + ParsePlugins(); + } + + protected static void ParsePlugins() + { + ParseDirectories(); + ParsePackagedPlugin(); + } + + private static void ParseDirectories() + { + string[] directories = Directory.GetDirectories(PluginPath); + foreach (string directory in directories) + { + string iniPath = directory + "\\" + PluginConfigName; + PluginMetadata metadata = GetMetadataFromIni(iniPath); + if (metadata != null) pluginMetadatas.Add(metadata); + } + } + + private static void ParsePackagedPlugin() + { + + } + + private static PluginMetadata GetMetadataFromIni(string iniPath) + { + if (!File.Exists(iniPath)) + { + Log.FileLogger.Error(string.Format("parse plugin {0} failed: didn't find config file.", iniPath)); + return null; + } + + + try + { + PluginMetadata metadata = new PluginMetadata(); + IniParser ini = new IniParser(iniPath); + metadata.Name = ini.GetSetting("plugin", "Name"); + metadata.Author = ini.GetSetting("plugin", "Author"); + metadata.Description = ini.GetSetting("plugin", "Description"); + metadata.Language = ini.GetSetting("plugin", "Language"); + metadata.Version = ini.GetSetting("plugin", "Version"); + + if (!AllowedLanguage.IsAllowed(metadata.Language)) + { + Log.FileLogger.Error(string.Format("Parse ini {0} failed: invalid language {1}", iniPath, metadata.Language)); + return null; + } + + return metadata; + } + catch (Exception e) + { + Log.FileLogger.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message)); + return null; + } + } + } +} diff --git a/WinAlfred/PluginLoader/CSharpPluginLoader.cs b/WinAlfred/PluginLoader/CSharpPluginLoader.cs new file mode 100644 index 0000000000..f9babbc38e --- /dev/null +++ b/WinAlfred/PluginLoader/CSharpPluginLoader.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using WinAlfred.Helper; +using WinAlfred.Plugin; + +namespace WinAlfred.PluginLoader +{ + public class CSharpPluginLoader:BasePluginLoader + { + private List plugins = new List(); + + public override List LoadPlugin() + { + return plugins; + } + } +} diff --git a/WinAlfred/PluginLoader/PythonPluginLoader.cs b/WinAlfred/PluginLoader/PythonPluginLoader.cs new file mode 100644 index 0000000000..588c7cffeb --- /dev/null +++ b/WinAlfred/PluginLoader/PythonPluginLoader.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using WinAlfred.Helper; +using WinAlfred.Plugin; + +namespace WinAlfred.PluginLoader +{ + public class PythonPluginLoader :BasePluginLoader + { + private List plugins = new List(); + + public override List LoadPlugin() + { + return plugins; + } + } +} diff --git a/WinAlfred/Properties/AssemblyInfo.cs b/WinAlfred/Properties/AssemblyInfo.cs index 6e40bfb435..d6daf1e7d5 100644 --- a/WinAlfred/Properties/AssemblyInfo.cs +++ b/WinAlfred/Properties/AssemblyInfo.cs @@ -34,3 +34,4 @@ using System.Runtime.InteropServices; // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)] \ No newline at end of file diff --git a/WinAlfred/WinAlfred.csproj b/WinAlfred/WinAlfred.csproj index 9921764eb4..e9f55f4d60 100644 --- a/WinAlfred/WinAlfred.csproj +++ b/WinAlfred/WinAlfred.csproj @@ -32,6 +32,9 @@ 4 + + ..\packages\log4net.2.0.3\lib\net35-full\log4net.dll + @@ -49,6 +52,11 @@ FrmMain.cs + + + + + @@ -63,6 +71,8 @@ True Resources.resx + + SettingsSingleFileGenerator Settings.Designer.cs @@ -73,6 +83,13 @@ True + + + {8451ecdd-2ea4-4966-bb0a-7bbc40138e80} + WinAlfred.Plugin + + +