-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserServicesView.xaml.cs
More file actions
396 lines (317 loc) · 13.5 KB
/
UserServicesView.xaml.cs
File metadata and controls
396 lines (317 loc) · 13.5 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
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
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Upsilon.Apps.Passkey.GUI.WPF.Helper;
using Upsilon.Apps.Passkey.GUI.WPF.Themes;
using Upsilon.Apps.Passkey.GUI.WPF.ViewModels;
using Upsilon.Apps.Passkey.GUI.WPF.ViewModels.Controls;
using Upsilon.Apps.Passkey.Interfaces.Enums;
namespace Upsilon.Apps.Passkey.GUI.WPF.Views
{
/// <summary>
/// Interaction logic for UserServicesView.xaml
/// </summary>
public partial class UserServicesView : Window
{
private readonly UserServicesViewModel _viewModel;
private int _autoLoginHotkeyId = 0;
private int _autoPasswordHotkeyId = 0;
private Task? _saveTask;
private UserServicesView()
{
InitializeComponent();
if (MainViewModel.Database is null) throw new NullReferenceException(nameof(MainViewModel.Database));
MainViewModel.GoToItem = _goToItem;
DataContext = _viewModel = new($"{MainViewModel.AppTitle} - '{MainViewModel.User}'");
_viewModel.FiltersRefreshed += _viewModel_FiltersRefreshed;
_services_LB.ItemsSource = _viewModel.Services;
if (_viewModel.Services.Count != 0)
{
_services_LB.SelectedIndex = 0;
}
_warnings_MI.Visibility = Visibility.Collapsed;
_ = _serviceFilter_TB.Focus();
_updateWarningsMenu();
MainViewModel.Database.DatabaseClosed += _database_DatabaseClosed;
MainViewModel.Database.WarningDetected += _database_WarningDetected;
Loaded += _userServicesView_Loaded;
}
private void _database_WarningDetected(object? sender, Interfaces.Events.WarningDetectedEventArgs e)
{
_ = Dispatcher.BeginInvoke(_updateWarningsMenu);
}
private void _viewModel_FiltersRefreshed(object? sender, EventArgs e)
{
if (_viewModel.Services.Count != 0)
{
_services_LB.SelectedIndex = 0;
}
}
private void _database_DatabaseClosed(object? sender, Interfaces.Events.LogoutEventArgs e)
{
try
{
Dispatcher.Invoke(() =>
{
DialogResult = true;
});
}
catch { }
}
public static bool ShowUser(Window owner)
{
return new UserServicesView()
{
Owner = owner,
}
.ShowDialog()
?? true;
}
private void _userServicesView_Loaded(object sender, RoutedEventArgs e)
{
_autoLoginHotkeyId = HotkeyHelper.Register(this, ModifierKeys.Control | ModifierKeys.Shift, Key.L);
_autoPasswordHotkeyId = HotkeyHelper.Register(this, ModifierKeys.Control | ModifierKeys.Shift, Key.P);
HotkeyHelper.HotkeyPressed += _hotkeyHelper_HotkeyPressed;
DarkMode.SetDarkMode(this);
}
private void _hotkeyHelper_HotkeyPressed(object? sender, HotkeyEventArgs e)
{
if (this.GetIsBusy()) return;
string? toInsert = null;
switch (e.Key)
{
case Key.L:
toInsert = _service_SV.GetSelectedIdentifier();
break;
case Key.P:
toInsert = _service_SV.GetSelectedPassword();
break;
}
if (!string.IsNullOrEmpty(toInsert))
{
QrCodeView.CopyToClipboard(toInsert);
HotkeyHelper.Send(ModifierKeys.Control, Key.V);
}
}
private void _userSettings_MenuItem_Click(object sender, RoutedEventArgs e)
{
_openSettings();
}
private void _openSettings()
{
if (this.GetIsBusy()) return;
UserSettingsView.ShowUserSettings(this);
_viewModel.RefreshFilters();
}
private void _generateRandomPassword_MenuItem_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
string? password = PasswordGenerator.ShowGeneratePasswordDialog(this);
if (password is null) return;
_service_SV.SetSelectedPassword(password);
}
private void _logout_MenuItem_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
DialogResult = true;
}
private void _window_Closed(object sender, EventArgs e)
{
_ = HotkeyHelper.Unregister(this, _autoLoginHotkeyId);
_ = HotkeyHelper.Unregister(this, _autoPasswordHotkeyId);
MainViewModel.GoToItem = null;
MainViewModel.AccountPasswordsWarningView?.Close();
MainViewModel.AccountPasswordsWarningView = null;
MainViewModel.DuplicatedPasswordsWarningView?.Close();
MainViewModel.DuplicatedPasswordsWarningView = null;
MainViewModel.UserActivitiesView?.Close();
MainViewModel.UserActivitiesView = null;
MainViewModel.Database?.Close();
MainViewModel.Database = null;
}
private void _services_LB_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (this.GetIsBusy()) return;
MainViewModel.User.Shake();
_service_SV.SetDataContext((ServiceViewModel)_services_LB.SelectedItem);
}
private void _save_MenuItem_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
string? serviceId = ((ServiceViewModel?)_services_LB.SelectedItem)?.ServiceId;
this.SetIsBusy(true);
if (_saveTask is not null
&& !_saveTask.IsCompleted)
{
return;
}
_saveTask = Task.Run(() =>
{
MainViewModel.Database?.Save();
Dispatcher.Invoke(() =>
{
_viewModel.RefreshFilters();
ServiceViewModel? service = _viewModel.Services.FirstOrDefault(x => x.ServiceId == serviceId);
_services_LB.ItemsSource = _viewModel.Services;
_services_LB.SelectedItem = service;
this.SetIsBusy(false);
});
});
}
private void _updateWarningsMenu()
{
int totalWarningCount = 0;
int activityWarnings = 0;
int expiredPasswordWarnings = 0;
int duplicatedPasswordWarnings = 0;
int leakedPasswordWarnings = 0;
if (MainViewModel.Database?.Warnings is not null)
{
activityWarnings = MainViewModel.Database.Warnings
.Where(x => x.WarningType.HasFlag(WarningType.ActivityReviewWarning))
.SelectMany(x => x.Activities ?? [])
.Count();
expiredPasswordWarnings = MainViewModel.Database.Warnings
.Where(x => x.WarningType.HasFlag(WarningType.PasswordUpdateReminderWarning))
.SelectMany(x => x.Accounts ?? [])
.Count();
duplicatedPasswordWarnings = MainViewModel.Database.Warnings
.Where(x => x.WarningType.HasFlag(WarningType.DuplicatedPasswordsWarning))
.Count();
leakedPasswordWarnings = MainViewModel.Database.Warnings
.Where(x => x.WarningType.HasFlag(WarningType.PasswordLeakedWarning))
.SelectMany(x => x.Accounts ?? [])
.Count();
totalWarningCount = activityWarnings + expiredPasswordWarnings + duplicatedPasswordWarnings + leakedPasswordWarnings;
_viewModel.ShowWarnings = $"Show {totalWarningCount} warnings";
_viewModel.ShowWarningsColor = (expiredPasswordWarnings + leakedPasswordWarnings) == 0 ? Brushes.Yellow : Brushes.Red;
_viewModel.ShowActivityWarnings = $"Show {activityWarnings} activities to review";
_viewModel.ShowExpiredPasswordWarnings = $"Show {expiredPasswordWarnings} expired passwords";
_viewModel.ShowDuplicatedPasswordWarnings = $"Show {duplicatedPasswordWarnings} duplicated passwords";
_viewModel.ShowLeakedPasswordWarnings = $"Show {leakedPasswordWarnings} leaked passwords";
}
_warnings_MI.Visibility = totalWarningCount != 0 ? Visibility.Visible : Visibility.Collapsed;
_activityWarnings_MI.Visibility = activityWarnings != 0 ? Visibility.Visible : Visibility.Collapsed;
_expiredPasswordWarnings_MI.Visibility = expiredPasswordWarnings != 0 ? Visibility.Visible : Visibility.Collapsed;
_duplicatedPasswordWarnings_MI.Visibility = duplicatedPasswordWarnings != 0 ? Visibility.Visible : Visibility.Collapsed;
_leakedPasswordWarnings_MI.Visibility = leakedPasswordWarnings != 0 ? Visibility.Visible : Visibility.Collapsed;
}
private void _addService_Button_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
_services_LB.SelectedItem = _viewModel.AddService();
}
private void _deleteService_Button_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
if (_services_LB.SelectedItem is not ServiceViewModel serviceViewModel
|| MessageBox.Show($"Are you sure you want to delete the service '{serviceViewModel.ServiceDisplay}'", "Delete Service", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
{
return;
}
_services_LB.SelectedIndex = _viewModel.DeleteService(serviceViewModel);
}
private void _filterClear_Button_Click(object sender, RoutedEventArgs e)
{
_clearFilter();
}
private void _clearFilter()
{
if (this.GetIsBusy()) return;
_viewModel.ServiceFilter = _viewModel.TextFilter = _viewModel.IdentifierFilter = string.Empty;
_viewModel.ChangedItemsOnly = false;
}
private void _showActivities_MenuItem_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
if (MainViewModel.UserActivitiesView is not null
&& MainViewModel.UserActivitiesView.IsLoaded)
{
UserActivitiesViewModel? vm = MainViewModel.UserActivitiesView.DataContext as UserActivitiesViewModel;
_ = (vm?.NeedsReview = false);
_ = MainViewModel.UserActivitiesView.Activate();
return;
}
MainViewModel.UserActivitiesView = new(needsReviewFilter: false);
MainViewModel.UserActivitiesView.Show();
}
private void _goToItem(string itemId)
{
if (MainViewModel.Database?.User is null) return;
if (string.IsNullOrEmpty(itemId)
|| MainViewModel.Database.User.ItemId == itemId)
{
_openSettings();
return;
}
_ = Activate();
_clearFilter();
switch (itemId[0])
{
case 'S':
_services_LB.SelectedItem = _viewModel.Services.FirstOrDefault(x => x.Service.ItemId == itemId);
break;
case 'A':
_services_LB.SelectedItem = _viewModel.Services.FirstOrDefault(x => x.Service.Accounts.Any(y => y.ItemId == itemId));
if (!_service_SV.SelectAccount(itemId))
{
_services_LB.SelectedItem = null;
}
break;
default:
break;
}
if (_services_LB.SelectedItem is not null)
{
_services_LB.ScrollIntoView(_services_LB.SelectedItem);
}
else
{
_ = MessageBox.Show($"The item '{itemId}' was not found.\nIt has been deleted.", "Item not found", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void _activityWarnings_MI_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
if (MainViewModel.UserActivitiesView is not null
&& MainViewModel.UserActivitiesView.IsLoaded)
{
UserActivitiesViewModel? vm = MainViewModel.UserActivitiesView.DataContext as UserActivitiesViewModel;
_ = (vm?.NeedsReview = true);
_ = MainViewModel.UserActivitiesView.Activate();
return;
}
MainViewModel.UserActivitiesView = new(needsReviewFilter: true);
MainViewModel.UserActivitiesView.Show();
}
private void _duplicatedPasswordWarnings_MI_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
if (MainViewModel.DuplicatedPasswordsWarningView is not null
&& MainViewModel.DuplicatedPasswordsWarningView.IsLoaded)
{
_ = MainViewModel.DuplicatedPasswordsWarningView.Activate();
return;
}
MainViewModel.DuplicatedPasswordsWarningView = new();
MainViewModel.DuplicatedPasswordsWarningView.Show();
}
private void _expiredOrLeakedPasswordWarnings_MI_Click(object sender, RoutedEventArgs e)
{
if (this.GetIsBusy()) return;
if (MainViewModel.AccountPasswordsWarningView is not null
&& MainViewModel.AccountPasswordsWarningView.IsLoaded)
{
AccountPasswordsWarningViewModel? vm = MainViewModel.AccountPasswordsWarningView.DataContext as AccountPasswordsWarningViewModel;
_ = (vm?.WarningType = sender == _expiredPasswordWarnings_MI
? WarningType.PasswordUpdateReminderWarning : WarningType.PasswordLeakedWarning);
_ = MainViewModel.AccountPasswordsWarningView.Activate();
return;
}
MainViewModel.AccountPasswordsWarningView = new(sender == _expiredPasswordWarnings_MI
? WarningType.PasswordUpdateReminderWarning : WarningType.PasswordLeakedWarning);
MainViewModel.AccountPasswordsWarningView.Show();
}
}
}