-
Notifications
You must be signed in to change notification settings - Fork 66
/
MainForm.cs
800 lines (678 loc) · 28.1 KB
/
MainForm.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Windows.Input;
#if !APPX
using System.Configuration;
using NetSparkle;
using NetSparkle.Enums;
#endif
using EasyConnect.Properties;
using EasyConnect.Protocols;
using EasyTabs;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;
using Win32Interop.Enums;
using Win32Interop.Methods;
using System.Threading.Tasks;
using System.Threading;
namespace EasyConnect
{
/// <summary>
/// Main application form for EasyConnect that hosts the tabs in which the various windows are displayed.
/// </summary>
public partial class MainForm : TitleBarTabs
{
/// <summary>
/// Delegate to open the <see cref="HistoricalConnection"/> whose <see cref="IConnection.Guid"/> matches <paramref name="historyGuid"/>.
/// </summary>
/// <param name="historyGuid">Value to use to match against <see cref="IConnection.Guid"/> when searching for a
/// <see cref="HistoricalConnection"/></param>
/// <returns>The newly created tab for the connection to the <see cref="HistoricalConnection"/> whose <see cref="IConnection.Guid"/>
/// matches <paramref name="historyGuid"/>.</returns>
public delegate Task<TitleBarTab> ConnectToHistoryDelegate(Guid historyGuid);
/// <summary>
/// Instance of the first created MainForm, used for remoting purposes to open history entries via the jump list.
/// </summary>
public static MainForm ActiveInstance = null;
/// <summary>
/// Remoting method to use to open history entries via the jump list.
/// </summary>
public static ConnectToHistoryDelegate ConnectToHistoryMethod = null;
/// <summary>
/// When the application is invoked via the jump list to open a historical connection, we use this IPC channel to communicate with an already-open
/// process (if it exists) to tell it to open the given historical connection.
/// </summary>
protected static IpcServerChannel _ipcChannel = null;
/// <summary>
/// Contains the UI for using bookmarks but also the data concerning all of the bookmarks and folders that the user has.
/// </summary>
protected BookmarksWindow _bookmarksWindow = null;
/// <summary>
/// Flag indicating whether or not the user is currently pressing the Ctrl key.
/// </summary>
protected bool _ctrlDown = false;
/// <summary>
/// Contains the UI for looking at a user's connection history but also the data concerning those historical connections.
/// </summary>
protected HistoryWindow _historyWindow = null;
/// <summary>
/// Pointer to the low-level mouse hook callback (<see cref="KeyboardHookCallback"/>).
/// </summary>
protected IntPtr _hookId;
/// <summary>
/// Delegate of <see cref="KeyboardHookCallback"/>; declared as a member variable to keep it from being garbage collected.
/// </summary>
protected HOOKPROC _hookproc = null;
/// <summary>
/// Jump list for this application.
/// </summary>
protected JumpList _jumpList = null;
/// <summary>
/// Application-level (not connection protocol defaults) that the user has set.
/// </summary>
protected GlobalSettings _settings;
/// <summary>
/// Encryption type that was previously selected by the user.
/// </summary>
protected EncryptionType _previousEncryptionType;
/// <summary>
/// Tab that the user had previously focused on.
/// </summary>
protected TitleBarTab _previouslyClickedTab;
/// <summary>
/// When populating the application's jump list with the recent connections that the user has made, this is the category that the items go under.
/// </summary>
protected JumpListCustomCategory _recentCategory = new JumpListCustomCategory("Recent");
/// <summary>
/// Queue of the ten most recent connections, indicating which ones are going to show up in the jump list.
/// </summary>
protected Queue<HistoricalConnection> _recentConnections = new Queue<HistoricalConnection>();
/// <summary>
/// Flag indicating whether the user is pressing the Shift button.
/// </summary>
protected bool _shiftDown = false;
#if !APPX
protected static Sparkle _sparkle;
#endif
/// <summary>
/// Default constructor.
/// </summary>
public MainForm()
{
InitializeComponent();
Init();
#if !APPX
if (_sparkle == null && ConfigurationManager.AppSettings["checkForUpdates"] != "false")
{
_sparkle = new Sparkle(
String.IsNullOrEmpty(ConfigurationManager.AppSettings["appCastUrl"])
? "https://lstratman.github.io/EasyConnect/updates/EasyConnect.xml"
: ConfigurationManager.AppSettings["appCastUrl"],
Icon,
SecurityMode.Strict,
"<DSAKeyValue><P>12ewCqvTWhA9MrRLFDyvohdnbTaNkktQCzu87Tolcs7pOQcwsA0wnZF5Tkp2m21H0Y4N5vILPttmXizYLUZUSndLRfJlBtKWJi45CrUVZaMsEgYECXNrxuPGt9U9guPX39xdn1wwB/8TZRmAzqVSyDnrNw6e+2Ln9EJUG6RwmVc=</P><Q>ydyEMyIgVPSHlJvv5pcp+/DE8wU=</Q><G>It/uZNoAH//7ZQDmcjMNtInYRQ4MJBAp3posEglTlsWHb/BcKLqDkd4R0FkL6T+ZCu8DeC7TK4bPiecWDPY9E3MRmtivCxJVGD3LPv1eTsuRl+MyOg4z6KsbPTtXFjnDdG1+0zm3hzKntzgHiY6OaHwJaYlyLMmlFoNePUo4zN4=</G><Y>J3eg75iIHjUqklOn/CUOR9FALbSz4LFiswBbQBAertYwWbjLvKnr85hMTLgoFKMW+s5PHJaTBSAMmZS4rLj01Rver12JcoXs9t/mwuJYSw6QNwIO1Oau1OXF4dqA8fOH/XCGGcRzMdLt8FxtzRTuU5H88afsg5yjui54ezg6c3U=</Y></DSAKeyValue>");
_sparkle.UIFactory = new NetSparkleUIFactory();
_sparkle.CloseApplication += _sparkle_CloseApplication;
_sparkle.StartLoop(
true,
true,
String.IsNullOrEmpty(ConfigurationManager.AppSettings["updateCheckMinutes"])
? TimeSpan.FromMinutes(60 * 24)
: TimeSpan.FromMinutes(Convert.ToInt32(ConfigurationManager.AppSettings["updateCheckMinutes"])));
}
#endif
}
private void _sparkle_CloseApplication()
{
Invoke(new Action(() => Close()));
}
/// <summary>
/// Constructor; initializes the UI.
/// </summary>
/// <param name="openToBookmarks">Bookmarks, if any, that we should open when initially creating the UI.</param>
public MainForm(IEnumerable<Guid> openToBookmarks)
: this()
{
if (openToBookmarks != null)
{
IEnumerable<Guid> toBookmarks = openToBookmarks.ToList();
if (toBookmarks.Any())
{
OpenToBookmarks = (from Guid bookmarkGuid in toBookmarks
select Bookmarks.Instance.FindBookmark(bookmarkGuid)).ToList();
}
}
// Show the bookmarks manager window initially if we're not opening bookmarks or history entries
if (OpenToBookmarks == null && OpenToHistory == Guid.Empty)
OpenBookmarkManager();
}
/// <summary>
/// Constructor; initializes the UI.
/// </summary>
/// <param name="openToBookmarks">Bookmarks, if any, that we should open when initially creating the UI.</param>
public MainForm(List<IConnection> openToBookmarks)
: this()
{
OpenToBookmarks = openToBookmarks;
// Show the bookmarks manager window initially if we're not opening bookmarks or history entries
if (OpenToBookmarks == null && OpenToHistory == Guid.Empty)
OpenBookmarkManager();
}
/// <summary>
/// Contains the UI for using bookmarks but also the data concerning all of the bookmarks and folders that the user has.
/// </summary>
public BookmarksWindow BookmarksWindow
{
get
{
if (_bookmarksWindow == null && ConnectionFactory.ReadyForCrypto)
_bookmarksWindow = new BookmarksWindow(this);
return _bookmarksWindow;
}
}
/// <summary>
/// Contains the UI for looking at a user's connection history but also the data concerning those historical connections.
/// </summary>
protected HistoryWindow HistoryWindow
{
get
{
if (_historyWindow == null && ConnectionFactory.ReadyForCrypto)
_historyWindow = new HistoryWindow(this);
return _historyWindow;
}
}
/// <summary>
/// <see cref="IConnection.Guid"/> of the <see cref="HistoricalConnection"/> that we should connect to initially.
/// </summary>
public Guid OpenToHistory
{
get;
set;
}
/// <summary>
/// Bookmarks, if any, that we should open when initially creating the UI.
/// </summary>
public List<IConnection> OpenToBookmarks
{
get;
set;
}
/// <summary>
/// Forces a complete redraw of the tabs overlay.
/// </summary>
public new void RedrawTabs()
{
if (_overlay != null)
_overlay.Render(true);
}
/// <summary>
/// Initializes the UI, loads the bookmark and history data, and sets up the IPC remoting channel and low-level keyboard hook.
/// </summary>
protected void Init()
{
AeroPeekEnabled = false;
// Create a remoting channel used to tell this window to open historical connections when entries in the jump list are clicked
if (_ipcChannel == null)
{
_ipcChannel = new IpcServerChannel("EasyConnect");
ChannelServices.RegisterChannel(_ipcChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof (HistoryMethods), "HistoryMethods", WellKnownObjectMode.SingleCall);
}
// Wire up the tab event handlers
TabClicked += MainForm_TabClicked;
ActiveInstance = this;
ConnectToHistoryMethod = ConnectToHistory;
TabRenderer = new ChromeTabRenderer(this);
// Get the low-level keyboard hook that will be used to process shortcut keys
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
_hookproc = KeyboardHookCallback;
_hookId = User32.SetWindowsHookEx(WH.WH_KEYBOARD_LL, _hookproc, Kernel32.GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// Sets the encryption type to use to protect the application settings. Calls
/// <see cref="ConnectionFactory.SetEncryptionType(EncryptionType, System.Security.SecureString)"/> and then saves the bookmarks, history, and main
/// application settings.
/// </summary>
/// <param name="encryptionType">Encryption type to use.</param>
/// <param name="encryptionPassword">Encryption password, if any, to use.</param>
private async Task SetEncryptionType(EncryptionType encryptionType, SecureString encryptionPassword)
{
ConnectionFactory.SetEncryptionType(encryptionType, encryptionPassword);
await Bookmarks.Instance.Save();
await History.Instance.Save();
await ConnectionFactory.SetDefaults(await ConnectionFactory.GetDefaults(await ConnectionFactory.GetDefaultProtocol()));
}
/// <summary>
/// Processes shortcut keys.
/// </summary>
/// <param name="nCode">Code indicating if we should process this message.</param>
/// <param name="wParam"><see cref="WM"/> enumeration value which is the message that's being passed to us.</param>
/// <param name="lParam">Virtual key code of the key being pressed.</param>
/// <returns>The result of the next hook in the queue (<see cref="User32.CallNextHookEx"/>).</returns>
protected IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
// Only process the key press if our window is active
if (nCode >= 0 && User32.GetActiveWindow() == Handle)
{
// Get the key that was pressed
Key key = KeyInterop.KeyFromVirtualKey(Marshal.ReadInt32(lParam));
switch ((WM) wParam.ToInt32())
{
case WM.WM_KEYDOWN:
case WM.WM_SYSKEYDOWN:
switch (key)
{
case Key.RightCtrl:
case Key.LeftCtrl:
_ctrlDown = true;
break;
case Key.RightShift:
case Key.LeftShift:
_shiftDown = true;
break;
// Ctrl+T creates a new tab
case Key.T:
if (_ctrlDown)
AddNewTab();
break;
// Ctrl+Tab cycles forward in the tab list, while Ctrl+Shift+Tab cycles backward
case Key.Tab:
if (Tabs.Count > 1)
{
if (_ctrlDown && _shiftDown)
{
if (SelectedTabIndex == 0)
SelectedTabIndex = Tabs.Count - 1;
else
SelectedTabIndex--;
}
else if (_ctrlDown)
{
if (SelectedTabIndex == Tabs.Count - 1)
SelectedTabIndex = 0;
else
SelectedTabIndex++;
}
}
break;
}
break;
case WM.WM_KEYUP:
case WM.WM_SYSKEYUP:
switch (key)
{
case Key.RightCtrl:
case Key.LeftCtrl:
_ctrlDown = false;
break;
case Key.RightShift:
case Key.LeftShift:
_shiftDown = false;
break;
}
break;
}
}
// Call the next hook in the queue
return User32.CallNextHookEx(_hookId, nCode, wParam, lParam);
}
/// <summary>
/// Handler method that's called when a tab is clicked on. This is different from the <see cref="TitleBarTabs.TabSelected"/> event handler in that
/// this is called even if the tab is currently active. This is used to show the toolbar for <see cref="ConnectionWindow"/> instances that
/// automatically hide their toolbars when the connection's UI is focused on.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void MainForm_TabClicked(object sender, TitleBarTabEventArgs e)
{
// Only show the toolbar if the user clicked on an already-selected tab
if (e.Tab.Content is ConnectionWindow && e.Tab == _previouslyClickedTab && !e.WasDragging)
(e.Tab.Content as ConnectionWindow).ShowToolbar();
_previouslyClickedTab = e.Tab;
}
/// <summary>
/// Handler method that's called when the user closes the <see cref="global::EasyConnect.BookmarksWindow"/> tab. Sets <see cref="_bookmarksWindow"/> to null so that we know we
/// need to create a new instance the next time the user tries to open it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
protected void Bookmarks_FormClosed(object sender, FormClosedEventArgs e)
{
_bookmarksWindow = null;
}
/// <summary>
/// Opens an <see cref="SettingsWindow"/> instance when the user clicks on the "Settings" menu item from a <see cref="ConnectionWindow"/>.
/// </summary>
public async Task OpenSettings()
{
TitleBarTab tab = Tabs.FirstOrDefault(t => t.Content is SettingsWindow);
// Focus on the settings tab if a window is already open
if (tab != null)
{
SelectedTab = tab;
return;
}
_previousEncryptionType = GlobalSettings.Instance.EncryptionType ?? EncryptionType.Rijndael;
// Create the settings window and then add entries for each protocol type to the window
SettingsWindow settingsWindow = new SettingsWindow(this);
GlobalSettingsWindow globalSettingsWindow = new GlobalSettingsWindow();
globalSettingsWindow.Closed += globalSettingsWindow_Closed;
settingsWindow.SettingsForms.Add(globalSettingsWindow);
foreach (IProtocol protocol in ConnectionFactory.GetProtocols())
{
Form settingsForm = await protocol.GetSettingsFormInDefaultsMode();
settingsForm.Closed += settingsForm_Closed;
settingsWindow.SettingsForms.Add(settingsForm);
}
ShowInEmptyTab(settingsWindow);
}
private async void settingsForm_Closed(object sender, EventArgs e)
{
await ConnectionFactory.SetDefaults(((ISettingsForm)sender).Connection);
}
/// <summary>
/// Handler method that's closed when the user closes the global settings window. Sets the encryption type and the password that the user selected.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void globalSettingsWindow_Closed(object sender, EventArgs e)
{
if (_previousEncryptionType != GlobalSettings.Instance.EncryptionType)
// ReSharper disable PossibleInvalidOperationException
await SetEncryptionType(GlobalSettings.Instance.EncryptionType.Value, (sender as GlobalSettingsWindow).EncryptionPassword);
// ReSharper restore PossibleInvalidOperationException
}
/// <summary>
/// Creates a new tab to hold <paramref name="form"/>.
/// </summary>
/// <param name="form">Form instance that we are to open in a new tab.</param>
protected void ShowInEmptyTab(Form form)
{
// If we're opening the form from an unconnected ConnectionWindow, just replace its content with the new form
if (SelectedTab != null && SelectedTab.Content is ConnectionWindow && !(SelectedTab.Content as ConnectionWindow).IsConnected)
{
Form oldWindow = SelectedTab.Content;
SelectedTab.Content = form;
ResizeTabContents();
oldWindow.Close();
}
// Otherwise, create a new tab associated with the form
else
{
TitleBarTab newTab = new TitleBarTab(this)
{
Content = form
};
Tabs.Add(newTab);
ResizeTabContents(newTab);
SelectedTabIndex = _tabs.Count - 1;
}
form.Show();
if (_overlay != null)
_overlay.Render(true);
}
/// <summary>
/// Opens a <see cref="global::EasyConnect.HistoryWindow"/> instance when the user clicks on the "History" menu item in the tools menu from a
/// <see cref="ConnectionWindow"/> instance.
/// </summary>
public void OpenHistory()
{
TitleBarTab tab = Tabs.FirstOrDefault(t => t.Content is HistoryWindow);
// Focus on the existing history window tab if it exists
if (tab != null)
{
SelectedTab = tab;
return;
}
HistoryWindow.FormClosed += History_FormClosed;
ShowInEmptyTab(HistoryWindow);
}
/// <summary>
/// Handler method that's called when the user closes the <see cref="global::EasyConnect.HistoryWindow"/> tab. Sets <see cref="_historyWindow"/> to null so that we know we
/// need to create a new instance the next time the user tries to open it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void History_FormClosed(object sender, FormClosedEventArgs e)
{
_historyWindow = null;
}
/// <summary>
/// Opens a <see cref="global::EasyConnect.BookmarksWindow"/> instance when the user clicks on the "Bookmarks" menu item in the tools menu from a
/// <see cref="ConnectionWindow"/> instance.
/// </summary>
public void OpenBookmarkManager()
{
TitleBarTab tab = Tabs.FirstOrDefault(t => t.Content is BookmarksWindow);
// Focus on the existing bookmarks manager tab if it exists
if (tab != null)
{
SelectedTab = tab;
return;
}
BookmarksWindow.FormClosed += Bookmarks_FormClosed;
ShowInEmptyTab(BookmarksWindow);
}
/// <summary>
/// Opens the <see cref="HistoricalConnection"/> whose <see cref="IConnection.Guid"/> property matches <paramref name="historyGuid"/>.
/// </summary>
/// <param name="historyGuid">GUID used to identify the <see cref="HistoricalConnection"/> to open.</param>
/// <returns><see cref="ConnectionWindow"/> tab for the <see cref="HistoricalConnection"/> whose <see cref="IConnection.Guid"/> property
/// matches <paramref name="historyGuid"/>.</returns>
public async Task<TitleBarTab> ConnectToHistory(Guid historyGuid)
{
IConnection connection = History.Instance.FindInHistory(historyGuid);
if (connection != null)
return await Connect(connection);
return null;
}
/// <summary>
/// Opens the <see cref="IConnection"/>s in <paramref name="bookmarks"/>.
/// </summary>
/// <param name="bookmarks">Bookmarks to open.</param>
public async Task ConnectToBookmarks(List<IConnection> bookmarks)
{
foreach (IConnection bookmark in bookmarks)
await Connect(bookmark);
SelectedTabIndex = Tabs.Count - 1;
}
/// <summary>
/// Opens a new tab for <paramref name="connection"/>.
/// </summary>
/// <param name="connection">Connection that we are to open a new tab for.</param>
/// <returns>Tab that was created for <paramref name="connection"/>.</returns>
public async Task<TitleBarTab> Connect(IConnection connection)
{
return await Connect(connection, false);
}
/// <summary>
/// Opens a new tab for <paramref name="connection"/>.
/// </summary>
/// <param name="connection">Connection that we are to open a new tab for.</param>
/// <param name="focusNewTab">Flag indicating whether we should focus on the new tab.</param>
/// <returns>Tab that was created for <paramref name="connection"/>.</returns>
public async Task<TitleBarTab> Connect(IConnection connection, bool focusNewTab)
{
ConnectionWindow connectionWindow = new ConnectionWindow(connection);
TitleBarTab newTab = new TitleBarTab(this)
{
Content = connectionWindow
};
Tabs.Insert(SelectedTabIndex + 1, newTab);
ResizeTabContents(newTab);
if (focusNewTab)
{
SelectedTab = newTab;
_previouslyClickedTab = newTab;
}
await connectionWindow.Connect();
return newTab;
}
/// <summary>
/// Called from <see cref="ConnectionWindow"/> instances when a connection is established. Creates a thumbnail preview for the tab (if one doesn't
/// exist already) for Aero Peek and adds the connection to the list of entries on the application's jump list.
/// </summary>
/// <param name="connectionWindow"></param>
/// <param name="connection"></param>
public async Task RegisterConnection(ConnectionWindow connectionWindow, IConnection connection)
{
await History.Instance.AddToHistory(connection);
// Add the connection to the jump list
if (_recentConnections.FirstOrDefault((HistoricalConnection c) => c.Connection.Guid == connection.Guid) == null)
{
try
{
_recentCategory.AddJumpListItems(
new JumpListLink(Application.ExecutablePath, connectionWindow.Text)
{
Arguments = "/openHistory:" + connection.Guid.ToString(),
IconReference =
new IconReference(Application.ExecutablePath, 0)
});
_jumpList.Refresh();
}
// Turning off the "show recent documents in the taskbar and start menu" setting in the start menu properties
// can cause adding a jump list item to throw an exception, so just ignore the error and continue
catch (Exception)
{
}
_recentConnections.Enqueue(History.Instance.Connections.First((HistoricalConnection c) => c.Connection.Guid == connection.Guid));
if (_recentConnections.Count > _jumpList.MaxSlotsInList)
_recentConnections.Dequeue();
}
}
/// <summary>
/// Handler method that's called when the form is closing; saves the bookmarks.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
await Bookmarks.Instance.Save();
foreach (TitleBarTab tab in Tabs.ToArray())
tab.Content.Close();
}
/// <summary>
/// Handler method that's called when the form is shown. Creates and initializes the jump list if necessary and, if they are specified, opens the
/// bookmarks specified by <see cref="OpenToBookmarks"/> or the history entries pointed to by <see cref="OpenToHistory"/>.
/// </summary>
/// <param name="e">Arguments associated with this event.</param>
protected override async void OnShown(EventArgs e)
{
base.OnShown(e);
if (_jumpList == null)
{
_jumpList = JumpList.CreateJumpList();
_jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Neither;
_jumpList.AddCustomCategories(_recentCategory);
// Get all of the historical connections and order them by their last connection times
List<HistoricalConnection> historicalConnections =
History.Instance.Connections.OrderBy((HistoricalConnection c) => c.LastConnection).ToList();
historicalConnections = historicalConnections.GetRange(0, Math.Min(historicalConnections.Count, Convert.ToInt32(_jumpList.MaxSlotsInList)));
// Add each history entry to the jump list
foreach (HistoricalConnection historicalConnection in historicalConnections)
{
try
{
_recentCategory.AddJumpListItems(
new JumpListLink(Application.ExecutablePath, historicalConnection.Connection.DisplayName)
{
Arguments = "/openHistory:" + historicalConnection.Connection.Guid.ToString(),
IconReference = new IconReference(Application.ExecutablePath, 0)
});
}
// Turning off the "show recent documents in the taskbar and start menu" setting in the start menu properties
// can cause adding a jump list item to throw an exception, so just ignore the error and continue
catch (Exception)
{
}
_recentConnections.Enqueue(historicalConnection);
}
try
{
_jumpList.Refresh();
}
// Turning off the "show recent documents in the taskbar and start menu" setting in the start menu properties
// can cause adding a jump list item to throw an exception, so just ignore the error and continue
catch (Exception)
{
}
if (OpenToHistory != Guid.Empty)
SelectedTab = await Connect(History.Instance.FindInHistory(OpenToHistory));
}
if (OpenToHistory == Guid.Empty && OpenToBookmarks != null)
await ConnectToBookmarks(OpenToBookmarks);
}
/// <summary>
/// Method to create a new tab when the add button in the title bar is clicked; creates a new <see cref="ConnectionWindow"/>.
/// </summary>
/// <returns>Tab for a new <see cref="ConnectionWindow"/> instance.</returns>
public override TitleBarTab CreateTab()
{
_previouslyClickedTab = new TitleBarTab(this)
{
Content = new ConnectionWindow()
};
return _previouslyClickedTab;
}
/// <summary>
/// Custom message pump method for the window. Processes the <see cref="WM.WM_MOUSEACTIVATE"/> message.
/// </summary>
/// <param name="m">Message that we are to process.</param>
protected override void WndProc(ref Message m)
{
switch ((WM) m.Msg)
{
// If the selected tab is a connection window and the cursor is over the content area of the window, focus on that content
case WM.WM_MOUSEACTIVATE:
if (SelectedTab != null && SelectedTab.Content is ConnectionWindow)
{
if ((SelectedTab.Content as ConnectionWindow).IsCursorOverContent)
(SelectedTab.Content as ConnectionWindow).FocusContent();
}
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
/// <summary>
/// Initiates an update check for the application.
/// </summary>
/// <returns>True if the update process was started successfully, false otherwise.</returns>
public void CheckForUpdate()
{
#if !APPX
if (_sparkle == null)
{
return;
}
_sparkle.UpdateCheckFinished += _sparkle_UpdateCheckFinished;
_sparkle.CheckForUpdatesQuietly();
#endif
}
#if !APPX
void _sparkle_UpdateCheckFinished(object sender, UpdateStatus status)
{
_sparkle.UpdateCheckFinished -= _sparkle_UpdateCheckFinished;
if (status != UpdateStatus.UpdateAvailable)
MessageBox.Show(this, "No updates are available.", "EasyConnect Updater", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endif
}
}