using System.Collections.Generic; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using DownKyi.Core.Aria2cNet.Server; using DownKyi.Core.Settings; using DownKyi.Core.Utils.Validator; using DownKyi.Events; using DownKyi.Services; using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; using Prism.Services.Dialogs; using IDialogService = DownKyi.PrismExtension.Dialog.IDialogService; namespace DownKyi.ViewModels.Settings; public class ViewNetworkViewModel : ViewModelBase { public const string Tag = "PageSettingsNetwork"; private bool _isOnNavigatedTo; #region 页面属性申明 private bool _useSsl; public bool UseSsl { get => _useSsl; set => SetProperty(ref _useSsl, value); } private string _userAgent; public string UserAgent { get => _userAgent; set => SetProperty(ref _userAgent, value); } private bool _builtin; public bool Builtin { get => _builtin; set => SetProperty(ref _builtin, value); } private bool _aria2C; public bool Aria2C { get => _aria2C; set => SetProperty(ref _aria2C, value); } private bool _customAria2C; public bool CustomAria2C { get => _customAria2C; set => SetProperty(ref _customAria2C, value); } private List _maxCurrentDownloads; public List MaxCurrentDownloads { get => _maxCurrentDownloads; set => SetProperty(ref _maxCurrentDownloads, value); } private int _selectedMaxCurrentDownload; public int SelectedMaxCurrentDownload { get => _selectedMaxCurrentDownload; set => SetProperty(ref _selectedMaxCurrentDownload, value); } private NetworkProxy _networkProxy; public NetworkProxy NetworkProxy { get => _networkProxy; set => SetProperty(ref _networkProxy, value); } private string? _customNetworkProxy; public string? CustomNetworkProxy { get => _customNetworkProxy; set => SetProperty(ref _customNetworkProxy, value); } private List _splits; public List Splits { get => _splits; set => SetProperty(ref _splits, value); } private int _selectedSplit; public int SelectedSplit { get => _selectedSplit; set => SetProperty(ref _selectedSplit, value); } private bool _isHttpProxy; public bool IsHttpProxy { get => _isHttpProxy; set => SetProperty(ref _isHttpProxy, value); } private string _httpProxy; public string HttpProxy { get => _httpProxy; set => SetProperty(ref _httpProxy, value); } private int _httpProxyPort; public int HttpProxyPort { get => _httpProxyPort; set => SetProperty(ref _httpProxyPort, value); } private string _ariaHost; public string AriaHost { get => _ariaHost; set => SetProperty(ref _ariaHost, value); } private int _ariaListenPort; public int AriaListenPort { get => _ariaListenPort; set => SetProperty(ref _ariaListenPort, value); } private string _ariaToken; public string AriaToken { get => _ariaToken; set => SetProperty(ref _ariaToken, value); } private List _ariaLogLevels; public List AriaLogLevels { get => _ariaLogLevels; set => SetProperty(ref _ariaLogLevels, value); } private string _selectedAriaLogLevel; public string SelectedAriaLogLevel { get => _selectedAriaLogLevel; set => SetProperty(ref _selectedAriaLogLevel, value); } private List _ariaMaxConcurrentDownloads; public List AriaMaxConcurrentDownloads { get => _ariaMaxConcurrentDownloads; set => SetProperty(ref _ariaMaxConcurrentDownloads, value); } private int _selectedAriaMaxConcurrentDownload; public int SelectedAriaMaxConcurrentDownload { get => _selectedAriaMaxConcurrentDownload; set => SetProperty(ref _selectedAriaMaxConcurrentDownload, value); } private List _ariaSplits; public List AriaSplits { get => _ariaSplits; set => SetProperty(ref _ariaSplits, value); } private int _selectedAriaSplit; public int SelectedAriaSplit { get => _selectedAriaSplit; set => SetProperty(ref _selectedAriaSplit, value); } private int _ariaMaxOverallDownloadLimit; public int AriaMaxOverallDownloadLimit { get => _ariaMaxOverallDownloadLimit; set => SetProperty(ref _ariaMaxOverallDownloadLimit, value); } private int _ariaMaxDownloadLimit; public int AriaMaxDownloadLimit { get => _ariaMaxDownloadLimit; set => SetProperty(ref _ariaMaxDownloadLimit, value); } private bool _isAriaHttpProxy; public bool IsAriaHttpProxy { get => _isAriaHttpProxy; set => SetProperty(ref _isAriaHttpProxy, value); } private string _ariaHttpProxy; public string AriaHttpProxy { get => _ariaHttpProxy; set => SetProperty(ref _ariaHttpProxy, value); } private int _ariaHttpProxyPort; public int AriaHttpProxyPort { get => _ariaHttpProxyPort; set => SetProperty(ref _ariaHttpProxyPort, value); } private List _ariaFileAllocations; public List AriaFileAllocations { get => _ariaFileAllocations; set => SetProperty(ref _ariaFileAllocations, value); } private string _selectedAriaFileAllocation; public string SelectedAriaFileAllocation { get => _selectedAriaFileAllocation; set => SetProperty(ref _selectedAriaFileAllocation, value); } #endregion public ViewNetworkViewModel(IEventAggregator eventAggregator, IDialogService dialogService) : base(eventAggregator, dialogService) { #region 属性初始化 // builtin同时下载数 MaxCurrentDownloads = new List(); for (var i = 1; i <= 10; i++) { MaxCurrentDownloads.Add(i); } // builtin最大线程数 Splits = new List(); for (var i = 1; i <= 10; i++) { Splits.Add(i); } // Aria的日志等级 AriaLogLevels = new List { "DEBUG", "INFO", "NOTICE", "WARN", "ERROR" }; // Aria同时下载数 AriaMaxConcurrentDownloads = new List(); for (var i = 1; i <= 10; i++) { AriaMaxConcurrentDownloads.Add(i); } // Aria最大线程数 AriaSplits = new List(); for (var i = 1; i <= 10; i++) { AriaSplits.Add(i); } // Aria文件预分配 AriaFileAllocations = new List { "NONE", "PREALLOC", "FALLOC" }; #endregion } /// /// 导航到页面时执行 /// /// public override void OnNavigatedTo(NavigationContext navigationContext) { base.OnNavigatedTo(navigationContext); _isOnNavigatedTo = true; // 启用https var useSsl = SettingsManager.GetInstance().GetUseSsl(); UseSsl = useSsl == AllowStatus.Yes; // UserAgent UserAgent = SettingsManager.GetInstance().GetUserAgent(); // 选择下载器 var downloader = SettingsManager.GetInstance().GetDownloader(); switch (downloader) { case Core.Settings.Downloader.NotSet: break; case Core.Settings.Downloader.BuiltIn: Builtin = true; break; case Core.Settings.Downloader.Aria: Aria2C = true; break; case Core.Settings.Downloader.CustomAria: CustomAria2C = true; break; } NetworkProxy = SettingsManager.GetInstance().GetNetworkProxy(); CustomNetworkProxy = SettingsManager.GetInstance().GetCustomProxy(); // builtin同时下载数 SelectedMaxCurrentDownload = SettingsManager.GetInstance().GetMaxCurrentDownloads(); // builtin最大线程数 SelectedSplit = SettingsManager.GetInstance().GetSplit(); // 是否开启builtin http代理 var isHttpProxy = SettingsManager.GetInstance().GetIsHttpProxy(); IsHttpProxy = isHttpProxy == AllowStatus.Yes; // builtin的http代理的地址 HttpProxy = SettingsManager.GetInstance().GetHttpProxy(); // builtin的http代理的端口 HttpProxyPort = SettingsManager.GetInstance().GetHttpProxyListenPort(); // Aria服务器host AriaHost = SettingsManager.GetInstance().GetAriaHost(); // Aria服务器端口 AriaListenPort = SettingsManager.GetInstance().GetAriaListenPort(); // Aria服务器Token AriaToken = SettingsManager.GetInstance().GetAriaToken(); // Aria的日志等级 var ariaLogLevel = SettingsManager.GetInstance().GetAriaLogLevel(); SelectedAriaLogLevel = ariaLogLevel.ToString("G"); // Aria同时下载数 SelectedAriaMaxConcurrentDownload = SettingsManager.GetInstance().GetMaxCurrentDownloads(); // Aria最大线程数 SelectedAriaSplit = SettingsManager.GetInstance().GetAriaSplit(); // Aria下载速度限制 AriaMaxOverallDownloadLimit = SettingsManager.GetInstance().GetAriaMaxOverallDownloadLimit(); // Aria下载单文件速度限制 AriaMaxDownloadLimit = SettingsManager.GetInstance().GetAriaMaxDownloadLimit(); // 是否开启Aria http代理 var isAriaHttpProxy = SettingsManager.GetInstance().GetIsAriaHttpProxy(); IsAriaHttpProxy = isAriaHttpProxy == AllowStatus.Yes; // Aria的http代理的地址 AriaHttpProxy = SettingsManager.GetInstance().GetAriaHttpProxy(); // Aria的http代理的端口 AriaHttpProxyPort = SettingsManager.GetInstance().GetAriaHttpProxyListenPort(); // Aria文件预分配 var ariaFileAllocation = SettingsManager.GetInstance().GetAriaFileAllocation(); SelectedAriaFileAllocation = ariaFileAllocation.ToString("G"); _isOnNavigatedTo = false; } #region 命令申明 // 是否启用https事件 private DelegateCommand? _useSslCommand; public DelegateCommand UseSslCommand => _useSslCommand ??= new DelegateCommand(ExecuteUseSslCommand); /// /// 是否启用https事件 /// private void ExecuteUseSslCommand() { var useSsl = UseSsl ? AllowStatus.Yes : AllowStatus.No; var isSucceed = SettingsManager.GetInstance().SetUseSsl(useSsl); PublishTip(isSucceed); } // 设置UserAgent事件 private DelegateCommand? _userAgentCommand; public DelegateCommand UserAgentCommand => _userAgentCommand ??= new DelegateCommand(ExecuteUserAgentCommand); /// /// 设置UserAgent事件 /// private void ExecuteUserAgentCommand() { var isSucceed = SettingsManager.GetInstance().SetUserAgent(UserAgent); PublishTip(isSucceed); } // 下载器选择事件 private DelegateCommand? _selectDownloaderCommand; public DelegateCommand SelectDownloaderCommand => _selectDownloaderCommand ??= new DelegateCommand(ExecuteSelectDownloaderCommand); /// /// 下载器选择事件 /// /// private async void ExecuteSelectDownloaderCommand(string parameter) { Core.Settings.Downloader downloader; switch (parameter) { case "Builtin": downloader = Core.Settings.Downloader.BuiltIn; break; case "Aria2c": downloader = Core.Settings.Downloader.Aria; break; case "CustomAria2c": downloader = Core.Settings.Downloader.CustomAria; break; default: downloader = SettingsManager.GetInstance().GetDownloader(); break; } var isSucceed = SettingsManager.GetInstance().SetDownloader(downloader); PublishTip(isSucceed); var alertService = new AlertService(DialogService); var result = await alertService.ShowInfo(DictionaryResource.GetString("ConfirmReboot")); if (result == ButtonResult.OK) { (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown(); // var dir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; // todo 暂时去掉自动重启,多平台需要不同实现 // if (dir != null) // { // Process.Start($"{dir}/DownKyi"); // } } } private DelegateCommand? _networkProxyCommand; public DelegateCommand NetworkProxyCommand => _networkProxyCommand ??= new DelegateCommand(ExecuteNetworkProxyCommand); private void ExecuteNetworkProxyCommand(object obj) { if (obj is not NetworkProxy networkProxy) return; NetworkProxy = networkProxy; var isSucceed = SettingsManager.GetInstance().SetNetworkProxy(networkProxy); PublishTip(isSucceed); } // builtin的http代理的地址事件 private DelegateCommand? _customNetworkProxyCommand; public DelegateCommand CustomNetworkProxyCommand => _customNetworkProxyCommand ??= new DelegateCommand(ExecuteCustomNetworkProxyCommand); /// /// builtin的http代理的地址事件 /// /// private void ExecuteCustomNetworkProxyCommand(string parameter) { var isSucceed = SettingsManager.GetInstance().SetCustomProxy(parameter); PublishTip(isSucceed); } // builtin同时下载数事件 private DelegateCommand? _maxCurrentDownloadsCommand; public DelegateCommand MaxCurrentDownloadsCommand => _maxCurrentDownloadsCommand ??= new DelegateCommand(ExecuteMaxCurrentDownloadsCommand); /// /// builtin同时下载数事件 /// /// private void ExecuteMaxCurrentDownloadsCommand(object parameter) { // SelectedMaxCurrentDownload = (int)parameter; var isSucceed = SettingsManager.GetInstance().SetMaxCurrentDownloads(SelectedMaxCurrentDownload); PublishTip(isSucceed); } // builtin最大线程数事件 private DelegateCommand? _splitsCommand; public DelegateCommand SplitsCommand => _splitsCommand ??= new DelegateCommand(ExecuteSplitsCommand); /// /// builtin最大线程数事件 /// /// private void ExecuteSplitsCommand(object parameter) { // SelectedSplit = (int)parameter; var isSucceed = SettingsManager.GetInstance().SetSplit(SelectedSplit); PublishTip(isSucceed); } // 是否开启builtin http代理事件 private DelegateCommand? _isHttpProxyCommand; public DelegateCommand IsHttpProxyCommand => _isHttpProxyCommand ??= new DelegateCommand(ExecuteIsHttpProxyCommand); /// /// 是否开启builtin http代理事件 /// private void ExecuteIsHttpProxyCommand() { var isHttpProxy = IsHttpProxy ? AllowStatus.Yes : AllowStatus.No; var isSucceed = SettingsManager.GetInstance().SetIsHttpProxy(isHttpProxy); PublishTip(isSucceed); } // builtin的http代理的地址事件 private DelegateCommand? _httpProxyCommand; public DelegateCommand HttpProxyCommand => _httpProxyCommand ??= new DelegateCommand(ExecuteHttpProxyCommand); /// /// builtin的http代理的地址事件 /// /// private void ExecuteHttpProxyCommand(string parameter) { var isSucceed = SettingsManager.GetInstance().SetHttpProxy(parameter); PublishTip(isSucceed); } // builtin的http代理的端口事件 private DelegateCommand? _httpProxyPortCommand; public DelegateCommand HttpProxyPortCommand => _httpProxyPortCommand ??= new DelegateCommand(ExecuteHttpProxyPortCommand); /// /// builtin的http代理的端口事件 /// /// private void ExecuteHttpProxyPortCommand(string parameter) { var httpProxyPort = (int)Number.GetInt(parameter); HttpProxyPort = httpProxyPort; var isSucceed = SettingsManager.GetInstance().SetHttpProxyListenPort(HttpProxyPort); PublishTip(isSucceed); } // Aria服务器host事件 private DelegateCommand? _ariaHostCommand; public DelegateCommand AriaHostCommand => _ariaHostCommand ??= new DelegateCommand(ExecuteAriaHostCommand); /// /// Aria服务器host事件 /// /// private void ExecuteAriaHostCommand(string parameter) { AriaHost = parameter; var isSucceed = SettingsManager.GetInstance().SetAriaHost(AriaHost); PublishTip(isSucceed); } // Aria服务器端口事件 private DelegateCommand? _ariaListenPortCommand; public DelegateCommand AriaListenPortCommand => _ariaListenPortCommand ??= new DelegateCommand(ExecuteAriaListenPortCommand); /// /// Aria服务器端口事件 /// /// private void ExecuteAriaListenPortCommand(string parameter) { var listenPort = (int)Number.GetInt(parameter); AriaListenPort = listenPort; var isSucceed = SettingsManager.GetInstance().SetAriaListenPort(AriaListenPort); PublishTip(isSucceed); } // Aria服务器token事件 private DelegateCommand? _ariaTokenCommand; public DelegateCommand AriaTokenCommand => _ariaTokenCommand ??= new DelegateCommand(ExecuteAriaTokenCommand); /// /// Aria服务器token事件 /// /// private void ExecuteAriaTokenCommand(string parameter) { AriaToken = parameter; var isSucceed = SettingsManager.GetInstance().SetAriaToken(AriaToken); PublishTip(isSucceed); } // Aria的日志等级事件 private DelegateCommand? _ariaLogLevelsCommand; public DelegateCommand AriaLogLevelsCommand => _ariaLogLevelsCommand ??= new DelegateCommand(ExecuteAriaLogLevelsCommand); /// /// Aria的日志等级事件 /// /// private void ExecuteAriaLogLevelsCommand(string parameter) { var ariaLogLevel = parameter switch { "DEBUG" => AriaConfigLogLevel.DEBUG, "INFO" => AriaConfigLogLevel.INFO, "NOTICE" => AriaConfigLogLevel.NOTICE, "WARN" => AriaConfigLogLevel.WARN, "ERROR" => AriaConfigLogLevel.ERROR, _ => AriaConfigLogLevel.INFO }; var isSucceed = SettingsManager.GetInstance().SetAriaLogLevel(ariaLogLevel); PublishTip(isSucceed); } // Aria同时下载数事件 private DelegateCommand? _ariaMaxConcurrentDownloadsCommand; public DelegateCommand AriaMaxConcurrentDownloadsCommand => _ariaMaxConcurrentDownloadsCommand ??= new DelegateCommand(ExecuteAriaMaxConcurrentDownloadsCommand); /// /// Aria同时下载数事件 /// /// private void ExecuteAriaMaxConcurrentDownloadsCommand(object? parameter) { if (parameter == null) return; SelectedAriaMaxConcurrentDownload = (int)parameter; var isSucceed = SettingsManager.GetInstance().SetMaxCurrentDownloads(SelectedAriaMaxConcurrentDownload); PublishTip(isSucceed); } // Aria最大线程数事件 private DelegateCommand? _ariaSplitsCommand; public DelegateCommand AriaSplitsCommand => _ariaSplitsCommand ??= new DelegateCommand(ExecuteAriaSplitsCommand); /// /// Aria最大线程数事件 /// /// private void ExecuteAriaSplitsCommand(object? parameter) { if (parameter == null) return; SelectedAriaSplit = (int)parameter; var isSucceed = SettingsManager.GetInstance().SetAriaSplit(SelectedAriaSplit); PublishTip(isSucceed); } // Aria下载速度限制事件 private DelegateCommand? _ariaMaxOverallDownloadLimitCommand; public DelegateCommand AriaMaxOverallDownloadLimitCommand => _ariaMaxOverallDownloadLimitCommand ??= new DelegateCommand( ExecuteAriaMaxOverallDownloadLimitCommand); /// /// Aria下载速度限制事件 /// /// private void ExecuteAriaMaxOverallDownloadLimitCommand(string parameter) { var downloadLimit = (int)Number.GetInt(parameter); AriaMaxOverallDownloadLimit = downloadLimit; var isSucceed = SettingsManager.GetInstance().SetAriaMaxOverallDownloadLimit(AriaMaxOverallDownloadLimit); PublishTip(isSucceed); } // Aria下载单文件速度限制事件 private DelegateCommand? _ariaMaxDownloadLimitCommand; public DelegateCommand AriaMaxDownloadLimitCommand => _ariaMaxDownloadLimitCommand ??= new DelegateCommand(ExecuteAriaMaxDownloadLimitCommand); /// /// Aria下载单文件速度限制事件 /// /// private void ExecuteAriaMaxDownloadLimitCommand(string parameter) { var downloadLimit = (int)Number.GetInt(parameter); AriaMaxDownloadLimit = downloadLimit; var isSucceed = SettingsManager.GetInstance().SetAriaMaxDownloadLimit(AriaMaxDownloadLimit); PublishTip(isSucceed); } // 是否开启Aria http代理事件 private DelegateCommand? _isAriaHttpProxyCommand; public DelegateCommand IsAriaHttpProxyCommand => _isAriaHttpProxyCommand ??= new DelegateCommand(ExecuteIsAriaHttpProxyCommand); /// /// 是否开启Aria http代理事件 /// private void ExecuteIsAriaHttpProxyCommand() { var isAriaHttpProxy = IsAriaHttpProxy ? AllowStatus.Yes : AllowStatus.No; var isSucceed = SettingsManager.GetInstance().SetIsAriaHttpProxy(isAriaHttpProxy); PublishTip(isSucceed); } // Aria的http代理的地址事件 private DelegateCommand? _ariaHttpProxyCommand; public DelegateCommand AriaHttpProxyCommand => _ariaHttpProxyCommand ??= new DelegateCommand(ExecuteAriaHttpProxyCommand); /// /// Aria的http代理的地址事件 /// /// private void ExecuteAriaHttpProxyCommand(string parameter) { var isSucceed = SettingsManager.GetInstance().SetAriaHttpProxy(parameter); PublishTip(isSucceed); } // Aria的http代理的端口事件 private DelegateCommand? _ariaHttpProxyPortCommand; public DelegateCommand AriaHttpProxyPortCommand => _ariaHttpProxyPortCommand ??= new DelegateCommand(ExecuteAriaHttpProxyPortCommand); /// /// Aria的http代理的端口事件 /// /// private void ExecuteAriaHttpProxyPortCommand(string parameter) { var httpProxyPort = (int)Number.GetInt(parameter); AriaHttpProxyPort = httpProxyPort; var isSucceed = SettingsManager.GetInstance().SetAriaHttpProxyListenPort(AriaHttpProxyPort); PublishTip(isSucceed); } // Aria文件预分配事件 private DelegateCommand? _ariaFileAllocationsCommand; public DelegateCommand AriaFileAllocationsCommand => _ariaFileAllocationsCommand ??= new DelegateCommand(ExecuteAriaFileAllocationsCommand); /// /// Aria文件预分配事件 /// /// private void ExecuteAriaFileAllocationsCommand(string parameter) { var ariaFileAllocation = parameter switch { "NONE" => AriaConfigFileAllocation.NONE, "PREALLOC" => AriaConfigFileAllocation.PREALLOC, "FALLOC" => AriaConfigFileAllocation.FALLOC, _ => AriaConfigFileAllocation.PREALLOC }; var isSucceed = SettingsManager.GetInstance().SetAriaFileAllocation(ariaFileAllocation); PublishTip(isSucceed); } #endregion /// /// 发送需要显示的tip /// /// private void PublishTip(bool isSucceed) { if (_isOnNavigatedTo) { return; } EventAggregator.GetEvent().Publish(isSucceed ? DictionaryResource.GetString("TipSettingUpdated") : DictionaryResource.GetString("TipSettingFailed")); } }