2019-09-30 18:03:06 +08:00
|
|
|
using System;
|
2015-11-26 09:27:18 +08:00
|
|
|
using System.Collections.Generic;
|
2014-07-25 08:32:19 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Runtime.InteropServices;
|
2015-11-26 09:27:18 +08:00
|
|
|
using System.Windows;
|
2016-01-06 14:45:08 +08:00
|
|
|
using System.Windows.Forms;
|
2015-11-26 09:27:18 +08:00
|
|
|
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
|
|
|
|
2015-01-03 15:20:34 +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
|
|
|
{
|
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;
|
2015-11-26 09:27:18 +08:00
|
|
|
|
2014-07-25 08:32:19 +08:00
|
|
|
[DllImport("user32")]
|
|
|
|
private static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
|
2015-11-26 09:27:18 +08:00
|
|
|
|
2014-07-25 08:32:19 +08:00
|
|
|
[DllImport("user32")]
|
|
|
|
private static extern void LockWorkStation();
|
|
|
|
|
2015-11-26 01:49:44 +08:00
|
|
|
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
|
2015-11-26 09:27:18 +08:00
|
|
|
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-11-26 01:49:44 +08:00
|
|
|
|
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
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
var results = Commands();
|
|
|
|
return new SysSettings(results);
|
2014-07-25 08:32:19 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public List<Result> Query(Query query)
|
2014-07-25 08:32:19 +08:00
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
var commands = Commands();
|
|
|
|
var results = new List<Result>();
|
|
|
|
foreach (var c in commands)
|
2014-07-25 08:32:19 +08:00
|
|
|
{
|
2019-09-29 13:18:05 +08:00
|
|
|
var titleScore = StringMatcher.FuzzySearch(query.Search, c.Title).ScoreAfterSearchPrecisionFilter();
|
|
|
|
var subTitleScore = StringMatcher.FuzzySearch(query.Search, c.SubTitle).ScoreAfterSearchPrecisionFilter();
|
2016-04-24 07:37:25 +08:00
|
|
|
var score = Math.Max(titleScore, subTitleScore);
|
|
|
|
if (score > 0)
|
2014-07-25 08:32:19 +08:00
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
c.Score = score;
|
|
|
|
results.Add(c);
|
2014-07-25 08:32:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
private List<Result> Commands()
|
2015-01-07 22:23:10 +08:00
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
var results = new List<Result>();
|
|
|
|
results.AddRange(new[]
|
2015-11-26 09:27:18 +08:00
|
|
|
{
|
2015-11-08 19:21:48 +08:00
|
|
|
new Result
|
|
|
|
{
|
|
|
|
Title = "Shutdown",
|
|
|
|
SubTitle = context.API.GetTranslation("wox_plugin_sys_shutdown_computer"),
|
2016-04-26 06:20:16 +08:00
|
|
|
IcoPath = "Images\\shutdown.png",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c =>
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
2019-10-07 23:08:06 +08:00
|
|
|
var reuslt = MessageBox.Show(context.API.GetTranslation("wox_plugin_sys_dlgtext_shutdown_computer"),
|
|
|
|
context.API.GetTranslation("wox_plugin_sys_shutdown_computer"),
|
|
|
|
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
2015-11-26 09:27:18 +08:00
|
|
|
if (reuslt == MessageBoxResult.Yes)
|
|
|
|
{
|
2015-11-08 19:21:48 +08:00
|
|
|
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"),
|
2016-04-26 06:20:16 +08:00
|
|
|
IcoPath = "Images\\restart.png",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c =>
|
2015-11-26 01:49:44 +08:00
|
|
|
{
|
2019-10-07 23:08:06 +08:00
|
|
|
var result = MessageBox.Show(context.API.GetTranslation("wox_plugin_sys_dlgtext_restart_computer"),
|
|
|
|
context.API.GetTranslation("wox_plugin_sys_restart_computer"),
|
|
|
|
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
2015-11-26 09:27:18 +08:00
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
|
{
|
2015-11-26 01:49:44 +08:00
|
|
|
Process.Start("shutdown", "/r /t 0");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Result
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
2019-10-07 23:08:06 +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",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c => ExitWindowsEx(EWX_LOGOFF, 0)
|
2015-11-08 19:21:48 +08:00
|
|
|
},
|
|
|
|
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",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c =>
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
|
|
|
LockWorkStation();
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-21 10:19:50 +08:00
|
|
|
},
|
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",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
|
2015-11-26 01:49:44 +08:00
|
|
|
},
|
2015-11-26 09:27:18 +08:00
|
|
|
new Result
|
2019-09-30 18:03:06 +08:00
|
|
|
{
|
|
|
|
Title = "Hibernate",
|
2019-10-07 23:08:06 +08:00
|
|
|
SubTitle = context.API.GetTranslation("wox_plugin_sys_hibernate"),
|
2019-09-30 18:03:06 +08:00
|
|
|
IcoPath = "Images\\sleep.png", // Icon change needed
|
|
|
|
Action = c => FormsApplication.SetSuspendState(PowerState.Hibernate, false, false)
|
|
|
|
},
|
|
|
|
new Result
|
2015-11-26 01:49:44 +08:00
|
|
|
{
|
|
|
|
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 =>
|
2015-11-26 01:49:44 +08:00
|
|
|
{
|
2015-11-26 09:27:18 +08:00
|
|
|
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
|
2018-12-22 14:23:28 +08:00
|
|
|
// FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
|
2015-11-26 09:27:18 +08:00
|
|
|
// 0 for nothing
|
|
|
|
var result = SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
|
2018-12-22 14:23:28 +08:00
|
|
|
if (result != (uint) HRESULT.S_OK && result != (uint)0x8000FFFF)
|
2015-11-26 01:49:44 +08:00
|
|
|
{
|
2015-11-26 09:27:18 +08:00
|
|
|
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);
|
2015-11-26 01:49:44 +08:00
|
|
|
}
|
|
|
|
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",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c =>
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
2016-05-25 08:00:10 +08:00
|
|
|
Application.Current.MainWindow.Close();
|
2015-11-08 19:21:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Result
|
2019-08-22 19:37:36 +08:00
|
|
|
{
|
2019-08-22 20:03:51 +08:00
|
|
|
Title = "Save Settings",
|
2019-10-07 23:08:06 +08:00
|
|
|
SubTitle = context.API.GetTranslation("wox_plugin_sys_save_all_settings"),
|
2019-08-22 19:37:36 +08:00
|
|
|
IcoPath = "Images\\app.png",
|
|
|
|
Action = c =>
|
|
|
|
{
|
|
|
|
context.API.SaveAppAllSettings();
|
2019-10-07 23:08:06 +08:00
|
|
|
context.API.ShowMsg(context.API.GetTranslation("wox_plugin_sys_dlgtitle_success"),
|
|
|
|
context.API.GetTranslation("wox_plugin_sys_dlgtext_all_settings_saved"));
|
2019-08-22 19:37:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Result
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
|
|
|
Title = "Restart Wox",
|
2015-01-07 22:23:10 +08:00
|
|
|
SubTitle = context.API.GetTranslation("wox_plugin_sys_restart"),
|
2016-04-26 06:20:16 +08:00
|
|
|
IcoPath = "Images\\app.png",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c =>
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
2015-11-26 10:04:44 +08:00
|
|
|
context.API.RestarApp();
|
|
|
|
return false;
|
2015-11-08 19:21:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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",
|
2016-01-07 05:34:42 +08:00
|
|
|
Action = c =>
|
2015-11-08 19:21:48 +08:00
|
|
|
{
|
|
|
|
context.API.OpenSettingDialog();
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-08 19:48:56 +08:00
|
|
|
},
|
|
|
|
new Result
|
|
|
|
{
|
|
|
|
Title = "Reload Plugin Data",
|
|
|
|
SubTitle = context.API.GetTranslation("wox_plugin_sys_reload_plugin_data"),
|
|
|
|
IcoPath = "Images\\app.png",
|
|
|
|
Action = c =>
|
|
|
|
{
|
|
|
|
context.API.ReloadAllPluginData();
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-08 19:21:48 +08:00
|
|
|
}
|
|
|
|
});
|
2017-02-21 10:19:50 +08:00
|
|
|
return results;
|
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
|
|
|
}
|
2015-11-26 09:27:18 +08:00
|
|
|
}
|