Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / reg4allmsdoc / reg4allmsi.cxx
blob1bc37af20eb9e757284c51c5734abe84af1227bf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: reg4allmsi.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #ifdef _MSC_VER
33 #pragma warning(push, 1) /* disable warnings within system headers */
34 #endif
35 #define WIN32_LEAN_AND_MEAN
36 #include <windows.h>
37 #include <msiquery.h>
38 #ifdef _MSC_VER
39 #pragma warning(pop)
40 #endif
42 #include <malloc.h>
43 #include <string>
44 #include <strsafe.h>
46 //----------------------------------------------------------
47 static const CHAR* g_Extensions[] =
49 ".doc", // Microsoft Word Text [0]
50 ".dot", // Microsoft Word Template
51 ".rtf", // rtf text
52 ".docx", // Office Word 2007 XML document
53 ".docm", // Office Word 2007 XML macro-enabled document
54 ".dotx", // Office Word 2007 XML template
55 ".dotm", // Office Word 2007 XML macro-enabled template
56 ".xlw", // Microsoft Excel
57 ".xls", // Microsoft Excel
58 ".xlt", // Microsoft Excel Template
59 ".xlsx", // Office Excel 2007 XML workbook
60 ".xlsm", // Office Excel 2007 XML macro-enabled workbook
61 ".xltx", // Office Excel 2007 XML template
62 ".xltm", // Office Excel 2007 XML macro-enabled template
63 ".xlsb", // Office Excel 2007 binary workbook (BIFF12)
64 ".ppt", // Microsoft Powerpoint
65 ".pps", // Microsoft Powerpoint
66 ".pot", // Microsoft Powerpoint Template
67 ".pptx", // Office PowerPoint 2007 XML presentation
68 ".pptm", // Office PowerPoint 2007 macro-enabled XML presentation
69 ".potx", // Office PowerPoint 2007 XML template
70 ".potm", // Office PowerPoint 2007 macro-enabled XML template
71 ".ppsx", // Office PowerPoint 2007 XML show
75 // ".xlam", // Office Excel 2007 XML macro-enabled add-in
76 // ".ppam", // Office PowerPoint 2007 macro-enabled XML add-in
77 // ".ppsm", // Office PowerPoint 2007 macro-enabled XML show
79 //----------------------------------------------------------
80 #ifdef DEBUG
81 inline void OutputDebugStringFormat( LPCSTR pFormat, ... )
83 CHAR buffer[1024];
84 va_list args;
86 va_start( args, pFormat );
87 StringCchVPrintfA( buffer, sizeof(buffer), pFormat, args );
88 OutputDebugStringA( buffer );
90 #else
91 static inline void OutputDebugStringFormat( LPCSTR, ... )
94 #endif
96 //----------------------------------------------------------
97 static bool IsModuleSelectedForInstallation( MSIHANDLE handle, LPCTSTR name )
99 INSTALLSTATE current_state;
100 INSTALLSTATE future_state;
101 MsiGetFeatureState(handle, name, &current_state, &future_state);
102 return (future_state == INSTALLSTATE_LOCAL);
105 //----------------------------------------------------------
106 static BOOL CheckExtensionInRegistry( LPCSTR lpSubKey )
108 BOOL bRet = false;
109 HKEY hKey = NULL;
110 LONG lResult = RegOpenKeyExA( HKEY_CLASSES_ROOT, lpSubKey, 0, KEY_QUERY_VALUE, &hKey );
112 if ( ERROR_SUCCESS == lResult )
114 CHAR szBuffer[1024];
115 DWORD nSize = sizeof( szBuffer );
117 lResult = RegQueryValueExA( hKey, "", NULL, NULL, (LPBYTE)szBuffer, &nSize );
118 if ( ERROR_SUCCESS == lResult )
120 szBuffer[nSize] = '\0';
121 OutputDebugStringFormat( "Found value [%s] for key [%s].\n", szBuffer, lpSubKey );
123 if ( strncmp( szBuffer, "WordPad.Document.1", 18 ) == 0 )
124 { // We will replace registration for word pad
125 bRet = true;
127 if ( strncmp( szBuffer, "OpenOffice.org.", 15 ) == 0 )
128 { // We will replace registration for our own types, too
129 bRet = true;
131 if ( strncmp( szBuffer, "ooostub.", 8 ) == 0 )
132 { // We will replace registration for ooostub, too
133 bRet = true;
136 else // no default value found -> return TRUE to register for that key
137 bRet = true;
139 RegCloseKey( hKey );
141 else // no key found -> return TRUE to register for that key
142 bRet = true;
144 return bRet;
147 //----------------------------------------------------------
148 static LONG DeleteSubKeyTree( HKEY RootKey, LPCSTR lpKey )
150 HKEY hKey;
151 LONG rc = RegOpenKeyExA( RootKey, lpKey, 0, KEY_READ | DELETE, &hKey );
153 if (ERROR_SUCCESS == rc)
155 LPCSTR lpSubKey;
156 DWORD nMaxSubKeyLen;
158 rc = RegQueryInfoKeyA( hKey, 0, 0, 0, 0, &nMaxSubKeyLen, 0, 0, 0, 0, 0, 0 );
159 nMaxSubKeyLen++; // space for trailing '\0'
160 lpSubKey = reinterpret_cast<CHAR*>( _alloca( nMaxSubKeyLen*sizeof(CHAR) ) );
162 while (ERROR_SUCCESS == rc)
164 DWORD nLen = nMaxSubKeyLen;
165 rc = RegEnumKeyExA( hKey, 0, (LPSTR)lpSubKey, &nLen, 0, 0, 0, 0); // always index zero
167 if ( ERROR_NO_MORE_ITEMS == rc )
169 rc = RegDeleteKeyA( RootKey, lpKey );
170 if ( rc == ERROR_SUCCESS )
171 OutputDebugStringFormat( "deleted key [%s] from registry.\n", lpKey );
172 else
173 OutputDebugStringFormat( "RegDeleteKeyA %s returned %ld.\n", lpKey, rc );
174 break;
176 else if ( rc == ERROR_SUCCESS )
178 rc = DeleteSubKeyTree( hKey, lpSubKey );
179 if ( ERROR_SUCCESS != rc )
180 OutputDebugStringFormat( "RegDeleteKeyA %s returned %ld.\n", lpSubKey, rc );
184 RegCloseKey(hKey);
186 else
188 OutputDebugStringFormat( "RegOpenKeyExA %s returned %ld.\n", lpKey, rc );
191 return rc;
194 //----------------------------------------------------------
195 static BOOL RemoveExtensionInRegistry( LPCSTR lpSubKey )
197 CHAR szBuffer[4096];
198 DWORD nSize = sizeof( szBuffer );
199 HKEY hKey = NULL;
200 HKEY hSubKey = NULL;
201 LONG lResult = RegOpenKeyExA( HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes", 0, KEY_QUERY_VALUE, &hKey );
203 if ( ERROR_SUCCESS == lResult )
205 lResult = RegOpenKeyExA( hKey, lpSubKey, 0, KEY_QUERY_VALUE, &hSubKey );
207 if ( ERROR_SUCCESS == lResult )
209 DWORD nSubKeys = 1;
210 szBuffer[0] = '\0';
212 // we get the value of the default key fist and while we are on querying,
213 // we ask for the subkey count, too
214 lResult = RegQueryValueExA( hSubKey, "", NULL, NULL, (LPBYTE)szBuffer, &nSize );
215 if ( ERROR_SUCCESS == lResult )
216 RegQueryInfoKeyA( hSubKey, 0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0 );
217 RegCloseKey( hSubKey );
219 // we will remove all key with an default value starting with ooostub but
220 // we have to be careful about MSO keys
221 if ( strncmp( szBuffer, "opendocument.", 13 ) == 0 )
223 if ( nSubKeys == 0 )
225 DeleteSubKeyTree( hKey, lpSubKey );
227 else
229 lResult = RegOpenKeyExA( hKey, lpSubKey, 0, KEY_SET_VALUE, &hSubKey );
230 if ( ERROR_SUCCESS == lResult )
231 RegDeleteValueA( hSubKey, "" );
232 else
233 OutputDebugStringFormat( "Could not open key %s for deleting: RegOpenKeyEx returned %ld.\n", lpSubKey, lResult );
238 RegCloseKey( hKey );
241 return ( ERROR_SUCCESS == lResult );
244 //----------------------------------------------------------
245 bool GetMsiProp( MSIHANDLE handle, LPCSTR name, /*out*/std::string& value )
247 DWORD sz = 0;
248 LPSTR dummy = "";
249 if (MsiGetPropertyA(handle, name, dummy, &sz) == ERROR_MORE_DATA)
251 sz++;
252 DWORD nbytes = sz * sizeof(TCHAR);
253 LPSTR buff = reinterpret_cast<LPSTR>(_alloca(nbytes));
254 ZeroMemory(buff, nbytes);
255 MsiGetPropertyA(handle, name, buff, &sz);
256 value = buff;
257 return true;
259 return false;
262 //----------------------------------------------------------
263 bool IsSetMsiProp( MSIHANDLE handle, LPCSTR name )
265 std::string val;
266 GetMsiProp( handle, name, val );
267 return (val == "1");
270 //----------------------------------------------------------
271 static void registerForExtensions( MSIHANDLE handle, BOOL bRegisterAll )
272 { // Check all file extensions
273 int nIndex = 0;
274 while ( g_Extensions[nIndex] != 0 )
276 BOOL bRegister = bRegisterAll || CheckExtensionInRegistry( g_Extensions[nIndex] );
277 if ( bRegister )
279 CHAR sPropName[256];
280 StringCchCopyA( sPropName, 256, "REGISTER_" );
281 StringCchCatA( sPropName, 256, (g_Extensions[nIndex])+1 );
282 CharUpperBuffA( sPropName+9, 4 );
283 MsiSetPropertyA( handle, sPropName, "1" );
284 OutputDebugStringFormat( "Set MSI property %s.\n", sPropName );
286 ++nIndex;
290 //----------------------------------------------------------
291 //----------------------------------------------------------
292 //----------------------------------------------------------
293 extern "C" UINT __stdcall FindRegisteredExtensions( MSIHANDLE handle )
295 bool bRegisterAll = IsSetMsiProp( handle, "REGISTER_ALL_MSO_TYPES" );
297 if ( IsSetMsiProp( handle, "REGISTER_NO_MSO_TYPES" ) )
299 OutputDebugStringFormat( "FindRegisteredExtensions: Register none!" );
300 return ERROR_SUCCESS;
302 else if ( bRegisterAll )
303 OutputDebugStringFormat( "FindRegisteredExtensions: Force all on" );
304 else
305 OutputDebugStringFormat( "FindRegisteredExtensions: " );
307 registerForExtensions( handle, bRegisterAll );
309 return ERROR_SUCCESS;
312 //----------------------------------------------------------
313 extern "C" UINT __stdcall DeleteRegisteredExtensions( MSIHANDLE /*handle*/ )
315 OutputDebugStringFormat( "DeleteRegisteredExtensions\n" );
317 // remove all file extensions
318 int nIndex = 0;
319 while ( g_Extensions[nIndex] != 0 )
321 RemoveExtensionInRegistry( g_Extensions[nIndex] );
322 ++nIndex;
325 return ERROR_SUCCESS;