winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

CatalogPackage.cpp (8282B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include <mutex>
      5 #include <winget/RepositorySource.h>
      6 #include "CatalogPackage.h"
      7 #include "CatalogPackage.g.cpp"
      8 #include "PackageCatalog.h"
      9 #include "PackageVersionInfo.h"
     10 #include "PackageVersionId.h"
     11 #include "PackageInstallerInstalledStatus.h"
     12 #include "CheckInstalledStatusResult.h"
     13 #include <ComContext.h>
     14 #include <wil\cppwinrt_wrl.h>
     15 #include <winget/PinningData.h>
     16 #include <winget/PackageVersionSelection.h>
     17 
     18 
     19 namespace winrt::Microsoft::Management::Deployment::implementation
     20 {
     21     void CatalogPackage::Initialize(
     22         ::AppInstaller::Repository::Source source,
     23         std::shared_ptr<::AppInstaller::Repository::ICompositePackage> package)
     24     {
     25         m_source = std::move(source);
     26         m_package = std::move(package);
     27     }
     28     hstring CatalogPackage::Id()
     29     {
     30         return winrt::to_hstring(m_package->GetProperty(::AppInstaller::Repository::PackageProperty::Id).get());
     31     }
     32     hstring CatalogPackage::Name()
     33     {
     34         return winrt::to_hstring(m_package->GetProperty(::AppInstaller::Repository::PackageProperty::Name));
     35     }
     36     Microsoft::Management::Deployment::PackageVersionInfo CatalogPackage::InstalledVersion()
     37     {
     38         std::call_once(m_installedVersionOnceFlag,
     39             [&]()
     40             {
     41                 std::shared_ptr<::AppInstaller::Repository::IPackageVersion> installedVersion = GetInstalledVersion(m_package);
     42                 if (installedVersion)
     43                 {
     44                     auto installedVersionImpl = winrt::make_self<wil::details::module_count_wrapper<
     45                         winrt::Microsoft::Management::Deployment::implementation::PackageVersionInfo>>();
     46                     installedVersionImpl->Initialize(std::move(installedVersion));
     47                     m_installedVersion = *installedVersionImpl;
     48                 }
     49             });
     50         return m_installedVersion;
     51     }
     52     Windows::Foundation::Collections::IVectorView<Microsoft::Management::Deployment::PackageVersionId> CatalogPackage::AvailableVersions()
     53     {
     54         std::call_once(m_availableVersionsOnceFlag,
     55             [&]()
     56             {
     57                 // Vector hasn't been populated yet.
     58                 for (auto const& versionKey : ::AppInstaller::Repository::GetAllAvailableVersions(m_package)->GetVersionKeys())
     59                 {
     60                     auto packageVersionId = winrt::make_self<wil::details::module_count_wrapper<
     61                         winrt::Microsoft::Management::Deployment::implementation::PackageVersionId>>();
     62                     packageVersionId->Initialize(versionKey);
     63                     m_availableVersions.Append(*packageVersionId);
     64                 }
     65             });
     66         return m_availableVersions.GetView();
     67     }
     68 
     69     void CatalogPackage::InitializeLatestApplicableVersion()
     70     {
     71         std::call_once(m_latestApplicableVersionOnceFlag,
     72             [&]()
     73             {
     74                 auto data = AppInstaller::Repository::GetLatestApplicableVersion(m_package);
     75 
     76                 m_updateAvailable = data.UpdateAvailable;
     77 
     78                 if (data.LatestApplicableVersion)
     79                 {
     80                     // DefaultInstallVersion hasn't been created yet, create and populate it.
     81                     // DefaultInstallVersion is the latest applicable version of the internal package object.
     82                     auto latestVersionImpl = winrt::make_self<wil::details::module_count_wrapper<
     83                         winrt::Microsoft::Management::Deployment::implementation::PackageVersionInfo>>();
     84                     latestVersionImpl->Initialize(std::move(data.LatestApplicableVersion));
     85                     m_latestApplicableVersion = *latestVersionImpl;
     86                 }
     87             });
     88     }
     89 
     90     Microsoft::Management::Deployment::PackageVersionInfo CatalogPackage::DefaultInstallVersion()
     91     {
     92         InitializeLatestApplicableVersion();
     93 
     94         if (m_latestApplicableVersion)
     95         {
     96             return m_latestApplicableVersion;
     97         }
     98         else if (AvailableVersions().Size() > 0)
     99         {
    100             return GetPackageVersionInfo(m_availableVersions.GetAt(0));
    101         }
    102         else
    103         {
    104             return nullptr;
    105         }
    106     }
    107 
    108     Microsoft::Management::Deployment::PackageVersionInfo CatalogPackage::GetPackageVersionInfo(Microsoft::Management::Deployment::PackageVersionId const& versionKey)
    109     {
    110         winrt::Microsoft::Management::Deployment::PackageVersionInfo packageVersionInfo{ nullptr };
    111 
    112         ::AppInstaller::Repository::PackageVersionKey internalVersionKey(winrt::to_string(versionKey.PackageCatalogId()), winrt::to_string(versionKey.Version()), winrt::to_string(versionKey.Channel()));
    113         std::shared_ptr<::AppInstaller::Repository::IPackageVersion> availableVersion =
    114             ::AppInstaller::Repository::GetAllAvailableVersions(m_package)->GetVersion(internalVersionKey);
    115         if (availableVersion)
    116         {
    117             auto packageVersionInfoImpl = winrt::make_self<wil::details::module_count_wrapper<
    118                 winrt::Microsoft::Management::Deployment::implementation::PackageVersionInfo>>();
    119             packageVersionInfoImpl->Initialize(std::move(availableVersion));
    120             packageVersionInfo =*packageVersionInfoImpl;
    121         }
    122         return packageVersionInfo;
    123     }
    124 
    125     bool CatalogPackage::IsUpdateAvailable()
    126     {
    127         InitializeLatestApplicableVersion();
    128         return m_updateAvailable;
    129     }
    130 
    131     Windows::Foundation::IAsyncOperation<winrt::Microsoft::Management::Deployment::CheckInstalledStatusResult> CatalogPackage::CheckInstalledStatusAsync(
    132         Microsoft::Management::Deployment::InstalledStatusType checkTypes)
    133     {
    134         co_return CheckInstalledStatus(checkTypes);
    135     }
    136     Microsoft::Management::Deployment::CheckInstalledStatusResult CatalogPackage::CheckInstalledStatus(
    137         Microsoft::Management::Deployment::InstalledStatusType checkTypes)
    138     {
    139         Microsoft::Management::Deployment::CheckInstalledStatusResultStatus status = winrt::Microsoft::Management::Deployment::CheckInstalledStatusResultStatus::Ok;
    140         Windows::Foundation::Collections::IVector<Microsoft::Management::Deployment::PackageInstallerInstalledStatus> installedStatus{
    141             winrt::single_threaded_vector<Microsoft::Management::Deployment::PackageInstallerInstalledStatus>() };
    142 
    143         try
    144         {
    145             auto checkResult = ::AppInstaller::Repository::CheckPackageInstalledStatus(m_package, static_cast<::AppInstaller::Repository::InstalledStatusType>(checkTypes));
    146 
    147             // Build the result object from the checkResult
    148             for (auto const& entry : checkResult)
    149             {
    150                 auto checkInstallerResult = winrt::make_self<wil::details::module_count_wrapper<
    151                     winrt::Microsoft::Management::Deployment::implementation::PackageInstallerInstalledStatus>>();
    152                 checkInstallerResult->Initialize(entry);
    153 
    154                 installedStatus.Append(*checkInstallerResult);
    155             }
    156         }
    157         catch (...)
    158         {
    159             status = winrt::Microsoft::Management::Deployment::CheckInstalledStatusResultStatus::InternalError;
    160         }
    161 
    162         auto checkInstalledStatusResult = winrt::make_self<wil::details::module_count_wrapper<
    163             winrt::Microsoft::Management::Deployment::implementation::CheckInstalledStatusResult>>();
    164         checkInstalledStatusResult->Initialize(status, installedStatus);
    165         return *checkInstalledStatusResult;
    166     }
    167     winrt::Windows::Foundation::IAsyncOperation<winrt::Microsoft::Management::Deployment::CheckInstalledStatusResult> CatalogPackage::CheckInstalledStatusAsync()
    168     {
    169         co_return CheckInstalledStatus(InstalledStatusType::AllChecks);
    170     }
    171     winrt::Microsoft::Management::Deployment::CheckInstalledStatusResult CatalogPackage::CheckInstalledStatus()
    172     {
    173         return CheckInstalledStatus(InstalledStatusType::AllChecks);
    174     }
    175     std::shared_ptr<::AppInstaller::Repository::ICompositePackage> CatalogPackage::GetRepositoryPackage()
    176     {
    177         return m_package;
    178     }
    179 }