Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / shellextensions / registerextensions.cxx
blobb1c1810e43e58c3f5129f7f346eb0f37a8980a99
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #undef UNICODE
30 #undef _UNICODE
32 #define _WIN32_WINDOWS 0x0410
34 #ifdef _MSC_VER
35 #pragma warning(push, 1) /* disable warnings within system headers */
36 #define WIN32_LEAN_AND_MEAN
37 #endif
38 #include <windows.h>
39 #include <msiquery.h>
40 #include <shellapi.h>
41 #ifdef _MSC_VER
42 #pragma warning(pop)
43 #endif
45 #include <malloc.h>
46 #include <assert.h>
47 #include <string.h>
49 #ifdef UNICODE
50 #define _UNICODE
51 #define _tstring wstring
52 #else
53 #define _tstring string
54 #endif
55 #include <tchar.h>
56 #include <string>
58 static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
60 std::_tstring result;
61 TCHAR szDummy[1] = TEXT("");
62 DWORD nChars = 0;
64 if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
66 DWORD nBytes = ++nChars * sizeof(TCHAR);
67 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
68 ZeroMemory( buffer, nBytes );
69 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
70 result = buffer;
73 return result;
76 /* creates a child process which is specified in lpCommand.
78 out_exitCode is the exit code of the child process
79 **/
80 static BOOL ExecuteCommand( LPCTSTR lpCommand, DWORD * out_exitCode)
82 BOOL fSuccess = FALSE;
83 STARTUPINFO si;
84 PROCESS_INFORMATION pi;
86 ZeroMemory( &si, sizeof(si) );
87 si.cb = sizeof(si);
89 fSuccess = CreateProcess(
90 NULL,
91 (LPTSTR)lpCommand,
92 NULL,
93 NULL,
94 FALSE,
96 NULL,
97 NULL,
98 &si,
99 &pi
102 if ( fSuccess )
104 WaitForSingleObject( pi.hProcess, INFINITE );
106 if (!GetExitCodeProcess( pi.hProcess, out_exitCode))
107 fSuccess = FALSE;
109 CloseHandle( pi.hProcess );
110 CloseHandle( pi.hThread );
113 return fSuccess;
116 static BOOL RemoveCompleteDirectory( std::_tstring sPath )
118 bool bDirectoryRemoved = true;
120 std::_tstring sPattern = sPath + TEXT("\\") + TEXT("*.*");
121 WIN32_FIND_DATA aFindData;
123 // Finding all content in sPath
125 HANDLE hFindContent = FindFirstFile( sPattern.c_str(), &aFindData );
127 if ( hFindContent != INVALID_HANDLE_VALUE )
129 bool fNextFile = false;
133 std::_tstring sFileName = aFindData.cFileName;
134 std::_tstring sCurrentDir = TEXT(".");
135 std::_tstring sParentDir = TEXT("..");
137 if (( strcmp(sFileName.c_str(),sCurrentDir.c_str()) != 0 ) &&
138 ( strcmp(sFileName.c_str(),sParentDir.c_str()) != 0 ))
140 std::_tstring sCompleteFileName = sPath + TEXT("\\") + sFileName;
142 if ( aFindData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
144 RemoveCompleteDirectory(sCompleteFileName);
146 else
148 DeleteFile( sCompleteFileName.c_str() );
152 fNextFile = FindNextFile( hFindContent, &aFindData );
154 } while ( fNextFile );
156 FindClose( hFindContent );
158 // empty directory can be removed now
159 // RemoveDirectory is only successful, if the last handle to the directory is closed
160 // -> first removing content -> closing handle -> remove empty directory
162 if ( ! ( RemoveDirectory(sPath.c_str()) ) )
164 bDirectoryRemoved = false;
168 return bDirectoryRemoved;
171 extern "C" UINT __stdcall RegisterExtensions(MSIHANDLE handle)
173 std::_tstring sInstDir = GetMsiProperty( handle, TEXT("CustomActionData") );
174 std::_tstring sUnoPkgFile = sInstDir + TEXT("program\\unopkg.exe");
175 std::_tstring mystr;
177 WIN32_FIND_DATA aFindFileData;
178 bool registrationError = false;
180 // Find unopkg.exe
181 HANDLE hFindUnopkg = FindFirstFile( sUnoPkgFile.c_str(), &aFindFileData );
183 if ( hFindUnopkg != INVALID_HANDLE_VALUE )
185 // unopkg.exe exists in program directory
186 std::_tstring sCommand = sUnoPkgFile + " sync";
188 DWORD exitCode = 0;
189 bool fSuccess = ExecuteCommand( sCommand.c_str(), & exitCode);
190 if ( ! fSuccess )
192 mystr = "ERROR: An error occured during registration of extensions!";
193 MessageBox(NULL, mystr.c_str(), "ERROR", MB_OK);
194 registrationError = true;
197 FindClose( hFindUnopkg );
200 if ( registrationError )
202 return 1;
204 else
206 return ERROR_SUCCESS;
211 extern "C" UINT __stdcall RemoveExtensions(MSIHANDLE handle)
213 std::_tstring sInstDir = GetMsiProperty( handle, TEXT("CustomActionData") );
215 // Removing complete directory "share\prereg\bundled"
216 RemoveCompleteDirectory( sInstDir + TEXT("share\\prereg\\bundled") );
218 // At this point we need to take care about removing upper directories,
219 // because we are after InstallFinalize. We remove only empty directories.
220 RemoveDirectory( (sInstDir + TEXT("share\\prereg")).c_str() );
221 RemoveDirectory( (sInstDir + TEXT("share")).c_str() );
222 RemoveDirectory( sInstDir.c_str() );
224 return ERROR_SUCCESS;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */