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