diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 2df430c3b..24ab3cf94 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -325,6 +325,9 @@ Log Folder Clear Logs Are you sure you want to delete all logs? + Clear Caches + Are you sure you want to delete all caches? + Failed to clear part of folders and files. Please see log file for more information Wizard User Data Location 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. diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs index 20f905411..6cfb98306 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using System.Windows; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core; -using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; @@ -16,6 +15,8 @@ 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; @@ -24,7 +25,16 @@ public partial class SettingsPaneAboutViewModel : BaseModel get { var size = GetLogFiles().Sum(file => file.Length); - return $"{InternationalizationManager.Instance.GetTranslation("clearlogfolder")} ({BytesToReadableString(size)})"; + return $"{App.API.GetTranslation("clearlogfolder")} ({BytesToReadableString(size)})"; + } + } + + public string CacheFolderSize + { + get + { + var size = GetCacheFiles().Sum(file => file.Length); + return $"{App.API.GetTranslation("clearcachefolder")} ({BytesToReadableString(size)})"; } } @@ -42,7 +52,7 @@ public partial class SettingsPaneAboutViewModel : BaseModel }; public string ActivatedTimes => string.Format( - InternationalizationManager.Instance.GetTranslation("about_activate_times"), + App.API.GetTranslation("about_activate_times"), _settings.ActivateTimes ); @@ -88,14 +98,35 @@ public partial class SettingsPaneAboutViewModel : BaseModel private void AskClearLogFolderConfirmation() { var confirmResult = App.API.ShowMsgBox( - InternationalizationManager.Instance.GetTranslation("clearlogfolderMessage"), - InternationalizationManager.Instance.GetTranslation("clearlogfolder"), + App.API.GetTranslation("clearlogfolderMessage"), + App.API.GetTranslation("clearlogfolder"), MessageBoxButton.YesNo ); if (confirmResult == MessageBoxResult.Yes) { - ClearLogFolder(); + if (!ClearLogFolder()) + { + App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage")); + } + } + } + + [RelayCommand] + private void AskClearCacheFolderConfirmation() + { + var confirmResult = App.API.ShowMsgBox( + App.API.GetTranslation("clearcachefolderMessage"), + App.API.GetTranslation("clearcachefolder"), + MessageBoxButton.YesNo + ); + + if (confirmResult == MessageBoxResult.Yes) + { + if (!ClearCacheFolder()) + { + App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage")); + } } } @@ -113,7 +144,6 @@ public partial class SettingsPaneAboutViewModel : BaseModel App.API.OpenDirectory(parentFolderPath); } - [RelayCommand] private void OpenLogsFolder() { @@ -121,21 +151,47 @@ public partial class SettingsPaneAboutViewModel : BaseModel } [RelayCommand] - private Task UpdateApp() => _updater.UpdateAppAsync(false); + private Task UpdateAppAsync() => _updater.UpdateAppAsync(false); - private void ClearLogFolder() + private bool ClearLogFolder() { + var success = true; var logDirectory = GetLogDir(); var logFiles = GetLogFiles(); - logFiles.ForEach(f => f.Delete()); + 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 => dir.Delete()); + .ForEach(dir => + { + try + { + dir.Delete(true); + } + 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 = "") @@ -148,6 +204,55 @@ public partial class SettingsPaneAboutViewModel : BaseModel return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList(); } + private bool ClearCacheFolder() + { + var success = true; + var cacheDirectory = GetCacheDir(); + 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; + } + }); + + cacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) + .ToList() + .ForEach(dir => + { + try + { + dir.Delete(true); + } + catch (Exception e) + { + App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e); + success = false; + } + }); + + OnPropertyChanged(nameof(CacheFolderSize)); + + return success; + } + + private static DirectoryInfo GetCacheDir() + { + return new DirectoryInfo(DataLocation.CacheDirectory); + } + + private static List GetCacheFiles() + { + return GetCacheDir().EnumerateFiles("*", SearchOption.AllDirectories).ToList(); + } + private static string BytesToReadableString(long bytes) { const int scale = 1024; @@ -156,8 +261,7 @@ public partial class SettingsPaneAboutViewModel : BaseModel foreach (string order in orders) { - if (bytes > max) - return $"{decimal.Divide(bytes, max):##.##} {order}"; + if (bytes > max) return $"{decimal.Divide(bytes, max):##.##} {order}"; max /= scale; } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml index 75c513411..f7deee7cf 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml @@ -90,6 +90,10 @@ Margin="0 12 0 0" Icon=""> +