Revision | 29473 (tree) |
---|---|
Time | 2022-11-16 02:43:20 |
Author | stefankueng |
Add a button to register the sparse package for the current user
@@ -1,6 +1,6 @@ | ||
1 | 1 | // TortoiseSVN - a Windows shell extension for easy version control |
2 | 2 | |
3 | -// Copyright (C) 2021 - TortoiseSVN | |
3 | +// Copyright (C) 2021-2022 - TortoiseSVN | |
4 | 4 | |
5 | 5 | // This program is free software; you can redistribute it and/or |
6 | 6 | // modify it under the terms of the GNU General Public License |
@@ -22,6 +22,17 @@ | ||
22 | 22 | #include "LoadIconEx.h" |
23 | 23 | #include "SetWin11ContextMenu.h" |
24 | 24 | |
25 | +#include <winrt/Windows.Management.Deployment.h> | |
26 | +#include <winrt/Windows.Foundation.Collections.h> | |
27 | +#include <winrt/Windows.ApplicationModel.h> | |
28 | + | |
29 | +#include "../../Utils/PathUtils.h" | |
30 | +#include "../../Utils/MiscUI/ProgressDlg.h" | |
31 | +using namespace winrt::Windows::Foundation; | |
32 | +using namespace winrt::Windows::Management::Deployment; | |
33 | + | |
34 | +#pragma comment(lib, "windowsapp.lib") | |
35 | + | |
25 | 36 | IMPLEMENT_DYNAMIC(CSetWin11ContextMenu, ISettingsPropPage) |
26 | 37 | CSetWin11ContextMenu::CSetWin11ContextMenu() |
27 | 38 | : ISettingsPropPage(CSetWin11ContextMenu::IDD) |
@@ -44,6 +55,7 @@ | ||
44 | 55 | |
45 | 56 | BEGIN_MESSAGE_MAP(CSetWin11ContextMenu, ISettingsPropPage) |
46 | 57 | ON_NOTIFY(LVN_ITEMCHANGED, IDC_MENULIST, OnLvnItemchangedMenulist) |
58 | + ON_BN_CLICKED(IDC_REGISTER, &CSetWin11ContextMenu::OnBnClickedRegister) | |
47 | 59 | END_MESSAGE_MAP() |
48 | 60 | |
49 | 61 | BOOL CSetWin11ContextMenu::OnInitDialog() |
@@ -126,3 +138,68 @@ | ||
126 | 138 | } |
127 | 139 | *pResult = 0; |
128 | 140 | } |
141 | + | |
142 | +void CSetWin11ContextMenu::OnBnClickedRegister() | |
143 | +{ | |
144 | + CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); | |
145 | + OnOutOfScope(CoUninitialize()); | |
146 | + PackageManager manager; | |
147 | + | |
148 | + // first unregister if already registered | |
149 | + Collections::IIterable<winrt::Windows::ApplicationModel::Package> packages; | |
150 | + try | |
151 | + { | |
152 | + packages = manager.FindPackagesForUser(L""); | |
153 | + } | |
154 | + catch (winrt::hresult_error const& ex) | |
155 | + { | |
156 | + std::wstring error = L"FindPackagesForUser failed (Errorcode: "; | |
157 | + error += std::to_wstring(ex.code().value); | |
158 | + error += L"):\n"; | |
159 | + error += ex.message(); | |
160 | + MessageBox(error.c_str(), nullptr, MB_ICONERROR); | |
161 | + return; | |
162 | + } | |
163 | + | |
164 | + for (const auto& package : packages) | |
165 | + { | |
166 | + if (package.Id().Name() != L"3A48D7FC-AEE2-4CBC-91D1-0007951B8006") | |
167 | + continue; | |
168 | + | |
169 | + winrt::hstring fullName = package.Id().FullName(); | |
170 | + auto deploymentOperation = manager.RemovePackageAsync(fullName, RemovalOptions::None); | |
171 | + auto deployResult = deploymentOperation.get(); | |
172 | + if (SUCCEEDED(deployResult.ExtendedErrorCode())) | |
173 | + break; | |
174 | + | |
175 | + // Undeployment failed | |
176 | + std::wstring error = L"RemovePackageAsync failed (Errorcode: "; | |
177 | + error += std::to_wstring(deployResult.ExtendedErrorCode()); | |
178 | + error += L"):\n"; | |
179 | + error += deployResult.ErrorText(); | |
180 | + MessageBox(error.c_str(), nullptr, MB_ICONERROR); | |
181 | + return; | |
182 | + } | |
183 | + | |
184 | + // now register the package | |
185 | + auto appDir = CPathUtils::GetAppParentDirectory(); | |
186 | + Uri externalUri(static_cast<LPCWSTR>(appDir)); | |
187 | + auto packagePath = appDir + L"bin\\package.msix"; | |
188 | + Uri packageUri(static_cast<LPCWSTR>(packagePath)); | |
189 | + AddPackageOptions options; | |
190 | + options.ExternalLocationUri(externalUri); | |
191 | + auto deploymentOperation = manager.AddPackageByUriAsync(packageUri, options); | |
192 | + | |
193 | + auto deployResult = deploymentOperation.get(); | |
194 | + | |
195 | + if (!SUCCEEDED(deployResult.ExtendedErrorCode())) | |
196 | + { | |
197 | + std::wstring error = L"AddPackageByUriAsync failed (Errorcode: "; | |
198 | + error += std::to_wstring(deployResult.ExtendedErrorCode()); | |
199 | + error += L"):\n"; | |
200 | + error += deployResult.ErrorText(); | |
201 | + MessageBox(error.c_str(), nullptr, MB_ICONERROR); | |
202 | + return; | |
203 | + } | |
204 | + MessageBox(CString(MAKEINTRESOURCE(IDS_PACKAGE_REGISTERED))); | |
205 | +} |
@@ -1,6 +1,6 @@ | ||
1 | 1 | // TortoiseSVN - a Windows shell extension for easy version control |
2 | 2 | |
3 | -// Copyright (C) 2021 - TortoiseSVN | |
3 | +// Copyright (C) 2021-2022 - TortoiseSVN | |
4 | 4 | |
5 | 5 | // This program is free software; you can redistribute it and/or |
6 | 6 | // modify it under the terms of the GNU General Public License |
@@ -61,4 +61,7 @@ | ||
61 | 61 | BOOL m_bModified; |
62 | 62 | TSVNContextMenuEntries m_topMenu; |
63 | 63 | bool m_bBlock; |
64 | + | |
65 | +public: | |
66 | + afx_msg void OnBnClickedRegister(); | |
64 | 67 | }; |