some API changes for Query class and renames.

This commit is contained in:
qianlifeng
2015-01-26 17:46:55 +08:00
parent ddf6154600
commit 7821f41723
29 changed files with 371 additions and 289 deletions

View File

@@ -10,59 +10,37 @@ using Newtonsoft.Json;
namespace Wox.Plugin.PluginManagement
{
public class WoxPluginResult
{
public string plugin_file;
public string description;
public int liked_count;
public string name;
public string version;
}
public class Main : IPlugin
{
private static string APIBASE = "https://api.getwox.com";
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
private static string PluginConfigName = "plugin.json";
private static string pluginSearchUrl = APIBASE +"/plugin/search/";
private static string pluginSearchUrl = APIBASE + "/plugin/search/";
private PluginInitContext context;
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
if (query.ActionParameters.Count == 0)
if (string.IsNullOrEmpty(query.Search))
{
results.Add(new Result("wpm install <pluginName>", "Images\\plugin.png", "search and install wox plugins")
results.Add(new Result("install <pluginName>", "Images\\plugin.png", "search and install wox plugins")
{
Action = e =>
{
context.API.ChangeQuery("wpm install ");
return false;
}
Action = e => ChangeToInstallCommand()
});
results.Add(new Result("wpm uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin")
results.Add(new Result("uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin")
{
Action = e =>
{
context.API.ChangeQuery("wpm uninstall ");
return false;
}
Action = e => ChangeToUninstallCommand()
});
results.Add(new Result("wpm list", "Images\\plugin.png", "list plugins installed")
results.Add(new Result("list", "Images\\plugin.png", "list plugins installed")
{
Action = e =>
{
context.API.ChangeQuery("wpm list");
return false;
}
Action = e => ChangeToListCommand()
});
return results;
}
if (query.ActionParameters.Count > 0)
if (!string.IsNullOrEmpty(query.FirstSearch))
{
bool hit = false;
switch (query.ActionParameters[0].ToLower())
switch (query.FirstSearch.ToLower())
{
case "list":
hit = true;
@@ -71,51 +49,39 @@ namespace Wox.Plugin.PluginManagement
case "uninstall":
hit = true;
results = ListUnInstalledPlugins(query);
results = UnInstallPlugins(query);
break;
case "install":
hit = true;
if (query.ActionParameters.Count > 1)
if (!string.IsNullOrEmpty(query.SecondSearch))
{
results = InstallPlugin(query);
results = InstallPlugin(query.SecondSearch);
}
break;
}
if (!hit)
{
if ("install".Contains(query.ActionParameters[0].ToLower()))
if ("install".Contains(query.FirstSearch.ToLower()))
{
results.Add(new Result("wpm install <pluginName>", "Images\\plugin.png", "search and install wox plugins")
results.Add(new Result("install <pluginName>", "Images\\plugin.png", "search and install wox plugins")
{
Action = e =>
{
context.API.ChangeQuery("wpm install ");
return false;
}
Action = e => ChangeToInstallCommand()
});
}
if ("uninstall".Contains(query.ActionParameters[0].ToLower()))
if ("uninstall".Contains(query.FirstSearch.ToLower()))
{
results.Add(new Result("wpm uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin")
results.Add(new Result("uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin")
{
Action = e =>
{
context.API.ChangeQuery("wpm uninstall ");
return false;
}
Action = e => ChangeToUninstallCommand()
});
}
if ("list".Contains(query.ActionParameters[0].ToLower()))
if ("list".Contains(query.FirstSearch.ToLower()))
{
results.Add(new Result("wpm list", "Images\\plugin.png", "list plugins installed")
results.Add(new Result("list", "Images\\plugin.png", "list plugins installed")
{
Action = e =>
{
context.API.ChangeQuery("wpm list");
return false;
}
Action = e => ChangeToListCommand()
});
}
}
@@ -124,10 +90,49 @@ namespace Wox.Plugin.PluginManagement
return results;
}
private List<Result> InstallPlugin(Query query)
private bool ChangeToListCommand()
{
if (context.CurrentPluginMetadata.ActionKeyword == "*")
{
context.API.ChangeQuery("list ");
}
else
{
context.API.ChangeQuery(string.Format("{0} list ", context.CurrentPluginMetadata.ActionKeyword));
}
return false;
}
private bool ChangeToUninstallCommand()
{
if (context.CurrentPluginMetadata.ActionKeyword == "*")
{
context.API.ChangeQuery("uninstall ");
}
else
{
context.API.ChangeQuery(string.Format("{0} uninstall ", context.CurrentPluginMetadata.ActionKeyword));
}
return false;
}
private bool ChangeToInstallCommand()
{
if (context.CurrentPluginMetadata.ActionKeyword == "*")
{
context.API.ChangeQuery("install ");
}
else
{
context.API.ChangeQuery(string.Format("{0} install ", context.CurrentPluginMetadata.ActionKeyword));
}
return false;
}
private List<Result> InstallPlugin(string queryPluginName)
{
List<Result> results = new List<Result>();
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + query.ActionParameters[1], context.Proxy);
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + queryPluginName, context.Proxy);
Stream s = response.GetResponseStream();
if (s != null)
{
@@ -140,7 +145,7 @@ namespace Wox.Plugin.PluginManagement
}
catch
{
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!",string.Empty);
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
return results;
}
@@ -194,19 +199,19 @@ namespace Wox.Plugin.PluginManagement
return results;
}
private List<Result> ListUnInstalledPlugins(Query query)
private List<Result> UnInstallPlugins(Query query)
{
List<Result> results = new List<Result>();
List<PluginMetadata> allInstalledPlugins = ParseRegularPlugins();
if (query.ActionParameters.Count > 1)
List<PluginMetadata> allInstalledPlugins = context.API.GetAllPlugins().Select(o => o.Metadata).ToList();
if (!string.IsNullOrEmpty(query.SecondSearch))
{
string pluginName = query.ActionParameters[1];
allInstalledPlugins =
allInstalledPlugins.Where(o => o.Name.ToLower().Contains(pluginName.ToLower())).ToList();
allInstalledPlugins.Where(o => o.Name.ToLower().Contains(query.SecondSearch.ToLower())).ToList();
}
foreach (PluginMetadata plugin in allInstalledPlugins)
{
var plugin1 = plugin;
results.Add(new Result()
{
Title = plugin.Name,
@@ -214,7 +219,7 @@ namespace Wox.Plugin.PluginManagement
IcoPath = plugin.FullIcoPath,
Action = e =>
{
UnInstalledPlugins(plugin);
UnInstallPlugin(plugin1);
return false;
}
});
@@ -222,7 +227,7 @@ namespace Wox.Plugin.PluginManagement
return results;
}
private void UnInstalledPlugins(PluginMetadata plugin)
private void UnInstallPlugin(PluginMetadata plugin)
{
string content = string.Format("Do you want to uninstall following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}", plugin.Name, plugin.Version, plugin.Author);
if (MessageBox.Show(content, "Wox", MessageBoxButtons.YesNo) == DialogResult.Yes)
@@ -235,7 +240,7 @@ namespace Wox.Plugin.PluginManagement
private List<Result> ListInstalledPlugins()
{
List<Result> results = new List<Result>();
foreach (PluginMetadata plugin in ParseRegularPlugins())
foreach (PluginMetadata plugin in context.API.GetAllPlugins().Select(o => o.Metadata))
{
results.Add(new Result()
{
@@ -247,61 +252,6 @@ namespace Wox.Plugin.PluginManagement
return results;
}
private static List<PluginMetadata> ParseRegularPlugins()
{
List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
if (!Directory.Exists(PluginPath))
Directory.CreateDirectory(PluginPath);
string[] directories = Directory.GetDirectories(PluginPath);
foreach (string directory in directories)
{
PluginMetadata metadata = GetMetadataFromJson(directory);
if (metadata != null) pluginMetadatas.Add(metadata);
}
return pluginMetadatas;
}
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
{
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
PluginMetadata metadata;
if (!File.Exists(configPath))
{
return null;
}
try
{
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginType = PluginType.User;
metadata.PluginDirectory = pluginDirectory;
}
catch (Exception)
{
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
return null;
}
if (!AllowedLanguage.IsAllowed(metadata.Language))
{
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
metadata.Language);
return null;
}
if (!File.Exists(metadata.ExecuteFilePath))
{
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
metadata.ExecuteFilePath);
return null;
}
return metadata;
}
public void Init(PluginInitContext context)
{
this.context = context;

View File

@@ -1,91 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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>{049490F0-ECD2-4148-9B39-2135EC346EBE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Wox.Plugin.PluginManagement</RootNamespace>
<AssemblyName>Wox.Plugin.PluginManagement</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Output\Debug\Plugins\Wox.Plugin.PluginManagement\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Output\Release\Plugins\Wox.Plugin.PluginManagement\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HttpRequest.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Wox.Plugin\Wox.Plugin.csproj">
<Project>{8451ecdd-2ea4-4966-bb0a-7bbc40138e80}</Project>
<Name>Wox.Plugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="Images\plugin.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。启用“NuGet 程序包还原”可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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>{049490F0-ECD2-4148-9B39-2135EC346EBE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Wox.Plugin.PluginManagement</RootNamespace>
<AssemblyName>Wox.Plugin.PluginManagement</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Output\Debug\Plugins\Wox.Plugin.PluginManagement\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Output\Release\Plugins\Wox.Plugin.PluginManagement\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HttpRequest.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WoxPluginResult.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Wox.Plugin\Wox.Plugin.csproj">
<Project>{8451ecdd-2ea4-4966-bb0a-7bbc40138e80}</Project>
<Name>Wox.Plugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="Images\plugin.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。启用“NuGet 程序包还原”可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<!-- 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

@@ -0,0 +1,11 @@
namespace Wox.Plugin.PluginManagement
{
public class WoxPluginResult
{
public string plugin_file;
public string description;
public int liked_count;
public string name;
public string version;
}
}