-
Notifications
You must be signed in to change notification settings - Fork 803
Expand file tree
/
Copy pathConnectionsViewModel.cs
More file actions
370 lines (287 loc) · 10.4 KB
/
ConnectionsViewModel.cs
File metadata and controls
370 lines (287 loc) · 10.4 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using log4net;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using NETworkManager.Localization;
using NETworkManager.Localization.Resources;
using NETworkManager.Models.Export;
using NETworkManager.Models.Network;
using NETworkManager.Settings;
using NETworkManager.Utilities;
using NETworkManager.Views;
namespace NETworkManager.ViewModels;
public class ConnectionsViewModel : ViewModelBase
{
#region Contructor, load settings
public ConnectionsViewModel(IDialogCoordinator instance)
{
_isLoading = true;
_dialogCoordinator = instance;
// Result view + search
ResultsView = CollectionViewSource.GetDefaultView(Results);
((ListCollectionView)ResultsView).CustomSort = Comparer<ConnectionInfo>.Create((x, y) =>
IPAddressHelper.CompareIPAddresses(x.LocalIPAddress, y.LocalIPAddress));
ResultsView.Filter = o =>
{
if (o is not ConnectionInfo info)
return false;
if (string.IsNullOrEmpty(Search))
return true;
// Search by local/remote IP Address, local/remote Port, Protocol and State
return info.LocalIPAddress.ToString().IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.LocalPort.ToString().IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.RemoteIPAddress.ToString().IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.RemoteHostname.IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.RemotePort.ToString().IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.Protocol.ToString().IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
ResourceTranslator.Translate(ResourceIdentifier.TcpState, info.TcpState)
.IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.ProcessId.ToString().IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.ProcessName.IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1 ||
info.ProcessPath.IndexOf(Search, StringComparison.OrdinalIgnoreCase) > -1;
};
// Get connections
Refresh().ConfigureAwait(false);
// Auto refresh
_autoRefreshTimer.Tick += AutoRefreshTimer_Tick;
AutoRefreshTimes = CollectionViewSource.GetDefaultView(AutoRefreshTime.GetDefaults);
SelectedAutoRefreshTime = AutoRefreshTimes.SourceCollection.Cast<AutoRefreshTimeInfo>().FirstOrDefault(x =>
x.Value == SettingsManager.Current.Connections_AutoRefreshTime.Value &&
x.TimeUnit == SettingsManager.Current.Connections_AutoRefreshTime.TimeUnit);
AutoRefreshEnabled = SettingsManager.Current.Connections_AutoRefreshEnabled;
_isLoading = false;
}
#endregion
#region Events
private async void AutoRefreshTimer_Tick(object sender, EventArgs e)
{
// Stop timer...
_autoRefreshTimer.Stop();
// Refresh
await Refresh();
// Restart timer...
_autoRefreshTimer.Start();
}
#endregion
#region Variables
private static readonly ILog Log = LogManager.GetLogger(typeof(ConnectionsViewModel));
private readonly IDialogCoordinator _dialogCoordinator;
private readonly bool _isLoading;
private readonly DispatcherTimer _autoRefreshTimer = new();
private string _search;
public string Search
{
get => _search;
set
{
if (value == _search)
return;
_search = value;
ResultsView.Refresh();
OnPropertyChanged();
}
}
private ObservableCollection<ConnectionInfo> _results = new();
public ObservableCollection<ConnectionInfo> Results
{
get => _results;
set
{
if (value == _results)
return;
_results = value;
OnPropertyChanged();
}
}
public ICollectionView ResultsView { get; }
private ConnectionInfo _selectedResult;
public ConnectionInfo SelectedResult
{
get => _selectedResult;
set
{
if (value == _selectedResult)
return;
_selectedResult = value;
OnPropertyChanged();
}
}
private IList _selectedResults = new ArrayList();
public IList SelectedResults
{
get => _selectedResults;
set
{
if (Equals(value, _selectedResults))
return;
_selectedResults = value;
OnPropertyChanged();
}
}
private bool _autoRefreshEnabled;
public bool AutoRefreshEnabled
{
get => _autoRefreshEnabled;
set
{
if (value == _autoRefreshEnabled)
return;
if (!_isLoading)
SettingsManager.Current.Connections_AutoRefreshEnabled = value;
_autoRefreshEnabled = value;
// Start timer to refresh automatically
if (value)
{
_autoRefreshTimer.Interval = AutoRefreshTime.CalculateTimeSpan(SelectedAutoRefreshTime);
_autoRefreshTimer.Start();
}
else
{
_autoRefreshTimer.Stop();
}
OnPropertyChanged();
}
}
public ICollectionView AutoRefreshTimes { get; }
private AutoRefreshTimeInfo _selectedAutoRefreshTime;
public AutoRefreshTimeInfo SelectedAutoRefreshTime
{
get => _selectedAutoRefreshTime;
set
{
if (value == _selectedAutoRefreshTime)
return;
if (!_isLoading)
SettingsManager.Current.Connections_AutoRefreshTime = value;
_selectedAutoRefreshTime = value;
if (AutoRefreshEnabled)
{
_autoRefreshTimer.Interval = AutoRefreshTime.CalculateTimeSpan(value);
_autoRefreshTimer.Start();
}
OnPropertyChanged();
}
}
private bool _isRefreshing;
public bool IsRefreshing
{
get => _isRefreshing;
set
{
if (value == _isRefreshing)
return;
_isRefreshing = value;
OnPropertyChanged();
}
}
private bool _isStatusMessageDisplayed;
public bool IsStatusMessageDisplayed
{
get => _isStatusMessageDisplayed;
set
{
if (value == _isStatusMessageDisplayed)
return;
_isStatusMessageDisplayed = value;
OnPropertyChanged();
}
}
private string _statusMessage;
public string StatusMessage
{
get => _statusMessage;
set
{
if (value == _statusMessage)
return;
_statusMessage = value;
OnPropertyChanged();
}
}
#endregion
#region ICommands & Actions
public ICommand RefreshCommand => new RelayCommand(_ => RefreshAction().ConfigureAwait(false), Refresh_CanExecute);
private bool Refresh_CanExecute(object parameter)
{
return Application.Current.MainWindow != null &&
!((MetroWindow)Application.Current.MainWindow).IsAnyDialogOpen &&
!ConfigurationManager.Current.IsChildWindowOpen;
}
private async Task RefreshAction()
{
IsStatusMessageDisplayed = false;
await Refresh();
}
public ICommand ExportCommand => new RelayCommand(_ => ExportAction().ConfigureAwait(false));
private async Task ExportAction()
{
var customDialog = new CustomDialog
{
Title = Strings.Export
};
var exportViewModel = new ExportViewModel(async instance =>
{
await _dialogCoordinator.HideMetroDialogAsync(this, customDialog);
try
{
ExportManager.Export(instance.FilePath, instance.FileType,
instance.ExportAll
? Results
: new ObservableCollection<ConnectionInfo>(SelectedResults.Cast<ConnectionInfo>()
.ToArray()));
}
catch (Exception ex)
{
Log.Error("Error while exporting data as " + instance.FileType, ex);
var settings = AppearanceManager.MetroDialog;
settings.AffirmativeButtonText = Strings.OK;
await _dialogCoordinator.ShowMessageAsync(this, Strings.Error,
Strings.AnErrorOccurredWhileExportingTheData + Environment.NewLine +
Environment.NewLine + ex.Message, MessageDialogStyle.Affirmative, settings);
}
SettingsManager.Current.Connections_ExportFileType = instance.FileType;
SettingsManager.Current.Connections_ExportFilePath = instance.FilePath;
}, _ => { _dialogCoordinator.HideMetroDialogAsync(this, customDialog); }, new[]
{
ExportFileType.Csv, ExportFileType.Xml, ExportFileType.Json
}, true, SettingsManager.Current.Connections_ExportFileType,
SettingsManager.Current.Connections_ExportFilePath);
customDialog.Content = new ExportDialog
{
DataContext = exportViewModel
};
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
}
#endregion
#region Methods
private async Task Refresh()
{
IsRefreshing = true;
Results.Clear();
(await Connection.GetActiveTcpConnectionsAsync()).ForEach(x => Results.Add(x));
IsRefreshing = false;
}
public void OnViewVisible()
{
// Restart timer...
if (AutoRefreshEnabled)
_autoRefreshTimer.Start();
}
public void OnViewHide()
{
// Temporarily stop timer...
if (AutoRefreshEnabled)
_autoRefreshTimer.Stop();
}
#endregion
}