diff --git a/src/common/VersionHelper.cpp b/src/common/VersionHelper.cpp index 904393135f..92f014d65c 100644 --- a/src/common/VersionHelper.cpp +++ b/src/common/VersionHelper.cpp @@ -27,8 +27,3 @@ VersionHelper::VersionHelper(int major, int minor, int revision) : revision(revision) { } - -bool VersionHelper::operator>(const VersionHelper& rhs) -{ - return std::tie(major, minor, revision) > std::tie(rhs.major, rhs.minor, rhs.revision); -} diff --git a/src/common/VersionHelper.h b/src/common/VersionHelper.h index 19a08b545b..1f575dd507 100644 --- a/src/common/VersionHelper.h +++ b/src/common/VersionHelper.h @@ -1,13 +1,14 @@ #pragma once #include +#include struct VersionHelper { VersionHelper(std::string str); VersionHelper(int major, int minor, int revision); - bool operator>(const VersionHelper& rhs); + auto operator<=>(const VersionHelper&) const = default; int major; int minor; diff --git a/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.cpp b/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.cpp index b00ff2e9b4..a333db4fd4 100644 --- a/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.cpp +++ b/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.cpp @@ -14,6 +14,7 @@ #include #include +#include #include "VersionHelper.h" @@ -23,6 +24,8 @@ namespace const wchar_t* DONT_SHOW_AGAIN_RECORD_REGISTRY_PATH = L"delete_previous_powertoys_confirm"; const wchar_t* USER_AGENT = L"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; const wchar_t* LATEST_RELEASE_ENDPOINT = L"https://api.github.com/repos/microsoft/PowerToys/releases/latest"; + const wchar_t* MSIX_PACKAGE_NAME = L"Microsoft.PowerToys"; + const wchar_t* MSIX_PACKAGE_PUBLISHER = L"CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US"; } namespace localized_strings @@ -125,4 +128,30 @@ std::future> check_for_new_github_relea { co_return std::nullopt; } -} \ No newline at end of file +} + +std::future uninstall_previous_msix_version_async() +{ + winrt::Windows::Management::Deployment::PackageManager package_manager; + + try + { + auto packages = package_manager.FindPackagesForUser({}, MSIX_PACKAGE_NAME, MSIX_PACKAGE_PUBLISHER); + VersionHelper current_version(VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION); + + for (auto package : packages) + { + VersionHelper msix_version(package.Id().Version().Major, package.Id().Version().Minor, package.Id().Version().Revision); + + if (msix_version < current_version) + { + co_await package_manager.RemovePackageAsync(package.Id().FullName()); + co_return true; + } + } + } + catch (...) + { + } + co_return false; +} diff --git a/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.h b/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.h index 8c70e53de3..8a54481766 100644 --- a/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.h +++ b/src/common/msi_to_msix_upgrade_lib/msi_to_msix_upgrade.h @@ -10,6 +10,8 @@ std::wstring get_msi_package_path(); bool uninstall_msi_version(const std::wstring& package_path); bool offer_msi_uninstallation(); +std::future uninstall_previous_msix_version_async(); + struct new_version_download_info { winrt::Windows::Foundation::Uri release_page_uri; diff --git a/src/runner/main.cpp b/src/runner/main.cpp index 73422b175a..17de58ffb3 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -32,6 +32,8 @@ extern "C" IMAGE_DOS_HEADER __ImageBase; namespace localized_strings { const wchar_t MSI_VERSION_IS_ALREADY_RUNNING[] = L"An older version of PowerToys is already running."; + const wchar_t OLDER_MSIX_UNINSTALLED[] = L"An older MSIX version of PowerToys was uninstalled."; + const wchar_t GITHUB_NEW_VERSION_AVAILABLE_OFFER_VISIT[] = L"An update to PowerToys is available. Visit our GitHub page to get "; const wchar_t GITHUB_NEW_VERSION_AGREE[] = L"Visit"; } @@ -180,6 +182,16 @@ int runner(bool isProcessElevated) start_msi_uninstallation_sequence(); } }.detach(); } + else + { + std::thread{[] { + if(uninstall_previous_msix_version_async().get()) + { + notifications::show_toast(localized_strings::OLDER_MSIX_UNINSTALLED); + } + }}.detach(); + + } notifications::register_background_toast_handler();