-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
215 lines (182 loc) · 6.61 KB
/
MainWindow.xaml.cs
File metadata and controls
215 lines (182 loc) · 6.61 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
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Upsilon.Apps.Passkey.Core.Models;
using Upsilon.Apps.Passkey.GUI.WPF.Helper;
using Upsilon.Apps.Passkey.GUI.WPF.ViewModels;
using Upsilon.Apps.Passkey.GUI.WPF.Views;
namespace Upsilon.Apps.Passkey.GUI.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly MainViewModel _mainViewModel;
private readonly DispatcherTimer _timer;
public MainWindow()
{
InitializeComponent();
DataContext = _mainViewModel = new MainViewModel();
_timer = new()
{
Interval = new TimeSpan(0, 0, 5),
};
_resetCredentials();
try
{
string[] args = Environment.GetCommandLineArgs();
string databaseFile = Path.GetFullPath(Environment.GetCommandLineArgs()[1]);
if (File.Exists(databaseFile))
{
_mainViewModel.DatabaseFile = databaseFile;
}
}
catch { }
_username_TB.KeyUp += _credential_TB_KeyUp;
_password_PB.KeyUp += _credential_TB_KeyUp;
_timer.Tick += _timer_Elapsed;
Loaded += (s, e) => this.PostLoadSetup();
}
private void _timer_Elapsed(object? sender, EventArgs e)
{
_resetCredentials();
MainViewModel.Database?.Close();
MainViewModel.Database = null;
}
private void _newUser_MenuItem_Click(object sender, RoutedEventArgs e)
{
UserSettingsView.ShowUserSettings(this);
}
private void _generatePassword_MenuItem_Click(object sender, RoutedEventArgs e)
{
_ = PasswordGenerator.ShowGeneratePasswordDialog(this);
}
private void _credential_TB_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
_timer.Stop();
if (sender == _username_TB)
{
if (string.IsNullOrEmpty(_username_TB.Text))
{
_timer.Start();
return;
}
if (!File.Exists(_mainViewModel.DatabaseFile))
{
string filename = MainViewModel.CryptographyCenter.GetHash(_username_TB.Text);
_mainViewModel.DatabaseFile = Path.GetFullPath($"{Path.GetDirectoryName(Environment.ProcessPath)}/raw/{filename}.pku");
}
try
{
MainViewModel.Database = Database.Open(MainViewModel.CryptographyCenter,
MainViewModel.SerializationCenter,
MainViewModel.PasswordFactory,
MainViewModel.ClipboardManager,
_mainViewModel.DatabaseFile,
_username_TB.Text);
MainViewModel.Database.DatabaseClosed += _database_DatabaseClosed;
MainViewModel.Database.AutoSaveDetected += _database_AutoSaveDetected;
}
catch { }
_mainViewModel.CredentialsLabel = "Password :";
_username_TB.Text = string.Empty;
_username_TB.Visibility = Visibility.Collapsed;
_password_PB.Password = string.Empty;
_password_PB.Visibility = Visibility.Visible;
_ = _password_PB.Focus();
}
else
{
if (string.IsNullOrEmpty(_password_PB.Password))
{
_timer.Start();
return;
}
if (MainViewModel.Database is not null)
{
_ = MainViewModel.Database.Login(_password_PB.Password);
if (MainViewModel.Database.User is not null)
{
Hide();
_resetCredentials();
if (!UserServicesView.ShowUser(this))
{
Close();
}
else
{
_resetCredentials();
}
}
}
}
_password_PB.Password = string.Empty;
_timer.Start();
}
else if (e.Key == Key.Escape)
{
_resetCredentials();
MainViewModel.Database?.Close();
MainViewModel.Database = null;
}
else
{
_timer.Stop();
_timer.Start();
}
}
private void _database_AutoSaveDetected(object? sender, Interfaces.Events.AutoSaveDetectedEventArgs e)
{
Hide();
MessageBoxResult result = MessageBox.Show("Unsaved changes have been detected.\nClick Yes to apply these changes.\nClick No to discard them.\nClick Cancel to ignore and keep the save file.", "Autosave detected", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
e.MergeBehavior = result switch
{
MessageBoxResult.Cancel => Passkey.Interfaces.Enums.AutoSaveMergeBehavior.MergeWithoutSavingAndKeepAutoSaveFile,
MessageBoxResult.No => Passkey.Interfaces.Enums.AutoSaveMergeBehavior.DontMergeAndRemoveAutoSaveFile,
_ => Passkey.Interfaces.Enums.AutoSaveMergeBehavior.MergeAndSaveThenRemoveAutoSaveFile,
};
}
private void _database_DatabaseClosed(object? sender, Interfaces.Events.LogoutEventArgs e)
{
try
{
Dispatcher.Invoke(() =>
{
_resetCredentials();
MainViewModel.Database = null;
Show();
});
}
catch { }
}
private void _resetCredentials()
{
_mainViewModel.DatabaseFile = string.Empty;
_mainViewModel.CredentialsLabel = "Username :";
_username_TB.Text = string.Empty;
_username_TB.Visibility = Visibility.Visible;
_ = _username_TB.Focus();
_password_PB.Password = string.Empty;
_password_PB.Visibility = Visibility.Collapsed;
_timer.Stop();
}
private void _openDatabase_MenuItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new()
{
Title = "Open user database file",
Filter = "Passkey user database file|*.pku",
};
if (!(dialog.ShowDialog() ?? false)) return;
_resetCredentials();
MainViewModel.Database?.Close();
MainViewModel.Database = null;
_mainViewModel.DatabaseFile = dialog.FileName;
}
}
}