Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / shellextensions / layerlinks.cxx
blob28ecbfa20766e42aace7e555da8c6b8aa0e1ca63
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #undef UNICODE
30 #undef _UNICODE
32 #define _WIN32_WINDOWS 0x0410
34 #ifdef _MSC_VER
35 #pragma warning(push, 1) /* disable warnings within system headers */
36 #endif
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #include <msiquery.h>
40 #ifdef _MSC_VER
41 #pragma warning(pop)
42 #endif
44 #include <malloc.h>
45 #include <assert.h>
47 #include <tchar.h>
48 #include <string>
49 #include <systools/win32/uwinapi.h>
51 #include <../tools/seterror.hxx>
53 using namespace std;
55 namespace
57 string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
59 string result;
60 TCHAR szDummy[1] = TEXT("");
61 DWORD nChars = 0;
63 if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
65 DWORD nBytes = ++nChars * sizeof(TCHAR);
66 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
67 ZeroMemory( buffer, nBytes );
68 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
69 result = buffer;
71 return result;
74 inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
76 return (GetMsiProperty(handle, sProperty).length() > 0);
79 inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
81 MsiSetProperty(handle, sProperty.c_str(), NULL);
84 inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string&)
86 MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
89 void stripFinalBackslash(std::string * path) {
90 std::string::size_type i = path->size();
91 if (i > 1) {
92 --i;
93 if ((*path)[i] == '\\') {
94 path->erase(i);
98 } // namespace
100 extern "C" UINT __stdcall CreateLayerLinks(MSIHANDLE handle)
102 string sInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
104 string sUreInstallPath = sInstallPath + TEXT("URE");
106 string sUreLinkPath = sInstallPath + TEXT("ure-link");
108 if ( IsSetMsiProperty(handle, TEXT("ADMININSTALL")) )
110 sUreInstallPath = TEXT("..\\URE");
113 stripFinalBackslash(&sUreInstallPath);
115 // creating ure-link
117 HANDLE hfile = CreateFile(
118 sUreLinkPath.c_str(),
119 GENERIC_WRITE,
121 NULL,
122 CREATE_NEW,
123 FILE_ATTRIBUTE_NORMAL,
124 NULL);
126 if (IsValidHandle(hfile))
128 DWORD dummy;
130 // Converting string into UTF-8 encoding and writing into file "ure-link"
132 int nCharsRequired = MultiByteToWideChar( CP_ACP, 0, sUreInstallPath.c_str(), -1, NULL, 0 );
133 if ( nCharsRequired )
135 LPWSTR lpPathW = new WCHAR[nCharsRequired];
136 if ( MultiByteToWideChar( CP_ACP, 0, sUreInstallPath.c_str(), -1, lpPathW, nCharsRequired ) )
138 nCharsRequired = WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, NULL, 0, NULL, NULL );
139 if ( nCharsRequired )
141 LPSTR lpPathUTF8 = new CHAR[nCharsRequired];
142 WideCharToMultiByte( CP_UTF8, 0, lpPathW, -1, lpPathUTF8, nCharsRequired, NULL, NULL );
144 WriteFile( hfile, lpPathUTF8, strlen(lpPathUTF8) ,&dummy, 0 );
146 delete lpPathUTF8;
150 delete lpPathW;
153 CloseHandle(hfile);
156 return ERROR_SUCCESS;
159 extern "C" UINT __stdcall RemoveLayerLinks(MSIHANDLE handle)
161 string sInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
163 string sUreLinkPath = sInstallPath + TEXT("ure-link");
164 string sUreDirName = sInstallPath + TEXT("URE\\bin");
166 // Check, if URE is still installed
167 bool ureDirExists = true;
168 WIN32_FIND_DATA aFindData;
169 HANDLE hFindContent = FindFirstFile( sUreDirName.c_str(), &aFindData );
170 if ( hFindContent == INVALID_HANDLE_VALUE ) { ureDirExists = false; }
171 FindClose( hFindContent );
173 // Deleting link to URE layer, if URE dir no longer exists
174 if ( ! ureDirExists )
176 DeleteFile(sUreLinkPath.c_str());
179 return ERROR_SUCCESS;
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */