Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #if defined WNT
22 : #if defined _MSC_VER
23 : #pragma warning(push, 1)
24 : #endif
25 : #include <windows.h>
26 : #if defined _MSC_VER
27 : #pragma warning(pop)
28 : #endif
29 : #endif
30 :
31 : #include <string>
32 : #include <string.h>
33 : #include "osl/mutex.hxx"
34 : #include "osl/module.hxx"
35 : #include "osl/thread.hxx"
36 : #include "rtl/ustring.hxx"
37 : #include "rtl/ustrbuf.hxx"
38 : #include "rtl/bootstrap.hxx"
39 : #include "osl/file.hxx"
40 : #include "osl/process.h"
41 : #include "rtl/instance.hxx"
42 : #include "rtl/uri.hxx"
43 : #include "osl/getglobalmutex.hxx"
44 : #include "com/sun/star/lang/IllegalArgumentException.hpp"
45 : #include "cppuhelper/bootstrap.hxx"
46 :
47 : #include "framework.hxx"
48 : #include "fwkutil.hxx"
49 :
50 : using namespace osl;
51 :
52 :
53 : namespace jfw
54 : {
55 :
56 51 : bool isAccessibilitySupportDesired()
57 : {
58 51 : OUString sValue;
59 102 : if ((sal_True == ::rtl::Bootstrap::get(
60 153 : OUString("JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY"), sValue)) && sValue == "1"
61 : )
62 0 : return false;
63 :
64 51 : bool retVal = false;
65 : #ifdef WNT
66 : HKEY hKey = 0;
67 : if (RegOpenKeyEx(HKEY_CURRENT_USER,
68 : "Software\\LibreOffice\\Accessibility\\AtToolSupport",
69 : 0, KEY_READ, &hKey) == ERROR_SUCCESS)
70 : {
71 : DWORD dwType = 0;
72 : DWORD dwLen = 16;
73 : unsigned char arData[16];
74 : if( RegQueryValueEx(hKey, "SupportAssistiveTechnology", NULL, &dwType, arData,
75 : & dwLen)== ERROR_SUCCESS)
76 : {
77 : if (dwType == REG_SZ)
78 : {
79 : if (strcmp((char*) arData, "true") == 0
80 : || strcmp((char*) arData, "1") == 0)
81 : retVal = true;
82 : else if (strcmp((char*) arData, "false") == 0
83 : || strcmp((char*) arData, "0") == 0)
84 : retVal = false;
85 : #if OSL_DEBUG_LEVEL > 1
86 : else
87 : OSL_ASSERT(0);
88 : #endif
89 : }
90 : else if (dwType == REG_DWORD)
91 : {
92 : if (arData[0] == 1)
93 : retVal = true;
94 : else if (arData[0] == 0)
95 : retVal = false;
96 : #if OSL_DEBUG_LEVEL > 1
97 : else
98 : OSL_ASSERT(0);
99 : #endif
100 : }
101 : }
102 : }
103 : RegCloseKey(hKey);
104 :
105 : #elif defined UNX
106 : char buf[16];
107 : // use 2 shells to suppress the eventual "gcontool-2 not found" message
108 : // of the shell trying to execute the command
109 51 : FILE* fp = popen( "/bin/sh 2>/dev/null -c \"gconftool-2 -g /desktop/gnome/interface/accessibility\"", "r" );
110 51 : if( fp )
111 : {
112 51 : if( fgets( buf, sizeof(buf), fp ) )
113 : {
114 0 : int nCompare = strncasecmp( buf, "true", 4 );
115 0 : retVal = (nCompare == 0 ? true : false);
116 : }
117 51 : pclose( fp );
118 : }
119 : #endif
120 51 : return retVal;
121 : }
122 :
123 :
124 0 : rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
125 : {
126 : static char EncodingTable[] =
127 : {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
128 0 : sal_Int32 lenRaw = rawData.getLength();
129 0 : char* pBuf = new char[lenRaw * 2];
130 0 : const sal_Int8* arRaw = rawData.getConstArray();
131 :
132 0 : char* pCurBuf = pBuf;
133 0 : for (int i = 0; i < lenRaw; i++)
134 : {
135 0 : unsigned char curChar = arRaw[i];
136 0 : curChar >>= 4;
137 :
138 0 : *pCurBuf = EncodingTable[curChar];
139 0 : pCurBuf++;
140 :
141 0 : curChar = arRaw[i];
142 0 : curChar &= 0x0F;
143 :
144 0 : *pCurBuf = EncodingTable[curChar];
145 0 : pCurBuf++;
146 : }
147 :
148 0 : rtl::ByteSequence ret((sal_Int8*) pBuf, lenRaw * 2);
149 0 : delete [] pBuf;
150 0 : return ret;
151 : }
152 :
153 0 : rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
154 : {
155 : static char decodingTable[] =
156 : {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
157 0 : sal_Int32 lenData = data.getLength();
158 0 : sal_Int32 lenBuf = lenData / 2; //always divisable by two
159 0 : unsigned char* pBuf = new unsigned char[lenBuf];
160 0 : const sal_Int8* pData = data.getConstArray();
161 0 : for (sal_Int32 i = 0; i < lenBuf; i++)
162 : {
163 0 : sal_Int8 curChar = *pData++;
164 : //find the index of the first 4bits
165 : // TODO What happens if text is not valid Hex characters?
166 0 : unsigned char nibble = 0;
167 0 : for (unsigned char j = 0; j < 16; j++)
168 : {
169 0 : if (curChar == decodingTable[j])
170 : {
171 0 : nibble = j;
172 0 : break;
173 : }
174 : }
175 0 : nibble <<= 4;
176 0 : curChar = *pData++;
177 : //find the index for the next 4bits
178 0 : for (unsigned char j = 0; j < 16; j++)
179 : {
180 0 : if (curChar == decodingTable[j])
181 : {
182 0 : nibble |= j;
183 0 : break;
184 : }
185 : }
186 0 : pBuf[i] = nibble;
187 : }
188 0 : rtl::ByteSequence ret((sal_Int8*) pBuf, lenBuf );
189 0 : delete [] pBuf;
190 0 : return ret;
191 : }
192 :
193 105 : OUString getDirFromFile(const OUString& usFilePath)
194 : {
195 105 : sal_Int32 index= usFilePath.lastIndexOf('/');
196 105 : return OUString(usFilePath.getStr(), index);
197 : }
198 :
199 0 : OUString getExecutableDirectory()
200 : {
201 0 : rtl_uString* sExe = NULL;
202 0 : if (osl_getExecutableFile( & sExe) != osl_Process_E_None)
203 : throw FrameworkException(
204 : JFW_E_ERROR,
205 0 : "[Java framework] Error in function getExecutableDirectory (fwkutil.cxx)");
206 :
207 0 : OUString ouExe(sExe, SAL_NO_ACQUIRE);
208 0 : return getDirFromFile(ouExe);
209 : }
210 :
211 684 : OUString findPlugin(
212 : const OUString & baseUrl, const OUString & plugin)
213 : {
214 684 : OUString expandedPlugin;
215 : try
216 : {
217 684 : expandedPlugin = cppu::bootstrap_expandUri(plugin);
218 : }
219 0 : catch (const com::sun::star::lang::IllegalArgumentException & e)
220 : {
221 : throw FrameworkException(
222 : JFW_E_ERROR,
223 : (OString(
224 : RTL_CONSTASCII_STRINGPARAM(
225 : "[Java framework] IllegalArgumentException in"
226 : " findPlugin: "))
227 0 : + OUStringToOString(e.Message, osl_getThreadTextEncoding())));
228 : }
229 1368 : OUString sUrl;
230 : try
231 : {
232 684 : sUrl = rtl::Uri::convertRelToAbs(baseUrl, expandedPlugin);
233 : }
234 0 : catch (const rtl::MalformedUriException & e)
235 : {
236 : throw FrameworkException(
237 : JFW_E_ERROR,
238 : (OString(
239 : RTL_CONSTASCII_STRINGPARAM(
240 : "[Java framework] rtl::MalformedUriException in"
241 : " findPlugin: "))
242 0 : + OUStringToOString(
243 0 : e.getMessage(), osl_getThreadTextEncoding())));
244 : }
245 684 : if (checkFileURL(sUrl) == jfw::FILE_OK)
246 : {
247 684 : return sUrl;
248 : }
249 0 : OUString retVal;
250 0 : OUString sProgDir = getExecutableDirectory();
251 0 : sUrl = sProgDir + OUString("/")
252 0 : + plugin;
253 0 : jfw::FileStatus s = checkFileURL(sUrl);
254 0 : if (s == jfw::FILE_INVALID || s == jfw::FILE_DOES_NOT_EXIST)
255 : {
256 : //If only the name of the library is given, then
257 : //use PATH, LD_LIBRARY_PATH etc. to locate the plugin
258 0 : if (plugin.indexOf('/') == -1)
259 : {
260 0 : OUString url;
261 : #ifdef UNX
262 : #if defined(MACOSX)
263 : OUString path = OUString("DYLD_LIBRARY_PATH");
264 : #elif defined(AIX)
265 : OUString path = OUString("LIBPATH");
266 : #else
267 0 : OUString path = OUString("LD_LIBRARY_PATH");
268 : #endif
269 0 : OUString env_path;
270 0 : oslProcessError err = osl_getEnvironment(path.pData, &env_path.pData);
271 0 : if (err != osl_Process_E_None && err != osl_Process_E_NotFound)
272 : throw FrameworkException(
273 : JFW_E_ERROR,
274 0 : "[Java framework] Error in function findPlugin (fwkutil.cxx).");
275 0 : if (err == osl_Process_E_NotFound)
276 0 : return retVal;
277 0 : if (osl_searchFileURL(plugin.pData, env_path.pData, &url.pData)
278 : == osl_File_E_None)
279 : #else
280 : if (osl_searchFileURL(plugin.pData, NULL, &url.pData)
281 : == osl_File_E_None)
282 : #endif
283 0 : retVal = url;
284 : else
285 : throw FrameworkException(
286 : JFW_E_ERROR,
287 0 : "[Java framework] Error in function findPlugin (fwkutil.cxx).");
288 0 : }
289 : }
290 : else
291 : {
292 0 : retVal = sUrl;
293 : }
294 684 : return retVal;
295 : }
296 :
297 105 : OUString getLibraryLocation()
298 : {
299 : OString sExcMsg("[Java framework] Error in function getLibraryLocation "
300 105 : "(fwkutil.cxx).");
301 210 : OUString libraryFileUrl;
302 :
303 105 : if (!osl::Module::getUrlFromAddress(
304 : reinterpret_cast< oslGenericFunction >(getLibraryLocation),
305 105 : libraryFileUrl))
306 0 : throw FrameworkException(JFW_E_ERROR, sExcMsg);
307 :
308 210 : return getDirFromFile(libraryFileUrl);
309 : }
310 :
311 843 : jfw::FileStatus checkFileURL(const OUString & sURL)
312 : {
313 843 : jfw::FileStatus ret = jfw::FILE_OK;
314 843 : DirectoryItem item;
315 843 : File::RC rc_item = DirectoryItem::get(sURL, item);
316 843 : if (File::E_None == rc_item)
317 : {
318 843 : osl::FileStatus status(osl_FileStatus_Mask_Validate);
319 :
320 843 : File::RC rc_stat = item.getFileStatus(status);
321 843 : if (File::E_None == rc_stat)
322 : {
323 843 : ret = FILE_OK;
324 : }
325 0 : else if (File::E_NOENT == rc_stat)
326 : {
327 0 : ret = FILE_DOES_NOT_EXIST;
328 : }
329 : else
330 : {
331 0 : ret = FILE_INVALID;
332 843 : }
333 : }
334 0 : else if (File::E_NOENT == rc_item)
335 : {
336 0 : ret = FILE_DOES_NOT_EXIST;
337 : }
338 : else
339 : {
340 0 : ret = FILE_INVALID;
341 : }
342 843 : return ret;
343 : }
344 :
345 : }
346 :
347 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|