2015-01-03 15:20:34 +08:00
using System.Collections.Generic ;
2014-07-25 08:32:19 +08:00
using System.Diagnostics ;
2015-01-07 22:23:10 +08:00
using System.IO ;
using System.Reflection ;
2014-07-25 08:32:19 +08:00
using System.Runtime.InteropServices ;
using System.Windows.Forms ;
2015-02-07 20:17:49 +08:00
using Wox.Infrastructure ;
2015-07-14 23:59:24 +08:00
using Control = System . Windows . Controls . Control ;
2014-07-25 08:32:19 +08:00
2015-01-03 15:20:34 +08:00
namespace Wox.Plugin.Sys
2014-07-25 08:32:19 +08:00
{
2015-02-27 18:04:49 +08:00
public class Sys : IPlugin , ISettingProvider , IPluginI18n
{
List < Result > availableResults = new List < Result > ( ) ;
2015-01-07 22:23:10 +08:00
private PluginInitContext context ;
2014-07-25 08:32:19 +08:00
2015-02-27 18:04:49 +08:00
#region DllImport
2014-07-25 08:32:19 +08:00
internal const int EWX_LOGOFF = 0x00000000 ;
internal const int EWX_SHUTDOWN = 0x00000001 ;
internal const int EWX_REBOOT = 0x00000002 ;
internal const int EWX_FORCE = 0x00000004 ;
internal const int EWX_POWEROFF = 0x00000008 ;
internal const int EWX_FORCEIFHUNG = 0x00000010 ;
[DllImport("user32")]
private static extern bool ExitWindowsEx ( uint uFlags , uint dwReason ) ;
[DllImport("user32")]
private static extern void LockWorkStation ( ) ;
2015-11-26 01:49:44 +08:00
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb762160(v=vs.85).aspx
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
private static extern uint SHEmptyRecycleBin ( System . IntPtr hwnd , string pszRootPath , uint dwFlags ) ;
2015-02-27 18:04:49 +08:00
#endregion
2014-07-25 08:32:19 +08:00
2015-07-14 23:59:24 +08:00
public Control CreateSettingPanel ( )
2014-07-25 08:32:19 +08:00
{
return new SysSettings ( availableResults ) ;
}
2015-01-03 15:20:34 +08:00
public List < Result > Query ( Query query )
2014-07-25 08:32:19 +08:00
{
List < Result > results = new List < Result > ( ) ;
foreach ( Result availableResult in availableResults )
{
2015-11-08 19:21:48 +08:00
int titleScore = StringMatcher . Match ( availableResult . Title , query . Search ) ;
int subTitleScore = StringMatcher . Match ( availableResult . SubTitle , query . Search ) ;
if ( titleScore > 0 | | subTitleScore > 0 )
2014-07-25 08:32:19 +08:00
{
2015-11-08 19:21:48 +08:00
availableResult . Score = titleScore > 0 ? titleScore : subTitleScore ;
2014-07-25 08:32:19 +08:00
results . Add ( availableResult ) ;
}
}
return results ;
}
2015-01-03 15:20:34 +08:00
public void Init ( PluginInitContext context )
2014-07-25 08:32:19 +08:00
{
2015-01-07 22:23:10 +08:00
this . context = context ;
2015-02-27 18:04:49 +08:00
LoadCommands ( ) ;
2015-01-07 22:23:10 +08:00
}
private void LoadCommands ( )
{
2015-11-08 19:21:48 +08:00
availableResults . AddRange ( new Result [ ] {
new Result
{
Title = "Shutdown" ,
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_shutdown_computer" ) ,
IcoPath = "Images\\exit.png" ,
Action = ( c ) = >
{
if ( MessageBox . Show ( "Are you sure you want to shut the computer down?" , "Shutdown Computer?" , MessageBoxButtons . YesNo , MessageBoxIcon . Warning ) = = DialogResult . Yes ) {
Process . Start ( "shutdown" , "/s /t 0" ) ;
}
return true ;
}
} ,
new Result
2015-11-26 01:49:44 +08:00
{
Title = "Restart" ,
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_restart_computer" ) ,
IcoPath = "Images\\restartcomp.png" ,
Action = ( c ) = >
{
if ( MessageBox . Show ( "Are you sure you want to restart the computer?" , "Restart Computer?" , MessageBoxButtons . YesNo , MessageBoxIcon . Warning ) = = DialogResult . Yes ) {
Process . Start ( "shutdown" , "/r /t 0" ) ;
}
return true ;
}
} ,
new Result
2015-11-08 19:21:48 +08:00
{
Title = "Log off" ,
2015-01-07 22:23:10 +08:00
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_log_off" ) ,
2015-11-08 19:21:48 +08:00
IcoPath = "Images\\logoff.png" ,
Action = ( c ) = > ExitWindowsEx ( EWX_LOGOFF , 0 )
} ,
new Result
{
Title = "Lock" ,
2015-01-07 22:23:10 +08:00
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_lock" ) ,
2015-11-08 19:21:48 +08:00
IcoPath = "Images\\lock.png" ,
Action = ( c ) = >
{
LockWorkStation ( ) ;
return true ;
}
} ,
2015-07-14 23:59:24 +08:00
new Result
{
Title = "Sleep" ,
2015-07-17 15:38:50 +08:00
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_sleep" ) ,
2015-07-14 23:59:24 +08:00
IcoPath = "Images\\sleep.png" ,
Action = ( c ) = > Application . SetSuspendState ( PowerState . Suspend , false , false )
2015-11-26 01:49:44 +08:00
} ,
new Result
{
Title = "Empty Recycle Bin" ,
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_emptyrecyclebin" ) ,
IcoPath = "Images\\recyclebin.png" ,
Action = ( c ) = >
{
try
{
// Using 0 for the last part, let's us use all the windows pop-up and sounds
uint result = SHEmptyRecycleBin ( System . IntPtr . Zero , null , 0 ) ;
}
catch ( System . Exception ex )
{
MessageBox . Show ( "Error emptying recycle bin. \n" + ex . Message , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
return true ;
}
2015-07-14 23:59:24 +08:00
} ,
new Result
2015-11-08 19:21:48 +08:00
{
Title = "Exit" ,
2015-01-07 22:23:10 +08:00
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_exit" ) ,
2015-11-08 19:21:48 +08:00
IcoPath = "Images\\app.png" ,
Action = ( c ) = >
{
context . API . CloseApp ( ) ;
return true ;
}
} ,
new Result
{
Title = "Restart Wox" ,
2015-01-07 22:23:10 +08:00
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_restart" ) ,
2015-11-08 19:21:48 +08:00
IcoPath = "Images\\restart.png" ,
Action = ( c ) = >
{
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 ( ) ;
return true ;
}
} ,
new Result
{
Title = "Settings" ,
2015-01-07 22:23:10 +08:00
SubTitle = context . API . GetTranslation ( "wox_plugin_sys_setting" ) ,
2015-11-08 19:21:48 +08:00
IcoPath = "Images\\app.png" ,
Action = ( c ) = >
{
context . API . OpenSettingDialog ( ) ;
return true ;
}
}
} ) ;
2014-07-25 08:32:19 +08:00
}
2015-01-07 22:23:10 +08:00
public string GetLanguagesFolder ( )
{
return Path . Combine ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) , "Languages" ) ;
2014-07-25 08:32:19 +08:00
2015-01-07 22:23:10 +08:00
}
2015-02-07 20:17:49 +08:00
public string GetTranslatedPluginTitle ( )
{
return context . API . GetTranslation ( "wox_plugin_sys_plugin_name" ) ;
}
public string GetTranslatedPluginDescription ( )
{
return context . API . GetTranslation ( "wox_plugin_sys_plugin_description" ) ;
}
2015-02-27 18:04:49 +08:00
}
2014-07-25 08:32:19 +08:00
}