-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettings.xaml.cs
216 lines (177 loc) · 8.94 KB
/
Settings.xaml.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
using System;
using System.Windows;
using System.Windows.Input;
using MahApps.Metro.Controls;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Windows.Media;
namespace TwoFactor
{
public partial class Settings : MetroWindow
{
public bool PasswordIsCorrect;
public Settings()
{
InitializeComponent();
//styles and texts
this.Title = Localizer.GetLocalized("settings-title");
txt_label1.Text = Localizer.GetLocalized("settings-sortorder");
txt_label2.Text = Localizer.GetLocalized("settings-saveqr");
txt_label3.Text = Localizer.GetLocalized("settings-askpass");
txt_warning.Text = Localizer.GetLocalized("settings-warning");
toggle_saveqr.OffContent = "";
toggle_saveqr.OnContent = "";
toggle_askpass.OffContent = "";
toggle_askpass.OnContent = "";
//combobox values
combobox_sortorder.Items.Add(new Classes.Setting.ComboboxSortorder() { sortorder = Classes.Enums.SortOrder.MostUsed, name = Localizer.GetLocalized("settings-sorting-mostused") });
combobox_sortorder.Items.Add(new Classes.Setting.ComboboxSortorder() { sortorder = Classes.Enums.SortOrder.Name, name = Localizer.GetLocalized("settings-sorting-name") });
combobox_sortorder.Items.Add(new Classes.Setting.ComboboxSortorder() { sortorder = Classes.Enums.SortOrder.DateAdded, name = Localizer.GetLocalized("settings-sorting-dateadded") });
combobox_sortorder.Items.Add(new Classes.Setting.ComboboxSortorder() { sortorder = Classes.Enums.SortOrder.DateLastUsed, name = Localizer.GetLocalized("settings-sorting-datelastused") });
toggle_saveqr.IsOn = MainWindow.Settings.store_barcodes;
toggle_askpass.IsOn = MainWindow.Settings.ask_password_on_open;
combobox_sortorder.SelectedValue = MainWindow.Settings.sortorder;
//add the toggled handler after setting the value from the settings
toggle_askpass.Toggled += toggle_askpass_Toggled;
//darkmode detected then change colors
if (Classes.Globals.IsDarkMode())
{
this.Background = Classes.ResourceController.BrushDarkModeBackground;
toggle_saveqr.Style = (Style)FindResource("ToggleSwitch_Normal_DarkMode");
toggle_askpass.Style = (Style)FindResource("ToggleSwitch_Normal_DarkMode");
combobox_sortorder.Foreground = (SolidColorBrush)new BrushConverter().ConvertFrom("#acb6d2");
txt_label1.Foreground = Classes.ResourceController.BrushDarkModeText;
txt_label2.Foreground = Classes.ResourceController.BrushDarkModeText;
txt_label3.Foreground = Classes.ResourceController.BrushDarkModeText;
txt_warning.Foreground = Classes.ResourceController.BrushDarkModeText;
button_save.Content = Classes.IconController.GetButtonIcon(Classes.Enums.Icon.Save, Classes.ResourceController.BrushBlack, Localizer.GetLocalized("editlogin-save"));
button_export.Content = Classes.IconController.GetButtonIcon(Classes.Enums.Icon.Export, Classes.ResourceController.BrushBlack, Localizer.GetLocalized("settings-export"));
}
else
{
button_save.Content = Classes.IconController.GetButtonIcon(Classes.Enums.Icon.Save, Classes.ResourceController.BrushWhite, Localizer.GetLocalized("editlogin-save"));
button_export.Content = Classes.IconController.GetButtonIcon(Classes.Enums.Icon.Export, Classes.ResourceController.BrushWhite, Localizer.GetLocalized("settings-export"));
}
//if there are no logins then hide export button
if (MainWindow.Settings.logins == null || MainWindow.Settings.logins.Count == 0)
{
button_export.Visibility = Visibility.Hidden;
}
}
/// <summary>
/// Saves the settings and closes the window
/// </summary>
private void Button_save_Click(object sender, RoutedEventArgs e)
{
MainWindow.Settings.store_barcodes = toggle_saveqr.IsOn;
MainWindow.Settings.ask_password_on_open = toggle_askpass.IsOn;
MainWindow.Settings.sortorder = ((Classes.Setting.ComboboxSortorder)combobox_sortorder.SelectedItem).sortorder;
MainWindow.Settings.Save();
MainWindow.Settings.Sort();
((MainWindow)Application.Current.MainWindow).AddLoginsToPanel();
this.DialogResult = true;
}
/// <summary>
/// Clears the combobox focus to set the border to the default style again
/// </summary>
private void ClearComboBoxFocus()
{
FocusManager.SetFocusedElement(FocusManager.GetFocusScope(combobox_sortorder), null);
Keyboard.ClearFocus();
}
/// <summary>
/// Handles the keypresses in the window to close it on escape or enter
/// </summary>
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
ClearComboBoxFocus();
if (e.Key == Key.Return || e.Key == Key.Enter || e.Key == Key.Escape)
{
this.DialogResult = true;
}
}
/// <summary>
/// Exports the settings to a zip file
/// </summary>
private void Button_export_Click(object sender, RoutedEventArgs e)
{
//create a zip archive
var ms = new MemoryStream();
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create))
{
//add stored qr code images to zip
foreach (var item in Directory.GetFiles(Classes.Globals.AppPath, "*.gif"))
{
var file = File.ReadAllBytes(item);
var zipArchiveEntry = archive.CreateEntry(Path.GetFileName(item), CompressionLevel.Optimal);
using (var zipStream = zipArchiveEntry.Open())
{
zipStream.Write(file, 0, file.Length);
}
}
//add the settings file to the zip
var settingsfile = File.ReadAllBytes(Classes.Globals.AppPathSettingsFile);
var zipArchiveEntry1 = archive.CreateEntry(Path.GetFileName(Classes.Globals.AppPathSettingsFile), CompressionLevel.Optimal);
using (var zipStream = zipArchiveEntry1.Open())
{
zipStream.Write(settingsfile, 0, settingsfile.Length);
}
//add the txt file to the zip
var textfile = Encoding.ASCII.GetBytes(Localizer.GetLocalized("settings-instructions"));
var zipArchiveEntry2 = archive.CreateEntry("Instructions.txt", CompressionLevel.Optimal);
using (var zipStream = zipArchiveEntry2.Open())
{
zipStream.Write(textfile, 0, textfile.Length);
}
}
byte[] bin = ms.ToArray();
//show save file dialog
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = $"{Classes.Globals.AppName}_backup_{DateTime.Now.ToString("yyyyMMdd")}.zip";
dialog.DefaultExt = ".zip";
Nullable<bool> result = dialog.ShowDialog();
//dialog ok then save
if (result == true)
{
File.WriteAllBytes(dialog.FileName, bin);
}
}
/// <summary>
/// Window left mouse up event handler
/// </summary>
private void MetroWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ClearComboBoxFocus();
}
/// <summary>
/// Combobox selection changed event handler
/// </summary>
private void combobox_sortorder_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ClearComboBoxFocus();
}
/// <summary>
/// Ask for the password when enabling it on startup
/// </summary>
private void toggle_askpass_Toggled(object sender, RoutedEventArgs e)
{
PasswordIsCorrect = false;
if (!toggle_askpass.IsOn)
{
return;
}
var askpass = new AskPassword()
{
Owner = this,
ForSettingsChange = true
};
askpass.ShowDialog();
//needed to capture clicking of the close button in AskPassword window that otherwise bypasses the password check
if (!PasswordIsCorrect)
{
toggle_askpass.IsOn = false;
}
}
}
}