/ src / modules / launcher / Plugins / Microsoft.Plugin.Program / Programs / PackagemanagerWrapper.cs
PackagemanagerWrapper.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Collections.Generic;
 7  using System.Linq;
 8  using System.Security.Principal;
 9  
10  using Windows.Management.Deployment;
11  using Wox.Plugin.Logger;
12  
13  using Package = Windows.ApplicationModel.Package;
14  
15  namespace Microsoft.Plugin.Program.Programs
16  {
17      public class PackageManagerWrapper : IPackageManager
18      {
19          private readonly PackageManager _packageManager;
20  
21          public PackageManagerWrapper()
22          {
23              _packageManager = new PackageManager();
24          }
25  
26          public IEnumerable<IPackage> FindPackagesForCurrentUser()
27          {
28              var user = WindowsIdentity.GetCurrent().User;
29  
30              return user != null
31                  ? _packageManager.FindPackagesForUser(user.Value).Select(TryGetWrapperFromPackage).Where(package => package != null)
32                  : Enumerable.Empty<IPackage>();
33          }
34  
35          private static PackageWrapper TryGetWrapperFromPackage(Package package)
36          {
37              try
38              {
39                  return PackageWrapper.GetWrapperFromPackage(package);
40              }
41              catch (Exception e)
42              {
43                  Log.Error(e.Message, typeof(PackageManagerWrapper));
44              }
45  
46              return null;
47          }
48      }
49  }