1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
25cdf0e10cSrcweir #ifdef _MSC_VER
26cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
29cdf0e10cSrcweir #include <windows.h>
30cdf0e10cSrcweir #include <msiquery.h>
31cdf0e10cSrcweir #ifdef _MSC_VER
32cdf0e10cSrcweir #pragma warning(pop)
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <malloc.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #ifdef UNICODE
38cdf0e10cSrcweir #define _UNICODE
39cdf0e10cSrcweir #define _tstring	wstring
40cdf0e10cSrcweir #else
41cdf0e10cSrcweir #define _tstring	string
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include <tchar.h>
44cdf0e10cSrcweir #include <string>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace std;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace
49cdf0e10cSrcweir {
GetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty)50cdf0e10cSrcweir 	std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
51cdf0e10cSrcweir 	{
52cdf0e10cSrcweir 		std::_tstring	result;
53cdf0e10cSrcweir 		TCHAR	szDummy[1] = TEXT("");
54cdf0e10cSrcweir 		DWORD	nChars = 0;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 		if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
57cdf0e10cSrcweir 		{
58cdf0e10cSrcweir 			DWORD nBytes = ++nChars * sizeof(TCHAR);
59cdf0e10cSrcweir 			LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
60cdf0e10cSrcweir 			ZeroMemory( buffer, nBytes );
61cdf0e10cSrcweir 			MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
62cdf0e10cSrcweir 			result = buffer;
63cdf0e10cSrcweir 		}
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 		return	result;
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir } // namespace
68cdf0e10cSrcweir 
CompleteInstallPath(MSIHANDLE handle)69cdf0e10cSrcweir extern "C" UINT __stdcall CompleteInstallPath( MSIHANDLE handle )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	// This CustomAction is necessary for updates from OOo 3.0, OOo 3.1 and OOo 3.2 to versions
72cdf0e10cSrcweir 	// OOo 3.3 or later. This is caused by a change of INSTALLLOCATION, that starting with OOo 3.3
73cdf0e10cSrcweir 	// contains the name of the product again (instead of only "c:\program files"). Unfortunately
74cdf0e10cSrcweir 	// this causes in an update installation, that INSTALLLOCATION is set to "c:\program files",
75cdf0e10cSrcweir 	// so that in an OOo 3.3 or later, the directory "program" or "share" are directly created
76cdf0e10cSrcweir 	// below "c:\program files".
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	TCHAR	szValue[8192];
79cdf0e10cSrcweir 	DWORD	nValueSize = sizeof(szValue);
80cdf0e10cSrcweir 	HKEY	hKey;
81cdf0e10cSrcweir 	std::_tstring	sInstDir;
82cdf0e10cSrcweir 	std::_tstring	mystr;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	// Reading property OFFICEDIRHOSTNAME_, that contains the part of the path behind
85cdf0e10cSrcweir 	// the program files folder.
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	std::_tstring	sInstallLocation = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
88cdf0e10cSrcweir 	std::_tstring	sOfficeDirHostname = GetMsiProperty( handle, TEXT("OFFICEDIRHOSTNAME_") );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	// If sInstallLocation ends with (contains) the string sOfficeDirHostname,
91cdf0e10cSrcweir 	// INSTALLLOCATION is good and nothing has to be done here.
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	bool pathCompletionRequired = true;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	if ( _tcsstr( sInstallLocation.c_str(), sOfficeDirHostname.c_str() ) )
96cdf0e10cSrcweir 	{
97cdf0e10cSrcweir 		pathCompletionRequired = false;  // nothing to do
98cdf0e10cSrcweir 		// mystr = "Nothing to do, officedir is included into installlocation";
99cdf0e10cSrcweir 		// MessageBox( NULL, mystr.c_str(), "It is part of installlocation", MB_OK );
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	// If the path INSTALLLOCATION does not end with this string, INSTALLLOCATION is maybe
103cdf0e10cSrcweir 	// transfered from an OOo 3.0, OOo 3.1 and OOo 3.2 and need to be changed therefore.
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	if ( pathCompletionRequired )
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		std::_tstring	sManufacturer = GetMsiProperty( handle, TEXT("Manufacturer") );
108cdf0e10cSrcweir 		std::_tstring	sDefinedName = GetMsiProperty( handle, TEXT("DEFINEDPRODUCT") );
109cdf0e10cSrcweir 		std::_tstring	sUpgradeCode = GetMsiProperty( handle, TEXT("UpgradeCode") );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 		// sUpdateVersion can be "3.0", "3.1" or "3.2"
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 		std::_tstring	sProductKey30 = "Software\\" + sManufacturer + "\\" + sDefinedName +
114cdf0e10cSrcweir 											"\\" + "3.0" + "\\" + sUpgradeCode;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		std::_tstring	sProductKey31 = "Software\\" + sManufacturer + "\\" + sDefinedName +
117cdf0e10cSrcweir 											"\\" + "3.1" + "\\" + sUpgradeCode;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 		std::_tstring	sProductKey32 = "Software\\" + sManufacturer + "\\" + sDefinedName +
120cdf0e10cSrcweir 											"\\" + "3.2" + "\\" + sUpgradeCode;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 		// mystr = "ProductKey: " + sProductKey;
123cdf0e10cSrcweir 		// MessageBox( NULL, mystr.c_str(), "ProductKey", MB_OK );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		// mystr = "Checking registry";
126cdf0e10cSrcweir 		// MessageBox( NULL, mystr.c_str(), "registry search", MB_OK );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		bool oldVersionExists = false;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 		if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey30.c_str(), &hKey ) )
131cdf0e10cSrcweir 		{
132cdf0e10cSrcweir 			oldVersionExists = true;
133cdf0e10cSrcweir 			RegCloseKey( hKey );
134cdf0e10cSrcweir 		}
135cdf0e10cSrcweir 		else if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey31.c_str(), &hKey ) )
136cdf0e10cSrcweir 		{
137cdf0e10cSrcweir 			oldVersionExists = true;
138cdf0e10cSrcweir 			RegCloseKey( hKey );
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir 		else if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey32.c_str(), &hKey ) )
141cdf0e10cSrcweir 		{
142cdf0e10cSrcweir 			oldVersionExists = true;
143cdf0e10cSrcweir 			RegCloseKey( hKey );
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 		else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey30.c_str(), &hKey ) )
146cdf0e10cSrcweir 		{
147cdf0e10cSrcweir 			oldVersionExists = true;
148cdf0e10cSrcweir 			RegCloseKey( hKey );
149cdf0e10cSrcweir 		}
150cdf0e10cSrcweir 		else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey31.c_str(), &hKey ) )
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			oldVersionExists = true;
153cdf0e10cSrcweir 			RegCloseKey( hKey );
154cdf0e10cSrcweir 		}
155cdf0e10cSrcweir 		else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey32.c_str(), &hKey ) )
156cdf0e10cSrcweir 		{
157cdf0e10cSrcweir 			oldVersionExists = true;
158cdf0e10cSrcweir 			RegCloseKey( hKey );
159cdf0e10cSrcweir 		}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 		if ( oldVersionExists )
162cdf0e10cSrcweir 		{
163cdf0e10cSrcweir 			// Adding the new path content sOfficeDirHostname
164cdf0e10cSrcweir 			sInstallLocation = sInstallLocation + sOfficeDirHostname;
165cdf0e10cSrcweir 			// Setting the new property value
166cdf0e10cSrcweir 			MsiSetProperty(handle, TEXT("INSTALLLOCATION"), sInstallLocation.c_str());
167cdf0e10cSrcweir 			// mystr = "Setting path to: " + sInstallLocation;
168cdf0e10cSrcweir 			// MessageBox( NULL, mystr.c_str(), "sInstallLocation", MB_OK );
169cdf0e10cSrcweir 		}
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	// mystr = "Ending with INSTALLLOCATION: " + sInstallLocation;
173cdf0e10cSrcweir 	// MessageBox( NULL, mystr.c_str(), "END", MB_OK );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	return ERROR_SUCCESS;
176cdf0e10cSrcweir }
177