-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
300 lines (277 loc) · 12.5 KB
/
App.xaml.cs
File metadata and controls
300 lines (277 loc) · 12.5 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System.Linq;
using System.Net;
using Power8.Helpers;
using Power8.Views;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
#if DEBUG
using System.Runtime.ExceptionServices;
#endif
namespace Power8
{
/// <summary>
/// Bootstrapper for the application
/// </summary>
public partial class App
{
public readonly Process Proc = Process.GetCurrentProcess();
/// <summary>
/// Application initializer. Performs compatibility check,
/// starts diagnostics if required, works settings around,
/// and initializes the process of generating of internal data structures.
/// </summary>
public App()
{
if(Util.OsIs.VistaExact) //If run on shit
{
MessageBox.Show(
Power8.Properties.Resources.Err_VistaDetected,
Power8.Properties.NoLoc.Stg_AppShortName, MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(2); //means: OS not found
}
//Global error mode setter - in 1st place to fix nasty startup error on Win10x86
API.SetErrorMode(API.ErrMode.FailCriticalErrors);
//Power8s in our session but with different pid
foreach (var p in Process.GetProcessesByName("Power8")
.Where(p => p.SessionId == Proc.SessionId && p.Id != Proc.Id))
{
p.Kill();
}
Util.MainDisp = Dispatcher; //store main thread dispatcher. Widely used in Application.
#if DEBUG
//System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("ru");
//Error handling and detection
var l = new TextWriterTraceListener(Environment.ExpandEnvironmentVariables(@"%temp%\p8log.txt"));
l.Write("\r\n\r\nPower8 Log opened at " + DateTime.Now + "\r\n\r\n");
Debug.AutoFlush = true;
Debug.Listeners.Add(l);
#endif
DispatcherUnhandledException += (sender, e) => Util.DispatchUnhandledException(e.Exception);
AppDomain.CurrentDomain.UnhandledException += HandleUnhandled;
ServicePointManager.SecurityProtocol |= (SecurityProtocolType) 3840; //Support for TLS 1.1 and TLS 1.2
var dbRoot = Util.GetSettingsIndependentDbRoot();
try
{
var ids = Directory.GetFiles(dbRoot, "*" + ClientIDExtension);
string clientId;
if (ids.Length == 0)
{
clientId = Guid.NewGuid().ToString();
File.Create(dbRoot + "\\" + clientId + ClientIDExtension);
}
else
{
clientId = Path.GetFileNameWithoutExtension(ids[0]);
}
Analytics.Init(TrackID, clientId, Power8.Properties.NoLoc.Stg_AppShortName,
Util.GetAppVersion().ToString());
}
catch (Exception ex)
{
Log.Raw("Unable to read client ID to init analytics: " + ex);
}
//Move settings from previous ver
var std = Power8.Properties.Settings.Default;
if (!std.FirstRunDone)
{
try
{
std.Upgrade();
}
catch (Exception ex)
{
Log.Raw("Unable to upgrade settings: " + ex);
}
std.Save();//FirstRunDone is updated later in Main Window code
Analytics.PostEvent(Analytics.Category.Deploy, std.FirstRunDone ? "Update" : "Fresh", null, 1);
BtnStck.Instanciated += (o, e) => Util.InstanciateClass(t: typeof (Donate));
}
//Initialize standard folder icon
ImageManager.GetImageContainerSync(new PowerItem { Argument = dbRoot, IsFolder = true }, API.Shgfi.SMALLICON);
//Build tree
Util.ForkPool(PowerItemTree.InitTree, "InitTree");
//react on DwmCompositionChanged event
ComponentDispatcher.ThreadFilterMessage += WndProc;
}
/// <summary>
/// Gets the running instance of App
/// </summary>
public static new App Current
{
get { return (App) Application.Current; }
}
private const string ClientIDExtension = ".clientid";
private const string TrackID = "UA-30314159-2";
/// <summary>
/// Handles Unhandled appdomain exception and calls the code to write that down everywhere.
/// Undr DEBUG it also catches uncatchable exceptions
/// </summary>
#if DEBUG
[HandleProcessCorruptedStateExceptions]
#endif
public void HandleUnhandled(object sender, UnhandledExceptionEventArgs e)
{
Util.DispatchUnhandledException(e.ExceptionObject as Exception);
}
#region DWM CompositionChanged event
// ReSharper disable RedundantAssignment
/// <summary>
/// App WndProc. Filter. Used only to hande DWMCOMPOSITIONCHANGED event.
/// </summary>
/// <param name="msg">Structure with message, lparame, wparam, etc.</param>
/// <param name="handled">IntPtr wrapped to bool as 1/0. Retval of WndProc.</param>
private void WndProc(ref MSG msg, ref bool handled)
{
if (msg.message == (int)API.WM.DWMCOMPOSITIONCHANGED)
{
var h = DwmCompositionChanged;
if (h != null)
h(this, null);
handled = true;
return;
}
handled = false;
}
// ReSharper restore RedundantAssignment
/// <summary>
/// WM_DWMCOMPOSITIONCHANGED converted to event model. e is always null.
/// Sender is this App.
/// </summary>
public event EventHandler DwmCompositionChanged;
#endregion
//App stores global context menu as resource.
#region Global Context menu
/// <summary>
/// Handles Run/Run As/Open/Open all users folder commands
/// </summary>
private void RunRunasOpenOpencommonClick(object sender, RoutedEventArgs e)
{
try
{
var n = ((MenuItem) sender).Name;
if (n == "AppRun" || n == "AppOpenFolder") //TODO: maybe compare byref to menuitems?
Util.ExtractRelatedPowerItem(e).Invoke();
else //Open common folder is also handled via "RunAsAdmin" command. See below.
Util.ExtractRelatedPowerItem(e).InvokeVerb(API.SEVerbs.RunAsAdmin);
//This was done to simplify the implementation. Passing this command switches
//the flag in ResolveItem() that exchanges discovered Common item (if exists)
//with User one. This relates to Start menu _folders_ explicitly and only.
//Along with that, this flag is passed to process start info, regardless of PowerItem
//type (file/folder/link...). That type, however, influences the enabled state of
//menu items, so you shouldn't be able to do something wrong.
}
catch (Exception ex)
{
Util.DispatchCaughtException(ex);
}
}
/// <summary>
/// Displays properties of a clicked object. Depending on which item
/// was clicked, the automatic link resolution may take place.
/// </summary>
private void ShowPropertiesClick(object sender, RoutedEventArgs e)
{
try
{
var info = new API.ShellExecuteInfo
{
fMask = //need all them for Properties verb
API.SEIFlags.SEE_MASK_INVOKEIDLIST | API.SEIFlags.SEE_MASK_NOCLOSEPROCESS |
API.SEIFlags.SEE_MASK_FLAG_NO_UI | API.SEIFlags.SEE_MASK_NOASYNC,
hwnd = BtnStck.Instance.GetHandle(), //otherwise will be in background
nShow = API.SWCommands.HIDE, //hides some other window, kind of worker one
lpVerb = API.SEVerbs.Properties,
lpFile = Args4PropsAndCont(Util.ExtractRelatedPowerItem(e), ((MenuItem)sender).Name)
};
var executer = new Util.ShellExecuteHelper(info); //Needed to be executed on STA htread
if (!executer.ShellExecuteOnSTAThread())
throw new ExternalException(string.Format(
Power8.Properties.Resources.Err_ShellExecExErrorFormatString, executer.ErrorCode, executer.ErrorText));
}
catch (Exception ex)
{
Util.DispatchCaughtException(ex);
}
}
/// <summary>
/// Shows source PowerItem in a folder
/// </summary>
private void OpenContainerClick(object sender, RoutedEventArgs e)
{
try
{
Util.StartExplorerSelect(Args4PropsAndCont(Util.ExtractRelatedPowerItem(e), ((MenuItem) sender).Name));
}
catch (Exception ex)
{
Util.DispatchCaughtException(ex);
}
}
/// <summary>
/// Handles click on "Remove item". Adds an exclusion to exclusions list.
/// </summary>
private void RemoveItemClick(object sender, RoutedEventArgs e)
{
MfuList.AddExclusion(Util.ExtractRelatedPowerItem(e));
}
/// <summary>
/// Handles click on "Add item to custom list". Adds an item to user's custom MFU list.
/// </summary>
private void IncludeCustom(object sender, RoutedEventArgs e)
{
MfuList.Add2Custom(Util.ExtractRelatedPowerItem(e));
}
/// <summary>
/// Handles click on "Remove item from custom list". Removes an item to user's custom MFU list.
/// </summary>
private void ExcludeCustom(object sender, RoutedEventArgs e)
{
MfuList.RemoveCustom(Util.ExtractRelatedPowerItem(e));
}
/// <summary>
/// Returns string, of a Path kind, that can be passed to a system, and will
/// represent the passed PowerItem. Depending on Caller Name, may invoke
/// automatic Link resolution for Link PowerItems. "Denamespaces" the
/// passed ControlPanel item returning open command for it.
/// </summary>
/// <param name="item">The PowerItem which has to be located/properties for
/// which have to be shown.</param>
/// <param name="callerName">String, the name of clicked menu item, hendler
/// of which is calling this method. Recognizes "AppOpenTargetContainer" and
/// "AppShowTargetProperties".</param>
/// <returns>Path to binary FS object that represents the passed PowerItem or
/// the target of its link.</returns>
private static string Args4PropsAndCont(PowerItem item, string callerName)
{
string arg = null;
if (item.IsControlPanelChildItem)
{
var executor = Util.GetOpenCommandForClass(item.Argument);
if (executor != null && File.Exists(executor.Item1))
arg = executor.Item1;
}
if (arg == null)
arg = PowerItemTree.GetResolvedArgument(item);
if (item.IsLink && (callerName == "AppOpenTargetContainer"
|| callerName == "AppShowTargetProperties"))
arg = item.ResolvedLink;
return arg;
}
/// <summary>
/// Gets or sets Data Context for the whole menu. MUST be called in ALL
/// ContextMenuOpening event handlers with something like:
/// App.Current.MDC = Util.ExtractRelatedPowerItem(e);
/// </summary>
public object MenuDataContext
{
get { return ((ContextMenu) Resources["fsMenuItemsContextMenu"]).DataContext; }
set { ((ContextMenu) Resources["fsMenuItemsContextMenu"]).DataContext = value; }
}
#endregion
}
}