xref: /AOO41X/main/setup_native/source/win32/customactions/shellextensions/copyextensiondata.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #undef UNICODE
30 #undef _UNICODE
31 
32 #define _WIN32_WINDOWS 0x0410
33 
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
44 
45 #include <malloc.h>
46 #include <assert.h>
47 #include <string.h>
48 
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>
57 
58 
59 static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
60 {
61     std::_tstring result;
62     TCHAR szDummy[1] = TEXT("");
63     DWORD nChars = 0;
64 
65     if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
66     {
67         DWORD nBytes = ++nChars * sizeof(TCHAR);
68         LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
69         ZeroMemory( buffer, nBytes );
70         MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
71         result = buffer;
72     }
73 
74     return result;
75 }
76 
77 extern "C" UINT __stdcall copyExtensionData(MSIHANDLE handle) {
78 
79     std::_tstring sSourceDir = GetMsiProperty( handle, TEXT("SourceDir") );
80     std::_tstring sExtensionDir = sSourceDir + TEXT("extension\\");
81     std::_tstring sPattern = sExtensionDir + TEXT("*.oxt");
82     // std::_tstring mystr;
83 
84     // Finding all oxt files in sExtensionDir
85 
86     WIN32_FIND_DATA aFindFileData;
87 
88     HANDLE hFindOxt = FindFirstFile( sPattern.c_str(), &aFindFileData );
89 
90     if ( hFindOxt != INVALID_HANDLE_VALUE )
91     {
92         bool fNextFile = false;
93         bool fSuccess = true;
94         bool bFailIfExist = true;
95 
96         std::_tstring sDestDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
97         std::_tstring sShareInstallDir = sDestDir + TEXT("share\\extension\\install\\");
98 
99         // creating directories
100         std::_tstring sShareDir = sDestDir + TEXT("share");
101         std::_tstring sExtDir = sShareDir + TEXT("\\extension");
102         std::_tstring sExtInstDir = sExtDir + TEXT("\\install");
103         bool bDir = CreateDirectory(sShareDir.c_str(), NULL);
104         bDir = CreateDirectory(sExtDir.c_str(), NULL);
105         bDir = CreateDirectory(sExtInstDir.c_str(), NULL);
106 
107         do
108         {
109             std::_tstring sOxtFile = aFindFileData.cFileName;
110 
111             std::_tstring sSourceFile = sExtensionDir + sOxtFile;
112             std::_tstring sDestFile = sShareInstallDir + sOxtFile;
113 
114             fSuccess = CopyFile( sSourceFile.c_str(), sDestFile.c_str(), bFailIfExist );
115 
116             fNextFile = FindNextFile( hFindOxt, &aFindFileData );
117 
118         } while ( fNextFile );
119 
120         FindClose( hFindOxt );
121     }
122 
123     return ERROR_SUCCESS;
124 }
125