regular commit

This commit is contained in:
qianlifeng 2013-12-20 19:38:10 +08:00
parent c8fca3d63f
commit f25f4f7dc8
17 changed files with 489 additions and 11 deletions

View File

@ -16,7 +16,7 @@
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>..\..\WinAlfred\bin\Debug\Plugins\Everything\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>

View File

@ -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();
}
}
}

View File

@ -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; }
}
}

View File

@ -11,12 +11,23 @@ namespace WinAlfred.Plugin
public Query(string rawQuery) public Query(string rawQuery)
{ {
RawQuery = rawQuery; RawQuery = rawQuery;
ActionParameters = new List<string>();
ParseQuery(); ParseQuery();
} }
private void 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]);
}
}
} }
} }
} }

View File

@ -1,11 +1,15 @@
using System; using System;
using System.Collections.Generic;
namespace WinAlfred.Plugin namespace WinAlfred.Plugin
{ {
public class Result public class Result
{ {
public string Title { get; set; } public string Title { get; set; }
public string SubTitle { get; set; }
public string IcoPath { get; set; }
public Action Action { get; set; } public Action Action { get; set; }
public int Score { get; set; } public int Score { get; set; }
public List<Result> ContextResults { get; set; }
} }
} }

View File

@ -38,7 +38,9 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AllowedLanguage.cs" />
<Compile Include="IPlugin.cs" /> <Compile Include="IPlugin.cs" />
<Compile Include="PluginMetadata.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Query.cs" /> <Compile Include="Query.cs" />
<Compile Include="Result.cs" /> <Compile Include="Result.cs" />

20
WinAlfred/App.config Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HH:mm:ss" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="Date%date Level%-5level Msg%message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>

View File

@ -28,29 +28,31 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.textBox1 = new System.Windows.Forms.TextBox(); this.tbQuery = new System.Windows.Forms.TextBox();
this.SuspendLayout(); this.SuspendLayout();
// //
// textBox1 // tbQuery
// //
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tbQuery.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(12, 12); this.tbQuery.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1"; this.tbQuery.Name = "tbQuery";
this.textBox1.Size = new System.Drawing.Size(471, 21); this.tbQuery.Size = new System.Drawing.Size(471, 21);
this.textBox1.TabIndex = 0; this.tbQuery.TabIndex = 0;
this.tbQuery.TextChanged += new System.EventHandler(this.TbQuery_TextChanged);
// //
// FrmMain // FrmMain
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(495, 45); this.ClientSize = new System.Drawing.Size(495, 45);
this.Controls.Add(this.textBox1); this.Controls.Add(this.tbQuery);
this.MaximizeBox = false; this.MaximizeBox = false;
this.MinimizeBox = false; this.MinimizeBox = false;
this.Name = "FrmMain"; this.Name = "FrmMain";
this.ShowIcon = false; this.ShowIcon = false;
this.Text = "Form1"; this.Text = "Form1";
this.TopMost = true; this.TopMost = true;
this.Load += new System.EventHandler(this.FrmMain_Load);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -58,7 +60,7 @@
#endregion #endregion
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox tbQuery;
} }

View File

@ -6,14 +6,36 @@ using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
namespace WinAlfred namespace WinAlfred
{ {
public partial class FrmMain : Form public partial class FrmMain : Form
{ {
public List<IPlugin> plugins = new List<IPlugin>();
private List<Result> results = new List<Result>();
public FrmMain() public FrmMain()
{ {
InitializeComponent(); 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);
}
} }
} }

View File

@ -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;
}
/// <summary>
/// Opens the INI file at the given path and enumerates the values in the IniParser.
/// </summary>
/// <param name="iniPath">Full path to INI file.</param>
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);
}
/// <summary>
/// Returns the value for the given section, key pair.
/// </summary>
/// <param name="sectionName">Section name.</param>
/// <param name="settingName">Key name.</param>
public String GetSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper().Trim();
sectionPair.Key = settingName.ToUpper().Trim();
return (String)keyPairs[sectionPair];
}
/// <summary>
/// Enumerates all lines for given section.
/// </summary>
/// <param name="sectionName">Section to enum.</param>
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));
}
/// <summary>
/// Adds or replaces a setting to the table to be saved.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
/// <param name="settingValue">Value of key.</param>
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);
}
/// <summary>
/// Adds or replaces a setting to the table to be saved with a null value.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void AddSetting(String sectionName, String settingName)
{
AddSetting(sectionName, settingName, null);
}
/// <summary>
/// Remove a setting.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void DeleteSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
}
/// <summary>
/// Save settings to new file.
/// </summary>
/// <param name="newFilePath">New file path.</param>
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;
}
}
/// <summary>
/// Save settings back to ini file.
/// </summary>
public void SaveSettings()
{
SaveSettings(iniFilePath);
}
}
}

19
WinAlfred/Helper/Log.cs Normal file
View File

@ -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; }
}
}
}

View File

@ -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<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
public abstract List<IPlugin> 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;
}
}
}
}

View File

@ -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<IPlugin> plugins = new List<IPlugin>();
public override List<IPlugin> LoadPlugin()
{
return plugins;
}
}
}

View File

@ -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<IPlugin> plugins = new List<IPlugin>();
public override List<IPlugin> LoadPlugin()
{
return plugins;
}
}
}

View File

@ -34,3 +34,4 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

View File

@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="log4net">
<HintPath>..\packages\log4net.2.0.3\lib\net35-full\log4net.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@ -49,6 +52,11 @@
<Compile Include="FrmMain.Designer.cs"> <Compile Include="FrmMain.Designer.cs">
<DependentUpon>FrmMain.cs</DependentUpon> <DependentUpon>FrmMain.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Helper\Log.cs" />
<Compile Include="PluginLoader\CSharpPluginLoader.cs" />
<Compile Include="PluginLoader\BasePluginLoader.cs" />
<Compile Include="Helper\IniParser.cs" />
<Compile Include="PluginLoader\PythonPluginLoader.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="FrmMain.resx"> <EmbeddedResource Include="FrmMain.resx">
@ -63,6 +71,8 @@
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<None Include="App.config" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@ -73,6 +83,13 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WinAlfred.Plugin\WinAlfred.Plugin.csproj">
<Project>{8451ecdd-2ea4-4966-bb0a-7bbc40138e80}</Project>
<Name>WinAlfred.Plugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net35" />
</packages>