Skip to content

Commit 3b43d45

Browse files
authored
Merge pull request #3411 from Flow-Launcher/clear_cache
Add clean cache option
2 parents a3c18a6 + 7fc4533 commit 3b43d45

File tree

3 files changed

+124
-13
lines changed

3 files changed

+124
-13
lines changed

Flow.Launcher/Languages/en.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@
325325
<system:String x:Key="logfolder">Log Folder</system:String>
326326
<system:String x:Key="clearlogfolder">Clear Logs</system:String>
327327
<system:String x:Key="clearlogfolderMessage">Are you sure you want to delete all logs?</system:String>
328+
<system:String x:Key="clearcachefolder">Clear Caches</system:String>
329+
<system:String x:Key="clearcachefolderMessage">Are you sure you want to delete all caches?</system:String>
330+
<system:String x:Key="clearfolderfailMessage">Failed to clear part of folders and files. Please see log file for more information</system:String>
328331
<system:String x:Key="welcomewindow">Wizard</system:String>
329332
<system:String x:Key="userdatapath">User Data Location</system:String>
330333
<system:String x:Key="userdatapathToolTip">User settings and installed plugins are saved in the user data folder. This location may vary depending on whether it's in portable mode or not.</system:String>

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs

Lines changed: 117 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Windows;
77
using CommunityToolkit.Mvvm.Input;
88
using Flow.Launcher.Core;
9-
using Flow.Launcher.Core.Resource;
109
using Flow.Launcher.Infrastructure;
1110
using Flow.Launcher.Infrastructure.Logger;
1211
using Flow.Launcher.Infrastructure.UserSettings;
@@ -16,6 +15,8 @@ namespace Flow.Launcher.SettingPages.ViewModels;
1615

1716
public partial class SettingsPaneAboutViewModel : BaseModel
1817
{
18+
private static readonly string ClassName = nameof(SettingsPaneAboutViewModel);
19+
1920
private readonly Settings _settings;
2021
private readonly Updater _updater;
2122

@@ -24,7 +25,16 @@ public string LogFolderSize
2425
get
2526
{
2627
var size = GetLogFiles().Sum(file => file.Length);
27-
return $"{InternationalizationManager.Instance.GetTranslation("clearlogfolder")} ({BytesToReadableString(size)})";
28+
return $"{App.API.GetTranslation("clearlogfolder")} ({BytesToReadableString(size)})";
29+
}
30+
}
31+
32+
public string CacheFolderSize
33+
{
34+
get
35+
{
36+
var size = GetCacheFiles().Sum(file => file.Length);
37+
return $"{App.API.GetTranslation("clearcachefolder")} ({BytesToReadableString(size)})";
2838
}
2939
}
3040

@@ -42,7 +52,7 @@ public string LogFolderSize
4252
};
4353

4454
public string ActivatedTimes => string.Format(
45-
InternationalizationManager.Instance.GetTranslation("about_activate_times"),
55+
App.API.GetTranslation("about_activate_times"),
4656
_settings.ActivateTimes
4757
);
4858

@@ -88,14 +98,35 @@ private void OpenWelcomeWindow()
8898
private void AskClearLogFolderConfirmation()
8999
{
90100
var confirmResult = App.API.ShowMsgBox(
91-
InternationalizationManager.Instance.GetTranslation("clearlogfolderMessage"),
92-
InternationalizationManager.Instance.GetTranslation("clearlogfolder"),
101+
App.API.GetTranslation("clearlogfolderMessage"),
102+
App.API.GetTranslation("clearlogfolder"),
93103
MessageBoxButton.YesNo
94104
);
95105

96106
if (confirmResult == MessageBoxResult.Yes)
97107
{
98-
ClearLogFolder();
108+
if (!ClearLogFolder())
109+
{
110+
App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage"));
111+
}
112+
}
113+
}
114+
115+
[RelayCommand]
116+
private void AskClearCacheFolderConfirmation()
117+
{
118+
var confirmResult = App.API.ShowMsgBox(
119+
App.API.GetTranslation("clearcachefolderMessage"),
120+
App.API.GetTranslation("clearcachefolder"),
121+
MessageBoxButton.YesNo
122+
);
123+
124+
if (confirmResult == MessageBoxResult.Yes)
125+
{
126+
if (!ClearCacheFolder())
127+
{
128+
App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage"));
129+
}
99130
}
100131
}
101132

