-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathHistory.cs
179 lines (152 loc) · 6.52 KB
/
History.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
using EasyConnect.Protocols;
using EasyTabs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
#if APPX
using Windows.Storage;
#endif
using static EasyConnect.HistoryWindow;
namespace EasyConnect
{
public class History
{
/// <summary>
/// Filename where history data is loaded from/saved to.
/// </summary>
private static string HistoryFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "EasyConnect", "History.xml");
private ListWithEvents<HistoricalConnection> _connections = new ListWithEvents<HistoricalConnection>();
private History()
{
}
public static History Instance
{
get;
private set;
}
public ListWithEvents<HistoricalConnection> Connections
{
get
{
return _connections;
}
}
/// <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.FirstOrDefault((HistoricalConnection c) => c.Connection.Guid == historyGuid) == null
? null
: _connections.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 async Task AddToHistory(IConnection connection)
{
HistoricalConnection historyEntry = new HistoricalConnection(connection)
{
LastConnection = DateTime.Now
};
AddToHistory(historyEntry);
await Save();
}
/// <summary>
/// Adds <paramref name="historyEntry"/> to <see cref="_connections"/>.
/// </summary>
/// <param name="historyEntry">Connection that we're adding to the history.</param>
protected void AddToHistory(HistoricalConnection historyEntry)
{
_connections.Add(historyEntry);
}
public static async Task Init()
{
Instance = new History();
#if APPX
IStorageFile historyFile = (IStorageFile)await ApplicationData.Current.LocalFolder.TryGetItemAsync("History.xml");
string historyFileText = null;
// If there isn't a file in the Windows Store app data directory, try the desktop app directory
if (historyFile == null)
{
try
{
if (File.Exists(HistoryFileName))
{
historyFileText = File.ReadAllText(HistoryFileName);
}
}
catch (Exception)
{
}
}
else
{
historyFileText = await FileIO.ReadTextAsync(historyFile);
}
if (String.IsNullOrEmpty(historyFileText))
{
return;
}
using (StringReader historyFileTextReader = new StringReader(historyFileText))
using (XmlReader historyXmlReader = new XmlTextReader(historyFileTextReader))
{
XmlSerializer historySerializer = new XmlSerializer(typeof(List<HistoricalConnection>));
List<HistoricalConnection> historicalConnections = (List<HistoricalConnection>)historySerializer.Deserialize(historyXmlReader);
foreach (HistoricalConnection historyEntry in historicalConnections)
Instance.AddToHistory(historyEntry);
}
#else
// 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)
Instance.AddToHistory(historyEntry);
}
#endif
}
public async Task Save()
{
XmlSerializer historySerializer = new XmlSerializer(typeof(List<HistoricalConnection>));
#if APPX
// Remove all connections older than two weeks
foreach (HistoricalConnection connection in _connections.Where(c => c.LastConnection < DateTime.Now.AddDays(-14)).ToList())
{
_connections.Remove(connection);
}
IStorageFile historyFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("History.xml", CreationCollisionOption.ReplaceExisting);
StringWriter historyFileText = new StringWriter();
historySerializer.Serialize(historyFileText, _connections.ToList());
await FileIO.WriteTextAsync(historyFile, historyFileText.ToString());
#else
FileInfo destinationFile = new FileInfo(HistoryFileName);
// ReSharper disable AssignNullToNotNullAttribute
Directory.CreateDirectory(destinationFile.DirectoryName);
// ReSharper restore AssignNullToNotNullAttribute
// Remove all connections older than two weeks
foreach (HistoricalConnection connection in _connections.Where(c => c.LastConnection < DateTime.Now.AddDays(-14)).ToList())
{
_connections.Remove(connection);
}
using (XmlWriter historyWriter = new XmlTextWriter(HistoryFileName, new UnicodeEncoding()))
{
historySerializer.Serialize(historyWriter, _connections.ToList());
historyWriter.Flush();
}
#endif
}
}
}