Flow.Launcher/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs

318 lines
8.6 KiB
C#
Raw Normal View History

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;
2025-03-13 04:53:26 +00:00
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.SettingPages.ViewModels;
public partial class SettingsPaneAboutViewModel : BaseModel
{
2025-04-02 02:11:16 +00:00
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);
2025-04-01 11:18:32 +00:00
return $"{App.API.GetTranslation("clearlogfolder")} ({BytesToReadableString(size)})";
}
}
2025-04-01 12:12:27 +00:00
public string CacheFolderSize
{
get
{
var size = GetCacheFiles().Sum(file => file.Length);
return $"{App.API.GetTranslation("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 Version => Constant.Version switch
{
"1.0.0" => Constant.Dev,
_ => Constant.Version
};
public string ActivatedTimes => string.Format(
2025-04-01 11:18:32 +00:00
App.API.GetTranslation("about_activate_times"),
_settings.ActivateTimes
);
2025-03-13 04:53:26 +00:00
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;
2025-03-13 04:53:26 +00:00
UpdateEnumDropdownLocalizations();
}
private void UpdateEnumDropdownLocalizations()
{
DropdownDataGeneric<LOGLEVEL>.UpdateLabels(LogLevels);
}
[RelayCommand]
private void OpenWelcomeWindow()
{
var window = new WelcomeWindow();
window.ShowDialog();
}
[RelayCommand]
private void AskClearLogFolderConfirmation()
{
2025-01-09 13:30:11 +00:00
var confirmResult = App.API.ShowMsgBox(
2025-04-01 11:18:32 +00:00
App.API.GetTranslation("clearlogfolderMessage"),
App.API.GetTranslation("clearlogfolder"),
MessageBoxButton.YesNo
);
if (confirmResult == MessageBoxResult.Yes)
{
2025-04-02 02:11:16 +00:00
if (!ClearLogFolder())
2025-04-01 12:54:36 +00:00
{
App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage"));
}
}
}
2025-04-01 12:12:27 +00:00
[RelayCommand]
private void AskClearCacheFolderConfirmation()
{
var confirmResult = App.API.ShowMsgBox(
App.API.GetTranslation("clearcachefolderMessage"),
App.API.GetTranslation("clearcachefolder"),
MessageBoxButton.YesNo
);
if (confirmResult == MessageBoxResult.Yes)
{
2025-04-02 02:11:16 +00:00
if (!ClearCacheFolder())
2025-04-01 12:54:36 +00:00
{
App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage"));
}
2025-04-01 12:12:27 +00:00
}
}
[RelayCommand]
private void OpenSettingsFolder()
{
App.API.OpenDirectory(DataLocation.SettingsDirectory);
}
[RelayCommand]
private void OpenParentOfSettingsFolder(object parameter)
{
2025-02-24 08:08:06 +00:00
string settingsFolderPath = Path.Combine(DataLocation.SettingsDirectory);
string parentFolderPath = Path.GetDirectoryName(settingsFolderPath);
App.API.OpenDirectory(parentFolderPath);
}
2025-04-28 02:55:19 +00:00
[RelayCommand]
private void OpenCacheFolder()
{
App.API.OpenDirectory(DataLocation.CacheDirectory);
}
[RelayCommand]
private void OpenLogsFolder()
{
App.API.OpenDirectory(GetLogDir(Constant.Version).FullName);
}
[RelayCommand]
2025-04-01 11:18:32 +00:00
private Task UpdateAppAsync() => _updater.UpdateAppAsync(false);
2025-04-02 02:11:16 +00:00
private bool ClearLogFolder()
{
2025-04-02 02:11:16 +00:00
var success = true;
var logDirectory = GetLogDir();
var logFiles = GetLogFiles();
2025-04-02 02:11:16 +00:00
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)
2025-04-02 02:11:16 +00:00
// Do not clean log files of current version
.Where(dir => !Constant.Version.Equals(dir.Name))
.ToList()
2025-04-02 02:11:16 +00:00
.ForEach(dir =>
{
try
{
2025-04-30 11:35:35 +00:00
// Log folders are the last level of folders
2025-04-30 11:38:15 +00:00
dir.Delete(recursive: false);
2025-04-02 02:11:16 +00:00
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete log directory: {dir.Name}", e);
success = false;
}
});
OnPropertyChanged(nameof(LogFolderSize));
2025-04-02 02:11:16 +00:00
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();
}
2025-04-02 02:11:16 +00:00
private bool ClearCacheFolder()
2025-04-01 12:12:27 +00:00
{
2025-04-02 02:11:16 +00:00
var success = true;
2025-04-01 12:12:27 +00:00
var cacheDirectory = GetCacheDir();
2025-04-30 11:21:59 +00:00
var pluginCacheDirectory = GetPluginCacheDir();
2025-04-01 12:12:27 +00:00
var cacheFiles = GetCacheFiles();
2025-04-02 02:11:16 +00:00
cacheFiles.ForEach(f =>
{
try
{
f.Delete();
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete cache file: {f.Name}", e);
success = false;
}
});
2025-04-01 12:12:27 +00:00
2025-04-30 11:21:59 +00:00
// Firstly, delete plugin cache directories
pluginCacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
.ToList()
.ForEach(dir =>
{
try
{
// Plugin may create directories in its cache directory
2025-04-30 11:38:15 +00:00
dir.Delete(recursive: true);
2025-04-30 11:21:59 +00:00
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
success = false;
}
});
// Then, delete plugin directory
2025-04-30 11:38:15 +00:00
var dir = GetPluginCacheDir();
2025-04-30 11:35:35 +00:00
try
{
2025-04-30 11:38:15 +00:00
dir.Delete(recursive: false);
2025-04-30 11:35:35 +00:00
}
catch (Exception e)
{
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
success = false;
}
2025-04-01 12:12:27 +00:00
OnPropertyChanged(nameof(CacheFolderSize));
2025-04-02 02:11:16 +00:00
return success;
2025-04-01 12:12:27 +00:00
}
private static DirectoryInfo GetCacheDir()
{
return new DirectoryInfo(DataLocation.CacheDirectory);
2025-04-30 11:21:59 +00:00
}
private static DirectoryInfo GetPluginCacheDir()
{
return new DirectoryInfo(DataLocation.PluginCacheDirectory);
2025-04-01 12:12:27 +00:00
}
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)
{
2025-04-01 12:54:36 +00:00
if (bytes > max) return $"{decimal.Divide(bytes, max):##.##} {order}";
max /= scale;
}
return "0 B";
}
2025-04-17 09:45:31 +00:00
public string SettingWindowFont
{
get => _settings.SettingWindowFont;
set
{
if (_settings.SettingWindowFont != value)
{
_settings.SettingWindowFont = value;
2025-04-19 03:25:39 +00:00
OnPropertyChanged();
2025-04-17 09:45:31 +00:00
}
}
}
[RelayCommand]
private void ResetSettingWindowFont()
{
2025-04-23 03:48:11 +00:00
SettingWindowFont = Win32Helper.GetSystemDefaultFont(false);
2025-04-17 09:45:31 +00:00
}
}