mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #3411 from Flow-Launcher/clear_cache
Add clean cache option
This commit is contained in:
commit
3b43d457d9
3 changed files with 124 additions and 13 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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<FileInfo> 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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,6 +90,10 @@
|
|||
Margin="0 12 0 0"
|
||||
Icon="">
|
||||
<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…
Reference in a new issue