/
WlipperForm.cs
681 lines (597 loc) · 22.7 KB
/
WlipperForm.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Net;
using System.Xml;
namespace Wlipper
{
public partial class WlipperForm : Form
{
#region Fields
private FileInfo readme;
private FileInfo tmpDoc;
private ToolStripMenuItem toolStripMenuItemEmpty;
private uint historyLength;
private uint entryLength;
private uint tooltipLength;
private bool formatted;
private uint histories;
Wlipper.Properties.Settings preferences;
public bool insertingHistory;
#endregion
#region Properties
private FileInfo ReadMe
{
get
{
return readme;
}
set
{
readme = value;
}
}
private FileInfo TmpDoc
{
get
{
return tmpDoc;
}
set
{
tmpDoc = value;
}
}
private ToolStripMenuItem ToolStripMenuItemEmpty
{
get
{
return toolStripMenuItemEmpty;
}
set
{
toolStripMenuItemEmpty = value;
}
}
private uint HistoryLength
{
get
{
return historyLength;
}
set
{
historyLength = value;
}
}
private uint EntryLength
{
get
{
return entryLength;
}
set
{
entryLength = value;
}
}
private uint TooltipLength
{
get
{
return tooltipLength;
}
set
{
tooltipLength = value;
}
}
private Wlipper.Properties.Settings Preferences
{
get
{
return preferences;
}
set
{
preferences = value;
}
}
public bool Formatted
{
get
{
return formatted;
}
set
{
formatted = value;
}
}
public uint Histories
{
get
{
return histories;
}
set
{
histories = value;
}
}
public bool InsertingHistory
{
get
{
return insertingHistory;
}
set
{
insertingHistory = value;
}
}
#endregion
#region Constructors
public WlipperForm()
{
InitializeComponent();
InitializeView();
InitializeDocument();
LoadPreferences();
RegisterWindowToChain();
CheckForUpdate(false);
}
#endregion
#region Initialization methods
private void InitializeView()
{
// Placeholder representing an empty list of clipboard elements
ToolStripMenuItemEmpty = new ToolStripMenuItem(Localization.EMPTY);
ToolStripMenuItemEmpty.Enabled = false;
contextMenuStrip.Items.Insert(2, ToolStripMenuItemEmpty);
//
// Set the standard button
buttonOk.Focus();
AcceptButton = buttonOk;
CancelButton = buttonCancel;
//
}
private void InitializeDocument()
{
Preferences = new Wlipper.Properties.Settings();
}
#endregion
#region Event callback methods
private void WlipperForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
LoadPreferences();
ToggleWindow();
}
}
private void toolStripMenuItemClear_Click(object sender, EventArgs e)
{
while (contextMenuStrip.Items.IndexOf(toolStripSeparator2) != 2)
{
contextMenuStrip.Items.RemoveAt(2);
}
contextMenuStrip.Items.Insert(2, ToolStripMenuItemEmpty);
}
private void ClipboardMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
Clipboarding.SetClipboardTextContent(Formatted, (object[])toolStripMenuItem.Tag);
contextMenuStrip.Items.Remove(toolStripMenuItem);
}
private void toolStripMenuItemPreferences_Click(object sender, EventArgs e)
{
if (Visible == false)
ToggleWindow();
}
/// <summary>
/// Retrieve documentation file from assembly resource for opening.
/// </summary>
private void toolStripMenuItemHelp_Click(object sender, EventArgs e)
{
try
{
if (TmpDoc == null)
{
// Vollen Ressourcenpfad zusammenstellen
string resourcePath = string.Concat(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, string.Concat(SpecialCharacters.DOT, Naming.README));
// Die Ressource per Stream auslesen
Stream resourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
// Create temporary documentation file
FileInfo tmpPath = new FileInfo(Path.GetTempFileName());
StreamReader resourceFile = new StreamReader(resourceStream, Encoding.UTF8, true);
StreamWriter tmpFile = new StreamWriter(tmpPath.FullName, false, Encoding.UTF8);
// Write resource to file
tmpFile.Write(resourceFile.ReadToEnd());
tmpFile.Close();
resourceFile.Close();
resourceStream.Close();
// Rename temporary file for opening
tmpPath.MoveTo(string.Concat(tmpPath.FullName, Naming.HTML_ENDING));
// For deleting temporary file(s) later
TmpDoc = tmpPath;
}
// Open documentation file
Process.Start(TmpDoc.FullName);
}
catch
{
MessageBox.Show(this, Localization.NOREADME, Localization.FAILURE, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void toolStripMenuItemQuit_Click(object sender, EventArgs e)
{
DeleteTmpDoc();
Dispose();
Application.Exit();
}
private void numericUpDown_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
private void buttonOk_Click(object sender, EventArgs e)
{
SavePreferences();
RearrangeList();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
LoadPreferences();
ToggleWindow();
}
/// <summary>
/// For testing purpose re-registering the application to the clipboard chain for listening on changes.
/// </summary>
void ReregisterToolStripMenuItemClick(object sender, EventArgs e)
{
UnregisterWindowFromChain();
RegisterWindowToChain();
}
private void notifyIcon_MouseUp(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
// Handle contextmenu with left mouse click: http://blog.xploiter.com/c-and-aspnet/need-to-left-click-on-a-notifyicon/
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon, null);
break;
}
}
private void toolStripMenuItemUpdate_Click(object sender, EventArgs e)
{
bool update = CheckForUpdate();
// If was not able to check for update
if (!update)
{
MessageBox.Show(Localization.UPDATE_FAIL, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Other methods
/// <summary>
/// Comparing two format arrays with content from clipboard
/// </summary>
/// <param name="alreadyRetrieved">TRUE if last and new item both are identical, FALSE if not</param>
/// <param name="lastItem">Array containing several content formats of last retrieved item</param>
/// <param name="newItem">Array containing several content formats of new retrieved item</param>
private void BothHaveIdenticalContentAndFormats(out bool alreadyRetrieved, object[] lastItem, object[] newItem)
{
// If the number of formats is different they cant't be equal
if (lastItem.Length != newItem.Length)
{
alreadyRetrieved = false;
}
else
{
// Check each format for equality
bool bothAreEqual = false;
for (int i = 0; i < lastItem.Length; i++)
{
if (null != lastItem[i] && null != newItem[i])
{
if (lastItem[i].Equals(newItem[i]))
{
bothAreEqual = true;
}
else
{
bothAreEqual = false;
break;
}
}
else if (null != lastItem[i] && null == newItem[i])
{
bothAreEqual = false;
}
else if (null == lastItem[i] && null != newItem[i])
{
bothAreEqual = false;
}
// Do not check other formats for equality if not preserving text formatting
if (!Formatted && i == 0)
{
break;
}
}
alreadyRetrieved = bothAreEqual;
}
}
/// <summary>
/// For processing the clipboard after the appropriate event occurred.
/// </summary>
private void MakeItSo()
{
// Processing new clipboard's text content
object[] newObjectFormats = Clipboarding.GetClipboardTextContent();
object[] lastObjectFormats = (object[])contextMenuStrip.Items[2].Tag; // Formats of last retrieved clipboard content
// Do not capture new clipboard history item if it is the same like the last one
bool alreadyRetrieved = false;
if(null != lastObjectFormats)
{
BothHaveIdenticalContentAndFormats(out alreadyRetrieved, lastObjectFormats, newObjectFormats);
}
if(alreadyRetrieved)
{
return;
}
// ---
string unformatted = newObjectFormats[0] as string;
if (unformatted != null)
{
// Set a new clipboard object without formatting information
// But only if the event notification happens twice
if (!Formatted)
{
InsertingHistory = true;
Clipboarding.SetClipboardTextContent(Formatted, newObjectFormats);
}
else
InsertingHistory = true;
if (InsertingHistory)
{
ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem(PrepareTextForToolStripMenuItem(unformatted), null, ClipboardMenuItem_Click);
toolStripMenuItem.Tag = newObjectFormats;
toolStripMenuItem.ToolTipText = PrepareTextForToolTipText(unformatted);
contextMenuStrip.Items.Insert(2, toolStripMenuItem);
}
// Delete the ToolStripMenuItem representing an empty list
contextMenuStrip.Items.Remove(ToolStripMenuItemEmpty);
// Cut history
CuttingHistory();
}
// No special exception handling needed
// Disallow insertion to history
InsertingHistory = false;
}
/// <summary>
/// Cuts the amount of history entries if needed.
/// </summary>
private void CuttingHistory()
{
int historyOffset = contextMenuStrip.Items.IndexOf(toolStripSeparator3);
int historyEndHard = (int)HistoryLength + historyOffset;
int historyEndSoft = contextMenuStrip.Items.IndexOf(toolStripSeparator2) - historyOffset;
if (historyEndSoft > historyEndHard)
{
do
{
contextMenuStrip.Items.RemoveAt(historyEndSoft);
historyEndSoft = contextMenuStrip.Items.IndexOf(toolStripSeparator2) - historyOffset;
}
while (historyEndSoft > historyEndHard);
}
}
/// <summary>
/// Removes all newlines and tabulators and cuts from both sides to length given via preferences.
/// </summary>
private string PrepareTextForToolStripMenuItem(string text)
{
// Removing newlines and tabulators
string[] newlines = new string[4];
newlines[0] = "\r\n";
newlines[1] = "\r";
newlines[2] = "\n";
newlines[3] = "\t";
string replacement = SpecialCharacters.WHITESPACE;
foreach (string newline in newlines)
{
text = text.Replace(newline, replacement);
}
// Evaluate special characters
string[] specialCharacters = new string[1];
specialCharacters[0] = SpecialCharacters.AMPERSAND;
foreach (string character in specialCharacters)
{
text = text.Replace(character, string.Concat(character, character));
}
// Splitting or not
if (EntryLength == 1)
{
text = string.Concat(text.Substring(0, (int)EntryLength), Naming.ETC);
}
else if (text.Length > EntryLength)
{
uint rightLength = EntryLength / 2;
uint leftLength = EntryLength - rightLength;
string left = text.Substring(0, (int)leftLength);
string right = text.Substring(text.Length - (int)rightLength, text.Length - (text.Length - (int)rightLength));
text = string.Concat(left, Naming.ETC);
text = string.Concat(text, right);
}
return text;
}
/// <summary>
/// Cuts to length given via preferences.
/// </summary>
private string PrepareTextForToolTipText(string text)
{
if (text.Length < TooltipLength)
{
return text;
}
else
{
return string.Concat(text.Substring(0, (int)TooltipLength), Naming.ETC);
}
}
/// <summary>
/// Toggles the visibility of the Form.
/// </summary>
private void ToggleWindow()
{
// Toggle window
// Using the ShowInTaskbar property there is a problem with the chained form
// The ShowInTaskbar property will be set implicitly
if (Visible)
{
Hide();
}
else
{
Show();
}
}
/// <summary>
/// Delete the temporary documentation file(s) on program exit.
/// </summary>
private void DeleteTmpDoc()
{
try
{
TmpDoc.Delete();
}
catch
{
// Temporary file can't be deleted because of still being used by another process
// Or the file was never been created
}
}
private void LoadPreferences()
{
HistoryLength = Preferences.HistoryLength;
EntryLength = Preferences.EntryLength;
TooltipLength = Preferences.TooltipLength;
Formatted = Preferences.Formatted;
numericUpDownHistory.Value = HistoryLength;
numericUpDownEntry.Value = EntryLength;
numericUpDownTooltip.Value = TooltipLength;
checkBoxFormatted.Checked = Formatted;
}
private void SavePreferences()
{
HistoryLength = (uint)numericUpDownHistory.Value;
EntryLength = (uint)numericUpDownEntry.Value;
TooltipLength = (uint)numericUpDownTooltip.Value;
Formatted = checkBoxFormatted.Checked;
Preferences.HistoryLength = HistoryLength;
Preferences.EntryLength = EntryLength;
Preferences.TooltipLength = TooltipLength;
Preferences.Formatted = Formatted;
try
{
Preferences.Save();
}
catch
{
MessageBox.Show(this, Localization.NOSAVE, Localization.FAILURE, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ToggleWindow();
}
/// <summary>
/// Rearrange list of captured clipboard entries
/// </summary>
private void RearrangeList()
{
// If history is cleared do nothing, otherwise some exceptions will occur
if (GetHistories() == 0)
return;
CuttingHistory();
int listStart = contextMenuStrip.Items.IndexOf(toolStripSeparator3) + 1;
int listEnd = contextMenuStrip.Items.IndexOf(toolStripSeparator2) - 1;
for (int i = listStart; i <= listEnd; i++)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)contextMenuStrip.Items[i];
toolStripMenuItem.Text = PrepareTextForToolStripMenuItem(((object[])toolStripMenuItem.Tag)[0] as string);
toolStripMenuItem.ToolTipText = PrepareTextForToolTipText(((object[])toolStripMenuItem.Tag)[0] as string);
}
}
/// <summary>
/// Returns the number of history items.
/// </summary>
/// <returns>The number of history items.</returns>
public uint GetHistories()
{
// This is an approximation due to the relative positions of the context menu arrangement only!
int toolStripSeparatorPosition2 = contextMenuStrip.Items.IndexOf(toolStripSeparator2);
int toolStripSeparatorPosition3 = contextMenuStrip.Items.IndexOf(toolStripSeparator3);
int difference = toolStripSeparatorPosition2 - toolStripSeparatorPosition3;
difference--;
if (1 == difference && contextMenuStrip.Items[2].Equals(ToolStripMenuItemEmpty))
Histories = 0;
else
Histories = (uint)difference;
return Histories;
}
/// <summary>
/// Check for update availability and pop up a message if so.
/// </summary>
private bool CheckForUpdate(bool silent = true)
{
try
{
// Fetch XML document
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Naming.UPDATE_URL);
request.UserAgent = string.Format("{0} {1}", Application.ProductName, Application.ProductVersion);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string xmlDocument = responseReader.ReadToEnd();
responseReader.Close();
// Handle XML document
XmlDocument xmlDom = new XmlDocument();
xmlDom.LoadXml(xmlDocument);
string version = xmlDom.GetElementsByTagName("version").Item(0).FirstChild.Value;
// Compare local version with remote one
// Removing dots from version number and converting it to integer type for an easier comparison
int localVersion = Convert.ToInt32(Application.ProductVersion.Replace(".", ""));
int remoteVersion = Convert.ToInt32(version.Replace(".", ""));
int comparison = localVersion.CompareTo(remoteVersion);
// --
if (-1 == comparison)
{
// Pop up a message
DialogResult answer = MessageBox.Show(string.Format(Localization.UPDATE_AVAILABLE, version, Application.ProductVersion), Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (DialogResult.Yes == answer)
{
// Open a browser with the project page
Process.Start(Naming.PROJECT_SITE);
}
}
// Do not notify for no available updates while instantiating
else if(0 == comparison && silent)
{
MessageBox.Show(Localization.NO_UPDATE_AVAILABLE, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
// The new version number cannot be retrieved
return false;
}
return true;
}
#endregion
}
}