PowerToys/Wox.Plugin.SystemPlugins/Sys.cs

137 lines
4.3 KiB
C#
Raw Normal View History

2013-12-22 00:44:56 +08:00
using System;
using System.Collections.Generic;
2014-01-06 22:21:08 +08:00
using System.Diagnostics;
2013-12-22 00:44:56 +08:00
using System.Linq;
2013-12-22 19:35:21 +08:00
using System.Runtime.InteropServices;
2013-12-22 00:44:56 +08:00
using System.Text;
2013-12-22 19:35:21 +08:00
using System.Windows.Forms;
2013-12-22 00:44:56 +08:00
namespace Wox.Plugin.SystemPlugins
2013-12-22 00:44:56 +08:00
{
2014-01-15 22:45:02 +08:00
public class Sys : BaseSystemPlugin
2013-12-22 00:44:56 +08:00
{
2013-12-22 19:35:21 +08:00
List<Result> availableResults = new List<Result>();
2014-01-04 20:26:13 +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;
2013-12-22 19:35:21 +08:00
[DllImport("user32")]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
[DllImport("user32")]
public static extern void LockWorkStation();
2013-12-22 00:44:56 +08:00
2014-01-15 22:45:02 +08:00
protected override List<Result> QueryInternal(Query query)
2013-12-22 00:44:56 +08:00
{
2014-06-16 14:08:11 +08:00
if (query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
2014-01-07 19:27:51 +08:00
2013-12-22 19:35:21 +08:00
List<Result> results = new List<Result>();
2014-01-03 23:52:36 +08:00
foreach (Result availableResult in availableResults)
2013-12-22 19:35:21 +08:00
{
2014-01-03 23:52:36 +08:00
if (availableResult.Title.ToLower().StartsWith(query.RawQuery.ToLower()))
{
results.Add(availableResult);
}
2013-12-22 00:44:56 +08:00
}
return results;
}
2014-01-15 22:45:02 +08:00
protected override void InitInternal(PluginInitContext context)
2013-12-22 00:44:56 +08:00
{
2013-12-22 19:35:21 +08:00
availableResults.Add(new Result
{
Title = "Shutdown",
SubTitle = "Shutdown Computer",
Score = 100,
2014-01-04 20:26:13 +08:00
IcoPath = "Images\\exit.png",
2014-02-28 23:21:01 +08:00
Action = (c) =>
{
Process.Start("shutdown", "/s /t 0");
return true;
}
2013-12-22 19:35:21 +08:00
});
availableResults.Add(new Result
{
Title = "Log off",
SubTitle = "Log off current user",
2014-01-04 20:26:13 +08:00
Score = 20,
IcoPath = "Images\\logoff.png",
Action = (c) => ExitWindowsEx(EWX_LOGOFF, 0)
2013-12-22 19:35:21 +08:00
});
availableResults.Add(new Result
{
Title = "Lock",
SubTitle = "Lock this computer",
Score = 20,
IcoPath = "Images\\lock.png",
2014-02-28 23:21:01 +08:00
Action = (c) =>
{
LockWorkStation();
return true;
}
2014-01-03 23:52:36 +08:00
});
2014-01-05 17:56:02 +08:00
availableResults.Add(new Result
{
Title = "Exit",
SubTitle = "Close this app",
Score = 110,
IcoPath = "Images\\app.png",
2014-02-28 23:21:01 +08:00
Action = (c) =>
{
context.CloseApp();
return true;
}
2014-01-05 17:56:02 +08:00
});
availableResults.Add(new Result
{
Title = "Restart Wox",
SubTitle = "Restart Wox",
Score = 110,
IcoPath = "Images\\restart.png",
Action = (c) =>
{
ProcessStartInfo Info = new ProcessStartInfo();
2014-06-16 14:08:48 +08:00
Info.Arguments = "/C sleep 1 && \"" + Application.ExecutablePath + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
context.CloseApp();
return true;
}
});
availableResults.Add(new Result
{
Title = "Setting",
SubTitle = "Tweak this app",
Score = 40,
IcoPath = "Images\\app.png",
Action = (c) =>
{
context.OpenSettingDialog();
return true;
}
});
}
public override string Name
{
get { return "System Commands"; }
}
public override string IcoPath
{
get { return @"Images\lock.png"; }
}
public override string Description
{
2014-06-30 21:31:13 +08:00
get { return "Provide System related commands. e.g. shutdown,lock,setting etc."; }
2014-01-03 23:52:36 +08:00
}
2013-12-22 00:44:56 +08:00
}
}