Skip to content

Add clean cache option & Add exception handler for cleaning folders #3411

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Flow.Launcher/Languages/en.xaml
Original file line number Diff line number Diff line change
@@ -325,6 +325,9 @@
<system:String x:Key="logfolder">Log Folder</system:String>
<system:String x:Key="clearlogfolder">Clear Logs</system:String>
<system:String x:Key="clearlogfolderMessage">Are you sure you want to delete all logs?</system:String>
<system:String x:Key="clearcachefolder">Clear Caches</system:String>
<system:String x:Key="clearcachefolderMessage">Are you sure you want to delete all caches?</system:String>
<system:String x:Key="clearfolderfailMessage">Failed to clear part of folders and files. Please see log file for more information</system:String>
<system:String x:Key="welcomewindow">Wizard</system:String>
<system:String x:Key="userdatapath">User Data Location</system:String>
<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>
130 changes: 117 additions & 13 deletions Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
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 string LogFolderSize
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 string LogFolderSize
};

public string ActivatedTimes => string.Format(
InternationalizationManager.Instance.GetTranslation("about_activate_times"),
App.API.GetTranslation("about_activate_times"),
_settings.ActivateTimes
);

@@ -88,14 +98,35 @@ private void OpenWelcomeWindow()
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,29 +144,54 @@ private void OpenParentOfSettingsFolder(object parameter)
App.API.OpenDirectory(parentFolderPath);
}


[RelayCommand]
private void OpenLogsFolder()
{
App.API.OpenDirectory(GetLogDir(Constant.Version).FullName);
}

[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 @@ private static List<FileInfo> GetLogFiles(string version = "")
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<FileInfo> GetCacheFiles()
{
return GetCacheDir().EnumerateFiles("*", SearchOption.AllDirectories).ToList();
}

private static string BytesToReadableString(long bytes)
{
const int scale = 1024;
@@ -156,8 +261,7 @@ private static string BytesToReadableString(long bytes)

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;
}
4 changes: 4 additions & 0 deletions Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml
Original file line number Diff line number Diff line change
@@ -90,6 +90,10 @@
Margin="0 12 0 0"
Icon="&#xf12b;">
<StackPanel Orientation="Horizontal">
<Button
Margin="0 0 12 0"
Command="{Binding AskClearCacheFolderConfirmationCommand}"
Content="{Binding CacheFolderSize, Mode=OneWay}" />
<Button
Margin="0 0 12 0"
Command="{Binding AskClearLogFolderConfirmationCommand}"
Loading