Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / shellextensions / layerlinks.cxx
bloba9a3463376e0383bde88d652d0a61a3bed2fc335
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: layerlinks.cxx,v $
10 * $Revision: 1.5 $
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 #undef UNICODE
32 #undef _UNICODE
34 #define _WIN32_WINDOWS 0x0410
36 #ifdef _MSC_VER
37 #pragma warning(push, 1) /* disable warnings within system headers */
38 #endif
39 #define WIN32_LEAN_AND_MEAN
40 #include <windows.h>
41 #include <msiquery.h>
42 #ifdef _MSC_VER
43 #pragma warning(pop)
44 #endif
46 #include <malloc.h>
47 #include <assert.h>
49 #include <tchar.h>
50 #include <string>
51 #include <systools/win32/uwinapi.h>
53 #include <../tools/seterror.hxx>
55 using namespace std;
57 namespace
59 string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
61 string result;
62 TCHAR szDummy[1] = TEXT("");
63 DWORD nChars = 0;
65 if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
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;
73 return result;
76 inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
78 return (GetMsiProperty(handle, sProperty).length() > 0);
81 inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
83 MsiSetProperty(handle, sProperty.c_str(), NULL);
86 inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string&)
88 MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
91 void stripFinalBackslash(std::string * path) {
92 std::string::size_type i = path->size();
93 if (i > 1) {
94 --i;
95 if ((*path)[i] == '\\') {
96 path->erase(i);
100 } // namespace
102 extern "C" UINT __stdcall CreateLayerLinks(MSIHANDLE handle)
104 string sOfficeInstallPath = GetMsiProperty(handle, TEXT("OFFICEINSTALLLOCATION"));
105 string sBasisInstallPath = GetMsiProperty(handle, TEXT("BASISINSTALLLOCATION"));
106 string sUreInstallPath = GetMsiProperty(handle, TEXT("UREINSTALLLOCATION"));
108 string sBasisLinkPath = sOfficeInstallPath + TEXT("basis-link");
109 string sUreLinkPath = sBasisInstallPath + TEXT("ure-link");
111 if ( IsSetMsiProperty(handle, TEXT("ADMININSTALL")) )
113 sBasisInstallPath = TEXT("Basis");
114 sUreInstallPath = TEXT("..\\URE");
117 stripFinalBackslash(&sBasisInstallPath);
118 stripFinalBackslash(&sUreInstallPath);
120 // string myText1 = TEXT("Creating Basis-Link: ") + sBasisLinkPath;
121 // string myText2 = TEXT("Creating Ure-Link: ") + sUreLinkPath;
122 // MessageBox(NULL, myText1.c_str(), "DEBUG", MB_OK);
123 // MessageBox(NULL, myText2.c_str(), "DEBUG", MB_OK);
125 // creating basis-link in brand layer
127 HANDLE h1file = CreateFile(
128 sBasisLinkPath.c_str(),
129 GENERIC_WRITE,
131 NULL,
132 CREATE_NEW,
133 FILE_ATTRIBUTE_NORMAL,
134 NULL);
136 if (IsValidHandle(h1file))
138 DWORD dummy;
140 // Converting string into UTF-8 encoding and writing into file "basis-link"
142 int nCharsRequired = MultiByteToWideChar( CP_ACP, 0, sBasisInstallPath.c_str(), -1, NULL, 0 );
143 if ( nCharsRequired )
145 LPWSTR lpPathW = new WCHAR[nCharsRequired];
146 if ( MultiByteToWideChar( CP_ACP, 0, sBasisInstallPath.c_str(), -1, lpPathW, nCharsRequired ) )
148 nCharsRequired = WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, NULL, 0, NULL, NULL );
149 if ( nCharsRequired )
151 LPSTR lpPathUTF8 = new CHAR[nCharsRequired];
152 WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, lpPathUTF8, nCharsRequired, NULL, NULL );
154 // WriteFile( h1file, sBasisInstallPath.c_str(), sBasisInstallPath.size() ,&dummy, 0 );
155 WriteFile( h1file, lpPathUTF8, strlen(lpPathUTF8) ,&dummy, 0 );
157 delete lpPathUTF8;
161 delete lpPathW;
164 CloseHandle(h1file);
167 // creating ure-link in basis layer
169 HANDLE h2file = CreateFile(
170 sUreLinkPath.c_str(),
171 GENERIC_WRITE,
173 NULL,
174 CREATE_NEW,
175 FILE_ATTRIBUTE_NORMAL,
176 NULL);
178 if (IsValidHandle(h2file))
180 DWORD dummy;
182 // Converting string into UTF-8 encoding and writing into file "basis-link"
184 int nCharsRequired = MultiByteToWideChar( CP_ACP, 0, sUreInstallPath.c_str(), -1, NULL, 0 );
185 if ( nCharsRequired )
187 LPWSTR lpPathW = new WCHAR[nCharsRequired];
188 if ( MultiByteToWideChar( CP_ACP, 0, sUreInstallPath.c_str(), -1, lpPathW, nCharsRequired ) )
190 nCharsRequired = WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, NULL, 0, NULL, NULL );
191 if ( nCharsRequired )
193 LPSTR lpPathUTF8 = new CHAR[nCharsRequired];
194 WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, lpPathUTF8, nCharsRequired, NULL, NULL );
196 // WriteFile( h2file, sUreInstallPath.c_str(), sUreInstallPath.size() ,&dummy, 0 );
197 WriteFile( h2file, lpPathUTF8, strlen(lpPathUTF8) ,&dummy, 0 );
199 delete lpPathUTF8;
203 delete lpPathW;
206 CloseHandle(h2file);
209 return ERROR_SUCCESS;
212 extern "C" UINT __stdcall RemoveLayerLinks(MSIHANDLE handle)
214 string sOfficeInstallPath = GetMsiProperty(handle, TEXT("OFFICEUNINSTALLLOCATION"));
215 string sBasisInstallPath = GetMsiProperty(handle, TEXT("BASISUNINSTALLLOCATION"));
216 string sUreInstallPath = GetMsiProperty(handle, TEXT("UREUNINSTALLLOCATION"));
218 string sBasisLinkPath = sOfficeInstallPath + TEXT("basis-link");
219 string sUreLinkPath = sBasisInstallPath + TEXT("ure-link");
220 string sUreDirName = sUreInstallPath + TEXT("bin");
222 // string myText2 = TEXT("Deleting Ure-Link: ") + sUreLinkPath;
223 // MessageBox(NULL, myText2.c_str(), "DEBUG", MB_OK);
225 // Deleting link to basis layer
226 // string myText1 = TEXT("Deleting Basis-Link: ") + sBasisLinkPath;
227 // MessageBox(NULL, myText1.c_str(), "DEBUG", MB_OK);
228 DeleteFile(sBasisLinkPath.c_str());
230 // Check, if URE is still installed
231 bool ureDirExists = true;
232 WIN32_FIND_DATA aFindData;
233 HANDLE hFindContent = FindFirstFile( sUreDirName.c_str(), &aFindData );
234 if ( hFindContent == INVALID_HANDLE_VALUE ) { ureDirExists = false; }
235 FindClose( hFindContent );
237 // if ( ureDirExists )
238 // {
239 // string myText3 = TEXT("URE directory still exists: ") + sUreDirName;
240 // MessageBox(NULL, myText3.c_str(), "DEBUG", MB_OK);
241 // string myText4 = TEXT("URE link NOT removed: ") + sUreLinkPath;
242 // MessageBox(NULL, myText4.c_str(), "DEBUG", MB_OK);
243 // }
245 // Deleting link to URE layer, if URE dir no longer exists
246 if ( ! ureDirExists )
248 // string myText5 = TEXT("URE directory does not exist: ") + sUreDirName;
249 // MessageBox(NULL, myText5.c_str(), "DEBUG", MB_OK);
250 DeleteFile(sUreLinkPath.c_str());
251 // string myText6 = TEXT("URE link removed: ") + sUreLinkPath;
252 // MessageBox(NULL, myText6.c_str(), "DEBUG", MB_OK);
255 return ERROR_SUCCESS;