forked from lstratman/EasyConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryWindow.cs
446 lines (386 loc) · 16.5 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using EasyConnect.Protocols;
using EasyConnect.Protocols.Rdp;
using Stratman.Windows.Forms.TitleBarTabs;
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>();
/// <summary>
/// Filename where history data is loaded from/saved to.
/// </summary>
protected string _historyFileName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\EasyConnect\\History.xml";
/// <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();
_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;
}
// Load the history data
if (File.Exists(_historyFileName))
{
XmlSerializer historySerializer = new XmlSerializer(typeof (List<HistoricalConnection>));
List<HistoricalConnection> historicalConnections = null;
using (XmlReader historyReader = new XmlTextReader(_historyFileName))
historicalConnections = (List<HistoricalConnection>) historySerializer.Deserialize(historyReader);
foreach (HistoricalConnection historyEntry in historicalConnections)
AddToHistory(historyEntry);
}
}
/// <summary>
/// List of all connections that the user has made in the last two weeks.
/// </summary>
public List<HistoricalConnection> Connections
{
get
{
return _connections.Values.ToList();
}
}
/// <summary>
/// Returns the connection, if any, in <see cref="Connections"/> whose <see cref="IConnection.Guid"/> matches <paramref name="historyGuid"/>.
/// </summary>
/// <param name="historyGuid">GUID we should use when matching against <see cref="IConnection.Guid"/> for each history entry.</param>
/// <returns>The connection, if any, in <see cref="Connections"/> whose <see cref="IConnection.Guid"/> matches
/// <paramref name="historyGuid"/>.</returns>
public IConnection FindInHistory(Guid historyGuid)
{
return _connections.Values.FirstOrDefault((HistoricalConnection c) => c.Connection.Guid == historyGuid) == null
? null
: _connections.Values.First((HistoricalConnection c) => c.Connection.Guid == historyGuid).Connection;
}
/// <summary>
/// Adds a connection that the user has made to the list of historical connections.
/// </summary>
/// <param name="connection">Connection that was made that should be added to the history.</param>
public void AddToHistory(IConnection connection)
{
HistoricalConnection historyEntry = new HistoricalConnection(connection)
{
LastConnection = DateTime.Now
};
AddToHistory(historyEntry);
Save();
}
/// <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>
/// Adds <paramref name="historyEntry"/> to <see cref="_historyListView"/> under the proper group.
/// </summary>
/// <param name="historyEntry">Connection that we're adding to the history.</param>
protected void AddToHistory(HistoricalConnection historyEntry)
{
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>
/// Writes the list of history entries to disk.
/// </summary>
public void Save()
{
FileInfo destinationFile = new FileInfo(_historyFileName);
XmlSerializer historySerializer = new XmlSerializer(typeof (List<HistoricalConnection>));
// ReSharper disable AssignNullToNotNullAttribute
Directory.CreateDirectory(destinationFile.DirectoryName);
// ReSharper restore AssignNullToNotNullAttribute
// Remove all connections older than two weeks
foreach (
KeyValuePair<ListViewItem, HistoricalConnection> connection in
_connections.Where(kvp => kvp.Value.LastConnection < DateTime.Now.AddDays(-14)).ToList())
{
connection.Key.Remove();
_connections.Remove(connection.Key);
}
using (XmlWriter historyWriter = new XmlTextWriter(_historyFileName, new UnicodeEncoding()))
{
historySerializer.Serialize(historyWriter, _connections.Values.ToList());
historyWriter.Flush();
}
}
/// <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 options 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 options form for the connection type and open it in a new tab
Form optionsWindow = ConnectionFactory.CreateOptionsForm(_connections[_historyListView.SelectedItems[0]].Connection);
TitleBarTab optionsTab = new TitleBarTab(_applicationForm)
{
Content = optionsWindow
};
_applicationForm.Tabs.Add(optionsTab);
_applicationForm.SelectedTab = optionsTab;
}
/// <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 void connectMenuItem_Click(object sender, EventArgs e)
{
_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 void _historyListView_DoubleClick(object sender, EventArgs e)
{
if (_historyListView.SelectedItems.Count > 0)
_applicationForm.Connect(_connections[_historyListView.SelectedItems[0]].Connection);
}
/// <summary>
/// Wraps a connection that the user made and the time that they made it.
/// </summary>
public class HistoricalConnection : ISerializable, IXmlSerializable
{
/// <summary>
/// Default constructor.
/// </summary>
public HistoricalConnection()
{
}
/// <summary>
/// Constructor for <see cref="ISerializable"/>.
/// </summary>
/// <param name="info">Container for serialized data.</param>
/// <param name="context">Serialization context.</param>
// ReSharper disable UnusedParameter.Local
public HistoricalConnection(SerializationInfo info, StreamingContext context)
// ReSharper restore UnusedParameter.Local
{
LastConnection = info.GetDateTime("LastConnection");
Connection = (IConnection) info.GetValue("Connection", typeof (IConnection));
}
/// <summary>
/// Constructor that initializes <see cref="Connection"/>.
/// </summary>
/// <param name="connection">Connection that the user made.</param>
public HistoricalConnection(IConnection connection)
{
Connection = connection;
}
/// <summary>
/// Connection that the user made.
/// </summary>
public IConnection Connection
{
get;
set;
}
/// <summary>
/// Date and time that the user made the connection.
/// </summary>
public DateTime LastConnection
{
get;
set;
}
/// <summary>
/// Serialization method for <see cref="ISerializable"/>.
/// </summary>
/// <param name="info">Container for serialized data.</param>
/// <param name="context">Serialization context.</param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("LastConnection", LastConnection);
info.AddValue("Connection", Connection);
}
/// <summary>
/// Schema method for <see cref="IXmlSerializable"/>.
/// </summary>
/// <returns>Null, always.</returns>
public XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Deserializes the historical connection data from an XML source.
/// </summary>
/// <param name="reader">XML source that we are deserializing from.</param>
public void ReadXml(XmlReader reader)
{
// If we are dealing with a legacy historical connection, deserialize it into LegacyHistoricalConnection
if (reader.GetAttribute("isLegacy") != "false")
{
XmlSerializer serializer = new XmlSerializer(typeof (LegacyHistoricalConnection));
LegacyHistoricalConnection legacyHistoricalConnection = (LegacyHistoricalConnection) serializer.Deserialize(reader);
LastConnection = legacyHistoricalConnection.LastConnection;
Connection = legacyHistoricalConnection;
}
// Otherwise, get the type of connection from the node name under Connection and deserialize it using that
else
{
reader.Read();
while (reader.MoveToContent() == XmlNodeType.Element)
{
switch (reader.LocalName)
{
case "LastConnection":
LastConnection = DateTime.Parse(reader.ReadElementContentAsString());
break;
case "Connection":
reader.Read();
XmlSerializer serializer =
new XmlSerializer(
reader.LocalName == "HistoricalConnection"
? typeof (LegacyHistoricalConnection)
: ConnectionFactory.GetProtocols().First(p => p.ConnectionType.Name == reader.LocalName).ConnectionType);
Connection = (IConnection) serializer.Deserialize(reader);
reader.Read();
break;
}
}
reader.Read();
}
}
/// <summary>
/// Serializes the connection data to XML.
/// </summary>
/// <param name="writer">XML destination that we're serializing to.</param>
public void WriteXml(XmlWriter writer)
{
// Write an explicit isLegacy="false" attribute so that we don't try to deserialize this using LegacyHistoricalConnection.
writer.WriteAttributeString("isLegacy", "false");
writer.WriteElementString("LastConnection", LastConnection.ToString());
writer.WriteStartElement("Connection");
XmlSerializer serializer = new XmlSerializer(Connection.GetType());
serializer.Serialize(writer, Connection);
writer.WriteEndElement();
}
}
/// <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);
}
}
/// <summary>
/// Legacy class designed to accommodate when derived the history connections from <see cref="RdpConnection"/>.
/// </summary>
[Serializable]
[XmlRoot("HistoricalConnection")]
public class LegacyHistoricalConnection : RdpConnection
{
/// <summary>
/// Time the user made the connection.
/// </summary>
public DateTime LastConnection
{
get;
set;
}
}
}
}