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

Generated by: LCOV version 1.10