private async Task <IList <InstalledPackage> > GetPackages(GetPackagesCommand command, CancellationToken cancellationToken, IProgress <ProgressData> progressData) { this.detector.StartListening(); var level = SelfElevationLevel.HighestAvailable; if (command.FindMode == PackageFindMode.AllUsers) { level = SelfElevationLevel.AsAdministrator; } var manager = await this.packageManagerProvider.GetProxyFor(level, cancellationToken).ConfigureAwait(false); PackageFindMode mode; if (command.FindMode.HasValue) { mode = command.FindMode.Value; if (mode == PackageFindMode.Auto) { var isAdmin = await UserHelper.IsAdministratorAsync(cancellationToken); mode = isAdmin ? PackageFindMode.AllUsers : PackageFindMode.CurrentUser; } } else { switch (this.ApplicationState.Packages.Mode) { case PackageContext.CurrentUser: mode = PackageFindMode.CurrentUser; break; case PackageContext.AllUsers: mode = PackageFindMode.AllUsers; break; default: throw new NotSupportedException(); } } var selected = this.ApplicationState.Packages.SelectedPackages.Select(p => p.ManifestLocation).ToArray(); var results = await manager.GetInstalledPackages(mode, cancellationToken, progressData).ConfigureAwait(false); // this.packageListSynchronizer.EnterWriteLock(); this.ApplicationState.Packages.AllPackages.Clear(); this.ApplicationState.Packages.AllPackages.AddRange(results); foreach (var item in this.detector.GetCurrentlyRunning(this.ApplicationState.Packages.AllPackages)) { item.IsRunning = true; } switch (mode) { case PackageFindMode.CurrentUser: this.ApplicationState.Packages.Mode = PackageContext.CurrentUser; break; case PackageFindMode.AllUsers: this.ApplicationState.Packages.Mode = PackageContext.AllUsers; break; } switch (mode) { case PackageFindMode.CurrentUser: this.ApplicationState.Packages.Mode = PackageContext.CurrentUser; break; case PackageFindMode.AllUsers: this.ApplicationState.Packages.Mode = PackageContext.AllUsers; break; default: throw new ArgumentOutOfRangeException(); } await this.Invoke(this, new SelectPackagesCommand(selected), cancellationToken).ConfigureAwait(false); return(results); }
public async Task <IList <InstalledPackage> > Handle(GetPackagesCommand request, CancellationToken cancellationToken) { var context = this.busyManager.Begin(OperationType.PackageLoading); try { var level = SelfElevationLevel.HighestAvailable; if (request.FindMode == PackageFindMode.AllUsers) { level = SelfElevationLevel.AsAdministrator; } var manager = await this.packageQueryProvider.GetProxyFor(level, cancellationToken).ConfigureAwait(false); PackageFindMode mode; if (request.FindMode.HasValue) { mode = request.FindMode.Value; if (mode == PackageFindMode.Auto) { var isAdmin = await UserHelper.IsAdministratorAsync(cancellationToken); mode = isAdmin ? PackageFindMode.AllUsers : PackageFindMode.CurrentUser; } } else { switch (this.commandExecutor.ApplicationState.Packages.Mode) { case PackageContext.CurrentUser: mode = PackageFindMode.CurrentUser; break; case PackageContext.AllUsers: mode = PackageFindMode.AllUsers; break; default: throw new NotSupportedException(); } } var selected = this.commandExecutor.ApplicationState.Packages.SelectedPackages.Select(p => p.ManifestLocation).ToArray(); var results = await manager.GetInstalledPackages(mode, cancellationToken, context).ConfigureAwait(false); // this.packageListSynchronizer.EnterWriteLock(); this.commandExecutor.ApplicationState.Packages.AllPackages.Clear(); this.commandExecutor.ApplicationState.Packages.AllPackages.AddRange(results); var currentlyRunning = new HashSet <string>(this.detector.GetCurrentlyRunningPackageNames(), StringComparer.Ordinal); foreach (var item in this.commandExecutor.ApplicationState.Packages.AllPackages) { item.IsRunning = currentlyRunning.Contains(item.PackageFamilyName); } switch (mode) { case PackageFindMode.CurrentUser: this.commandExecutor.ApplicationState.Packages.Mode = PackageContext.CurrentUser; break; case PackageFindMode.AllUsers: this.commandExecutor.ApplicationState.Packages.Mode = PackageContext.AllUsers; break; } switch (mode) { case PackageFindMode.CurrentUser: this.commandExecutor.ApplicationState.Packages.Mode = PackageContext.CurrentUser; break; case PackageFindMode.AllUsers: this.commandExecutor.ApplicationState.Packages.Mode = PackageContext.AllUsers; break; default: throw new ArgumentOutOfRangeException(); } await this.commandExecutor.Invoke(this, new SelectPackagesCommand(selected), cancellationToken).ConfigureAwait(false); return(results); } finally { this.busyManager.End(context); } }