1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #undef UNICODE
26cdf0e10cSrcweir #undef _UNICODE
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #ifdef _MSC_VER
31cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
32cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <windows.h>
35cdf0e10cSrcweir #include <msiquery.h>
36cdf0e10cSrcweir #include <shellapi.h>
37cdf0e10cSrcweir #ifdef _MSC_VER
38cdf0e10cSrcweir #pragma warning(pop)
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <malloc.h>
42cdf0e10cSrcweir #include <assert.h>
43cdf0e10cSrcweir #include <string.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #ifdef UNICODE
46cdf0e10cSrcweir #define _UNICODE
47cdf0e10cSrcweir #define _tstring	wstring
48cdf0e10cSrcweir #else
49cdf0e10cSrcweir #define _tstring	string
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir #include <tchar.h>
52cdf0e10cSrcweir #include <string>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
GetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty)55cdf0e10cSrcweir static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     std::_tstring result;
58cdf0e10cSrcweir     TCHAR szDummy[1] = TEXT("");
59cdf0e10cSrcweir     DWORD nChars = 0;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         DWORD nBytes = ++nChars * sizeof(TCHAR);
64cdf0e10cSrcweir         LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
65cdf0e10cSrcweir         ZeroMemory( buffer, nBytes );
66cdf0e10cSrcweir         MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
67cdf0e10cSrcweir         result = buffer;
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     return result;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
copyExtensionData(MSIHANDLE handle)73cdf0e10cSrcweir extern "C" UINT __stdcall copyExtensionData(MSIHANDLE handle) {
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     std::_tstring sSourceDir = GetMsiProperty( handle, TEXT("SourceDir") );
76cdf0e10cSrcweir     std::_tstring sExtensionDir = sSourceDir + TEXT("extension\\");
77cdf0e10cSrcweir     std::_tstring sPattern = sExtensionDir + TEXT("*.oxt");
78cdf0e10cSrcweir     // std::_tstring mystr;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     // Finding all oxt files in sExtensionDir
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     WIN32_FIND_DATA aFindFileData;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     HANDLE hFindOxt = FindFirstFile( sPattern.c_str(), &aFindFileData );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     if ( hFindOxt != INVALID_HANDLE_VALUE )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         bool fNextFile = false;
89cdf0e10cSrcweir         bool fSuccess = true;
90cdf0e10cSrcweir         bool bFailIfExist = true;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         std::_tstring sDestDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
93cdf0e10cSrcweir         std::_tstring sShareInstallDir = sDestDir + TEXT("share\\extension\\install\\");
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // creating directories
96cdf0e10cSrcweir         std::_tstring sShareDir = sDestDir + TEXT("share");
97cdf0e10cSrcweir         std::_tstring sExtDir = sShareDir + TEXT("\\extension");
98cdf0e10cSrcweir         std::_tstring sExtInstDir = sExtDir + TEXT("\\install");
99cdf0e10cSrcweir         bool bDir = CreateDirectory(sShareDir.c_str(), NULL);
100cdf0e10cSrcweir         bDir = CreateDirectory(sExtDir.c_str(), NULL);
101cdf0e10cSrcweir         bDir = CreateDirectory(sExtInstDir.c_str(), NULL);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         do
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             std::_tstring sOxtFile = aFindFileData.cFileName;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir             std::_tstring sSourceFile = sExtensionDir + sOxtFile;
108cdf0e10cSrcweir             std::_tstring sDestFile = sShareInstallDir + sOxtFile;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             fSuccess = CopyFile( sSourceFile.c_str(), sDestFile.c_str(), bFailIfExist );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir             fNextFile = FindNextFile( hFindOxt, &aFindFileData );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         } while ( fNextFile );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         FindClose( hFindOxt );
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     return ERROR_SUCCESS;
120cdf0e10cSrcweir }
121