1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #undef UNICODE
29*cdf0e10cSrcweir #undef _UNICODE
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifdef _MSC_VER
34*cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
37*cdf0e10cSrcweir #include <windows.h>
38*cdf0e10cSrcweir #include <msiquery.h>
39*cdf0e10cSrcweir #ifdef _MSC_VER
40*cdf0e10cSrcweir #pragma warning(pop)
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <malloc.h>
44*cdf0e10cSrcweir #include <assert.h>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <tchar.h>
47*cdf0e10cSrcweir #include <string>
48*cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <../tools/seterror.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir using namespace std;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir namespace
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir         string  result;
59*cdf0e10cSrcweir         TCHAR   szDummy[1] = TEXT("");
60*cdf0e10cSrcweir         DWORD   nChars = 0;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir         if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
63*cdf0e10cSrcweir         {
64*cdf0e10cSrcweir             DWORD nBytes = ++nChars * sizeof(TCHAR);
65*cdf0e10cSrcweir             LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
66*cdf0e10cSrcweir             ZeroMemory( buffer, nBytes );
67*cdf0e10cSrcweir             MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
68*cdf0e10cSrcweir             result = buffer;
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir         return result;
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         return (GetMsiProperty(handle, sProperty).length() > 0);
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         MsiSetProperty(handle, sProperty.c_str(), NULL);
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string&)
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir         MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
86*cdf0e10cSrcweir     }
87*cdf0e10cSrcweir } // namespace
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir extern "C" UINT __stdcall GetUserInstallMode(MSIHANDLE handle)
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir     string sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     // MessageBox(NULL, sOfficeInstallPath.c_str(), "DEBUG", MB_OK);
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     // unsetting all properties
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("INVALIDDIRECTORY") );
98*cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("ISWRONGPRODUCT") );
99*cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("PATCHISOLDER") );
100*cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("ALLUSERS") );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     // 1. Searching for "ProductCode" in setup.ini
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     string sSetupiniPath = sOfficeInstallPath + TEXT("program\\setup.ini");
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     TCHAR szValue[32767];
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     GetPrivateProfileString(
109*cdf0e10cSrcweir         TEXT("Bootstrap"),
110*cdf0e10cSrcweir         TEXT("ProductCode"),
111*cdf0e10cSrcweir         TEXT("INVALIDDIRECTORY"),
112*cdf0e10cSrcweir         szValue,
113*cdf0e10cSrcweir         elementsof(szValue),
114*cdf0e10cSrcweir         sSetupiniPath.c_str()
115*cdf0e10cSrcweir         );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     if ( !_tcsicmp( szValue, TEXT("INVALIDDIRECTORY") ) )
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir     	// No setup.ini or no "ProductCode" in setup.ini. This is an invalid directory.
120*cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("INVALIDDIRECTORY"), TEXT("YES") );
121*cdf0e10cSrcweir         // MessageBox(NULL, "INVALIDDIRECTORY set, no setup.ini or ProductCode in setup.ini.", "DEBUG", MB_OK);
122*cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_INVALIDDIRECTORY );
123*cdf0e10cSrcweir         return ERROR_SUCCESS;
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     // 2. Comparing first three characters of "PRODUCTMAJOR" from property table and "buildid" from InfoFile
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     szValue[0] = '\0';
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     GetPrivateProfileString(
131*cdf0e10cSrcweir         TEXT("Bootstrap"),
132*cdf0e10cSrcweir         TEXT("buildid"),
133*cdf0e10cSrcweir         TEXT("ISWRONGPRODUCT"),
134*cdf0e10cSrcweir         szValue,
135*cdf0e10cSrcweir         elementsof(szValue),
136*cdf0e10cSrcweir         sSetupiniPath.c_str()
137*cdf0e10cSrcweir         );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     if ( !_tcsicmp( szValue, TEXT("ISWRONGPRODUCT") ) )
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("ISWRONGPRODUCT"), TEXT("YES") );
142*cdf0e10cSrcweir         // MessageBox(NULL, "ISWRONGPRODUCT 1 set after searching buildid", "DEBUG", MB_OK);
143*cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_ISWRONGPRODUCT );
144*cdf0e10cSrcweir         return ERROR_SUCCESS;
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     string ProductMajor = GetMsiProperty(handle, TEXT("PRODUCTMAJOR"));
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     // Comparing the first three characters, for example "680"
150*cdf0e10cSrcweir     // If not equal, this version is not suited for patch or language pack
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     if (_tcsnicmp(ProductMajor.c_str(), szValue, 3))
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("ISWRONGPRODUCT"), TEXT("YES") );
155*cdf0e10cSrcweir         // MessageBox(NULL, "ISWRONGPRODUCT 2 set after searching PRODUCTMAJOR", "DEBUG", MB_OK);
156*cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_ISWRONGPRODUCT );
157*cdf0e10cSrcweir         return ERROR_SUCCESS;
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     // 3. Only for patch: Comparing "PRODUCTMINOR from property table and "ProductMinor" from InfoFile
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     string isPatch = GetMsiProperty(handle, TEXT("ISPATCH"));
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     if (isPatch=="1")
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         string ProductMinor = GetMsiProperty(handle, TEXT("PRODUCTBUILDID"));
167*cdf0e10cSrcweir         int PatchProductMinor = atoi(ProductMinor.c_str());
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         szValue[0] = '\0';
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         GetPrivateProfileString(
172*cdf0e10cSrcweir             TEXT("Bootstrap"),
173*cdf0e10cSrcweir             TEXT("ProductBuildid"),
174*cdf0e10cSrcweir             TEXT("8918"),
175*cdf0e10cSrcweir             szValue,
176*cdf0e10cSrcweir             elementsof(szValue),
177*cdf0e10cSrcweir             sSetupiniPath.c_str()
178*cdf0e10cSrcweir             );
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         int InstalledProductMinor = atoi(szValue);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         if ( InstalledProductMinor >= PatchProductMinor )
183*cdf0e10cSrcweir         {
184*cdf0e10cSrcweir             SetMsiProperty( handle, TEXT("PATCHISOLDER"), TEXT("YES") );
185*cdf0e10cSrcweir             // MessageBox(NULL, "PATCHISOLDER set", "DEBUG", MB_OK);
186*cdf0e10cSrcweir             SetMsiErrorCode( MSI_ERROR_PATCHISOLDER );
187*cdf0e10cSrcweir             return ERROR_SUCCESS;
188*cdf0e10cSrcweir         }
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     // 4. Setting property ALLUSERS with value from "setup.ini"
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     szValue[0] = '\0';
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     GetPrivateProfileString(
196*cdf0e10cSrcweir         TEXT("Bootstrap"),
197*cdf0e10cSrcweir         TEXT("ALLUSERS"),
198*cdf0e10cSrcweir         TEXT(""),
199*cdf0e10cSrcweir         szValue,
200*cdf0e10cSrcweir         elementsof(szValue),
201*cdf0e10cSrcweir         sSetupiniPath.c_str()
202*cdf0e10cSrcweir         );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     if ( szValue[0] )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("ALLUSERS"), szValue );
207*cdf0e10cSrcweir         // MessageBox(NULL, "ALLUSERS set", "DEBUG", MB_OK);
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     return ERROR_SUCCESS;
211*cdf0e10cSrcweir }
212