Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / shellextensions / upgrade.cxx
blob6280bfb5b2e5d75fc8363b5fcd4e616232cfd0a3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #undef UNICODE
30 #undef _UNICODE
32 #define _WIN32_WINDOWS 0x0410
34 #ifdef _MSC_VER
35 #pragma warning(push, 1) /* disable warnings within system headers */
36 #endif
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #include <msiquery.h>
40 #ifdef _MSC_VER
41 #pragma warning(pop)
42 #endif
44 #include <malloc.h>
45 #include <assert.h>
47 #include <tchar.h>
48 #include <string>
50 using namespace std;
52 namespace
54 // The provided GUID must be without surounding '{}'
55 string GetGuidPart(const string& guid, int index)
57 assert((guid.length() == 36) && "No GUID or wrong format!");
58 assert(((index > -1) && (index < 5)) && "Out of range!");
60 if (index == 0) return string(guid.c_str(), 8);
61 if (index == 1) return string(guid.c_str() + 9, 4);
62 if (index == 2) return string(guid.c_str() + 14, 4);
63 if (index == 3) return string(guid.c_str() + 19, 4);
64 if (index == 4) return string(guid.c_str() + 24, 12);
66 return string();
69 void Swap(char* p1, char* p2)
71 char tmp = *p1;
72 *p1 = *p2;
73 *p2 = tmp;
76 string Invert(const string& str)
78 char* buff = reinterpret_cast<char*>(_alloca(str.length()));
79 strncpy(buff, str.c_str(), str.length());
81 char* front = buff;
82 char* back = buff + str.length() - 1;
84 while (front < back)
85 Swap(front++, back--);
87 return string(buff, str.length());
90 // Convert the upgrade code (which is a GUID) according
91 // to the way the windows installer does when writing it
92 // to the registry
93 // The first 8 bytes will be inverted, from the the last
94 // 8 bytes always the nibbles will be inverted for further
95 // details look in the MSDN under compressed registry keys
96 string ConvertGuid(const string& guid)
98 string convertedGuid;
100 string part = GetGuidPart(guid, 0);
101 convertedGuid = Invert(part);
103 part = GetGuidPart(guid, 1);
104 convertedGuid += Invert(part);
106 part = GetGuidPart(guid, 2);
107 convertedGuid += Invert(part);
109 part = GetGuidPart(guid, 3);
110 convertedGuid += Invert(string(part.c_str(), 2));
111 convertedGuid += Invert(string(part.c_str() + 2, 2));
113 part = GetGuidPart(guid, 4);
114 int pos = 0;
115 for (int i = 0; i < 6; i++)
117 convertedGuid += Invert(string(part.c_str() + pos, 2));
118 pos += 2;
120 return convertedGuid;
123 string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
125 string result;
126 TCHAR szDummy[1] = TEXT("");
127 DWORD nChars = 0;
129 if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
131 DWORD nBytes = ++nChars * sizeof(TCHAR);
132 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
133 ZeroMemory( buffer, nBytes );
134 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
135 result = buffer;
137 return result;
140 inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
142 return (GetMsiProperty(handle, sProperty).length() > 0);
145 inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
147 MsiSetProperty(handle, sProperty.c_str(), NULL);
150 inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty)
152 MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
155 bool RegistryKeyHasUpgradeSubKey(
156 HKEY hRootKey, const string& regKey, const string& upgradeKey)
158 HKEY hKey;
159 if (RegOpenKey(hRootKey, regKey.c_str(), &hKey) == ERROR_SUCCESS)
161 DWORD nSubKeys;
162 DWORD lLongestSubKey;
164 if (RegQueryInfoKey(
165 hKey, NULL, NULL, NULL, &nSubKeys, &lLongestSubKey, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
167 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(lLongestSubKey + 1));
169 for (DWORD i = 0; i < nSubKeys; i++)
171 LONG ret = RegEnumKey(hKey, i, buffer, lLongestSubKey + 1);
172 if ((ret == ERROR_SUCCESS) && (buffer == upgradeKey))
173 return true;
177 return false;
179 } // namespace
181 extern "C" UINT __stdcall SetProductInstallMode(MSIHANDLE handle)
183 string upgradeCode = GetMsiProperty(handle, TEXT("UpgradeCode"));
184 upgradeCode = ConvertGuid(string(upgradeCode.c_str() + 1, upgradeCode.length() - 2));
186 //MessageBox(NULL, upgradeCode.c_str(), TEXT("Debug"), MB_OK);
188 if (RegistryKeyHasUpgradeSubKey(
189 HKEY_CURRENT_USER,
190 TEXT("Software\\Microsoft\\Installer\\UpgradeCodes"),
191 upgradeCode) && IsSetMsiProperty(handle, TEXT("ALLUSERS")))
193 UnsetMsiProperty(handle, TEXT("ALLUSERS"));
194 //MessageBox(NULL, "ALLUSERS removed", "DEBUG", MB_OK);
196 else if (RegistryKeyHasUpgradeSubKey(
197 HKEY_LOCAL_MACHINE,
198 TEXT("Software\\Classes\\Installer\\UpgradeCodes"),
199 upgradeCode) && !IsSetMsiProperty(handle, TEXT("ALLUSERS")))
201 SetMsiProperty(handle, TEXT("ALLUSERS"));
202 //MessageBox(NULL, "ALLUSERS set", "DEBUG", MB_OK);
204 return ERROR_SUCCESS;
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */