Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / shellextensions / vistaspecial.cxx
blob40ff772ff878a41580a0ef58f9f4f4d176bb1576
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 #define _WIN32_WINNT 0x0401
31 #ifdef _MSC_VER
32 #pragma warning(push, 1) /* disable warnings within system headers */
33 #endif
34 #define WIN32_LEAN_AND_MEAN
35 #include <windows.h>
36 #include <msiquery.h>
37 #ifdef _MSC_VER
38 #pragma warning(pop)
39 #endif
41 #include <malloc.h>
42 #include <assert.h>
44 #ifdef UNICODE
45 #define _UNICODE
46 #define _tstring wstring
47 #else
48 #define _tstring string
49 #endif
50 #include <tchar.h>
51 #include <string>
52 #include <queue>
53 #include <stdio.h>
54 #include <strsafe.h>
56 #include <systools/win32/uwinapi.h>
57 #include <../tools/seterror.hxx>
59 //----------------------------------------------------------
60 #ifdef DEBUG
61 inline void OutputDebugStringFormat( LPCSTR pFormat, ... )
63 CHAR buffer[1024];
64 va_list args;
66 va_start( args, pFormat );
67 StringCchVPrintfA( buffer, sizeof(buffer), pFormat, args );
68 OutputDebugStringA( buffer );
70 #else
71 static inline void OutputDebugStringFormat( LPCSTR, ... )
74 #endif
77 static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
79 std::_tstring result;
80 TCHAR szDummy[1] = TEXT("");
81 DWORD nChars = 0;
83 if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
85 DWORD nBytes = ++nChars * sizeof(TCHAR);
86 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
87 ZeroMemory( buffer, nBytes );
88 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
89 result = buffer;
92 return result;
95 static BOOL RemoveCompleteDirectory( std::_tstring sPath )
97 bool bDirectoryRemoved = true;
99 std::_tstring sPattern = sPath + TEXT("\\") + TEXT("*.*");
100 WIN32_FIND_DATA aFindData;
102 // Finding all content in sPath
104 HANDLE hFindContent = FindFirstFile( sPattern.c_str(), &aFindData );
106 if ( hFindContent != INVALID_HANDLE_VALUE )
108 bool fNextFile = false;
109 std::_tstring sCurrentDir = TEXT(".");
110 std::_tstring sParentDir = TEXT("..");
114 std::_tstring sFileName = aFindData.cFileName;
116 if (( strcmp(sFileName.c_str(),sCurrentDir.c_str()) != 0 ) &&
117 ( strcmp(sFileName.c_str(),sParentDir.c_str()) != 0 ))
119 std::_tstring sCompleteFileName = sPath + TEXT("\\") + sFileName;
121 if ( aFindData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
123 RemoveCompleteDirectory(sCompleteFileName);
125 else
127 DeleteFile( sCompleteFileName.c_str() );
131 fNextFile = FindNextFile( hFindContent, &aFindData );
133 } while ( fNextFile );
135 FindClose( hFindContent );
137 // empty directory can be removed now
138 // RemoveDirectory is only successful, if the last handle to the directory is closed
139 // -> first removing content -> closing handle -> remove empty directory
142 if( !( RemoveDirectory(sPath.c_str()) ) )
144 bDirectoryRemoved = false;
148 return bDirectoryRemoved;
153 extern "C" UINT __stdcall RenamePrgFolder( MSIHANDLE handle )
155 std::_tstring sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
157 std::_tstring sRenameSrc = sOfficeInstallPath + TEXT("program");
158 std::_tstring sRenameDst = sOfficeInstallPath + TEXT("program_old");
160 bool bSuccess = MoveFile( sRenameSrc.c_str(), sRenameDst.c_str() );
161 if ( !bSuccess )
163 TCHAR sAppend[2] = TEXT("0");
164 for ( int i = 0; i < 10; i++ )
166 sRenameDst = sOfficeInstallPath + TEXT("program_old") + sAppend;
167 bSuccess = MoveFile( sRenameSrc.c_str(), sRenameDst.c_str() );
168 if ( bSuccess )
169 break;
170 sAppend[0] += 1;
174 return ERROR_SUCCESS;
177 extern "C" UINT __stdcall RemovePrgFolder( MSIHANDLE handle )
179 std::_tstring sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
180 std::_tstring sRemoveDir = sOfficeInstallPath + TEXT("program_old");
182 RemoveCompleteDirectory( sRemoveDir );
184 TCHAR sAppend[2] = TEXT("0");
185 for ( int i = 0; i < 10; i++ )
187 sRemoveDir = sOfficeInstallPath + TEXT("program_old") + sAppend;
188 RemoveCompleteDirectory( sRemoveDir );
189 sAppend[0] += 1;
192 return ERROR_SUCCESS;
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */