xref: /AOO41X/main/setup_native/source/win32/customactions/shellextensions/checkdirectory.cxx (revision 79aad27f7f29270c03e208e3d687e8e3850af11d)
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
10cdf0e10cSrcweir  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #define _WIN32_WINNT 0x0401
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifdef _MSC_VER
27cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
30cdf0e10cSrcweir #include <windows.h>
31cdf0e10cSrcweir #include <msiquery.h>
32cdf0e10cSrcweir #ifdef _MSC_VER
33cdf0e10cSrcweir #pragma warning(pop)
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <malloc.h>
37cdf0e10cSrcweir #include <assert.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #ifdef UNICODE
40cdf0e10cSrcweir #define _UNICODE
41cdf0e10cSrcweir #define _tstring	wstring
42cdf0e10cSrcweir #else
43cdf0e10cSrcweir #define _tstring	string
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #include <tchar.h>
46cdf0e10cSrcweir #include <string>
47cdf0e10cSrcweir #include <queue>
48cdf0e10cSrcweir #include <stdio.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
51cdf0e10cSrcweir #include <../tools/seterror.hxx>
52cdf0e10cSrcweir 
GetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty)53cdf0e10cSrcweir static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     std::_tstring result;
56cdf0e10cSrcweir     TCHAR szDummy[1] = TEXT("");
57cdf0e10cSrcweir     DWORD nChars = 0;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         DWORD nBytes = ++nChars * sizeof(TCHAR);
62cdf0e10cSrcweir         LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
63cdf0e10cSrcweir         ZeroMemory( buffer, nBytes );
64cdf0e10cSrcweir         MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
65cdf0e10cSrcweir         result = buffer;
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     return result;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
UnsetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty)71cdf0e10cSrcweir static void UnsetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     MsiSetProperty(handle, sProperty.c_str(), NULL);
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
SetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty,const std::_tstring &)76cdf0e10cSrcweir static void SetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty, const std::_tstring&)
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
CheckInstallDirectory(MSIHANDLE handle)81cdf0e10cSrcweir extern "C" UINT __stdcall CheckInstallDirectory(MSIHANDLE handle)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     std::_tstring sInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
84cdf0e10cSrcweir     std::_tstring sOfficeHostnamePath = GetMsiProperty(handle, TEXT("OFFICEDIRHOSTNAME"));
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     // MessageBox(NULL, sInstallPath.c_str(), "DEBUG", MB_OK);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     // unsetting all properties
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("DIRECTORY_NOT_EMPTY") );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     // 1. Searching for file setup.ini
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     std::_tstring sSetupIniPath = sInstallPath + sOfficeHostnamePath + TEXT("\\program\\setup.ini");
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     WIN32_FIND_DATA data;
97cdf0e10cSrcweir     HANDLE hdl = FindFirstFile(sSetupIniPath.c_str(), &data);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     // std::_tstring mystr = "Searching for " + sSetupIniPath;
100cdf0e10cSrcweir     // MessageBox(NULL, mystr.c_str(), "DEBUG", MB_OK);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     if ( IsValidHandle(hdl) )
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         // setup.ini found -> directory cannot be used for installation.
105cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("DIRECTORY_NOT_EMPTY"), TEXT("1") );
106cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_DIRECTORY_NOT_EMPTY );
107cdf0e10cSrcweir         // std::_tstring notEmptyStr = "Directory is not empty. Please choose another installation directory.";
108cdf0e10cSrcweir         // std::_tstring notEmptyTitle = "Directory not empty";
109cdf0e10cSrcweir         // MessageBox(NULL, notEmptyStr.c_str(), notEmptyTitle.c_str(), MB_OK);
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     return ERROR_SUCCESS;
113cdf0e10cSrcweir }
114