public async Task <List <int> > FindFile(string file_name, bool withDeps = true) { Package package = InstalledPackages.FirstOrDefault(x => x.FilesContained.Contains(file_name)); if (package != default) { return new List <int>() { package.PackageId } } ; lock (CachedPackages) package = CachedPackages.FirstOrDefault(x => x.FilesContained.Contains(file_name)); if (package != default) { return new List <int>() { package.PackageId } } ; Package onlinePackage = await WebWrapper.SearchForFile(file_name); if (onlinePackage != null && onlinePackage.PackageId > 0) { lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == onlinePackage.PackageId)) { CachedPackages.Add(onlinePackage); } } HashSet <int> dependencyPkgIds = new HashSet <int>(); if (withDeps) { await GetDependencies(new HashSet <int>() { onlinePackage.PackageId }, dependencyPkgIds); } return(new List <int>() { onlinePackage.PackageId }.Union(dependencyPkgIds).ToList()); } return(new List <int>()); }
public async Task ReceiveMSMQ() { string queueFile = Path.Combine(Path.GetTempPath(), "DLS.queue"); HashSet <string> queuedPkgs = File.Exists(queueFile) ? File.ReadAllText(queueFile).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet() : new HashSet <string>(); if (queuedPkgs.Count > 0) { MainWindow.Dispatcher.Invoke(() => { MainWindow.Activate(); }); if (await CheckLogin(1) < 0 || App.IsDownloading) { File.WriteAllText(queueFile, string.Empty); return; } int idToDownload = Convert.ToInt32(queuedPkgs.PopOne()); if (!InstalledPackages.Exists(x => x.PackageId == idToDownload)) { Task.Run(async() => { Package packageToDownload = await WebWrapper.GetPackage(idToDownload); lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == packageToDownload.PackageId)) { CachedPackages.Add(packageToDownload); } } if (packageToDownload.IsPaid) { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download package", Content = "This is paid package. Paid packages cannot be downloaded through this app.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); return; } HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { packageToDownload.PackageId }, depsPkgs); HashSet <int> packageIds = new HashSet <int>() { packageToDownload.PackageId }.Union(depsPkgs).ToHashSet(); if (packageIds.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); await MainWindow.DownloadDialog.DownloadPackages(packageIds, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter); App.IsDownloading = false; } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download packages", Content = "An error ocured when trying to install package.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); }).Start(); } }).Wait(); } else { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download package", Content = "This package is already downloaded.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); } File.WriteAllText(queueFile, string.Join(",", queuedPkgs)); await ReceiveMSMQ(); } }
public async Task ReceiveMSMQ() { string queueFile = Path.Combine(Path.GetTempPath(), "DLS.queue"); HashSet <string> queuedPkgs = File.Exists(queueFile) ? File.ReadAllText(queueFile).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet() : new HashSet <string>(); if (queuedPkgs.Count > 0) { MainWindow.Dispatcher.Invoke(() => { MainWindow.Activate(); }); if (!await Utils.CheckLogin(async delegate { await ReceiveMSMQ(); MSMQRunning = false; }, MainWindow, ApiUrl) || App.IsDownloading) { //File.WriteAllText(queueFile, string.Empty); return; } int idToDownload = Convert.ToInt32(queuedPkgs.PopOne()); if (!InstalledPackages.Exists(x => x.PackageId == idToDownload)) { Task.Run(async() => { Package packageToDownload = await WebWrapper.GetPackage(idToDownload); lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == packageToDownload.PackageId)) { CachedPackages.Add(packageToDownload); } } if (packageToDownload.IsPaid) { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.PaidPackageFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); return; } HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { packageToDownload.PackageId }, depsPkgs); HashSet <int> packageIds = new HashSet <int>() { packageToDownload.PackageId }.Union(depsPkgs).ToHashSet(); if (packageIds.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); await MainWindow.DownloadDialog.DownloadPackages(packageIds, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter); App.IsDownloading = false; } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.InstalFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); }).Start(); } }).Wait(); } else { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.AlreadyInstalFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); } File.WriteAllText(queueFile, string.Join(",", queuedPkgs)); await ReceiveMSMQ(); } }