LCOV - code coverage report
Current view: top level - libreoffice/jvmfwk/source - fwkutil.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 117 0.0 %
Date: 2012-12-27 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10