xref: /AOO41X/main/setup_native/source/win32/customactions/shellextensions/layerlinks.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 #undef UNICODE
29 #undef _UNICODE
30 
31 #define _WIN32_WINDOWS 0x0410
32 
33 #ifdef _MSC_VER
34 #pragma warning(push, 1) /* disable warnings within system headers */
35 #endif
36 #define WIN32_LEAN_AND_MEAN
37 #include <windows.h>
38 #include <msiquery.h>
39 #ifdef _MSC_VER
40 #pragma warning(pop)
41 #endif
42 
43 #include <malloc.h>
44 #include <assert.h>
45 
46 #include <tchar.h>
47 #include <string>
48 #include <systools/win32/uwinapi.h>
49 
50 #include <../tools/seterror.hxx>
51 
52 using namespace std;
53 
54 namespace
55 {
56     string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
57     {
58         string  result;
59         TCHAR   szDummy[1] = TEXT("");
60         DWORD   nChars = 0;
61 
62         if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
63         {
64             DWORD nBytes = ++nChars * sizeof(TCHAR);
65             LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
66             ZeroMemory( buffer, nBytes );
67             MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
68             result = buffer;
69         }
70         return result;
71     }
72 
73     inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
74     {
75         return (GetMsiProperty(handle, sProperty).length() > 0);
76     }
77 
78     inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
79     {
80         MsiSetProperty(handle, sProperty.c_str(), NULL);
81     }
82 
83     inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string&)
84     {
85         MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
86     }
87 
88     void stripFinalBackslash(std::string * path) {
89         std::string::size_type i = path->size();
90         if (i > 1) {
91             --i;
92             if ((*path)[i] == '\\') {
93                 path->erase(i);
94             }
95         }
96     }
97 } // namespace
98 
99 extern "C" UINT __stdcall CreateLayerLinks(MSIHANDLE handle)
100 {
101     string sInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
102 
103     string sOfficeInstallPath = sInstallPath;
104     string sBasisInstallPath = sInstallPath + TEXT("Basis\\");
105     string sUreInstallPath = sInstallPath + TEXT("URE\\");
106 
107     string sBasisLinkPath = sInstallPath + TEXT("basis-link");
108     string sUreLinkPath = sInstallPath + TEXT("Basis\\ure-link");
109 
110     if ( IsSetMsiProperty(handle, TEXT("ADMININSTALL")) )
111     {
112         sBasisInstallPath = TEXT("Basis");
113         sUreInstallPath = TEXT("..\\URE");
114     }
115 
116     stripFinalBackslash(&sBasisInstallPath);
117     stripFinalBackslash(&sUreInstallPath);
118 
119     // string myText1 = TEXT("Creating Basis-Link: ") + sBasisLinkPath;
120     // string myText2 = TEXT("Creating Ure-Link: ") + sUreLinkPath;
121     // MessageBox(NULL, myText1.c_str(), "DEBUG", MB_OK);
122     // MessageBox(NULL, myText2.c_str(), "DEBUG", MB_OK);
123 
124     // creating basis-link in brand layer
125 
126     HANDLE h1file = CreateFile(
127         sBasisLinkPath.c_str(),
128         GENERIC_WRITE,
129         0,
130         NULL,
131         CREATE_NEW,
132         FILE_ATTRIBUTE_NORMAL,
133         NULL);
134 
135     if (IsValidHandle(h1file))
136     {
137         DWORD dummy;
138 
139         // Converting string into UTF-8 encoding and writing into file "basis-link"
140 
141         int nCharsRequired = MultiByteToWideChar( CP_ACP, 0, sBasisInstallPath.c_str(), -1, NULL, 0 );
142         if ( nCharsRequired )
143         {
144             LPWSTR	lpPathW = new WCHAR[nCharsRequired];
145             if ( MultiByteToWideChar( CP_ACP, 0, sBasisInstallPath.c_str(), -1, lpPathW, nCharsRequired ) )
146             {
147                 nCharsRequired = WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, NULL, 0, NULL, NULL );
148                 if ( nCharsRequired )
149                 {
150                     LPSTR	lpPathUTF8 = new CHAR[nCharsRequired];
151                     WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, lpPathUTF8, nCharsRequired, NULL, NULL );
152 
153                     // WriteFile( h1file, sBasisInstallPath.c_str(), sBasisInstallPath.size() ,&dummy, 0 );
154                     WriteFile( h1file, lpPathUTF8, strlen(lpPathUTF8) ,&dummy, 0 );
155 
156                     delete lpPathUTF8;
157                 }
158             }
159 
160             delete lpPathW;
161         }
162 
163         CloseHandle(h1file);
164     }
165 
166     // creating ure-link in basis layer
167 
168     HANDLE h2file = CreateFile(
169         sUreLinkPath.c_str(),
170         GENERIC_WRITE,
171         0,
172         NULL,
173         CREATE_NEW,
174         FILE_ATTRIBUTE_NORMAL,
175         NULL);
176 
177     if (IsValidHandle(h2file))
178     {
179         DWORD dummy;
180 
181         // Converting string into UTF-8 encoding and writing into file "basis-link"
182 
183         int nCharsRequired = MultiByteToWideChar( CP_ACP, 0, sUreInstallPath.c_str(), -1, NULL, 0 );
184         if ( nCharsRequired )
185         {
186             LPWSTR	lpPathW = new WCHAR[nCharsRequired];
187             if ( MultiByteToWideChar( CP_ACP, 0, sUreInstallPath.c_str(), -1, lpPathW, nCharsRequired ) )
188             {
189                 nCharsRequired = WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, NULL, 0, NULL, NULL );
190                 if ( nCharsRequired )
191                 {
192                     LPSTR	lpPathUTF8 = new CHAR[nCharsRequired];
193                     WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, lpPathUTF8, nCharsRequired, NULL, NULL );
194 
195                     // WriteFile( h2file, sUreInstallPath.c_str(), sUreInstallPath.size() ,&dummy, 0 );
196                     WriteFile( h2file, lpPathUTF8, strlen(lpPathUTF8) ,&dummy, 0 );
197 
198                     delete lpPathUTF8;
199                 }
200             }
201 
202             delete lpPathW;
203         }
204 
205         CloseHandle(h2file);
206     }
207 
208     return ERROR_SUCCESS;
209 }
210 
211 extern "C" UINT __stdcall RemoveLayerLinks(MSIHANDLE handle)
212 {
213     string sInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
214 
215     string sOfficeInstallPath = sInstallPath;
216     string sBasisInstallPath = sInstallPath + TEXT("Basis\\");
217     string sUreInstallPath = sInstallPath + TEXT("URE\\");
218 
219     string sBasisLinkPath = sOfficeInstallPath + TEXT("basis-link");
220     string sUreLinkPath = sBasisInstallPath + TEXT("ure-link");
221     string sUreDirName = sUreInstallPath + TEXT("bin");
222 
223     // string myText2 = TEXT("Deleting Ure-Link: ") + sUreLinkPath;
224     // MessageBox(NULL, myText2.c_str(), "DEBUG", MB_OK);
225 
226     // Deleting link to basis layer
227     // string myText1 = TEXT("Deleting Basis-Link: ") + sBasisLinkPath;
228     // MessageBox(NULL, myText1.c_str(), "DEBUG", MB_OK);
229     DeleteFile(sBasisLinkPath.c_str());
230 
231     // Check, if URE is still installed
232     bool ureDirExists = true;
233     WIN32_FIND_DATA aFindData;
234     HANDLE hFindContent = FindFirstFile( sUreDirName.c_str(), &aFindData );
235     if ( hFindContent == INVALID_HANDLE_VALUE ) { ureDirExists = false; }
236     FindClose( hFindContent );
237 
238     // if ( ureDirExists )
239     // {
240     //     string myText3 = TEXT("URE directory still exists: ") + sUreDirName;
241     //     MessageBox(NULL, myText3.c_str(), "DEBUG", MB_OK);
242     //     string myText4 = TEXT("URE link NOT removed: ") + sUreLinkPath;
243     //     MessageBox(NULL, myText4.c_str(), "DEBUG", MB_OK);
244     // }
245 
246     // Deleting link to URE layer, if URE dir no longer exists
247     if ( ! ureDirExists )
248     {
249     //     string myText5 = TEXT("URE directory does not exist: ") + sUreDirName;
250     //     MessageBox(NULL, myText5.c_str(), "DEBUG", MB_OK);
251     	DeleteFile(sUreLinkPath.c_str());
252     //     string myText6 = TEXT("URE link removed: ") + sUreLinkPath;
253     //     MessageBox(NULL, myText6.c_str(), "DEBUG", MB_OK);
254     }
255 
256     return ERROR_SUCCESS;
257 }
258