/
Commands.cs
233 lines (220 loc) · 12.9 KB
/
Commands.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
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Common.Win32;
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
/// <summary>
/// This class holds all available results
/// </summary>
internal static class Commands
{
internal const int EWXLOGOFF = 0x00000000;
internal const int EWXSHUTDOWN = 0x00000001;
internal const int EWXREBOOT = 0x00000002;
internal const int EWXFORCE = 0x00000004;
internal const int EWXPOWEROFF = 0x00000008;
internal const int EWXFORCEIFHUNG = 0x00000010;
// Cache for network interface information to save query time
private const int UpdateCacheIntervalSeconds = 5;
private static List<NetworkConnectionProperties> networkPropertiesCache = new List<NetworkConnectionProperties>();
private static DateTime timeOfLastNetworkQuery;
/// <summary>
/// Returns a list with all system command results
/// </summary>
/// <param name="isUefi">Value indicating if the system is booted in uefi mode</param>
/// <param name="splitRecycleBinResults">Value indicating if we should show two results for Recycle Bin.</param>
/// <param name="confirmCommands">A value indicating if the user should confirm the system commands</param>
/// <param name="emptyRBSuccessMessage">Show a success message after empty Recycle Bin.</param>
/// <param name="iconTheme">The current theme to use for the icons</param>
/// <param name="culture">The culture to use for the result's title and sub title</param>
/// <returns>A list of all results</returns>
internal static List<Result> GetSystemCommands(bool isUefi, bool splitRecycleBinResults, bool confirmCommands, bool emptyRBSuccessMessage, string iconTheme, CultureInfo culture)
{
var results = new List<Result>();
results.AddRange(new[]
{
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_shutdown_computer", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_shutdown_computer_description", culture),
IcoPath = $"Images\\shutdown.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_shutdown_computer_confirmation, () => Helper.OpenInShell("shutdown", "/s /hybrid /t 0"));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_restart_computer", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_restart_computer_description", culture),
IcoPath = $"Images\\restart.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_restart_computer_confirmation, () => Helper.OpenInShell("shutdown", "/r /t 0"));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sign_out", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sign_out_description", culture),
IcoPath = $"Images\\logoff.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_sign_out_confirmation, () => NativeMethods.ExitWindowsEx(EWXLOGOFF, 0));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_lock", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_lock_description", culture),
IcoPath = $"Images\\lock.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_lock_confirmation, () => NativeMethods.LockWorkStation());
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sleep", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sleep_description", culture),
IcoPath = $"Images\\sleep.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_sleep_confirmation, () => NativeMethods.SetSuspendState(false, true, true));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_hibernate", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_hibernate_description", culture),
IcoPath = $"Images\\sleep.{iconTheme}.png", // Icon change needed
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_hibernate_confirmation, () => NativeMethods.SetSuspendState(true, true, true));
},
},
});
// Show Recycle Bin results based on setting.
if (splitRecycleBinResults)
{
results.AddRange(new[]
{
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBinOpen", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_description", culture),
IcoPath = $"Images\\recyclebin.{iconTheme}.png",
Action = c =>
{
return Helper.OpenInShell("explorer.exe", "shell:RecycleBinFolder");
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBinEmptyResult", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBinEmpty_description", culture),
IcoPath = $"Images\\recyclebin.{iconTheme}.png",
Action = c =>
{
ResultHelper.EmptyRecycleBinAsync(emptyRBSuccessMessage);
return true;
},
},
});
}
else
{
results.Add(new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_description", culture),
IcoPath = $"Images\\recyclebin.{iconTheme}.png",
ContextData = new SystemPluginContext { Type = ResultContextType.RecycleBinCommand, SearchTag = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_searchTag", culture) },
Action = c =>
{
return Helper.OpenInShell("explorer.exe", "shell:RecycleBinFolder");
},
});
}
// UEFI command/result. It is only available on systems booted in UEFI mode.
if (isUefi)
{
results.Add(new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_uefi", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_uefi_description", culture),
IcoPath = $"Images\\firmwareSettings.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_uefi_confirmation, () => Helper.OpenInShell("shutdown", "/r /fw /t 0", null, Helper.ShellRunAsType.Administrator));
},
});
}
return results;
}
/// <summary>
/// Returns a list of all ip and mac results
/// </summary>
/// <param name="iconTheme">The theme to use for the icons</param>
/// <param name="culture">The culture to use for the result's title and sub title</param>
/// <returns>The list of available results</returns>
internal static List<Result> GetNetworkConnectionResults(string iconTheme, CultureInfo culture)
{
var results = new List<Result>();
// We update the cache only if the last query is older than 'updateCacheIntervalSeconds' seconds
DateTime timeOfLastNetworkQueryBefore = timeOfLastNetworkQuery;
timeOfLastNetworkQuery = DateTime.Now; // Set time of last query to this query
if ((timeOfLastNetworkQuery - timeOfLastNetworkQueryBefore).TotalSeconds >= UpdateCacheIntervalSeconds)
{
networkPropertiesCache = NetworkConnectionProperties.GetList();
}
foreach (NetworkConnectionProperties intInfo in networkPropertiesCache)
{
if (!string.IsNullOrEmpty(intInfo.IPv4))
{
results.Add(new Result()
{
Title = intInfo.IPv4,
SubTitle = string.Format(CultureInfo.InvariantCulture, Resources.ResourceManager.GetString("Microsoft_plugin_sys_ip4_description", culture), intInfo.ConnectionName) + " - " + Resources.ResourceManager.GetString("Microsoft_plugin_sys_SubTitle_CopyHint", culture),
IcoPath = $"Images\\networkAdapter.{iconTheme}.png",
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_sys_ConnectionDetails, intInfo.GetConnectionDetails()),
ContextData = new SystemPluginContext { Type = ResultContextType.NetworkAdapterInfo, Data = intInfo.GetConnectionDetails() },
Action = _ => ResultHelper.CopyToClipBoard(intInfo.IPv4),
});
}
if (!string.IsNullOrEmpty(intInfo.IPv6Primary))
{
results.Add(new Result()
{
Title = intInfo.IPv6Primary,
SubTitle = string.Format(CultureInfo.InvariantCulture, Resources.ResourceManager.GetString("Microsoft_plugin_sys_ip6_description", culture), intInfo.ConnectionName) + " - " + Resources.ResourceManager.GetString("Microsoft_plugin_sys_SubTitle_CopyHint", culture),
IcoPath = $"Images\\networkAdapter.{iconTheme}.png",
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_sys_ConnectionDetails, intInfo.GetConnectionDetails()),
ContextData = new SystemPluginContext { Type = ResultContextType.NetworkAdapterInfo, Data = intInfo.GetConnectionDetails() },
Action = _ => ResultHelper.CopyToClipBoard(intInfo.IPv6Primary),
});
}
if (!string.IsNullOrEmpty(intInfo.PhysicalAddress))
{
results.Add(new Result()
{
Title = intInfo.PhysicalAddress,
SubTitle = string.Format(CultureInfo.InvariantCulture, Resources.ResourceManager.GetString("Microsoft_plugin_sys_mac_description", culture), intInfo.Adapter, intInfo.ConnectionName) + " - " + Resources.ResourceManager.GetString("Microsoft_plugin_sys_SubTitle_CopyHint", culture),
IcoPath = $"Images\\networkAdapter.{iconTheme}.png",
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_sys_AdapterDetails, intInfo.GetAdapterDetails()),
ContextData = new SystemPluginContext { Type = ResultContextType.NetworkAdapterInfo, Data = intInfo.GetAdapterDetails() },
Action = _ => ResultHelper.CopyToClipBoard(intInfo.PhysicalAddress),
});
}
}
return results;
}
}
}