-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Main.cs
261 lines (246 loc) · 10.5 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control;
using FormsApplication = System.Windows.Forms.Application;
using MessageBox = System.Windows.MessageBox;
namespace Wox.Plugin.Sys
{
public class Main : IPlugin, ISettingProvider, IPluginI18n
{
private PluginInitContext context;
#region DllImport
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();
[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
}
#endregion
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
public Control CreateSettingPanel()
{
var results = Commands();
return new SysSettings(results);
}
public List<Result> Query(Query query)
{
var commands = Commands();
var results = new List<Result>();
foreach (var c in commands)
{
var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
if (titleMatch.Score > 0)
{
c.Score = titleMatch.Score;
c.TitleHighlightData = titleMatch.MatchData;
results.Add(c);
}
}
return results;
}
public void Init(PluginInitContext context)
{
this.context = context;
}
private List<Result> Commands()
{
var results = new List<Result>();
results.AddRange(new[]
{
new Result
{
Title = "Shutdown",
SubTitle = context.API.GetTranslation("wox_plugin_sys_shutdown_computer"),
IcoPath = "Images\\shutdown.png",
Action = c =>
{
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);
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",
Action = c =>
{
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);
if (result == MessageBoxResult.Yes)
{
Process.Start("shutdown", "/r /t 0");
}
return true;
}
},
new Result
{
Title = "Log Off",
SubTitle = context.API.GetTranslation("wox_plugin_sys_log_off"),
IcoPath = "Images\\logoff.png",
Action = c => ExitWindowsEx(EWX_LOGOFF, 0)
},
new Result
{
Title = "Lock",
SubTitle = context.API.GetTranslation("wox_plugin_sys_lock"),
IcoPath = "Images\\lock.png",
Action = c =>
{
LockWorkStation();
return true;
}
},
new Result
{
Title = "Sleep",
SubTitle = context.API.GetTranslation("wox_plugin_sys_sleep"),
IcoPath = "Images\\sleep.png",
Action = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
},
new Result
{
Title = "Hibernate",
SubTitle = context.API.GetTranslation("wox_plugin_sys_hibernate"),
IcoPath = "Images\\sleep.png", // Icon change needed
Action = c => FormsApplication.SetSuspendState(PowerState.Hibernate, false, false)
},
new Result
{
Title = "Empty Recycle Bin",
SubTitle = context.API.GetTranslation("wox_plugin_sys_emptyrecyclebin"),
IcoPath = "Images\\recyclebin.png",
Action = c =>
{
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
// FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
// 0 for nothing
var result = SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
if (result != (uint) HRESULT.S_OK && result != (uint)0x8000FFFF)
{
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;
}
},
new Result
{
Title = "Exit",
SubTitle = context.API.GetTranslation("wox_plugin_sys_exit"),
IcoPath = "Images\\app.png",
Action = c =>
{
Application.Current.MainWindow.Close();
return true;
}
},
new Result
{
Title = "Save Settings",
SubTitle = context.API.GetTranslation("wox_plugin_sys_save_all_settings"),
IcoPath = "Images\\app.png",
Action = c =>
{
context.API.SaveAppAllSettings();
context.API.ShowMsg(context.API.GetTranslation("wox_plugin_sys_dlgtitle_success"),
context.API.GetTranslation("wox_plugin_sys_dlgtext_all_settings_saved"));
return true;
}
},
new Result
{
Title = "Restart Wox",
SubTitle = context.API.GetTranslation("wox_plugin_sys_restart"),
IcoPath = "Images\\app.png",
Action = c =>
{
context.API.RestarApp();
return false;
}
},
new Result
{
Title = "Settings",
SubTitle = context.API.GetTranslation("wox_plugin_sys_setting"),
IcoPath = "Images\\app.png",
Action = c =>
{
context.API.OpenSettingDialog();
return true;
}
},
new Result
{
Title = "Reload Plugin Data",
SubTitle = context.API.GetTranslation("wox_plugin_sys_reload_plugin_data"),
IcoPath = "Images\\app.png",
Action = c =>
{
// Hide the window first then show msg after done because sometimes the reload could take a while, so not to make user think it's frozen.
Application.Current.MainWindow.Hide();
context.API.ReloadAllPluginData();
context.API.ShowMsg(context.API.GetTranslation("wox_plugin_sys_dlgtitle_success"),
context.API.GetTranslation("wox_plugin_sys_dlgtext_all_applicableplugins_reloaded"));
return true;
}
},
new Result
{
PluginDirectory = context.CurrentPluginMetadata.PluginDirectory,
Title = "Check For Update",
SubTitle = "Check for new Wox update",
IcoPath = "Images\\update.png",
Action = c =>
{
Application.Current.MainWindow.Hide();
context.API.CheckForNewUpdate();
context.API.ShowMsg("Please wait...",
"Checking for new update");
return true;
}
}
});
return results;
}
public string GetTranslatedPluginTitle()
{
return context.API.GetTranslation("wox_plugin_sys_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return context.API.GetTranslation("wox_plugin_sys_plugin_description");
}
}
}