LibreOffice Module setup_native (master) 1
migrateinstallpath.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include "shlxtmsi.hxx"
21#include <algorithm>
22#include <sstream>
23#include <systools/win32/uwinapi.h>
24
25extern "C" __declspec(dllexport) UINT __stdcall MigrateInstallPath(MSIHANDLE handle)
26{
27 std::wstring sInstDir = GetMsiPropertyW(handle, L"INSTALLLOCATION");
28 if (!sInstDir.empty())
29 return ERROR_SUCCESS; // Don't overwrite explicitly set value
30
31 auto RegValue = [](HKEY hRoot, const WCHAR* sKey, const WCHAR* sVal) {
32 std::wstring sResult;
33 WCHAR buf[32767]; // max longpath
34 DWORD bufsize = sizeof(buf); // yes, it is the number of bytes
35 if (RegGetValueW(hRoot, sKey, sVal, RRF_RT_REG_SZ, nullptr, buf, &bufsize) == ERROR_SUCCESS)
36 sResult = buf; // RegGetValueW null-terminates strings
37
38 return sResult;
39 };
40
41 const std::wstring sManufacturer = GetMsiPropertyW( handle, L"Manufacturer" );
42 const std::wstring sDefinedName = GetMsiPropertyW( handle, L"DEFINEDPRODUCT" );
43 const std::wstring sUpdateVersion = GetMsiPropertyW( handle, L"DEFINEDVERSION" );
44 const std::wstring sUpgradeCode = GetMsiPropertyW( handle, L"UpgradeCode" );
45 const std::wstring sBrandPackageVersion = GetMsiPropertyW(handle, L"BRANDPACKAGEVERSION");
46
47 std::wstring sKey = L"Software\\" + sManufacturer + L"\\" + sDefinedName +
48 L"\\" + sUpdateVersion + L"\\" + sUpgradeCode;
49
50 sInstDir = RegValue(HKEY_CURRENT_USER, sKey.c_str(), L"INSTALLLOCATION");
51 if (sInstDir.empty())
52 sInstDir = RegValue(HKEY_LOCAL_MACHINE, sKey.c_str(), L"INSTALLLOCATION");
53 // See #i93032# for layers description
54 if (sInstDir.empty())
55 {
56 sKey = L"Software\\LibreOffice\\Layers\\" + sDefinedName + L"\\" + sBrandPackageVersion;
57 sInstDir = RegValue(HKEY_CURRENT_USER, sKey.c_str(), L"INSTALLLOCATION");
58 }
59 if (sInstDir.empty())
60 {
61 sKey = L"Software\\LibreOffice\\Layers_\\" + sDefinedName + L"\\" + sBrandPackageVersion;
62 sInstDir = RegValue(HKEY_CURRENT_USER, sKey.c_str(), L"INSTALLLOCATION");
63 }
64 if (sInstDir.empty())
65 {
66 sKey = L"Software\\LibreOffice\\Layers\\" + sDefinedName + L"\\" + sBrandPackageVersion;
67 sInstDir = RegValue(HKEY_LOCAL_MACHINE, sKey.c_str(), L"INSTALLLOCATION");
68 }
69 if (sInstDir.empty())
70 {
71 sKey = L"Software\\LibreOffice\\Layers_\\" + sDefinedName + L"\\" + sBrandPackageVersion;
72 sInstDir = RegValue(HKEY_LOCAL_MACHINE, sKey.c_str(), L"INSTALLLOCATION");
73 }
74 if (sInstDir.empty())
75 {
76 std::wistringstream sOlds{ GetMsiPropertyW(handle, L"OLDPRODUCTS") };
77 std::wstring sOld;
78 while (std::getline(sOlds, sOld, L';'))
79 {
80 if (sOld.empty())
81 continue;
82 sKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + sOld;
83 sInstDir = RegValue(HKEY_LOCAL_MACHINE, sKey.c_str(), L"InstallLocation");
84 if (!sInstDir.empty())
85 break;
86 }
87 }
88
89 if (!sInstDir.empty())
90 MsiSetPropertyW(handle, L"INSTALLLOCATION", sInstDir.c_str());
91
92 return ERROR_SUCCESS;
93
94}
95
96/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
__declspec(dllexport) UINT __stdcall MigrateInstallPath(MSIHANDLE handle)
void * RegValue
static std::wstring GetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty)
Definition: shlxtmsi.hxx:29