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: fwkutil.cxx,v $
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"
36 #pragma warning(push, 1)
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"
69 bool isAccessibilitySupportDesired()
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")))
81 if (RegOpenKeyEx(HKEY_CURRENT_USER
,
82 "Software\\OpenOffice.org\\Accessibility\\AtToolSupport",
83 0, KEY_READ
, &hKey
) == ERROR_SUCCESS
)
87 unsigned char arData
[16];
88 if( RegQueryValueEx(hKey
, "SupportAssistiveTechnology", NULL
, &dwType
, arData
,
89 & dwLen
)== ERROR_SUCCESS
)
93 if (strcmp((char*) arData
, "true") == 0
94 || strcmp((char*) arData
, "1") == 0)
96 else if (strcmp((char*) arData
, "false") == 0
97 || strcmp((char*) arData
, "0") == 0)
99 #if OSL_DEBUG_LEVEL > 1
104 else if (dwType
== REG_DWORD
)
108 else if (arData
[0] == 0)
110 #if OSL_DEBUG_LEVEL > 1
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" );
126 if( fgets( buf
, sizeof(buf
), fp
) )
128 int nCompare
= strncasecmp( buf
, "true", 4 );
129 retVal
= (nCompare
== 0 ? true : false);
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
];
152 *pCurBuf
= EncodingTable
[curChar
];
158 *pCurBuf
= EncodingTable
[curChar
];
162 rtl::ByteSequence
ret((sal_Int8
*) pBuf
, lenRaw
* 2);
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
])
191 //find the index for the next 4bits
192 for (unsigned char j
= 0; j
< 16; j
++)
194 if (curChar
== decodingTable
[j
])
202 rtl::ByteSequence
ret((sal_Int8
*) pBuf
, lenBuf
);
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(
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(
238 RTL_CONSTASCII_STRINGPARAM(
239 "[Java framework] IllegalArgumentException in"
241 + rtl::OUStringToOString(e
.Message
, osl_getThreadTextEncoding())));
246 sUrl
= rtl::Uri::convertRelToAbs(baseUrl
, expandedPlugin
);
248 catch (rtl::MalformedUriException
& e
)
250 throw FrameworkException(
253 RTL_CONSTASCII_STRINGPARAM(
254 "[Java framework] rtl::MalformedUriException in"
256 + rtl::OUStringToOString(
257 e
.getMessage(), osl_getThreadTextEncoding())));
259 if (checkFileURL(sUrl
) == jfw::FILE_OK
)
263 rtl::OUString retVal
;
264 rtl::OUString sProgDir
= getExecutableDirectory();
265 sUrl
= sProgDir
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"))
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)
277 rtl::OUString path
= rtl::OUString::createFromAscii("DYLD_LIBRARY_PATH");
279 rtl::OUString path
= rtl::OUString::createFromAscii("LD_LIBRARY_PATH");
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(
286 "[Java framework] Error in function findPlugin (fwkutil.cxx).");
287 if (err
== osl_Process_E_NotFound
)
289 if (osl_searchFileURL(plugin
.pData
, env_path
.pData
, &url
.pData
)
292 if (osl_searchFileURL(plugin
.pData
, NULL
, &url
.pData
)
297 throw FrameworkException(
299 "[Java framework] Error in function findPlugin (fwkutil.cxx).");
309 rtl::OUString
getLibraryLocation()
311 rtl::OString
sExcMsg("[Java framework] Error in function getLibraryLocation "
313 rtl::OUString libraryFileUrl
;
315 if (!osl::Module::getUrlFromAddress(
316 reinterpret_cast< oslGenericFunction
>(getLibraryLocation
),
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
;
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
)
337 else if (File::E_NOENT
== rc_stat
)
339 ret
= FILE_DOES_NOT_EXIST
;
346 else if (File::E_NOENT
== rc_item
)
348 ret
= FILE_DOES_NOT_EXIST
;