-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathHistoryWindow.cs
385 lines (334 loc) · 17.3 KB
/
HistoryWindow.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using EasyConnect.Protocols;
using EasyConnect.Protocols.Rdp;
using EasyTabs;
using EasyConnect.Properties;
using System.Configuration;
using TheArtOfDev.HtmlRenderer.WinForms;
using System.Globalization;
namespace EasyConnect
{
/// <summary>
/// Displays a history of the connections that the user has made over the past two weeks, separated by each day.
/// </summary>
public partial class HistoryWindow : Form
{
/// <summary>
/// Lookup that associates each list view item with its historical connection.
/// </summary>
private readonly Dictionary<ListViewItem, HistoricalConnection> _connections = new Dictionary<ListViewItem, HistoricalConnection>();
/// <summary>
/// Main application form that this window is associated with.
/// </summary>
protected MainForm _applicationForm = null;
/// <summary>
/// Lookup that associates each connection protocol with its icon in <see cref="historyImageList"/>.
/// </summary>
protected Dictionary<Type, int> _connectionTypeIcons = new Dictionary<Type, int>();
protected HtmlPanel _urlPanel;
/// <summary>
/// Constructor; loads the history data and gets the icons for each protocol.
/// </summary>
/// <param name="applicationForm">Main application for for this window.</param>
public HistoryWindow(MainForm applicationForm)
{
_applicationForm = applicationForm;
InitializeComponent();
historyContextMenu.Renderer = new EasyConnectToolStripRender();
_historyListView.ListViewItemSorter = new HistoryComparer(_connections);
// Get the icon for each protocol
foreach (IProtocol protocol in ConnectionFactory.GetProtocols())
{
Icon icon = new Icon(protocol.ProtocolIcon, 16, 16);
historyImageList.Images.Add(icon);
_connectionTypeIcons[protocol.ConnectionType] = historyImageList.Images.Count - 1;
}
Connections_CollectionModified(History.Instance.Connections, new ListModificationEventArgs(ListModification.RangeAdded, 0, History.Instance.Connections.Count));
History.Instance.Connections.CollectionModified += Connections_CollectionModified;
_toolsMenu.Renderer = new EasyConnectToolStripRender();
_iconPictureBox.Image = new Icon(Icon, 16, 16).ToBitmap();
_urlPanel = new HtmlPanel
{
AutoScroll = false,
Width = _urlPanelContainer.Width,
Height = _urlPanelContainer.Height,
Left = 0,
Top = 0,
Font = urlTextBox.Font,
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top,
BackColor = Color.FromArgb(241, 243, 244)
};
_urlPanelContainer.Controls.Add(_urlPanel);
_urlPanel.Text = String.Format(CultureInfo.InvariantCulture,
@"<span style=""background-color: #F1F3F4; font-family: {2}; font-size: {1}pt; height: {0}px; color: #707172"">easyconnect://<font color=""black"">history</font></span>",
_urlPanel.Height, urlTextBox.Font.SizeInPoints, urlTextBox.Font.FontFamily.GetName(0));
#if APPX
_updatesMenuItem.Visible = false;
_toolsMenuSeparator2.Visible = false;
#else
_updatesMenuItem.Visible = ConfigurationManager.AppSettings["checkForUpdates"] != "false";
_toolsMenuSeparator2.Visible = ConfigurationManager.AppSettings["checkForUpdates"] != "false";
#endif
}
private void Connections_CollectionModified(object sender, ListModificationEventArgs e)
{
ListWithEvents<HistoricalConnection> history = sender as ListWithEvents<HistoricalConnection>;
if (e.Modification == ListModification.ItemModified || e.Modification == ListModification.ItemAdded || e.Modification == ListModification.RangeAdded)
{
for (int i = e.StartIndex; i < e.StartIndex + e.Count; i++)
{
HistoricalConnection historyEntry = history[i];
if (_historyListView.Groups[historyEntry.LastConnection.ToString("yyyy-MM-dd")] == null)
{
int insertIndex = 0;
string groupName = historyEntry.LastConnection.ToString("yyyy-MM-dd");
for (insertIndex = 0; insertIndex < _historyListView.Groups.Count; insertIndex++)
{
if (String.Compare(_historyListView.Groups[insertIndex].Name, groupName, StringComparison.Ordinal) < 0)
break;
}
_historyListView.Groups.Insert(insertIndex, new ListViewGroup(groupName, historyEntry.LastConnection.ToLongDateString()));
}
// To account for legacy versions of the application where everything was an RDP connection, if a history entry was LegacyHistoricalConnection,
// we use the RDP icon
ListViewItem listViewItem = new ListViewItem(
historyEntry.LastConnection.ToLongTimeString(), _connectionTypeIcons[historyEntry.Connection.GetType() == typeof(LegacyHistoricalConnection)
? typeof(RdpConnection)
: historyEntry.Connection.GetType()]);
listViewItem.SubItems.Add(historyEntry.Connection.DisplayName);
listViewItem.SubItems.Add(historyEntry.Connection.Host);
_connections[listViewItem] = historyEntry;
_historyListView.Items.Add(listViewItem);
_historyListView.Groups[historyEntry.LastConnection.ToString("yyyy-MM-dd")].Items.Add(listViewItem);
_historyListView.Columns[0].Width = 119;
_historyListView.Columns[1].Width = 143;
_historyListView.Columns[2].Width = 419;
}
if (IsHandleCreated)
_historyListView.BeginInvoke(new Action(_historyListView.Sort));
}
}
/// <summary>
/// Handler method that's called when the window is shown; sorts <see cref="_historyListView"/>.
/// </summary>
/// <param name="e"></param>
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
_historyListView.BeginInvoke(new Action(_historyListView.Sort));
}
/// <summary>
/// Handler method that's called when the user clicks on the "Properties..." menu item in the context menu that appears when the user right-clicks on
/// a history entry; opens up the settings for the connection in a new tab.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Argument associated with this event.</param>
private void propertiesMenuItem_Click(object sender, EventArgs e)
{
// Get the settings form for the connection type and open it in a new tab
Form settingsWindow = ConnectionFactory.CreateSettingsForm(_connections[_historyListView.SelectedItems[0]].Connection);
TitleBarTab settingsTab = new TitleBarTab(_applicationForm)
{
Content = settingsWindow
};
_applicationForm.Tabs.Add(settingsTab);
_applicationForm.SelectedTab = settingsTab;
}
/// <summary>
/// Handler method that's called when the user clicks on the "Connect" menu item in the context menu that appears when the user right-clicks on
/// a history entry; opens up a connection window in a new tab.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Argument associated with this event.</param>
private async void connectMenuItem_Click(object sender, EventArgs e)
{
await _applicationForm.Connect(_connections[_historyListView.SelectedItems[0]].Connection);
}
/// <summary>
/// Handler method that's called when the user clicks on an item in <see cref="_historyListView"/>; if it's a right-click, we open
/// <see cref="historyContextMenu"/>.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_historyListView"/>.</param>
/// <param name="e">Argument associated with this event.</param>
private void _historyListView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && _historyListView.SelectedItems.Count > 0)
historyContextMenu.Show(Cursor.Position);
}
/// <summary>
/// Handler method that's called when the user double-clicks on an item in <see cref="_historyListView"/>; opens up a connection window in a new tab.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Argument associated with this event.</param>
private async void _historyListView_DoubleClick(object sender, EventArgs e)
{
if (_historyListView.SelectedItems.Count > 0)
await _applicationForm.Connect(_connections[_historyListView.SelectedItems[0]].Connection);
}
/// <summary>
/// Main application instance that this window is associated with, which is used to call back into application functionality.
/// </summary>
protected MainForm ParentTabs
{
get
{
return (MainForm)Parent;
}
}
/// <summary>
/// Handler method that's called when the user's cursor goes over <see cref="_toolsButton"/>. Sets the button's background to the standard
/// "hover" image.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_toolsButton"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _toolsButton_MouseEnter(object sender, EventArgs e)
{
_toolsButton.BackgroundImage = Resources.ButtonHoverBackground;
}
/// <summary>
/// Handler method that's called when the user's cursor leaves <see cref="_toolsButton"/>. Sets the button's background to nothing.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_toolsButton"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _toolsButton_MouseLeave(object sender, EventArgs e)
{
if (!_toolsMenu.Visible)
_toolsButton.BackgroundImage = null;
}
/// <summary>
/// Handler method that's called when the user clicks the "Exit" menu item in the tools menu. Exits the entire application.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _exitMenuItem_Click(object sender, EventArgs e)
{
((Form)Parent).Close();
}
/// <summary>
/// Handler method that's called when the user clicks the "Tools" icon in the toolbar. Opens up <see cref="_toolsMenu"/>.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _toolsButton_Click(object sender, EventArgs e)
{
_toolsButton.BackgroundImage = Resources.ButtonPressedBackground;
_toolsMenu.DefaultDropDownDirection = ToolStripDropDownDirection.Left;
_toolsMenu.Show(_toolsButton, -1 * _toolsMenu.Width + _toolsButton.Width, _toolsButton.Height);
}
private void _aboutMenuItem_Click(object sender, EventArgs e)
{
AboutBox aboutBox = new AboutBox();
aboutBox.ShowDialog(ParentTabs);
}
/// <summary>
/// Handler method that's called when the user clicks on the "Check for updates" menu item under the tools menu. Starts the update check process by
/// calling <see cref="MainForm.CheckForUpdate"/> on <see cref="ParentTabs"/>.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _updatesMenuItem_Click(object sender, EventArgs e)
{
ParentTabs.CheckForUpdate();
}
/// <summary>
/// Handler method that's called when the user clicks the "History" menu item under the tools menu. Creates the history tab if one doesn't exist
/// already and then switches to it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _historyToolStripMenuItem_Click(object sender, EventArgs e)
{
ParentTabs.OpenHistory();
}
/// <summary>
/// Handler method that's called when the user clicks the <see cref="_newWindowMenuItem"/> in the tools menu. Creates a new <see cref="MainForm"/>
/// instance and opens it.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_newWindowMenuItem"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _newWindowMenuItem_Click(object sender, EventArgs e)
{
MainForm newWindow = new MainForm(new List<Guid>());
ParentTabs.ApplicationContext.OpenWindow(newWindow);
newWindow.Show();
}
/// <summary>
/// Handler method that's called when the user clicks the "New tab" menu item under the tools menu. Creates a new tab and then switches to it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _newTabMenuItem_Click(object sender, EventArgs e)
{
ParentTabs.AddNewTab();
}
/// <summary>
/// Handler method that's called when the user clicks the "Settings" menu item under the tools menu. Creates the settings tab if one doesn't exist
/// already and then switches to it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void _settingsMenuItem_Click(object sender, EventArgs e)
{
await ParentTabs.OpenSettings();
}
private void _toolsMenu_VisibleChanged(object sender, EventArgs e)
{
if (!_toolsMenu.Visible)
_toolsButton.BackgroundImage = null;
}
/// <summary>
/// Custom sorting class that sorts <see cref="ListViewItem"/>s by getting their corresponding <see cref="HistoricalConnection"/> instances and
/// comparing their <see cref="HistoricalConnection.LastConnection"/> properties.
/// </summary>
protected class HistoryComparer : IComparer
{
/// <summary>
/// List of connections correlated with <see cref="ListViewItem"/>s.
/// </summary>
private readonly Dictionary<ListViewItem, HistoricalConnection> _connections = null;
/// <summary>
/// Constructor that initializes <see cref="_connections"/>.
/// </summary>
/// <param name="connections">Connections to correlate each <see cref="ListViewItem"/> with.</param>
public HistoryComparer(Dictionary<ListViewItem, HistoricalConnection> connections)
{
_connections = connections;
}
/// <summary>
/// Compares the <see cref="HistoricalConnection.LastConnection"/> properties of the <see cref="HistoricalConnection"/> instances for each
/// <see cref="ListViewItem"/>.
/// </summary>
/// <param name="x">First <see cref="ListViewItem"/> that we should compare.</param>
/// <param name="y">Second <see cref="ListViewItem"/> that we should compare.</param>
/// <returns>The result of <see cref="DateTime.CompareTo(DateTime)"/> of <paramref name="x"/> when provided with
/// <see cref="HistoricalConnection.LastConnection"/> for <paramref name="y"/>.</returns>
public int Compare(object x, object y)
{
HistoricalConnection connectionX = _connections[(ListViewItem) x];
HistoricalConnection connectionY = _connections[(ListViewItem) y];
return connectionY.LastConnection.CompareTo(connectionX.LastConnection);
}
}
private void urlBackground_Resize(object sender, EventArgs e)
{
_urlPanel.AutoScroll = false;
}
private void HistoryWindow_FormClosing(object sender, FormClosingEventArgs e)
{
History.Instance.Connections.CollectionModified -= Connections_CollectionModified;
}
private void _toolsButton_MouseDown(object sender, MouseEventArgs e)
{
_toolsButton.BackgroundImage = Resources.ButtonPressedBackground;
}
}
}