xref: /trunk/main/setup_native/source/win32/customactions/shellextensions/completeinstallpath.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #define _WIN32_WINDOWS 0x0410
29 #ifdef _MSC_VER
30 #pragma warning(push, 1) /* disable warnings within system headers */
31 #endif
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34 #include <msiquery.h>
35 #ifdef _MSC_VER
36 #pragma warning(pop)
37 #endif
38 
39 #include <malloc.h>
40 
41 #ifdef UNICODE
42 #define _UNICODE
43 #define _tstring    wstring
44 #else
45 #define _tstring    string
46 #endif
47 #include <tchar.h>
48 #include <string>
49 
50 using namespace std;
51 
52 namespace
53 {
54     std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
55     {
56         std::_tstring   result;
57         TCHAR   szDummy[1] = TEXT("");
58         DWORD   nChars = 0;
59 
60         if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
61         {
62             DWORD nBytes = ++nChars * sizeof(TCHAR);
63             LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
64             ZeroMemory( buffer, nBytes );
65             MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
66             result = buffer;
67         }
68 
69         return  result;
70     }
71 } // namespace
72 
73 extern "C" UINT __stdcall CompleteInstallPath( MSIHANDLE handle )
74 {
75     // This CustomAction is necessary for updates from OOo 3.0, OOo 3.1 and OOo 3.2 to versions
76     // OOo 3.3 or later. This is caused by a change of INSTALLLOCATION, that starting with OOo 3.3
77     // contains the name of the product again (instead of only "c:\program files"). Unfortunately
78     // this causes in an update installation, that INSTALLLOCATION is set to "c:\program files",
79     // so that in an OOo 3.3 or later, the directory "program" or "share" are directly created
80     // below "c:\program files".
81 
82     TCHAR   szValue[8192];
83     DWORD   nValueSize = sizeof(szValue);
84     HKEY    hKey;
85     std::_tstring   sInstDir;
86     std::_tstring   mystr;
87 
88     // Reading property OFFICEDIRHOSTNAME_, that contains the part of the path behind
89     // the program files folder.
90 
91     std::_tstring   sInstallLocation = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
92     std::_tstring   sOfficeDirHostname = GetMsiProperty( handle, TEXT("OFFICEDIRHOSTNAME_") );
93 
94     // If sInstallLocation ends with (contains) the string sOfficeDirHostname,
95     // INSTALLLOCATION is good and nothing has to be done here.
96 
97     bool pathCompletionRequired = true;
98 
99     if ( _tcsstr( sInstallLocation.c_str(), sOfficeDirHostname.c_str() ) )
100     {
101         pathCompletionRequired = false;  // nothing to do
102         // mystr = "Nothing to do, officedir is included into installlocation";
103         // MessageBox( NULL, mystr.c_str(), "It is part of installlocation", MB_OK );
104     }
105 
106     // If the path INSTALLLOCATION does not end with this string, INSTALLLOCATION is maybe
107     // transfered from an OOo 3.0, OOo 3.1 and OOo 3.2 and need to be changed therefore.
108 
109     if ( pathCompletionRequired )
110     {
111         std::_tstring   sManufacturer = GetMsiProperty( handle, TEXT("Manufacturer") );
112         std::_tstring   sDefinedName = GetMsiProperty( handle, TEXT("DEFINEDPRODUCT") );
113         std::_tstring   sUpgradeCode = GetMsiProperty( handle, TEXT("UpgradeCode") );
114 
115         // sUpdateVersion can be "3.0", "3.1" or "3.2"
116 
117         std::_tstring   sProductKey30 = "Software\\" + sManufacturer + "\\" + sDefinedName +
118                                             "\\" + "3.0" + "\\" + sUpgradeCode;
119 
120         std::_tstring   sProductKey31 = "Software\\" + sManufacturer + "\\" + sDefinedName +
121                                             "\\" + "3.1" + "\\" + sUpgradeCode;
122 
123         std::_tstring   sProductKey32 = "Software\\" + sManufacturer + "\\" + sDefinedName +
124                                             "\\" + "3.2" + "\\" + sUpgradeCode;
125 
126         // mystr = "ProductKey: " + sProductKey;
127         // MessageBox( NULL, mystr.c_str(), "ProductKey", MB_OK );
128 
129         // mystr = "Checking registry";
130         // MessageBox( NULL, mystr.c_str(), "registry search", MB_OK );
131 
132         bool oldVersionExists = false;
133 
134         if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey30.c_str(), &hKey ) )
135         {
136             oldVersionExists = true;
137             RegCloseKey( hKey );
138         }
139         else if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey31.c_str(), &hKey ) )
140         {
141             oldVersionExists = true;
142             RegCloseKey( hKey );
143         }
144         else if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey32.c_str(), &hKey ) )
145         {
146             oldVersionExists = true;
147             RegCloseKey( hKey );
148         }
149         else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey30.c_str(), &hKey ) )
150         {
151             oldVersionExists = true;
152             RegCloseKey( hKey );
153         }
154         else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey31.c_str(), &hKey ) )
155         {
156             oldVersionExists = true;
157             RegCloseKey( hKey );
158         }
159         else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey32.c_str(), &hKey ) )
160         {
161             oldVersionExists = true;
162             RegCloseKey( hKey );
163         }
164 
165         if ( oldVersionExists )
166         {
167             // Adding the new path content sOfficeDirHostname
168             sInstallLocation = sInstallLocation + sOfficeDirHostname;
169             // Setting the new property value
170             MsiSetProperty(handle, TEXT("INSTALLLOCATION"), sInstallLocation.c_str());
171             // mystr = "Setting path to: " + sInstallLocation;
172             // MessageBox( NULL, mystr.c_str(), "sInstallLocation", MB_OK );
173         }
174     }
175 
176     // mystr = "Ending with INSTALLLOCATION: " + sInstallLocation;
177     // MessageBox( NULL, mystr.c_str(), "END", MB_OK );
178 
179     return ERROR_SUCCESS;
180 }
181