-
-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathSettingsPaneAboutViewModel.cs
More file actions
334 lines (286 loc) · 9.21 KB
/
Copy pathSettingsPaneAboutViewModel.cs
File metadata and controls
334 lines (286 loc) · 9.21 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.SettingPages.ViewModels;
public partial class SettingsPaneAboutViewModel : BaseModel
{
private static readonly string ClassName = nameof(SettingsPaneAboutViewModel);
private readonly Settings _settings;
private readonly Updater _updater;
public string LogFolderSize
{
get
{
var size = GetLogFiles().Sum(file => file.Length);
return $"{Localize.clearlogfolder()} ({BytesToReadableString(size)})";
}
}
public string CacheFolderSize
{
get
{
var size = GetCacheFiles().Sum(file => file.Length);
return $"{Localize.clearcachefolder()} ({BytesToReadableString(size)})";
}
}
public string Website => Constant.Website;
public string SponsorPage => Constant.SponsorPage;
public string ReleaseNotes => _updater.GitHubRepository + "/releases/latest";
public string Documentation => Constant.Documentation;
public string Docs => Constant.Docs;
public string Github => Constant.GitHub;
public string Crowdin => Constant.CrowdinProjectUrl;
public string Version => Constant.Version switch
{
"1.0.0" => Constant.Dev,
_ => Constant.Version
};
public string ActivatedTimes => Localize.about_activate_times(_settings.ActivateTimes);
public class LogLevelData : DropdownDataGeneric<LOGLEVEL> { }
public List<LogLevelData> LogLevels { get; } =
DropdownDataGeneric<LOGLEVEL>.GetValues<LogLevelData>("LogLevel");
public LOGLEVEL LogLevel
{
get => _settings.LogLevel;
set
{
if (_settings.LogLevel != value)
{
_settings.LogLevel = value;
Log.SetLogLevel(value);
}
}
}
public SettingsPaneAboutViewModel(Settings settings, Updater updater)
{
_settings = settings;
_updater = updater;
UpdateEnumDropdownLocalizations();
}
private void UpdateEnumDropdownLocalizations()
{
DropdownDataGeneric<LOGLEVEL>.UpdateLabels(LogLevels);
}
[RelayCommand]
private void OpenWelcomeWindow()
{
var window = new WelcomeWindow();
window.ShowDialog();
}
[RelayCommand]
private void AskClearLogFolderConfirmation()
{
var confirmResult = App.API.ShowMsgBox(
Localize.clearlogfolderMessage(),
Localize.clearlogfolder(),
MessageBoxButton.YesNo
);
if (confirmResult == MessageBoxResult.Yes)
{
if (!ClearLogFolder())
{
App.API.ShowMsgBox(Localize.clearfolderfailMessage());
}
}
}
[RelayCommand]
private void AskClearCacheFolderConfirmation()
{
var confirmResult = App.API.ShowMsgBox(
Localize.clearcachefolderMessage(),
Localize.clearcachefolder(),
MessageBoxButton.YesNo
);
if (confirmResult == MessageBoxResult.Yes)
{
if (!ClearCacheFolder())
{
App.API.ShowMsgBox(Localize.clearfolderfailMessage());
}
}
}
[RelayCommand]
private void OpenSettingsFolder()
{
App.API.OpenDirectory(DataLocation.SettingsDirectory);
}
[RelayCommand]
private void OpenParentOfSettingsFolder(object parameter)
{
string settingsFolderPath = Path.Combine(DataLocation.SettingsDirectory);
string parentFolderPath = Path.GetDirectoryName(settingsFolderPath);
App.API.OpenDirectory(parentFolderPath);
}
[RelayCommand]
private void OpenCacheFolder()
{
App.API.OpenDirectory(DataLocation.CacheDirectory);
}
[RelayCommand]
private void OpenLogsFolder()
{
App.API.OpenDirectory(GetLogDir(Constant.Version).FullName);
}
[RelayCommand]
private Task UpdateAppAsync() => _updater.UpdateAppAsync(false);
private bool ClearLogFolder()
{
var success = true;
var logDirectory = GetLogDir();
var logFiles = GetLogFiles();
logFiles.ForEach(f =>
{
try
{
f.Delete();
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete log file: {f.Name}", e);
success = false;
}
});
logDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
// Do not clean log files of current version
.Where(dir => !Constant.Version.Equals(dir.Name))
.ToList()
.ForEach(dir =>
{
try
{
// Log folders are the last level of folders
dir.Delete(recursive: false);
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete log directory: {dir.Name}", e);
success = false;
}
});
OnPropertyChanged(nameof(LogFolderSize));
return success;
}
private static DirectoryInfo GetLogDir(string version = "")
{
return new DirectoryInfo(Path.Combine(DataLocation.LogDirectory, version));
}
private static List<FileInfo> GetLogFiles(string version = "")
{
return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList();
}
private bool ClearCacheFolder()
{
var success = true;
var cacheDirectory = GetCacheDir();
var pluginCacheDirectory = GetPluginCacheDir();
var cacheFiles = GetCacheFiles();
cacheFiles.ForEach(f =>
{
try
{
f.Delete();
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete cache file: {f.Name}", e);
success = false;
}
});
// Check if plugin cache directory exists before attempting to delete
// Or it will throw DirectoryNotFoundException in `pluginCacheDirectory.EnumerateDirectories`
if (pluginCacheDirectory.Exists)
{
// Firstly, delete plugin cache directories
pluginCacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
.ToList()
.ForEach(dir =>
{
try
{
// Plugin may create directories in its cache directory
dir.Delete(recursive: true);
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
success = false;
}
});
// Then, delete plugin directory
var dir = pluginCacheDirectory;
try
{
dir.Delete(recursive: false);
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
success = false;
}
}
// Raise regardless to cover scenario where size needs to be recalculated if the folder is manually removed on disk.
OnPropertyChanged(nameof(CacheFolderSize));
return success;
}
private static DirectoryInfo GetCacheDir()
{
return new DirectoryInfo(DataLocation.CacheDirectory);
}
private static DirectoryInfo GetPluginCacheDir()
{
return new DirectoryInfo(DataLocation.PluginCacheDirectory);
}
private static List<FileInfo> GetCacheFiles()
{
return GetCacheDir().EnumerateFiles("*", SearchOption.AllDirectories).ToList();
}
private static string BytesToReadableString(long bytes)
{
const int scale = 1024;
string[] orders = { "GB", "MB", "KB", "B" };
long max = (long)Math.Pow(scale, orders.Length - 1);
foreach (string order in orders)
{
if (bytes > max) return $"{decimal.Divide(bytes, max):##.##} {order}";
max /= scale;
}
return "0 B";
}
public string SettingWindowFont
{
get => _settings.SettingWindowFont;
set
{
if (_settings.SettingWindowFont != value)
{
_settings.SettingWindowFont = value;
OnPropertyChanged();
}
}
}
[RelayCommand]
private void ResetSettingWindowFont()
{
SettingWindowFont = Win32Helper.GetSystemDefaultFont(false);
}
[RelayCommand]
private void OpenReleaseNotes()
{
var releaseNotesWindow = new ReleaseNotesWindow();
releaseNotesWindow.Show();
}
[RelayCommand]
private void OpenSponsorPage()
{
App.API.OpenUrl(SponsorPage);
}
}