Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / shellextensions / vistaspecial.cxx
blobbd3e69fe686e4ba25eb5c56b212a8df6a1f14706
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: checkdirectory.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #define _WIN32_WINNT 0x0401
33 #ifdef _MSC_VER
34 #pragma warning(push, 1) /* disable warnings within system headers */
35 #endif
36 #define WIN32_LEAN_AND_MEAN
37 #include <windows.h>
38 #include <msiquery.h>
39 #ifdef _MSC_VER
40 #pragma warning(pop)
41 #endif
43 #include <malloc.h>
44 #include <assert.h>
46 #ifdef UNICODE
47 #define _UNICODE
48 #define _tstring wstring
49 #else
50 #define _tstring string
51 #endif
52 #include <tchar.h>
53 #include <string>
54 #include <queue>
55 #include <stdio.h>
56 #include <strsafe.h>
58 #include <systools/win32/uwinapi.h>
59 #include <../tools/seterror.hxx>
61 //----------------------------------------------------------
62 #ifdef DEBUG
63 inline void OutputDebugStringFormat( LPCSTR pFormat, ... )
65 CHAR buffer[1024];
66 va_list args;
68 va_start( args, pFormat );
69 StringCchVPrintfA( buffer, sizeof(buffer), pFormat, args );
70 OutputDebugStringA( buffer );
72 #else
73 static inline void OutputDebugStringFormat( LPCSTR, ... )
76 #endif
79 static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
81 std::_tstring result;
82 TCHAR szDummy[1] = TEXT("");
83 DWORD nChars = 0;
85 if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
87 DWORD nBytes = ++nChars * sizeof(TCHAR);
88 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
89 ZeroMemory( buffer, nBytes );
90 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
91 result = buffer;
94 return result;
97 static BOOL RemoveCompleteDirectory( std::_tstring sPath )
99 bool bDirectoryRemoved = true;
101 std::_tstring mystr;
102 std::_tstring sPattern = sPath + TEXT("\\") + TEXT("*.*");
103 WIN32_FIND_DATA aFindData;
105 // Finding all content in sPath
107 HANDLE hFindContent = FindFirstFile( sPattern.c_str(), &aFindData );
109 if ( hFindContent != INVALID_HANDLE_VALUE )
111 bool fNextFile = false;
112 std::_tstring sCurrentDir = TEXT(".");
113 std::_tstring sParentDir = TEXT("..");
117 std::_tstring sFileName = aFindData.cFileName;
119 mystr = "Current short file: " + sFileName;
120 // MessageBox(NULL, mystr.c_str(), "Current Content", MB_OK);
122 if (( strcmp(sFileName.c_str(),sCurrentDir.c_str()) != 0 ) &&
123 ( strcmp(sFileName.c_str(),sParentDir.c_str()) != 0 ))
125 std::_tstring sCompleteFileName = sPath + TEXT("\\") + sFileName;
127 if ( aFindData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
129 bool fSuccess = RemoveCompleteDirectory(sCompleteFileName);
130 if ( fSuccess )
132 mystr = "Successfully removed content of dir " + sCompleteFileName;
133 // MessageBox(NULL, mystr.c_str(), "Removed Directory", MB_OK);
135 else
137 mystr = "An error occured during removing content of " + sCompleteFileName;
138 // MessageBox(NULL, mystr.c_str(), "Error removing directory", MB_OK);
141 else
143 bool fSuccess = DeleteFile( sCompleteFileName.c_str() );
144 if ( fSuccess )
146 mystr = "Successfully removed file " + sCompleteFileName;
147 // MessageBox(NULL, mystr.c_str(), "Removed File", MB_OK);
149 else
151 mystr = "An error occured during removal of file " + sCompleteFileName;
152 // MessageBox(NULL, mystr.c_str(), "Error removing file", MB_OK);
157 fNextFile = FindNextFile( hFindContent, &aFindData );
159 } while ( fNextFile );
161 FindClose( hFindContent );
163 // empty directory can be removed now
164 // RemoveDirectory is only successful, if the last handle to the directory is closed
165 // -> first removing content -> closing handle -> remove empty directory
167 bool fRemoveDirSuccess = RemoveDirectory(sPath.c_str());
169 if ( fRemoveDirSuccess )
171 mystr = "Successfully removed dir " + sPath;
172 // MessageBox(NULL, mystr.c_str(), "Removed Directory", MB_OK);
174 else
176 mystr = "An error occured during removal of empty directory " + sPath;
177 // MessageBox(NULL, mystr.c_str(), "Error removing directory", MB_OK);
178 bDirectoryRemoved = false;
182 return bDirectoryRemoved;
187 extern "C" UINT __stdcall RenamePrgFolder( MSIHANDLE handle )
189 std::_tstring sOfficeInstallPath = GetMsiProperty(handle, TEXT("OFFICEINSTALLLOCATION"));
191 std::_tstring sRenameSrc = sOfficeInstallPath + TEXT("program");
192 std::_tstring sRenameDst = sOfficeInstallPath + TEXT("program_old");
194 // MessageBox(NULL, sRenameSrc.c_str(), "OFFICEINSTALLLOCATION", MB_OK);
196 bool bSuccess = MoveFile( sRenameSrc.c_str(), sRenameDst.c_str() );
197 if ( !bSuccess )
199 TCHAR sAppend[2] = TEXT("0");
200 for ( int i = 0; i < 10; i++ )
202 sRenameDst = sOfficeInstallPath + TEXT("program_old") + sAppend;
203 bSuccess = MoveFile( sRenameSrc.c_str(), sRenameDst.c_str() );
204 if ( bSuccess )
205 break;
206 sAppend[0] += 1;
210 #if 0
211 if ( !bSuccess )
212 MessageBox(NULL, "Renaming folder failed", "RenamePrgFolder", MB_OK);
213 else
214 MessageBox(NULL, "Renaming folder successful", "RenamePrgFolder", MB_OK);
215 #endif
217 return ERROR_SUCCESS;
220 extern "C" UINT __stdcall RemovePrgFolder( MSIHANDLE handle )
222 std::_tstring sOfficeInstallPath = GetMsiProperty(handle, TEXT("OFFICEINSTALLLOCATION"));
223 std::_tstring sRemoveDir = sOfficeInstallPath + TEXT("program_old");
225 // MessageBox(NULL, sRemoveDir.c_str(), "REMOVING OLD DIR", MB_OK);
227 bool bSuccess = RemoveCompleteDirectory( sRemoveDir );
229 TCHAR sAppend[2] = TEXT("0");
230 for ( int i = 0; i < 10; i++ )
232 sRemoveDir = sOfficeInstallPath + TEXT("program_old") + sAppend;
233 bSuccess = RemoveCompleteDirectory( sRemoveDir );
234 sAppend[0] += 1;
237 #if 0
238 if ( bSuccess )
239 MessageBox(NULL, "Removing folder successful", "RemovePrgFolder", MB_OK);
240 else
241 MessageBox(NULL, "Removing folder failed", "RemovePrgFolder", MB_OK);
242 #endif
244 return ERROR_SUCCESS;