2014-03-11 23:54:37 +08:00
using System ;
using System.Collections.Generic ;
2015-02-07 21:40:25 +08:00
using System.Diagnostics ;
2014-03-11 23:54:37 +08:00
using System.IO ;
using System.Linq ;
2014-03-13 22:31:44 +08:00
using System.Net ;
2015-02-07 21:27:48 +08:00
using System.Reflection ;
2014-03-13 22:31:44 +08:00
using System.Text ;
using System.Threading ;
2014-03-11 23:54:37 +08:00
using System.Windows.Forms ;
using Newtonsoft.Json ;
2014-07-18 20:00:55 +08:00
namespace Wox.Plugin.PluginManagement
{
2015-02-07 21:27:48 +08:00
public class Main : IPlugin , IPluginI18n
2014-07-18 20:00:55 +08:00
{
2014-12-14 22:24:05 +08:00
private static string APIBASE = "https://api.getwox.com" ;
2014-07-18 20:00:55 +08:00
private static string PluginConfigName = "plugin.json" ;
2015-01-26 17:46:55 +08:00
private static string pluginSearchUrl = APIBASE + "/plugin/search/" ;
2014-07-18 20:00:55 +08:00
private PluginInitContext context ;
public List < Result > Query ( Query query )
{
List < Result > results = new List < Result > ( ) ;
2015-01-26 17:46:55 +08:00
if ( string . IsNullOrEmpty ( query . Search ) )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
results . Add ( new Result ( "install <pluginName>" , "Images\\plugin.png" , "search and install wox plugins" )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
Action = e = > ChangeToInstallCommand ( )
2014-07-18 20:00:55 +08:00
} ) ;
2015-01-26 17:46:55 +08:00
results . Add ( new Result ( "uninstall <pluginName>" , "Images\\plugin.png" , "uninstall plugin" )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
Action = e = > ChangeToUninstallCommand ( )
2014-07-18 20:00:55 +08:00
} ) ;
2015-01-26 17:46:55 +08:00
results . Add ( new Result ( "list" , "Images\\plugin.png" , "list plugins installed" )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
Action = e = > ChangeToListCommand ( )
2014-07-18 20:00:55 +08:00
} ) ;
return results ;
}
2015-01-26 17:46:55 +08:00
if ( ! string . IsNullOrEmpty ( query . FirstSearch ) )
2014-07-18 20:00:55 +08:00
{
bool hit = false ;
2015-01-26 17:46:55 +08:00
switch ( query . FirstSearch . ToLower ( ) )
2014-07-18 20:00:55 +08:00
{
case "list" :
hit = true ;
results = ListInstalledPlugins ( ) ;
break ;
case "uninstall" :
hit = true ;
2015-01-26 17:46:55 +08:00
results = UnInstallPlugins ( query ) ;
2014-07-18 20:00:55 +08:00
break ;
case "install" :
hit = true ;
2015-01-26 17:46:55 +08:00
if ( ! string . IsNullOrEmpty ( query . SecondSearch ) )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
results = InstallPlugin ( query . SecondSearch ) ;
2014-07-18 20:00:55 +08:00
}
break ;
}
if ( ! hit )
{
2015-01-26 17:46:55 +08:00
if ( "install" . Contains ( query . FirstSearch . ToLower ( ) ) )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
results . Add ( new Result ( "install <pluginName>" , "Images\\plugin.png" , "search and install wox plugins" )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
Action = e = > ChangeToInstallCommand ( )
2014-07-18 20:00:55 +08:00
} ) ;
}
2015-01-26 17:46:55 +08:00
if ( "uninstall" . Contains ( query . FirstSearch . ToLower ( ) ) )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
results . Add ( new Result ( "uninstall <pluginName>" , "Images\\plugin.png" , "uninstall plugin" )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
Action = e = > ChangeToUninstallCommand ( )
2014-07-18 20:00:55 +08:00
} ) ;
}
2015-01-26 17:46:55 +08:00
if ( "list" . Contains ( query . FirstSearch . ToLower ( ) ) )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
results . Add ( new Result ( "list" , "Images\\plugin.png" , "list plugins installed" )
2014-07-18 20:00:55 +08:00
{
2015-01-26 17:46:55 +08:00
Action = e = > ChangeToListCommand ( )
2014-07-18 20:00:55 +08:00
} ) ;
}
}
}
return results ;
}
2015-01-26 17:46:55 +08:00
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 )
2014-07-18 20:00:55 +08:00
{
List < Result > results = new List < Result > ( ) ;
2015-01-26 17:46:55 +08:00
HttpWebResponse response = HttpRequest . CreateGetHttpResponse ( pluginSearchUrl + queryPluginName , context . Proxy ) ;
2014-07-18 20:00:55 +08:00
Stream s = response . GetResponseStream ( ) ;
if ( s ! = null )
{
StreamReader reader = new StreamReader ( s , Encoding . UTF8 ) ;
string json = reader . ReadToEnd ( ) ;
2014-12-14 22:24:05 +08:00
List < WoxPluginResult > searchedPlugins = null ;
try
{
searchedPlugins = JsonConvert . DeserializeObject < List < WoxPluginResult > > ( json ) ;
}
catch
{
2015-01-26 17:46:55 +08:00
context . API . ShowMsg ( "Coundn't parse api search results" , "Please update your Wox!" , string . Empty ) ;
2014-12-14 22:24:05 +08:00
return results ;
}
foreach ( WoxPluginResult r in searchedPlugins )
2014-07-18 20:00:55 +08:00
{
WoxPluginResult r1 = r ;
results . Add ( new Result ( )
{
Title = r . name ,
SubTitle = r . description ,
IcoPath = "Images\\plugin.png" ,
Action = e = >
{
DialogResult result = MessageBox . Show ( "Are your sure to install " + r . name + " plugin" ,
"Install plugin" , MessageBoxButtons . YesNo ) ;
if ( result = = DialogResult . Yes )
{
string folder = Path . Combine ( Path . GetTempPath ( ) , "WoxPluginDownload" ) ;
if ( ! Directory . Exists ( folder ) ) Directory . CreateDirectory ( folder ) ;
string filePath = Path . Combine ( folder , Guid . NewGuid ( ) . ToString ( ) + ".wox" ) ;
context . API . StartLoadingBar ( ) ;
ThreadPool . QueueUserWorkItem ( delegate
{
using ( WebClient Client = new WebClient ( ) )
{
try
{
2014-12-14 22:24:05 +08:00
string pluginUrl = APIBASE + "/media/" + r1 . plugin_file ;
Client . DownloadFile ( pluginUrl , filePath ) ;
2014-07-18 20:00:55 +08:00
context . API . InstallPlugin ( filePath ) ;
context . API . ReloadPlugins ( ) ;
}
catch ( Exception exception )
{
MessageBox . Show ( "download plugin " + r . name + "failed. " + exception . Message ) ;
}
finally
{
2014-07-21 19:48:17 +08:00
context . API . StopLoadingBar ( ) ;
2014-07-18 20:00:55 +08:00
}
}
} ) ;
}
return false ;
}
} ) ;
}
}
return results ;
}
2015-01-26 17:46:55 +08:00
private List < Result > UnInstallPlugins ( Query query )
2014-07-18 20:00:55 +08:00
{
List < Result > results = new List < Result > ( ) ;
2015-01-26 17:46:55 +08:00
List < PluginMetadata > allInstalledPlugins = context . API . GetAllPlugins ( ) . Select ( o = > o . Metadata ) . ToList ( ) ;
if ( ! string . IsNullOrEmpty ( query . SecondSearch ) )
2014-07-18 20:00:55 +08:00
{
allInstalledPlugins =
2015-01-26 17:46:55 +08:00
allInstalledPlugins . Where ( o = > o . Name . ToLower ( ) . Contains ( query . SecondSearch . ToLower ( ) ) ) . ToList ( ) ;
2014-07-18 20:00:55 +08:00
}
foreach ( PluginMetadata plugin in allInstalledPlugins )
{
2015-01-26 17:46:55 +08:00
var plugin1 = plugin ;
2014-07-18 20:00:55 +08:00
results . Add ( new Result ( )
{
Title = plugin . Name ,
SubTitle = plugin . Description ,
2014-12-09 23:45:23 +08:00
IcoPath = plugin . FullIcoPath ,
2014-07-18 20:00:55 +08:00
Action = e = >
{
2015-01-26 17:46:55 +08:00
UnInstallPlugin ( plugin1 ) ;
2014-07-18 20:00:55 +08:00
return false ;
}
} ) ;
}
return results ;
}
2015-01-26 17:46:55 +08:00
private void UnInstallPlugin ( PluginMetadata plugin )
2014-07-18 20:00:55 +08:00
{
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 )
{
File . Create ( Path . Combine ( plugin . PluginDirectory , "NeedDelete.txt" ) ) . Close ( ) ;
2015-02-07 21:40:25 +08:00
if ( MessageBox . Show (
"You have uninstalled plugin " + plugin . Name + " successfully.\r\n Restart Wox to take effect?" ,
"Install plugin" ,
MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = = DialogResult . Yes )
{
ProcessStartInfo Info = new ProcessStartInfo ( ) ;
Info . Arguments = "/C ping 127.0.0.1 -n 1 && \"" + Application . ExecutablePath + "\"" ;
Info . WindowStyle = ProcessWindowStyle . Hidden ;
Info . CreateNoWindow = true ;
Info . FileName = "cmd.exe" ;
Process . Start ( Info ) ;
context . API . CloseApp ( ) ;
}
2014-07-18 20:00:55 +08:00
}
}
private List < Result > ListInstalledPlugins ( )
{
List < Result > results = new List < Result > ( ) ;
2015-01-26 17:46:55 +08:00
foreach ( PluginMetadata plugin in context . API . GetAllPlugins ( ) . Select ( o = > o . Metadata ) )
2014-07-18 20:00:55 +08:00
{
results . Add ( new Result ( )
{
Title = plugin . Name + " - " + plugin . ActionKeyword ,
SubTitle = plugin . Description ,
2014-12-09 23:45:23 +08:00
IcoPath = plugin . FullIcoPath
2014-07-18 20:00:55 +08:00
} ) ;
}
return results ;
}
public void Init ( PluginInitContext context )
{
this . context = context ;
}
2015-02-07 21:27:48 +08:00
public string GetLanguagesFolder ( )
{
return Path . Combine ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) , "Languages" ) ;
}
public string GetTranslatedPluginTitle ( )
{
return context . API . GetTranslation ( "wox_plugin_plugin_management_plugin_name" ) ;
}
public string GetTranslatedPluginDescription ( )
{
return context . API . GetTranslation ( "wox_plugin_plugin_management_plugin_description" ) ;
}
2014-07-18 20:00:55 +08:00
}
2014-03-11 23:54:37 +08:00
}