PowerToys/Plugins/Wox.Plugin.Sys/Main.cs

203 lines
7.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2014-07-25 08:32:19 +08:00
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
2016-01-06 14:45:08 +08:00
using System.Windows.Forms;
using System.Windows.Interop;
2015-02-07 20:17:49 +08:00
using Wox.Infrastructure;
2016-01-06 14:45:08 +08:00
using Application = System.Windows.Application;
2015-07-14 23:59:24 +08:00
using Control = System.Windows.Controls.Control;
2016-01-06 14:45:08 +08:00
using FormsApplication = System.Windows.Forms.Application;
using MessageBox = System.Windows.MessageBox;
2014-07-25 08:32:19 +08:00
namespace Wox.Plugin.Sys
2014-07-25 08:32:19 +08:00
{
2016-05-07 10:55:09 +08:00
public class Main : IPlugin, ISettingProvider, IPluginI18n
2015-02-27 18:04:49 +08:00
{
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;
2014-07-25 08:32:19 +08:00
[DllImport("user32")]
private static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
2014-07-25 08:32:19 +08:00
[DllImport("user32")]
private static extern void LockWorkStation();
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
private static extern uint SHEmptyRecycleBin(IntPtr hWnd, uint dwFlags);
// http://www.pinvoke.net/default.aspx/Enums/HRESULT.html
private enum HRESULT : uint
{
S_FALSE = 0x0001,
S_OK = 0x0000
}
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);
}
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)
{
var titleScore = StringMatcher.Score(availableResult.Title, query.Search);
var subTitleScore = StringMatcher.Score(availableResult.SubTitle, query.Search);
var score = Math.Max(titleScore, subTitleScore);
if (score > 0)
2014-07-25 08:32:19 +08:00
{
availableResult.Score = score;
2014-07-25 08:32:19 +08:00
results.Add(availableResult);
}
}
return results;
}
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()
{
2016-01-07 05:34:42 +08:00
availableResults.AddRange(new[]
{
new Result
{
Title = "Shutdown",
SubTitle = context.API.GetTranslation("wox_plugin_sys_shutdown_computer"),
IcoPath = "Images\\shutdown.png",
2016-01-07 05:34:42 +08:00
Action = c =>
{
var reuslt = MessageBox.Show("Are you sure you want to shut the computer down?",
"Shutdown Computer?", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (reuslt == MessageBoxResult.Yes)
{
Process.Start("shutdown", "/s /t 0");
}
return true;
}
},
new Result
{
Title = "Restart",
SubTitle = context.API.GetTranslation("wox_plugin_sys_restart_computer"),
IcoPath = "Images\\restart.png",
2016-01-07 05:34:42 +08:00
Action = c =>
{
var result = MessageBox.Show("Are you sure you want to restart the computer?",
"Restart Computer?", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
Process.Start("shutdown", "/r /t 0");
}
return true;
}
},
new Result
{
Title = "Log off",
2015-01-07 22:23:10 +08:00
SubTitle = context.API.GetTranslation("wox_plugin_sys_log_off"),
IcoPath = "Images\\logoff.png",
2016-01-07 05:34:42 +08:00
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"),
IcoPath = "Images\\lock.png",
2016-01-07 05:34:42 +08:00
Action = c =>
{
LockWorkStation();
return true;
}
},
2015-07-14 23:59:24 +08:00
new Result
{
Title = "Sleep",
SubTitle = context.API.GetTranslation("wox_plugin_sys_sleep"),
2015-07-14 23:59:24 +08:00
IcoPath = "Images\\sleep.png",
2016-01-07 05:34:42 +08:00
Action = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
},
new Result
{
Title = "Empty Recycle Bin",
SubTitle = context.API.GetTranslation("wox_plugin_sys_emptyrecyclebin"),
IcoPath = "Images\\recyclebin.png",
2016-01-07 05:34:42 +08:00
Action = c =>
{
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
// 0 for nothing
var result = SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
if (result != (uint) HRESULT.S_OK)
{
MessageBox.Show($"Error emptying recycle bin, error code: {result}\n" +
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137",
"Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
return true;
}
2015-07-14 23:59:24 +08:00
},
new Result
{
Title = "Exit",
2015-01-07 22:23:10 +08:00
SubTitle = context.API.GetTranslation("wox_plugin_sys_exit"),
IcoPath = "Images\\app.png",
2016-01-07 05:34:42 +08:00
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"),
IcoPath = "Images\\app.png",
2016-01-07 05:34:42 +08:00
Action = c =>
{
context.API.RestarApp();
return false;
}
},
new Result
{
Title = "Settings",
2015-01-07 22:23:10 +08:00
SubTitle = context.API.GetTranslation("wox_plugin_sys_setting"),
IcoPath = "Images\\app.png",
2016-01-07 05:34:42 +08:00
Action = c =>
{
context.API.OpenSettingDialog();
return true;
}
}
});
2014-07-25 08:32:19 +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
}
}