Add system commands

This commit is contained in:
qianlifeng 2014-01-03 18:16:05 +08:00
parent 5893564f46
commit 1eb3f449e2
15 changed files with 317 additions and 45 deletions

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinAlfred.Plugin.System
{
public class CMD:IPlugin
{
public List<Result> Query(Query query)
{
throw new NotImplementedException();
}
public void Init()
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinAlfred.Plugin.System
{
public interface ISystemPlugin : IPlugin
{
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WinAlfred.Plugin.System")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Oracle Corporation")]
[assembly: AssemblyProduct("WinAlfred.Plugin.System")]
[assembly: AssemblyCopyright("Copyright © Oracle Corporation 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("bab8d3dc-d6be-42b3-8aa4-24801d667528")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{69CE0206-CB41-453D-88AF-DF86092EF9B8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WinAlfred.Plugin.System</RootNamespace>
<AssemblyName>WinAlfred.Plugin.System</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CMD.cs" />
<Compile Include="ISystemPlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WinAlfred.Plugin\WinAlfred.Plugin.csproj">
<Project>{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}</Project>
<Name>WinAlfred.Plugin</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -16,5 +16,6 @@ namespace WinAlfred.Plugin
public string ExecuteFileName { get; set; }
public string PluginDirecotry { get; set; }
public string ActionKeyword { get; set; }
public PluginType PluginType { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinAlfred.Plugin
{
public enum PluginType
{
System,
ThirdParty
}
}

View File

@ -61,6 +61,7 @@
<Compile Include="IPlugin.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="PluginMetadata.cs" />
<Compile Include="PluginType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PythonResult.cs" />
<Compile Include="Query.cs" />

View File

@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred", "WinAlfred\WinA
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PyWinAlfred", "PyWinAlfred\PyWinAlfred.vcxproj", "{D03FD663-38A8-4C1A-8431-EB44F93E7EBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred.Plugin.System", "WinAlfred.Plugin.System\WinAlfred.Plugin.System.csproj", "{69CE0206-CB41-453D-88AF-DF86092EF9B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -104,6 +106,20 @@ Global
{D03FD663-38A8-4C1A-8431-EB44F93E7EBA}.Release|x64.Build.0 = Release|x64
{D03FD663-38A8-4C1A-8431-EB44F93E7EBA}.Release|x86.ActiveCfg = Release|Win32
{D03FD663-38A8-4C1A-8431-EB44F93E7EBA}.Release|x86.Build.0 = Release|Win32
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|Win32.ActiveCfg = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|x64.ActiveCfg = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Debug|x86.ActiveCfg = Debug|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|Any CPU.Build.0 = Release|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|Win32.ActiveCfg = Release|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|x64.ActiveCfg = Release|Any CPU
{69CE0206-CB41-453D-88AF-DF86092EF9B8}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinAlfred.Helper;
using WinAlfred.Plugin;
namespace WinAlfred.Commands
{
public class CommandDispatcher
{
private PluginCommand pluginCmd = new PluginCommand();
private SystemCommand systemCmd = new SystemCommand();
//public delegate void resultUpdateDelegate(List<Result> results);
//public event resultUpdateDelegate OnResultUpdateEvent;
//protected virtual void OnOnResultUpdateEvent(List<Result> list)
//{
// resultUpdateDelegate handler = OnResultUpdateEvent;
// if (handler != null) handler(list);
//}
public void DispatchCommand(Query query)
{
systemCmd.Dispatch(query);
pluginCmd.Dispatch(query);
}
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
namespace WinAlfred.Commands
{
public class PluginCommand
{
public void Dispatch(Query q)
{
//ThreadPool.QueueUserWorkItem(state =>
//{
foreach (PluginPair pair in Plugins.AllPlugins)
{
if (pair.Metadata.ActionKeyword == q.ActionName)
{
try
{
pair.Plugin.Query(q).ForEach(o => o.PluginDirectory = pair.Metadata.PluginDirecotry);
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", pair.Metadata.Name,
queryException.Message));
#if (DEBUG)
{
throw;
}
#endif
}
}
}
resultCtrl.Dispatcher.Invoke(new Action(() =>
{
resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList());
resultCtrl.SelectFirst();
}));
//});
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinAlfred.Plugin;
namespace WinAlfred.Commands
{
public class SystemCommand
{
public void Dispatch(Query query)
{
}
}
}

View File

@ -8,6 +8,7 @@ using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using WinAlfred.Commands;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
@ -18,9 +19,9 @@ namespace WinAlfred
public partial class MainWindow : Window
{
private KeyboardHook hook = new KeyboardHook();
public List<PluginPair> plugins = new List<PluginPair>();
private List<Result> results = new List<Result>();
private NotifyIcon notifyIcon = null;
private CommandDispatcher cmdDispatcher = new CommandDispatcher();
public MainWindow()
{
@ -67,38 +68,8 @@ namespace WinAlfred
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
string query = tbQuery.Text;
//ThreadPool.QueueUserWorkItem(state =>
//{
results.Clear();
foreach (PluginPair pair in plugins)
{
var q = new Query(query);
if (pair.Metadata.ActionKeyword == q.ActionName)
{
try
{
results.AddRange(pair.Plugin.Query(q));
results.ForEach(o => o.PluginDirectory = pair.Metadata.PluginDirecotry);
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", pair.Metadata.Name,
queryException.Message));
#if (DEBUG)
{
throw;
}
#endif
}
}
}
resultCtrl.Dispatcher.Invoke(new Action(() =>
{
resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList());
resultCtrl.SelectFirst();
}));
//});
var q = new Query(tbQuery.Text);
cmdDispatcher.DispatchCommand(q);
}
private void HideWinAlfred()
@ -116,9 +87,7 @@ namespace WinAlfred
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
Plugins.Init();
ShowWinAlfred();
InitialTray();
}

View File

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.Plugin.System;
namespace WinAlfred.PluginLoader
{
@ -21,11 +24,34 @@ namespace WinAlfred.PluginLoader
private static void ParsePlugins()
{
ParseDirectories();
ParsePackagedPlugin();
ParseSystemPlugins();
ParseThirdPartyPlugins();
}
private static void ParseDirectories()
private static void ParseSystemPlugins()
{
try
{
Assembly asm = Assembly.GetAssembly(typeof (CMD));
List<Type> types = asm.GetTypes().Where(o => o.GetInterfaces().Contains(typeof(ISystemPlugin))).ToList();
foreach (Type type in types)
{
ISystemPlugin sysPlugin = Activator.CreateInstance(types[0]) as ISystemPlugin;
PluginMetadata metadata = new PluginMetadata();
}
}
catch (Exception e)
{
Log.Error(string.Format("Cound't load system plugin: {0}", e.Message));
#if (DEBUG)
{
throw;
}
#endif
}
}
private static void ParseThirdPartyPlugins()
{
string[] directories = Directory.GetDirectories(PluginPath);
foreach (string directory in directories)
@ -35,11 +61,6 @@ namespace WinAlfred.PluginLoader
}
}
private static void ParsePackagedPlugin()
{
}
private static PluginMetadata GetMetadataFromIni(string directory)
{
string iniPath = directory + "\\" + PluginConfigName;
@ -50,7 +71,6 @@ namespace WinAlfred.PluginLoader
return null;
}
try
{
PluginMetadata metadata = new PluginMetadata();
@ -60,6 +80,7 @@ namespace WinAlfred.PluginLoader
metadata.Description = ini.GetSetting("plugin", "Description");
metadata.Language = ini.GetSetting("plugin", "Language");
metadata.Version = ini.GetSetting("plugin", "Version");
metadata.PluginType = PluginType.ThirdParty;
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
metadata.ExecuteFilePath = AppDomain.CurrentDomain.BaseDirectory + directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
metadata.PluginDirecotry = AppDomain.CurrentDomain.BaseDirectory + directory + "\\";

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public static class Plugins
{
private static List<PluginPair> plugins = new List<PluginPair>();
public static void Init()
{
plugins.Clear();
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
}
public static List<PluginPair> AllPlugins
{
get { return plugins; }
}
}
}

View File

@ -64,12 +64,16 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Commands\CommandDispatcher.cs" />
<Compile Include="Commands\PluginCommand.cs" />
<Compile Include="Commands\SystemCommand.cs" />
<Compile Include="Helper\IniParser.cs" />
<Compile Include="Helper\KeyboardHook.cs" />
<Compile Include="Helper\Log.cs" />
<Compile Include="Helper\WinAlfredException.cs" />
<Compile Include="PluginLoader\BasePluginLoader.cs" />
<Compile Include="PluginLoader\CSharpPluginLoader.cs" />
<Compile Include="PluginLoader\Plugins.cs" />
<Compile Include="PluginLoader\PythonPluginLoader.cs" />
<Compile Include="PluginLoader\PythonPluginWrapper.cs" />
<Compile Include="ResultPanel.xaml.cs">
@ -128,6 +132,10 @@
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WinAlfred.Plugin.System\WinAlfred.Plugin.System.csproj">
<Project>{69ce0206-cb41-453d-88af-df86092ef9b8}</Project>
<Name>WinAlfred.Plugin.System</Name>
</ProjectReference>
<ProjectReference Include="..\WinAlfred.Plugin\WinAlfred.Plugin.csproj">
<Project>{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}</Project>
<Name>WinAlfred.Plugin</Name>