Update ooo320-m1
[ooovba.git] / jvmfwk / source / fwkutil.cxx
blob3ee1ec6c8b0e2934801f8faea6906448faab1f1a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fwkutil.cxx,v $
10 * $Revision: 1.31 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_jvmfwk.hxx"
34 #if defined WNT
35 #if defined _MSC_VER
36 #pragma warning(push, 1)
37 #endif
38 #include <windows.h>
39 #if defined _MSC_VER
40 #pragma warning(pop)
41 #endif
42 #endif
44 #include <string>
45 #include <string.h>
46 #include "osl/mutex.hxx"
47 #include "osl/module.hxx"
48 #include "osl/thread.hxx"
49 #include "rtl/ustring.hxx"
50 #include "rtl/ustrbuf.hxx"
51 #include "rtl/bootstrap.hxx"
52 #include "osl/file.hxx"
53 #include "osl/process.h"
54 #include "rtl/instance.hxx"
55 #include "rtl/uri.hxx"
56 #include "osl/getglobalmutex.hxx"
57 #include "com/sun/star/lang/IllegalArgumentException.hpp"
58 #include "cppuhelper/bootstrap.hxx"
60 #include "framework.hxx"
61 #include "fwkutil.hxx"
63 using namespace rtl;
64 using namespace osl;
66 namespace jfw
69 bool isAccessibilitySupportDesired()
71 OUString sValue;
72 if ((sal_True == ::rtl::Bootstrap::get(
73 OUString(RTL_CONSTASCII_USTRINGPARAM("JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY")), sValue))
74 && sValue.equals(OUString(RTL_CONSTASCII_USTRINGPARAM("1")))
76 return false;
78 bool retVal = false;
79 #ifdef WNT
80 HKEY hKey = 0;
81 if (RegOpenKeyEx(HKEY_CURRENT_USER,
82 "Software\\OpenOffice.org\\Accessibility\\AtToolSupport",
83 0, KEY_READ, &hKey) == ERROR_SUCCESS)
85 DWORD dwType = 0;
86 DWORD dwLen = 16;
87 unsigned char arData[16];
88 if( RegQueryValueEx(hKey, "SupportAssistiveTechnology", NULL, &dwType, arData,
89 & dwLen)== ERROR_SUCCESS)
91 if (dwType == REG_SZ)
93 if (strcmp((char*) arData, "true") == 0
94 || strcmp((char*) arData, "1") == 0)
95 retVal = true;
96 else if (strcmp((char*) arData, "false") == 0
97 || strcmp((char*) arData, "0") == 0)
98 retVal = false;
99 #if OSL_DEBUG_LEVEL > 1
100 else
101 OSL_ASSERT(0);
102 #endif
104 else if (dwType == REG_DWORD)
106 if (arData[0] == 1)
107 retVal = true;
108 else if (arData[0] == 0)
109 retVal = false;
110 #if OSL_DEBUG_LEVEL > 1
111 else
112 OSL_ASSERT(0);
113 #endif
117 RegCloseKey(hKey);
119 #elif UNX
120 char buf[16];
121 // use 2 shells to suppress the eventual "gcontool-2 not found" message
122 // of the shell trying to execute the command
123 FILE* fp = popen( "/bin/sh 2>/dev/null -c \"gconftool-2 -g /desktop/gnome/interface/accessibility\"", "r" );
124 if( fp )
126 if( fgets( buf, sizeof(buf), fp ) )
128 int nCompare = strncasecmp( buf, "true", 4 );
129 retVal = (nCompare == 0 ? true : false);
131 pclose( fp );
133 #endif
134 return retVal;
138 rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
140 static char EncodingTable[] =
141 {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
142 sal_Int32 lenRaw = rawData.getLength();
143 char* pBuf = new char[lenRaw * 2];
144 const sal_Int8* arRaw = rawData.getConstArray();
146 char* pCurBuf = pBuf;
147 for (int i = 0; i < lenRaw; i++)
149 unsigned char curChar = arRaw[i];
150 curChar >>= 4;
152 *pCurBuf = EncodingTable[curChar];
153 pCurBuf++;
155 curChar = arRaw[i];
156 curChar &= 0x0F;
158 *pCurBuf = EncodingTable[curChar];
159 pCurBuf++;
162 rtl::ByteSequence ret((sal_Int8*) pBuf, lenRaw * 2);
163 delete [] pBuf;
164 return ret;
167 rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
169 static char decodingTable[] =
170 {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
171 sal_Int32 lenData = data.getLength();
172 sal_Int32 lenBuf = lenData / 2; //always divisable by two
173 unsigned char* pBuf = new unsigned char[lenBuf];
174 const sal_Int8* pData = data.getConstArray();
175 for (sal_Int32 i = 0; i < lenBuf; i++)
177 sal_Int8 curChar = *pData++;
178 //find the index of the first 4bits
179 // TODO What happens if text is not valid Hex characters?
180 unsigned char nibble = 0;
181 for (unsigned char j = 0; j < 16; j++)
183 if (curChar == decodingTable[j])
185 nibble = j;
186 break;
189 nibble <<= 4;
190 curChar = *pData++;
191 //find the index for the next 4bits
192 for (unsigned char j = 0; j < 16; j++)
194 if (curChar == decodingTable[j])
196 nibble |= j;
197 break;
200 pBuf[i] = nibble;
202 rtl::ByteSequence ret((sal_Int8*) pBuf, lenBuf );
203 delete [] pBuf;
204 return ret;
207 rtl::OUString getDirFromFile(const rtl::OUString& usFilePath)
209 sal_Int32 index= usFilePath.lastIndexOf('/');
210 return rtl::OUString(usFilePath.getStr(), index);
213 rtl::OUString getExecutableDirectory()
215 rtl_uString* sExe = NULL;
216 if (osl_getExecutableFile( & sExe) != osl_Process_E_None)
217 throw FrameworkException(
218 JFW_E_ERROR,
219 "[Java framework] Error in function getExecutableDirectory (fwkutil.cxx)");
221 rtl::OUString ouExe(sExe, SAL_NO_ACQUIRE);
222 return getDirFromFile(ouExe);
225 rtl::OUString findPlugin(
226 const rtl::OUString & baseUrl, const rtl::OUString & plugin)
228 rtl::OUString expandedPlugin;
231 expandedPlugin = cppu::bootstrap_expandUri(plugin);
233 catch (com::sun::star::lang::IllegalArgumentException & e)
235 throw FrameworkException(
236 JFW_E_ERROR,
237 (rtl::OString(
238 RTL_CONSTASCII_STRINGPARAM(
239 "[Java framework] IllegalArgumentException in"
240 " findPlugin: "))
241 + rtl::OUStringToOString(e.Message, osl_getThreadTextEncoding())));
243 rtl::OUString sUrl;
246 sUrl = rtl::Uri::convertRelToAbs(baseUrl, expandedPlugin);
248 catch (rtl::MalformedUriException & e)
250 throw FrameworkException(
251 JFW_E_ERROR,
252 (rtl::OString(
253 RTL_CONSTASCII_STRINGPARAM(
254 "[Java framework] rtl::MalformedUriException in"
255 " findPlugin: "))
256 + rtl::OUStringToOString(
257 e.getMessage(), osl_getThreadTextEncoding())));
259 if (checkFileURL(sUrl) == jfw::FILE_OK)
261 return sUrl;
263 rtl::OUString retVal;
264 rtl::OUString sProgDir = getExecutableDirectory();
265 sUrl = sProgDir + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"))
266 + plugin;
267 jfw::FileStatus s = checkFileURL(sUrl);
268 if (s == jfw::FILE_INVALID || s == jfw::FILE_DOES_NOT_EXIST)
270 //If only the name of the library is given, then
271 //use PATH, LD_LIBRARY_PATH etc. to locate the plugin
272 if (plugin.indexOf('/') == -1)
274 rtl::OUString url;
275 #ifdef UNX
276 #ifdef MACOSX
277 rtl::OUString path = rtl::OUString::createFromAscii("DYLD_LIBRARY_PATH");
278 #else
279 rtl::OUString path = rtl::OUString::createFromAscii("LD_LIBRARY_PATH");
280 #endif
281 rtl::OUString env_path;
282 oslProcessError err = osl_getEnvironment(path.pData, &env_path.pData);
283 if (err != osl_Process_E_None && err != osl_Process_E_NotFound)
284 throw FrameworkException(
285 JFW_E_ERROR,
286 "[Java framework] Error in function findPlugin (fwkutil.cxx).");
287 if (err == osl_Process_E_NotFound)
288 return retVal;
289 if (osl_searchFileURL(plugin.pData, env_path.pData, &url.pData)
290 == osl_File_E_None)
291 #else
292 if (osl_searchFileURL(plugin.pData, NULL, &url.pData)
293 == osl_File_E_None)
294 #endif
295 retVal = url;
296 else
297 throw FrameworkException(
298 JFW_E_ERROR,
299 "[Java framework] Error in function findPlugin (fwkutil.cxx).");
302 else
304 retVal = sUrl;
306 return retVal;
309 rtl::OUString getLibraryLocation()
311 rtl::OString sExcMsg("[Java framework] Error in function getLibraryLocation "
312 "(fwkutil.cxx).");
313 rtl::OUString libraryFileUrl;
315 if (!osl::Module::getUrlFromAddress(
316 reinterpret_cast< oslGenericFunction >(getLibraryLocation),
317 libraryFileUrl))
318 throw FrameworkException(JFW_E_ERROR, sExcMsg);
320 return getDirFromFile(libraryFileUrl);
323 jfw::FileStatus checkFileURL(const rtl::OUString & sURL)
325 jfw::FileStatus ret = jfw::FILE_OK;
326 DirectoryItem item;
327 File::RC rc_item = DirectoryItem::get(sURL, item);
328 if (File::E_None == rc_item)
330 osl::FileStatus status(FileStatusMask_Validate);
332 File::RC rc_stat = item.getFileStatus(status);
333 if (File::E_None == rc_stat)
335 ret = FILE_OK;
337 else if (File::E_NOENT == rc_stat)
339 ret = FILE_DOES_NOT_EXIST;
341 else
343 ret = FILE_INVALID;
346 else if (File::E_NOENT == rc_item)
348 ret = FILE_DOES_NOT_EXIST;
350 else
352 ret = FILE_INVALID;
354 return ret;