132b1fd08SAndrew Rist /**************************************************************
2*c36aa0f1Smseidel  *
332b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
432b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
532b1fd08SAndrew Rist  * distributed with this work for additional information
632b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
732b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
832b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
932b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c36aa0f1Smseidel  *
1132b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c36aa0f1Smseidel  *
1332b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1432b1fd08SAndrew Rist  * software distributed under the License is distributed on an
1532b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1632b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
1732b1fd08SAndrew Rist  * specific language governing permissions and limitations
1832b1fd08SAndrew Rist  * under the License.
19*c36aa0f1Smseidel  *
2032b1fd08SAndrew Rist  *************************************************************/
2132b1fd08SAndrew Rist 
2232b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifdef _MSC_VER
25cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
26cdf0e10cSrcweir #endif
27*c36aa0f1Smseidel #define WIN32_LEAN_AND_MEAN
28cdf0e10cSrcweir #include <windows.h>
29cdf0e10cSrcweir #include <msiquery.h>
30cdf0e10cSrcweir #ifdef _MSC_VER
31cdf0e10cSrcweir #pragma warning(pop)
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <malloc.h>
35cdf0e10cSrcweir #include <string>
36cdf0e10cSrcweir #include <strsafe.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //----------------------------------------------------------
39cdf0e10cSrcweir static const CHAR* g_Extensions[] =
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     ".doc",     // Microsoft Word Text [0]
42cdf0e10cSrcweir     ".dot",     // Microsoft Word Template
43*c36aa0f1Smseidel     ".rtf",     // RTF text
44cdf0e10cSrcweir     ".docx",    // Office Word 2007 XML document
45cdf0e10cSrcweir     ".docm",    // Office Word 2007 XML macro-enabled document
46cdf0e10cSrcweir     ".dotx",    // Office Word 2007 XML template
47cdf0e10cSrcweir     ".dotm",    // Office Word 2007 XML macro-enabled template
48cdf0e10cSrcweir     ".xlw",     // Microsoft Excel
49cdf0e10cSrcweir     ".xls",     // Microsoft Excel
50cdf0e10cSrcweir     ".xlt",     // Microsoft Excel Template
51cdf0e10cSrcweir     ".xlsx",    // Office Excel 2007 XML workbook
52cdf0e10cSrcweir     ".xlsm",    // Office Excel 2007 XML macro-enabled workbook
53cdf0e10cSrcweir     ".xltx",    // Office Excel 2007 XML template
54cdf0e10cSrcweir     ".xltm",    // Office Excel 2007 XML macro-enabled template
55cdf0e10cSrcweir     ".xlsb",    // Office Excel 2007 binary workbook (BIFF12)
56*c36aa0f1Smseidel     ".ppt",     // Microsoft PowerPoint
57*c36aa0f1Smseidel     ".pps",     // Microsoft PowerPoint
58*c36aa0f1Smseidel     ".pot",     // Microsoft PowerPoint Template
59cdf0e10cSrcweir     ".pptx",    // Office PowerPoint 2007 XML presentation
60cdf0e10cSrcweir     ".pptm",    // Office PowerPoint 2007 macro-enabled XML presentation
61cdf0e10cSrcweir     ".potx",    // Office PowerPoint 2007 XML template
62cdf0e10cSrcweir     ".potm",    // Office PowerPoint 2007 macro-enabled XML template
63cdf0e10cSrcweir     ".ppsx",    // Office PowerPoint 2007 XML show
64cdf0e10cSrcweir     0
65cdf0e10cSrcweir };
66cdf0e10cSrcweir 
67cdf0e10cSrcweir static const int WORD_START = 0;
68cdf0e10cSrcweir static const int EXCEL_START = 7;
69cdf0e10cSrcweir static const int POWERPOINT_START = 15;
70cdf0e10cSrcweir static const int POWERPOINT_END = 23;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir //    ".xlam",    // Office Excel 2007 XML macro-enabled add-in
73cdf0e10cSrcweir //    ".ppam",    // Office PowerPoint 2007 macro-enabled XML add-in
74cdf0e10cSrcweir //    ".ppsm",    // Office PowerPoint 2007 macro-enabled XML show
75cdf0e10cSrcweir 
76cdf0e10cSrcweir //----------------------------------------------------------
77cdf0e10cSrcweir #ifdef DEBUG
OutputDebugStringFormat(LPCSTR pFormat,...)78cdf0e10cSrcweir inline void OutputDebugStringFormat( LPCSTR pFormat, ... )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	CHAR    buffer[1024];
81cdf0e10cSrcweir 	va_list args;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	va_start( args, pFormat );
84cdf0e10cSrcweir 	StringCchVPrintfA( buffer, sizeof(buffer), pFormat, args );
85cdf0e10cSrcweir 	OutputDebugStringA( buffer );
86cdf0e10cSrcweir }
87cdf0e10cSrcweir #else
OutputDebugStringFormat(LPCSTR,...)88cdf0e10cSrcweir static inline void OutputDebugStringFormat( LPCSTR, ... )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir }
91cdf0e10cSrcweir #endif
92cdf0e10cSrcweir 
93cdf0e10cSrcweir //----------------------------------------------------------
CheckExtensionInRegistry(LPCSTR lpSubKey)94cdf0e10cSrcweir static BOOL CheckExtensionInRegistry( LPCSTR lpSubKey )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     BOOL    bRet = false;
97cdf0e10cSrcweir     HKEY    hKey = NULL;
98cdf0e10cSrcweir     LONG    lResult = RegOpenKeyExA( HKEY_CLASSES_ROOT, lpSubKey, 0, KEY_QUERY_VALUE, &hKey );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	if ( ERROR_SUCCESS == lResult )
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir         CHAR    szBuffer[1024];
103cdf0e10cSrcweir         DWORD   nSize = sizeof( szBuffer );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         lResult = RegQueryValueExA( hKey, "", NULL, NULL, (LPBYTE)szBuffer, &nSize );
106cdf0e10cSrcweir         if ( ERROR_SUCCESS == lResult )
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             szBuffer[nSize] = '\0';
109cdf0e10cSrcweir             OutputDebugStringFormat( "Found value [%s] for key [%s].\n", szBuffer, lpSubKey );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             if ( strncmp( szBuffer, "WordPad.Document.1", 18 ) == 0 )
112*c36aa0f1Smseidel             {   // We will replace registration for WordPad
113cdf0e10cSrcweir                 bRet = true;
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir             else if ( strncmp( szBuffer, "OpenOffice.org.", 15 ) == 0 )
116ff3f4ebcSOliver-Rainer Wittmann             {   // We will replace registration for our (former) own types, too
117ff3f4ebcSOliver-Rainer Wittmann                 bRet = true;
118ff3f4ebcSOliver-Rainer Wittmann             }
119599cc5b4SOliver-Rainer Wittmann             else if ( strncmp( szBuffer, "OpenOffice.", 11 ) == 0 )
120cdf0e10cSrcweir             {   // We will replace registration for our own types, too
121cdf0e10cSrcweir                 bRet = true;
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir             else if ( strncmp( szBuffer, "ooostub.", 8 ) == 0 )
124cdf0e10cSrcweir             {   // We will replace registration for ooostub, too
125cdf0e10cSrcweir                 bRet = true;
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir             else
128cdf0e10cSrcweir             {
129cdf0e10cSrcweir                 OutputDebugStringFormat( "  Checking OpenWithList of [%s].\n", lpSubKey );
130cdf0e10cSrcweir                 HKEY hSubKey;
131cdf0e10cSrcweir                 lResult = RegOpenKeyExA( hKey, "OpenWithList", 0, KEY_ENUMERATE_SUB_KEYS, &hSubKey );
132cdf0e10cSrcweir                 if ( ERROR_SUCCESS == lResult )
133cdf0e10cSrcweir                 {
134cdf0e10cSrcweir                     DWORD nIndex = 0;
135cdf0e10cSrcweir                     while ( ERROR_SUCCESS == lResult )
136cdf0e10cSrcweir                     {
137cdf0e10cSrcweir                         nSize = sizeof( szBuffer );
138cdf0e10cSrcweir                         lResult = RegEnumKeyExA( hSubKey, nIndex++, szBuffer, &nSize, NULL, NULL, NULL, NULL );
139cdf0e10cSrcweir                         if ( ERROR_SUCCESS == lResult )
140cdf0e10cSrcweir                         {
141cdf0e10cSrcweir                             OutputDebugStringFormat( "    Found value [%s] in OpenWithList of [%s].\n", szBuffer, lpSubKey );
142cdf0e10cSrcweir                             if ( strncmp( szBuffer, "WordPad.exe", 11 ) == 0 )
143*c36aa0f1Smseidel                             {   // We will replace registration for WordPad
144cdf0e10cSrcweir                                 bRet = true;
145cdf0e10cSrcweir                             }
146cdf0e10cSrcweir                             else if ( nSize > 0 )
147cdf0e10cSrcweir                                 bRet = false;
148cdf0e10cSrcweir                         }
149cdf0e10cSrcweir                     }
150cdf0e10cSrcweir                 }
151cdf0e10cSrcweir                 else
152cdf0e10cSrcweir                 {
153cdf0e10cSrcweir                     OutputDebugStringFormat( "  No OpenWithList found!\n" );
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir         else    // no default value found -> return TRUE to register for that key
158cdf0e10cSrcweir             bRet = true;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		RegCloseKey( hKey );
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir     else // no key found -> return TRUE to register for that key
163cdf0e10cSrcweir         bRet = true;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     return bRet;
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir //----------------------------------------------------------
DeleteSubKeyTree(HKEY RootKey,LPCSTR lpKey)169cdf0e10cSrcweir static LONG DeleteSubKeyTree( HKEY RootKey, LPCSTR lpKey )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir 	HKEY hKey;
172cdf0e10cSrcweir 	LONG rc = RegOpenKeyExA( RootKey, lpKey, 0, KEY_READ | DELETE, &hKey );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	if (ERROR_SUCCESS == rc)
175*c36aa0f1Smseidel 	{
176cdf0e10cSrcweir 		LPCSTR    lpSubKey;
177cdf0e10cSrcweir 		DWORD     nMaxSubKeyLen;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 		rc = RegQueryInfoKeyA( hKey, 0, 0, 0, 0, &nMaxSubKeyLen, 0, 0, 0, 0, 0, 0 );
180cdf0e10cSrcweir 		nMaxSubKeyLen++; // space for trailing '\0'
181cdf0e10cSrcweir 		lpSubKey = reinterpret_cast<CHAR*>( _alloca( nMaxSubKeyLen*sizeof(CHAR) ) );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		while (ERROR_SUCCESS == rc)
184cdf0e10cSrcweir         {
185cdf0e10cSrcweir 			DWORD nLen = nMaxSubKeyLen;
186*c36aa0f1Smseidel 			rc = RegEnumKeyExA( hKey, 0, (LPSTR)lpSubKey, &nLen, 0, 0, 0, 0); // always index zero
187cdf0e10cSrcweir 
188cdf0e10cSrcweir             if ( ERROR_NO_MORE_ITEMS == rc )
189cdf0e10cSrcweir             {
190cdf0e10cSrcweir 				rc = RegDeleteKeyA( RootKey, lpKey );
191cdf0e10cSrcweir                 if ( rc == ERROR_SUCCESS )
192cdf0e10cSrcweir                     OutputDebugStringFormat( "deleted key [%s] from registry.\n", lpKey );
193cdf0e10cSrcweir                 else
194cdf0e10cSrcweir                     OutputDebugStringFormat( "RegDeleteKeyA %s returned %ld.\n", lpKey, rc );
195cdf0e10cSrcweir                 break;
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir             else if ( rc == ERROR_SUCCESS )
198cdf0e10cSrcweir 			{
199cdf0e10cSrcweir 				rc = DeleteSubKeyTree( hKey, lpSubKey );
200cdf0e10cSrcweir                 if ( ERROR_SUCCESS != rc )
201cdf0e10cSrcweir                     OutputDebugStringFormat( "RegDeleteKeyA %s returned %ld.\n", lpSubKey, rc );
202cdf0e10cSrcweir 			}
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir         RegCloseKey(hKey);
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir     else
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         OutputDebugStringFormat( "RegOpenKeyExA %s returned %ld.\n", lpKey, rc );
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 	return rc;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir //----------------------------------------------------------
RemoveExtensionInRegistry(LPCSTR lpSubKey)216cdf0e10cSrcweir static BOOL RemoveExtensionInRegistry( LPCSTR lpSubKey )
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     CHAR    szBuffer[4096];
219cdf0e10cSrcweir     DWORD   nSize = sizeof( szBuffer );
220cdf0e10cSrcweir     HKEY    hKey = NULL;
221cdf0e10cSrcweir     HKEY    hSubKey = NULL;
222cdf0e10cSrcweir     LONG    lResult = RegOpenKeyExA( HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes", 0, KEY_QUERY_VALUE, &hKey );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	if ( ERROR_SUCCESS == lResult )
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir 		lResult = RegOpenKeyExA( hKey, lpSubKey, 0, KEY_QUERY_VALUE, &hSubKey );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         if ( ERROR_SUCCESS == lResult )
229cdf0e10cSrcweir         {
230cdf0e10cSrcweir             DWORD nSubKeys = 1;
231cdf0e10cSrcweir             szBuffer[0] = '\0';
232cdf0e10cSrcweir 
233*c36aa0f1Smseidel             // we get the value of the default key first and while we are on querying,
234cdf0e10cSrcweir             // we ask for the subkey count, too
235cdf0e10cSrcweir             lResult = RegQueryValueExA( hSubKey, "", NULL, NULL, (LPBYTE)szBuffer, &nSize );
236cdf0e10cSrcweir             if ( ERROR_SUCCESS == lResult )
237cdf0e10cSrcweir                 RegQueryInfoKeyA( hSubKey, 0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0 );
238cdf0e10cSrcweir             RegCloseKey( hSubKey );
239cdf0e10cSrcweir 
240*c36aa0f1Smseidel             // we will remove all key with a default value starting with ooostub but
241cdf0e10cSrcweir             // we have to be careful about MSO keys
242cdf0e10cSrcweir             if ( strncmp( szBuffer, "opendocument.", 13 ) == 0 )
243cdf0e10cSrcweir             {
244cdf0e10cSrcweir                 if ( nSubKeys == 0 )
245cdf0e10cSrcweir                 {
246cdf0e10cSrcweir                     DeleteSubKeyTree( hKey, lpSubKey );
247cdf0e10cSrcweir                 }
248cdf0e10cSrcweir                 else
249cdf0e10cSrcweir                 {
250cdf0e10cSrcweir                     lResult = RegOpenKeyExA( hKey, lpSubKey, 0, KEY_SET_VALUE, &hSubKey );
251cdf0e10cSrcweir                     if ( ERROR_SUCCESS == lResult )
252cdf0e10cSrcweir                         RegDeleteValueA( hSubKey, "" );
253cdf0e10cSrcweir                     else
254cdf0e10cSrcweir                         OutputDebugStringFormat( "Could not open key %s for deleting: RegOpenKeyEx returned %ld.\n", lpSubKey, lResult );
255cdf0e10cSrcweir                 }
256cdf0e10cSrcweir             }
257cdf0e10cSrcweir         }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir         RegCloseKey( hKey );
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     return ( ERROR_SUCCESS == lResult );
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir //----------------------------------------------------------
GetMsiProp(MSIHANDLE handle,LPCSTR name,std::string & value)266cdf0e10cSrcweir bool GetMsiProp( MSIHANDLE handle, LPCSTR name, /*out*/std::string& value )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     DWORD sz = 0;
269cdf0e10cSrcweir     LPSTR dummy = "";
270cdf0e10cSrcweir     if (MsiGetPropertyA(handle, name, dummy, &sz) == ERROR_MORE_DATA)
271cdf0e10cSrcweir     {
272cdf0e10cSrcweir         sz++;
273cdf0e10cSrcweir         DWORD nbytes = sz * sizeof(TCHAR);
274cdf0e10cSrcweir         LPSTR buff = reinterpret_cast<LPSTR>(_alloca(nbytes));
275cdf0e10cSrcweir         ZeroMemory(buff, nbytes);
276cdf0e10cSrcweir         MsiGetPropertyA(handle, name, buff, &sz);
277cdf0e10cSrcweir         value = buff;
278cdf0e10cSrcweir         return true;
279*c36aa0f1Smseidel     }
280cdf0e10cSrcweir     return false;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir //----------------------------------------------------------
IsSetMsiProp(MSIHANDLE handle,LPCSTR name)284cdf0e10cSrcweir bool IsSetMsiProp( MSIHANDLE handle, LPCSTR name )
285cdf0e10cSrcweir {
286cdf0e10cSrcweir     std::string val;
287cdf0e10cSrcweir     GetMsiProp( handle, name, val );
288cdf0e10cSrcweir     return (val == "1");
289cdf0e10cSrcweir }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir //----------------------------------------------------------
registerForExtension(MSIHANDLE handle,const int nIndex,bool bRegister)292cdf0e10cSrcweir static void registerForExtension( MSIHANDLE handle, const int nIndex, bool bRegister )
293cdf0e10cSrcweir {
294cdf0e10cSrcweir     CHAR sPropName[256];
295cdf0e10cSrcweir     StringCchCopyA( sPropName, 256, "REGISTER_" );
296cdf0e10cSrcweir     StringCchCatA( sPropName, 256, (g_Extensions[nIndex])+1 );
297cdf0e10cSrcweir     CharUpperBuffA( sPropName+9, 4 );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir     if ( bRegister ) {
300cdf0e10cSrcweir         MsiSetPropertyA( handle, sPropName, "1" );
301cdf0e10cSrcweir         OutputDebugStringFormat( "Set MSI property %s.\n", sPropName );
302cdf0e10cSrcweir     } else {
303cdf0e10cSrcweir         MsiSetPropertyA( handle, sPropName, "0" );
304cdf0e10cSrcweir         OutputDebugStringFormat( "Unset MSI property %s.\n", sPropName );
305cdf0e10cSrcweir     }
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir //----------------------------------------------------------
registerForExtensions(MSIHANDLE handle,BOOL bRegisterAll)309cdf0e10cSrcweir static void registerForExtensions( MSIHANDLE handle, BOOL bRegisterAll )
310cdf0e10cSrcweir { // Check all file extensions
311cdf0e10cSrcweir     int nIndex = 0;
312cdf0e10cSrcweir     while ( g_Extensions[nIndex] != 0 )
313cdf0e10cSrcweir     {
314cdf0e10cSrcweir         BOOL bRegister = bRegisterAll || CheckExtensionInRegistry( g_Extensions[nIndex] );
315cdf0e10cSrcweir         if ( bRegister )
316cdf0e10cSrcweir             registerForExtension( handle, nIndex, true );
317cdf0e10cSrcweir         ++nIndex;
318cdf0e10cSrcweir     }
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir //----------------------------------------------------------
checkSomeExtensionInRegistry(const int nStart,const int nEnd)322cdf0e10cSrcweir static bool checkSomeExtensionInRegistry( const int nStart, const int nEnd )
323cdf0e10cSrcweir { // Check all file extensions
324cdf0e10cSrcweir     int nIndex = nStart;
325cdf0e10cSrcweir     bool bFound = false;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     while ( !bFound && ( g_Extensions[nIndex] != 0 ) && ( nIndex < nEnd ) )
328cdf0e10cSrcweir     {
329cdf0e10cSrcweir         bFound = ! CheckExtensionInRegistry( g_Extensions[nIndex] );
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         if ( bFound )
332cdf0e10cSrcweir             OutputDebugStringFormat( "Found registration for [%s].\n", g_Extensions[nIndex] );
333cdf0e10cSrcweir 
334cdf0e10cSrcweir         ++nIndex;
335cdf0e10cSrcweir     }
336cdf0e10cSrcweir     return bFound;
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir //----------------------------------------------------------
registerSomeExtensions(MSIHANDLE handle,const int nStart,const int nEnd,bool bRegister)340cdf0e10cSrcweir static void registerSomeExtensions( MSIHANDLE handle, const int nStart, const int nEnd, bool bRegister )
341cdf0e10cSrcweir { // Check all file extensions
342cdf0e10cSrcweir     int nIndex = nStart;
343cdf0e10cSrcweir 
344cdf0e10cSrcweir     while ( ( g_Extensions[nIndex] != 0 ) && ( nIndex < nEnd ) )
345cdf0e10cSrcweir     {
346cdf0e10cSrcweir         registerForExtension( handle, nIndex++, bRegister );
347cdf0e10cSrcweir     }
348cdf0e10cSrcweir }
349cdf0e10cSrcweir 
350cdf0e10cSrcweir //----------------------------------------------------------
351cdf0e10cSrcweir //----------------------------------------------------------
352cdf0e10cSrcweir //----------------------------------------------------------
LookForRegisteredExtensions(MSIHANDLE handle)353cdf0e10cSrcweir extern "C" UINT __stdcall LookForRegisteredExtensions( MSIHANDLE handle )
354cdf0e10cSrcweir {
355cdf0e10cSrcweir     OutputDebugStringFormat( "LookForRegisteredExtensions: " );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     INSTALLSTATE current_state;
358cdf0e10cSrcweir     INSTALLSTATE future_state;
359cdf0e10cSrcweir 
360cdf0e10cSrcweir     bool bWriterEnabled = false;
361cdf0e10cSrcweir     bool bCalcEnabled = false;
362cdf0e10cSrcweir     bool bImpressEnabled = false;
363cdf0e10cSrcweir     bool bRegisterNone = IsSetMsiProp( handle, "REGISTER_NO_MSO_TYPES" );
364cdf0e10cSrcweir 
365cdf0e10cSrcweir     if ( ( ERROR_SUCCESS == MsiGetFeatureState( handle, L"gm_p_Wrt", &current_state, &future_state ) ) &&
366cdf0e10cSrcweir          ( (future_state == INSTALLSTATE_LOCAL) || ((current_state == INSTALLSTATE_LOCAL) && (future_state == INSTALLSTATE_UNKNOWN) ) ) )
367cdf0e10cSrcweir         bWriterEnabled = true;
368cdf0e10cSrcweir 
369cdf0e10cSrcweir     OutputDebugStringFormat( "LookForRegisteredExtensions: Install state Writer is [%d], will be [%d]", current_state, future_state );
370cdf0e10cSrcweir     if ( bWriterEnabled )
371cdf0e10cSrcweir         OutputDebugStringFormat( "LookForRegisteredExtensions: Writer is enabled" );
372cdf0e10cSrcweir     else
373cdf0e10cSrcweir         OutputDebugStringFormat( "LookForRegisteredExtensions: Writer is NOT enabled" );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     if ( ( ERROR_SUCCESS == MsiGetFeatureState( handle, L"gm_p_Calc", &current_state, &future_state ) ) &&
376cdf0e10cSrcweir          ( (future_state == INSTALLSTATE_LOCAL) || ((current_state == INSTALLSTATE_LOCAL) && (future_state == INSTALLSTATE_UNKNOWN) ) ) )
377cdf0e10cSrcweir         bCalcEnabled = true;
378cdf0e10cSrcweir 
379cdf0e10cSrcweir     OutputDebugStringFormat( "LookForRegisteredExtensions: Install state Calc is [%d], will be [%d]", current_state, future_state );
380cdf0e10cSrcweir     if ( bCalcEnabled )
381cdf0e10cSrcweir         OutputDebugStringFormat( "LookForRegisteredExtensions: Calc is enabled" );
382cdf0e10cSrcweir     else
383cdf0e10cSrcweir         OutputDebugStringFormat( "LookForRegisteredExtensions: Calc is NOT enabled" );
384cdf0e10cSrcweir 
385cdf0e10cSrcweir     if ( ( ERROR_SUCCESS == MsiGetFeatureState( handle, L"gm_p_Impress", &current_state, &future_state ) ) &&
386cdf0e10cSrcweir          ( (future_state == INSTALLSTATE_LOCAL) || ((current_state == INSTALLSTATE_LOCAL) && (future_state == INSTALLSTATE_UNKNOWN) ) ) )
387cdf0e10cSrcweir         bImpressEnabled = true;
388cdf0e10cSrcweir 
389cdf0e10cSrcweir     OutputDebugStringFormat( "LookForRegisteredExtensions: Install state Impress is [%d], will be [%d]", current_state, future_state );
390cdf0e10cSrcweir     if ( bImpressEnabled )
391cdf0e10cSrcweir         OutputDebugStringFormat( "LookForRegisteredExtensions: Impress is enabled" );
392cdf0e10cSrcweir     else
393cdf0e10cSrcweir         OutputDebugStringFormat( "LookForRegisteredExtensions: Impress is NOT enabled" );
394cdf0e10cSrcweir 
395cdf0e10cSrcweir     MsiSetPropertyA( handle, "SELECT_WORD", "" );
396cdf0e10cSrcweir     MsiSetPropertyA( handle, "SELECT_EXCEL", "" );
397cdf0e10cSrcweir     MsiSetPropertyA( handle, "SELECT_POWERPOINT", "" );
398cdf0e10cSrcweir 
399cdf0e10cSrcweir     if ( ! bRegisterNone )
400cdf0e10cSrcweir     {
401cdf0e10cSrcweir         if ( IsSetMsiProp( handle, "REGISTER_ALL_MSO_TYPES" ) )
402cdf0e10cSrcweir         {
403cdf0e10cSrcweir             if ( bWriterEnabled )
404cdf0e10cSrcweir                 MsiSetPropertyA( handle, "SELECT_WORD", "1" );
405cdf0e10cSrcweir             if ( bCalcEnabled )
406cdf0e10cSrcweir                 MsiSetPropertyA( handle, "SELECT_EXCEL", "1" );
407cdf0e10cSrcweir             if ( bImpressEnabled )
408cdf0e10cSrcweir                 MsiSetPropertyA( handle, "SELECT_POWERPOINT", "1" );
409cdf0e10cSrcweir         }
410cdf0e10cSrcweir         else
411cdf0e10cSrcweir         {
412cdf0e10cSrcweir             if ( bWriterEnabled && ! checkSomeExtensionInRegistry( WORD_START, EXCEL_START ) )
413cdf0e10cSrcweir             {
414cdf0e10cSrcweir                 MsiSetPropertyA( handle, "SELECT_WORD", "1" );
415*c36aa0f1Smseidel                 OutputDebugStringFormat( "LookForRegisteredExtensions: Register for Microsoft Word" );
416cdf0e10cSrcweir             }
417cdf0e10cSrcweir             if ( bCalcEnabled && ! checkSomeExtensionInRegistry( EXCEL_START, POWERPOINT_START ) )
418cdf0e10cSrcweir             {
419cdf0e10cSrcweir                 MsiSetPropertyA( handle, "SELECT_EXCEL", "1" );
420*c36aa0f1Smseidel                 OutputDebugStringFormat( "LookForRegisteredExtensions: Register for Microsoft Excel" );
421cdf0e10cSrcweir             }
422cdf0e10cSrcweir             if ( bImpressEnabled && ! checkSomeExtensionInRegistry( POWERPOINT_START, POWERPOINT_END ) )
423cdf0e10cSrcweir             {
424cdf0e10cSrcweir                 MsiSetPropertyA( handle, "SELECT_POWERPOINT", "1" );
425*c36aa0f1Smseidel                 OutputDebugStringFormat( "LookForRegisteredExtensions: Register for Microsoft PowerPoint" );
426cdf0e10cSrcweir             }
427cdf0e10cSrcweir         }
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir     MsiSetPropertyA( handle, "FILETYPEDIALOGUSED", "1" );
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     return ERROR_SUCCESS;
433cdf0e10cSrcweir }
434cdf0e10cSrcweir 
435cdf0e10cSrcweir //----------------------------------------------------------
RegisterSomeExtensions(MSIHANDLE handle)436cdf0e10cSrcweir extern "C" UINT __stdcall RegisterSomeExtensions( MSIHANDLE handle )
437cdf0e10cSrcweir {
438cdf0e10cSrcweir     OutputDebugStringFormat( "RegisterSomeExtensions: " );
439cdf0e10cSrcweir 
440cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "SELECT_WORD" ) )
441cdf0e10cSrcweir     {
442cdf0e10cSrcweir         registerSomeExtensions( handle, WORD_START, EXCEL_START, true );
443cdf0e10cSrcweir         MsiSetFeatureState( handle, L"gm_p_Wrt_MSO_Reg", INSTALLSTATE_LOCAL );
444*c36aa0f1Smseidel         OutputDebugStringFormat( "RegisterSomeExtensions: Register for Microsoft Word" );
445cdf0e10cSrcweir     }
446cdf0e10cSrcweir     else
447cdf0e10cSrcweir     {
448cdf0e10cSrcweir         registerSomeExtensions( handle, WORD_START, EXCEL_START, false );
449cdf0e10cSrcweir         MsiSetFeatureState( handle, L"gm_p_Wrt_MSO_Reg", INSTALLSTATE_ABSENT );
450cdf0e10cSrcweir     }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "SELECT_EXCEL" ) )
453cdf0e10cSrcweir     {
454cdf0e10cSrcweir         registerSomeExtensions( handle, EXCEL_START, POWERPOINT_START, true );
455cdf0e10cSrcweir         MsiSetFeatureState( handle, L"gm_p_Calc_MSO_Reg", INSTALLSTATE_LOCAL );
456*c36aa0f1Smseidel         OutputDebugStringFormat( "RegisterSomeExtensions: Register for Microsoft Excel" );
457cdf0e10cSrcweir     }
458cdf0e10cSrcweir     else
459cdf0e10cSrcweir     {
460cdf0e10cSrcweir         registerSomeExtensions( handle, EXCEL_START, POWERPOINT_START, false );
461cdf0e10cSrcweir         MsiSetFeatureState( handle, L"gm_p_Calc_MSO_Reg", INSTALLSTATE_ABSENT );
462cdf0e10cSrcweir     }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "SELECT_POWERPOINT" ) )
465cdf0e10cSrcweir     {
466cdf0e10cSrcweir         registerSomeExtensions( handle, POWERPOINT_START, POWERPOINT_END, true );
467cdf0e10cSrcweir         MsiSetFeatureState( handle, L"gm_p_Impress_MSO_Reg", INSTALLSTATE_LOCAL );
468*c36aa0f1Smseidel         OutputDebugStringFormat( "RegisterSomeExtensions: Register for Microsoft PowerPoint" );
469cdf0e10cSrcweir     }
470cdf0e10cSrcweir     else
471cdf0e10cSrcweir     {
472cdf0e10cSrcweir         registerSomeExtensions( handle, POWERPOINT_START, POWERPOINT_END, false );
473cdf0e10cSrcweir         MsiSetFeatureState( handle, L"gm_p_Impress_MSO_Reg", INSTALLSTATE_ABSENT );
474cdf0e10cSrcweir     }
475cdf0e10cSrcweir 
476cdf0e10cSrcweir     return ERROR_SUCCESS;
477cdf0e10cSrcweir }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir //----------------------------------------------------------
FindRegisteredExtensions(MSIHANDLE handle)480cdf0e10cSrcweir extern "C" UINT __stdcall FindRegisteredExtensions( MSIHANDLE handle )
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "FILETYPEDIALOGUSED" ) )
483cdf0e10cSrcweir     {
484cdf0e10cSrcweir         OutputDebugStringFormat( "FindRegisteredExtensions: FILETYPEDIALOGUSED!" );
485cdf0e10cSrcweir         return ERROR_SUCCESS;
486cdf0e10cSrcweir     }
487cdf0e10cSrcweir 
488cdf0e10cSrcweir     OutputDebugStringFormat( "FindRegisteredExtensions:" );
489*c36aa0f1Smseidel 
490cdf0e10cSrcweir     bool bRegisterAll = IsSetMsiProp( handle, "REGISTER_ALL_MSO_TYPES" );
491cdf0e10cSrcweir 
492cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "REGISTER_NO_MSO_TYPES" ) )
493cdf0e10cSrcweir     {
494cdf0e10cSrcweir         OutputDebugStringFormat( "FindRegisteredExtensions: Register none!" );
495cdf0e10cSrcweir         return ERROR_SUCCESS;
496cdf0e10cSrcweir     }
497cdf0e10cSrcweir     else if ( bRegisterAll )
498cdf0e10cSrcweir         OutputDebugStringFormat( "FindRegisteredExtensions: Force all on" );
499cdf0e10cSrcweir     else
500cdf0e10cSrcweir         OutputDebugStringFormat( "FindRegisteredExtensions: " );
501cdf0e10cSrcweir 
502*c36aa0f1Smseidel     // setting the msi properties SELECT_* will force registering for all corresponding
503cdf0e10cSrcweir     // file types
504cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "SELECT_WORD" ) )
505cdf0e10cSrcweir         registerSomeExtensions( handle, WORD_START, EXCEL_START, true );
506cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "SELECT_EXCEL" ) )
507cdf0e10cSrcweir         registerSomeExtensions( handle, EXCEL_START, POWERPOINT_START, true );
508cdf0e10cSrcweir     if ( IsSetMsiProp( handle, "SELECT_POWERPOINT" ) )
509cdf0e10cSrcweir         registerSomeExtensions( handle, POWERPOINT_START, POWERPOINT_END, true );
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     registerForExtensions( handle, bRegisterAll );
512cdf0e10cSrcweir 
513cdf0e10cSrcweir     return ERROR_SUCCESS;
514cdf0e10cSrcweir }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir //----------------------------------------------------------
DeleteRegisteredExtensions(MSIHANDLE)517cdf0e10cSrcweir extern "C" UINT __stdcall DeleteRegisteredExtensions( MSIHANDLE /*handle*/ )
518*c36aa0f1Smseidel {
519cdf0e10cSrcweir     OutputDebugStringFormat( "DeleteRegisteredExtensions\n" );
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     // remove all file extensions
522cdf0e10cSrcweir     int nIndex = 0;
523cdf0e10cSrcweir     while ( g_Extensions[nIndex] != 0 )
524cdf0e10cSrcweir     {
525cdf0e10cSrcweir         RemoveExtensionInRegistry( g_Extensions[nIndex] );
526cdf0e10cSrcweir         ++nIndex;
527cdf0e10cSrcweir     }
528cdf0e10cSrcweir 
529cdf0e10cSrcweir     return ERROR_SUCCESS;
530cdf0e10cSrcweir }
531