@@ -113,29 +144,54 @@ private void OpenParentOfSettingsFolder(object parameter)
113144
App.API.OpenDirectory(parentFolderPath);
114145
}
115146

116-
117147
[RelayCommand]
118148
private void OpenLogsFolder()
119149
{
120150
App.API.OpenDirectory(GetLogDir(Constant.Version).FullName);
121151
}
122152

123153
[RelayCommand]
124-
private Task UpdateApp() => _updater.UpdateAppAsync(false);
154+
private Task UpdateAppAsync() => _updater.UpdateAppAsync(false);
125155

126-
private void ClearLogFolder()
156+
private bool ClearLogFolder()
127157
{
158+
var success = true;
128159
var logDirectory = GetLogDir();
129160
var logFiles = GetLogFiles();
130161

131-
logFiles.ForEach(f => f.Delete());
162+
logFiles.ForEach(f =>
163+
{
164+
try
165+
{
166+
f.Delete();
167+
}
168+
catch (Exception e)
169+
{
170+
App.API.LogException(ClassName, $"Failed to delete log file: {f.Name}", e);
171+
success = false;
172+
}
173+
});
132174

133175
logDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
176+
// Do not clean log files of current version
134177
.Where(dir => !Constant.Version.Equals(dir.Name))
135178
.ToList()
136-
.ForEach(dir => dir.Delete());
179+
.ForEach(dir =>
180+
{
181+
try
182+
{
183+
dir.Delete(true);
184+
}
185+
catch (Exception e)
186+
{
187+
App.API.LogException(ClassName, $"Failed to delete log directory: {dir.Name}", e);
188+
success = false;
189+
}
190+
});
137191

138192
OnPropertyChanged(nameof(LogFolderSize));
193+
194+
return success;
139195
}
140196

141197
private static DirectoryInfo GetLogDir(string version = "")
@@ -148,6 +204,55 @@ private static List<FileInfo> GetLogFiles(string version = "")
148204
return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList();
149205
}
150206

207+
private bool ClearCacheFolder()
208+
{
209+
var success = true;
210+
var cacheDirectory = GetCacheDir();
211+
var cacheFiles = GetCacheFiles();
212+
213+
cacheFiles.ForEach(f =>
214+
{
215+
try
216+
{
217+
f.Delete();
218+
}
219+
catch (Exception e)
220+
{
221+
App.API.LogException(ClassName, $"Failed to delete cache file: {f.Name}", e);
222+
success = false;
223+
}
224+
});
225+
226+
cacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
227+
.ToList()
228+
.ForEach(dir =>
229+
{
230+
try
231+
{
232+
dir.Delete(true);
233+
}
234+
catch (Exception e)
235+
{
236+
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
237+
success = false;
238+
}
239+
});
240+
241+
OnPropertyChanged(nameof(CacheFolderSize));
242+
243+
return success;
244+
}
245+
246+
private static DirectoryInfo GetCacheDir()
247+
{
248+
return new DirectoryInfo(DataLocation.CacheDirectory);
249+
}
250+
251+
private static List<FileInfo> GetCacheFiles()
252+
{
253+
return GetCacheDir().EnumerateFiles("*", SearchOption.AllDirectories).ToList();
254+
}
255+
151256
private static string BytesToReadableString(long bytes)
152257
{
153258
const int scale = 1024;
@@ -156,8 +261,7 @@ private static string BytesToReadableString(long bytes)
156261

157262
foreach (string order in orders)
158263
{
159-
if (bytes > max)
160-
return $"{decimal.Divide(bytes, max):##.##} {order}";
264+
if (bytes > max) return $"{decimal.Divide(bytes, max):##.##} {order}";
161265

162266
max /= scale;
163267
}

Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
Margin="0 12 0 0"
9191
Icon="&#xf12b;">
9292
<StackPanel Orientation="Horizontal">
93+
<Button
94+
Margin="0 0 12 0"
95+
Command="{Binding AskClearCacheFolderConfirmationCommand}"
96+
Content="{Binding CacheFolderSize, Mode=OneWay}" />
9397
<Button
9498
Margin="0 0 12 0"
9599
Command="{Binding AskClearLogFolderConfirmationCommand}"

0 commit comments

Comments
 (0)