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 System.Collections.Generic; namespace DownKyi.ViewModels.Settings { public class ViewNetworkViewModel : BaseViewModel { public const string Tag = "PageSettingsNetwork"; private bool isOnNavigatedTo; #region 页面属性申明 private bool useSSL; public bool UseSSL { get => useSSL; set => SetProperty(ref useSSL, 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 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 (int i = 1; i <= 10; i++) { MaxCurrentDownloads.Add(i); } // builtin最大线程数 Splits = new List(); for (int i = 1; i <= 10; i++) { Splits.Add(i); } // Aria的日志等级 AriaLogLevels = new List { "DEBUG", "INFO", "NOTICE", "WARN", "ERROR" }; // Aria同时下载数 AriaMaxConcurrentDownloads = new List(); for (int i = 1; i <= 10; i++) { AriaMaxConcurrentDownloads.Add(i); } // Aria最大线程数 AriaSplits = new List(); for (int 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 AllowStatus useSSL = SettingsManager.GetInstance().UseSSL(); UseSSL = useSSL == AllowStatus.YES; // 选择下载器 var downloader = SettingsManager.GetInstance().GetDownloader(); switch (downloader) { case Downloader.NOT_SET: break; case Downloader.BUILT_IN: Builtin = true; break; case Downloader.ARIA: Aria2c = true; break; case Downloader.CUSTOM_ARIA: CustomAria2c = true; break; } // builtin同时下载数 SelectedMaxCurrentDownload = SettingsManager.GetInstance().GetMaxCurrentDownloads(); // builtin最大线程数 SelectedSplit = SettingsManager.GetInstance().GetSplit(); // 是否开启builtin http代理 AllowStatus isHttpProxy = SettingsManager.GetInstance().IsHttpProxy(); 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的日志等级 AriaConfigLogLevel 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代理 AllowStatus isAriaHttpProxy = SettingsManager.GetInstance().IsAriaHttpProxy(); IsAriaHttpProxy = isAriaHttpProxy == AllowStatus.YES; // Aria的http代理的地址 AriaHttpProxy = SettingsManager.GetInstance().GetAriaHttpProxy(); // Aria的http代理的端口 AriaHttpProxyPort = SettingsManager.GetInstance().GetAriaHttpProxyListenPort(); // Aria文件预分配 AriaConfigFileAllocation ariaFileAllocation = SettingsManager.GetInstance().GetAriaFileAllocation(); SelectedAriaFileAllocation = ariaFileAllocation.ToString("G"); isOnNavigatedTo = false; } #region 命令申明 // 是否启用https事件 private DelegateCommand useSSLCommand; public DelegateCommand UseSSLCommand => useSSLCommand ?? (useSSLCommand = new DelegateCommand(ExecuteUseSSLCommand)); /// /// 是否启用https事件 /// private void ExecuteUseSSLCommand() { AllowStatus useSSL = UseSSL ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().UseSSL(useSSL); PublishTip(isSucceed); } // 下载器选择事件 private DelegateCommand selectDownloaderCommand; public DelegateCommand SelectDownloaderCommand => selectDownloaderCommand ?? (selectDownloaderCommand = new DelegateCommand(ExecuteSelectDownloaderCommand)); /// /// 下载器选择事件 /// /// private void ExecuteSelectDownloaderCommand(string parameter) { Downloader downloader; switch (parameter) { case "Builtin": downloader = Downloader.BUILT_IN; break; case "Aria2c": downloader = Downloader.ARIA; break; case "CustomAria2c": downloader = Downloader.CUSTOM_ARIA; break; default: downloader = SettingsManager.GetInstance().GetDownloader(); break; } bool isSucceed = SettingsManager.GetInstance().SetDownloader(downloader); PublishTip(isSucceed); AlertService alertService = new AlertService(dialogService); ButtonResult result = alertService.ShowInfo(DictionaryResource.GetString("ConfirmReboot")); if (result == ButtonResult.OK) { System.Windows.Application.Current.Shutdown(); System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); } } // builtin同时下载数事件 private DelegateCommand maxCurrentDownloadsCommand; public DelegateCommand MaxCurrentDownloadsCommand => maxCurrentDownloadsCommand ?? (maxCurrentDownloadsCommand = new DelegateCommand(ExecuteMaxCurrentDownloadsCommand)); /// /// builtin同时下载数事件 /// /// private void ExecuteMaxCurrentDownloadsCommand(object parameter) { SelectedMaxCurrentDownload = (int)parameter; bool isSucceed = SettingsManager.GetInstance().SetMaxCurrentDownloads(SelectedMaxCurrentDownload); PublishTip(isSucceed); } // builtin最大线程数事件 private DelegateCommand splitsCommand; public DelegateCommand SplitsCommand => splitsCommand ?? (splitsCommand = new DelegateCommand(ExecuteSplitsCommand)); /// /// builtin最大线程数事件 /// /// private void ExecuteSplitsCommand(object parameter) { SelectedSplit = (int)parameter; bool isSucceed = SettingsManager.GetInstance().SetSplit(SelectedSplit); PublishTip(isSucceed); } // 是否开启builtin http代理事件 private DelegateCommand isHttpProxyCommand; public DelegateCommand IsHttpProxyCommand => isHttpProxyCommand ?? (isHttpProxyCommand = new DelegateCommand(ExecuteIsHttpProxyCommand)); /// /// 是否开启builtin http代理事件 /// private void ExecuteIsHttpProxyCommand() { AllowStatus isHttpProxy = IsHttpProxy ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().IsHttpProxy(isHttpProxy); PublishTip(isSucceed); } // builtin的http代理的地址事件 private DelegateCommand httpProxyCommand; public DelegateCommand HttpProxyCommand => httpProxyCommand ?? (httpProxyCommand = new DelegateCommand(ExecuteHttpProxyCommand)); /// /// builtin的http代理的地址事件 /// /// private void ExecuteHttpProxyCommand(string parameter) { bool isSucceed = SettingsManager.GetInstance().SetHttpProxy(parameter); PublishTip(isSucceed); } // builtin的http代理的端口事件 private DelegateCommand httpProxyPortCommand; public DelegateCommand HttpProxyPortCommand => httpProxyPortCommand ?? (httpProxyPortCommand = new DelegateCommand(ExecuteHttpProxyPortCommand)); /// /// builtin的http代理的端口事件 /// /// private void ExecuteHttpProxyPortCommand(string parameter) { int httpProxyPort = (int)Number.GetInt(parameter); HttpProxyPort = httpProxyPort; bool isSucceed = SettingsManager.GetInstance().SetHttpProxyListenPort(HttpProxyPort); PublishTip(isSucceed); } // Aria服务器host事件 private DelegateCommand ariaHostCommand; public DelegateCommand AriaHostCommand => ariaHostCommand ?? (ariaHostCommand = new DelegateCommand(ExecuteAriaHostCommand)); /// /// Aria服务器host事件 /// /// private void ExecuteAriaHostCommand(string parameter) { AriaHost = parameter; bool isSucceed = SettingsManager.GetInstance().SetAriaHost(AriaHost); PublishTip(isSucceed); } // Aria服务器端口事件 private DelegateCommand ariaListenPortCommand; public DelegateCommand AriaListenPortCommand => ariaListenPortCommand ?? (ariaListenPortCommand = new DelegateCommand(ExecuteAriaListenPortCommand)); /// /// Aria服务器端口事件 /// /// private void ExecuteAriaListenPortCommand(string parameter) { int listenPort = (int)Number.GetInt(parameter); AriaListenPort = listenPort; bool isSucceed = SettingsManager.GetInstance().SetAriaListenPort(AriaListenPort); PublishTip(isSucceed); } // Aria服务器token事件 private DelegateCommand ariaTokenCommand; public DelegateCommand AriaTokenCommand => ariaTokenCommand ?? (ariaTokenCommand = new DelegateCommand(ExecuteAriaTokenCommand)); /// /// Aria服务器token事件 /// /// private void ExecuteAriaTokenCommand(string parameter) { AriaToken = parameter; bool isSucceed = SettingsManager.GetInstance().SetAriaToken(AriaToken); PublishTip(isSucceed); } // Aria的日志等级事件 private DelegateCommand ariaLogLevelsCommand; public DelegateCommand AriaLogLevelsCommand => ariaLogLevelsCommand ?? (ariaLogLevelsCommand = new DelegateCommand(ExecuteAriaLogLevelsCommand)); /// /// Aria的日志等级事件 /// /// private void ExecuteAriaLogLevelsCommand(string parameter) { AriaConfigLogLevel ariaLogLevel; switch (parameter) { case "DEBUG": ariaLogLevel = AriaConfigLogLevel.DEBUG; break; case "INFO": ariaLogLevel = AriaConfigLogLevel.INFO; break; case "NOTICE": ariaLogLevel = AriaConfigLogLevel.NOTICE; break; case "WARN": ariaLogLevel = AriaConfigLogLevel.WARN; break; case "ERROR": ariaLogLevel = AriaConfigLogLevel.ERROR; break; default: ariaLogLevel = AriaConfigLogLevel.INFO; break; } bool isSucceed = SettingsManager.GetInstance().SetAriaLogLevel(ariaLogLevel); PublishTip(isSucceed); } // Aria同时下载数事件 private DelegateCommand ariaMaxConcurrentDownloadsCommand; public DelegateCommand AriaMaxConcurrentDownloadsCommand => ariaMaxConcurrentDownloadsCommand ?? (ariaMaxConcurrentDownloadsCommand = new DelegateCommand(ExecuteAriaMaxConcurrentDownloadsCommand)); /// /// Aria同时下载数事件 /// /// private void ExecuteAriaMaxConcurrentDownloadsCommand(object parameter) { SelectedAriaMaxConcurrentDownload = (int)parameter; bool isSucceed = SettingsManager.GetInstance().SetMaxCurrentDownloads(SelectedAriaMaxConcurrentDownload); PublishTip(isSucceed); } // Aria最大线程数事件 private DelegateCommand ariaSplitsCommand; public DelegateCommand AriaSplitsCommand => ariaSplitsCommand ?? (ariaSplitsCommand = new DelegateCommand(ExecuteAriaSplitsCommand)); /// /// Aria最大线程数事件 /// /// private void ExecuteAriaSplitsCommand(object parameter) { SelectedAriaSplit = (int)parameter; bool isSucceed = SettingsManager.GetInstance().SetAriaSplit(SelectedAriaSplit); PublishTip(isSucceed); } // Aria下载速度限制事件 private DelegateCommand ariaMaxOverallDownloadLimitCommand; public DelegateCommand AriaMaxOverallDownloadLimitCommand => ariaMaxOverallDownloadLimitCommand ?? (ariaMaxOverallDownloadLimitCommand = new DelegateCommand(ExecuteAriaMaxOverallDownloadLimitCommand)); /// /// Aria下载速度限制事件 /// /// private void ExecuteAriaMaxOverallDownloadLimitCommand(string parameter) { int downloadLimit = (int)Number.GetInt(parameter); AriaMaxOverallDownloadLimit = downloadLimit; bool isSucceed = SettingsManager.GetInstance().SetAriaMaxOverallDownloadLimit(AriaMaxOverallDownloadLimit); PublishTip(isSucceed); } // Aria下载单文件速度限制事件 private DelegateCommand ariaMaxDownloadLimitCommand; public DelegateCommand AriaMaxDownloadLimitCommand => ariaMaxDownloadLimitCommand ?? (ariaMaxDownloadLimitCommand = new DelegateCommand(ExecuteAriaMaxDownloadLimitCommand)); /// /// Aria下载单文件速度限制事件 /// /// private void ExecuteAriaMaxDownloadLimitCommand(string parameter) { int downloadLimit = (int)Number.GetInt(parameter); AriaMaxDownloadLimit = downloadLimit; bool isSucceed = SettingsManager.GetInstance().SetAriaMaxDownloadLimit(AriaMaxDownloadLimit); PublishTip(isSucceed); } // 是否开启Aria http代理事件 private DelegateCommand isAriaHttpProxyCommand; public DelegateCommand IsAriaHttpProxyCommand => isAriaHttpProxyCommand ?? (isAriaHttpProxyCommand = new DelegateCommand(ExecuteIsAriaHttpProxyCommand)); /// /// 是否开启Aria http代理事件 /// private void ExecuteIsAriaHttpProxyCommand() { AllowStatus isAriaHttpProxy = IsAriaHttpProxy ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().IsAriaHttpProxy(isAriaHttpProxy); PublishTip(isSucceed); } // Aria的http代理的地址事件 private DelegateCommand ariaHttpProxyCommand; public DelegateCommand AriaHttpProxyCommand => ariaHttpProxyCommand ?? (ariaHttpProxyCommand = new DelegateCommand(ExecuteAriaHttpProxyCommand)); /// /// Aria的http代理的地址事件 /// /// private void ExecuteAriaHttpProxyCommand(string parameter) { bool isSucceed = SettingsManager.GetInstance().SetAriaHttpProxy(parameter); PublishTip(isSucceed); } // Aria的http代理的端口事件 private DelegateCommand ariaHttpProxyPortCommand; public DelegateCommand AriaHttpProxyPortCommand => ariaHttpProxyPortCommand ?? (ariaHttpProxyPortCommand = new DelegateCommand(ExecuteAriaHttpProxyPortCommand)); /// /// Aria的http代理的端口事件 /// /// private void ExecuteAriaHttpProxyPortCommand(string parameter) { int httpProxyPort = (int)Number.GetInt(parameter); AriaHttpProxyPort = httpProxyPort; bool isSucceed = SettingsManager.GetInstance().SetAriaHttpProxyListenPort(AriaHttpProxyPort); PublishTip(isSucceed); } // Aria文件预分配事件 private DelegateCommand ariaFileAllocationsCommand; public DelegateCommand AriaFileAllocationsCommand => ariaFileAllocationsCommand ?? (ariaFileAllocationsCommand = new DelegateCommand(ExecuteAriaFileAllocationsCommand)); /// /// Aria文件预分配事件 /// /// private void ExecuteAriaFileAllocationsCommand(string parameter) { AriaConfigFileAllocation ariaFileAllocation; switch (parameter) { case "NONE": ariaFileAllocation = AriaConfigFileAllocation.NONE; break; case "PREALLOC": ariaFileAllocation = AriaConfigFileAllocation.PREALLOC; break; case "FALLOC": ariaFileAllocation = AriaConfigFileAllocation.FALLOC; break; default: ariaFileAllocation = AriaConfigFileAllocation.PREALLOC; break; } bool isSucceed = SettingsManager.GetInstance().SetAriaFileAllocation(ariaFileAllocation); PublishTip(isSucceed); } #endregion /// /// 发送需要显示的tip /// /// private void PublishTip(bool isSucceed) { if (isOnNavigatedTo) { return; } if (isSucceed) { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingUpdated")); } else { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingFailed")); } } } }