-
Notifications
You must be signed in to change notification settings - Fork 1
/
Core.cs
336 lines (298 loc) · 10.5 KB
/
Core.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using Nfu.Models;
using Nfu.Properties;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Nfu
{
public partial class Core : Form
{
public bool UpdateAvailable;
private Image _screenshot;
/// <summary>
/// Constructor.
/// </summary>
public Core()
{
InitializeComponent();
}
/// <summary>
/// Constructor extension.
/// </summary>
public void Setup()
{
buttonUpdate.FlatStyle = FlatStyle.System;
Misc.SendMessage(buttonUpdate.Handle, 0x160C, 0, 0xFFFFFFFF);
if (Settings.Default.HandlePause && !Misc.RegisterHotKey(Keys.Pause, HotKeyPause, 10000, Handle))
{
MessageBox.Show(string.Format(Resources.HotKeyNotRegistered, "Pause"),
Resources.HotKeyNotRegisteredTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (Settings.Default.HandlePrintScreen && !Misc.RegisterHotKey(Keys.PrintScreen, HotKeyPrintScreen, 20000, Handle))
{
MessageBox.Show(string.Format(Resources.HotKeyNotRegistered, "PrintScreen"),
Resources.HotKeyNotRegisteredTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Start the update check.
/// </summary>
private async void CoreShown(object sender, EventArgs e)
{
var taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
await Task.Run(() => CheckForUpdate()).ContinueWith(u =>
{
if (UpdateAvailable)
{
buttonUpdate.Enabled = true;
labelUpdate.Text = Resources.NewVersion;
Misc.ShowInfo(Resources.UpdateAvailableTitle, Resources.UpdateAvailable);
}
else
{
labelUpdate.Text = Resources.LatestVersion;
}
}, taskScheduler);
}
/// <summary>
/// Minimize to system tray.
/// </summary>
private void CoreResize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized && Settings.Default.MinimizeSystemTray)
{
if (!Settings.Default.TooltipShown)
{
Misc.ShowInfo(Resources.StillActiveTitle, Resources.StillActive);
Settings.Default.TooltipShown = true;
}
Hide();
}
}
/// <summary>
/// Minimize instead of close.
/// </summary>
private void CoreFormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
}
/// <summary>
/// Restore window using System Tray.
/// </summary>
private void NotifyIconNfuClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
if (WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Minimized;
}
else
{
Show();
WindowState = FormWindowState.Normal;
}
}
/// <summary>
/// Handler for the PrintScreen key.
/// </summary>
private void HotKeyPrintScreen()
{
ButtonScreenshot(null, null);
}
/// <summary>
/// Handler for the Pause key.
/// </summary>
private void HotKeyPause()
{
ButtonImport(null, null);
}
/// <summary>
/// Select file(s) to upload.
/// </summary>
private void ButtonFile(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Uploader.Upload(openFileDialog.FileNames.Select(fileName => new UploadFile
{
Path = fileName
}).ToArray());
}
}
/// <summary>
/// Take a screenshot to upload.
/// </summary>
private void ButtonScreenshot(object sender, EventArgs e)
{
if (Snipper.IsActive)
return;
_screenshot = Snipper.Snip();
if (_screenshot != null)
{
reuploadScreenshotToolStripMenuItem.Enabled = true;
switch (Snipper.ReturnType)
{
case Snipper.ReturnTypes.Default:
if (!Uploader.UploadImage(_screenshot))
{
Misc.HandleError(new Exception(Resources.UploadFailed), Resources.ScreenShot);
}
break;
case Snipper.ReturnTypes.ToClipboard:
Clipboard.SetImage(_screenshot);
Misc.ShowInfo(Resources.ScreenShotCopiedTitle, Resources.ScreenShotCopied);
break;
}
}
else
{
Misc.HandleError(new ArgumentException(Resources.NoFilesSelected), Resources.ScreenShot);
}
}
/// <summary>
/// Reupload the last screenshot.
/// </summary>
private void ReuploadScreenshot(object sender, EventArgs e)
{
if (!Uploader.UploadImage(_screenshot))
{
Misc.HandleError(new Exception(Resources.UploadFailed), Resources.ScreenShot);
}
}
/// <summary>
/// Import data from the clipboard to upload.
/// </summary>
private void ButtonImport(object sender, EventArgs e)
{
if (Clipboard.ContainsFileDropList())
{
var files = Clipboard.GetFileDropList();
Uploader.Upload((from string file in files
select new UploadFile
{
Path = file
}).ToArray());
}
else if (Clipboard.ContainsImage())
{
if (!Uploader.UploadImage(Clipboard.GetImage()))
{
Misc.HandleError(new Exception(Resources.UploadFailed), Resources.Import);
}
}
else if (Clipboard.ContainsText())
{
if (!Uploader.UploadText(Clipboard.GetText()))
{
Misc.HandleError(new Exception(Resources.UploadFailed), Resources.Import);
}
}
else
{
var dataObject = Clipboard.GetDataObject();
if (dataObject != null)
{
Misc.HandleError(new ArgumentException(string.Format(Resources.CannotHandleContentTypes,
string.Join(",", dataObject.GetFormats()))), Resources.Import);
}
else
{
Misc.HandleError(new ArgumentException(Resources.UnsupportedData), Resources.Import);
}
}
}
/// <summary>
/// Open the settings dialog.
/// </summary>
private void OpenSettings(object sender, EventArgs e)
{
Program.FormCp.ShowDialog();
}
/// <summary>
/// Open the about dialog.
/// </summary>
private void OpenAbout(object sender, EventArgs e)
{
Program.FormAbout.ShowDialog();
}
/// <summary>
/// Exit NFU.
/// </summary>
private void ExitNfu(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// Check for NFU updates.
/// </summary>
private async Task CheckForUpdate()
{
try
{
using (var updateClient = new WebClient())
{
var latestVersion = await updateClient.DownloadStringTaskAsync(new Uri(Settings.Default.VersionUrl));
if (latestVersion != Application.ProductVersion)
{
UpdateAvailable = true;
}
}
}
catch (Exception err)
{
Misc.HandleError(err, Resources.UpdateCheck);
}
}
/// <summary>
/// Update NFU.
/// </summary>
private void StartUpdate(object sender, EventArgs e)
{
try
{
var tempNfu = Misc.GetTempFileName();
var tempCmd = Misc.GetTempFileName();
if (tempNfu == null || tempCmd == null)
{
Misc.HandleError(new Exception(Resources.UpdateFailed), Resources.Update);
return;
}
tempCmd += ".cmd";
using (var updateClient = new WebClient())
{
updateClient.DownloadFile(Settings.Default.ExecutableUrl, tempNfu);
}
File.WriteAllText(tempCmd, $"@ECHO OFF{Environment.NewLine}" +
$"TITLE {Resources.UpdateTitle}{Environment.NewLine}" +
$"ECHO {Resources.WaitingToExit}{Environment.NewLine}" +
$"TIMEOUT /T 5{Environment.NewLine}" +
$"ECHO.{Environment.NewLine}" +
$"ECHO {Resources.Updating}{Environment.NewLine}" +
$"COPY /B /Y \"{tempNfu}\" \"{Application.ExecutablePath}\"{Environment.NewLine}" +
$"START \"\" \"{Application.ExecutablePath}\"");
var startInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = tempCmd,
Verb = "runas"
};
Process.Start(startInfo);
Application.Exit();
}
catch (Exception err)
{
Misc.HandleError(err, Resources.Update);
}
}
}
}