LCOV - code coverage report
Current view: top level - extensions/source/resource - ResourceIndexAccess.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 83 0.0 %
Date: 2014-04-14 Functions: 0 21 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             : 
      10             : #include <ResourceIndexAccess.hxx>
      11             : 
      12             : #include <com/sun/star/container/XIndexAccess.hpp>
      13             : #include <com/sun/star/beans/PropertyValue.hpp>
      14             : #include <osl/mutex.hxx>
      15             : #include <tools/rcid.h>
      16             : #include <tools/resary.hxx>
      17             : #include <tools/resmgr.hxx>
      18             : #include <vcl/svapp.hxx>
      19             : 
      20             : using namespace ::extensions::resource;
      21             : using namespace ::com::sun::star::uno;
      22             : using namespace ::com::sun::star::lang;
      23             : using namespace ::com::sun::star::beans;
      24             : using namespace ::com::sun::star::container;
      25             : 
      26             : namespace
      27             : {
      28           0 :     static ::boost::shared_ptr<ResMgr> GetResMgr(Sequence<Any> const& rArgs)
      29             :     {
      30           0 :         if(rArgs.getLength()!=1)
      31           0 :             return ::boost::shared_ptr<ResMgr>();
      32           0 :         OUString sFilename;
      33           0 :         rArgs[0] >>= sFilename;
      34           0 :         SolarMutexGuard aGuard;
      35           0 :         const OString sEncName(OUStringToOString(sFilename, osl_getThreadTextEncoding()));
      36           0 :         return ::boost::shared_ptr<ResMgr>(ResMgr::CreateResMgr(sEncName.getStr()));
      37             :     }
      38             : 
      39           0 :     class ResourceIndexAccessBase : public cppu::WeakImplHelper1< ::com::sun::star::container::XIndexAccess>
      40             :     {
      41             :         public:
      42           0 :             ResourceIndexAccessBase( ::boost::shared_ptr<ResMgr> pResMgr)
      43           0 :                 : m_pResMgr(pResMgr)
      44             :             {
      45             :                 OSL_ENSURE(m_pResMgr, "no resource manager given");
      46           0 :             }
      47             : 
      48             :             // XIndexAccess
      49           0 :             virtual ::sal_Int32 SAL_CALL getCount(  ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      50           0 :                 { return m_pResMgr.get() ? SAL_MAX_UINT16 : 0; };
      51             :             // XElementAccess
      52           0 :             virtual sal_Bool SAL_CALL hasElements(  ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      53           0 :                 { return static_cast<bool>(m_pResMgr.get()); };
      54             : 
      55             :         protected:
      56             :             // m_pResMgr should never be NULL
      57             :             const ::boost::shared_ptr<ResMgr> m_pResMgr;
      58             :     };
      59             : 
      60           0 :     class ResourceStringIndexAccess : public ResourceIndexAccessBase
      61             :     {
      62             :         public:
      63           0 :             ResourceStringIndexAccess( ::boost::shared_ptr<ResMgr> pResMgr)
      64           0 :                 : ResourceIndexAccessBase(pResMgr) {}
      65             :             // XIndexAccess
      66             :             virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      67             :             // XElementAccessBase
      68           0 :             virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      69           0 :                 { return ::getCppuType(static_cast< OUString*>(0)); };
      70             :     };
      71             : 
      72           0 :     class ResourceStringListIndexAccess : public ResourceIndexAccessBase
      73             :     {
      74             :         public:
      75           0 :             ResourceStringListIndexAccess( ::boost::shared_ptr<ResMgr> pResMgr)
      76           0 :                 : ResourceIndexAccessBase(pResMgr) {}
      77             :             // XIndexAccess
      78             :             virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      79             :             // XElementAccessBase
      80           0 :             virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      81           0 :                 { return ::getCppuType(static_cast<Sequence<PropertyValue> * >(0)); };
      82             :     };
      83             : }
      84             : 
      85           0 : ResourceIndexAccess::ResourceIndexAccess(Sequence<Any> const& rArgs, Reference<XComponentContext> const&)
      86           0 :     : m_pResMgr(GetResMgr(rArgs))
      87           0 : {};
      88             : 
      89           0 : Reference<XInterface> initResourceIndexAccess(ResourceIndexAccess* pResourceIndexAccess)
      90             : {
      91           0 :     Reference<XInterface> xResult(static_cast<cppu::OWeakObject*>(pResourceIndexAccess));
      92           0 :     if(!pResourceIndexAccess->hasElements())
      93             :         // xResult does not help the client to analyse the problem
      94             :         // and will crash on getByIndex calls, better just give back an empty Reference
      95             :         // so that such ResourceStringIndexAccess instances are never release into the wild
      96             :         throw RuntimeException(
      97             :             OUString("resource manager could not get initialized"),
      98           0 :             /* xResult */ Reference<XInterface>());
      99           0 :     return xResult;
     100             : }
     101             : 
     102           0 : Any SAL_CALL ResourceIndexAccess::getByName(const OUString& aName)
     103             :     throw (NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
     104             : {
     105           0 :     const Sequence<OUString> aNames(getElementNames());
     106           0 :     Reference<XIndexAccess> xResult;
     107           0 :     switch(::std::find(aNames.begin(), aNames.end(), aName) - aNames.begin())
     108             :     {
     109             :         case 0:
     110           0 :             xResult = Reference<XIndexAccess>(new ResourceStringIndexAccess(m_pResMgr));
     111           0 :             break;
     112             :         case 1:
     113           0 :             xResult = Reference<XIndexAccess>(new ResourceStringListIndexAccess(m_pResMgr));
     114           0 :             break;
     115             :         default:
     116           0 :             throw NoSuchElementException();
     117             :     }
     118           0 :     return makeAny(xResult);
     119             : }
     120             : 
     121           0 : Sequence<OUString> SAL_CALL ResourceIndexAccess::getElementNames(  )
     122             :     throw (RuntimeException, std::exception)
     123             : {
     124           0 :     static Sequence<OUString> aResult;
     125           0 :     if( aResult.getLength() == 0)
     126             :     {
     127           0 :         aResult.realloc(2);
     128           0 :         aResult[0] = "String";
     129           0 :         aResult[1] = "StringList";
     130             :     }
     131           0 :     return aResult;
     132             : }
     133             : 
     134           0 : sal_Bool SAL_CALL ResourceIndexAccess::hasByName(const OUString& aName)
     135             :     throw (RuntimeException, std::exception)
     136             : {
     137           0 :     const Sequence<OUString> aNames(getElementNames());
     138           0 :     return (::std::find(aNames.begin(), aNames.end(), aName) != aNames.end());
     139             : }
     140             : 
     141           0 : Any SAL_CALL ResourceStringIndexAccess::getByIndex(sal_Int32 nIdx)
     142             :     throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
     143             : {
     144           0 :     if(nIdx > SAL_MAX_UINT16 || nIdx < 0)
     145           0 :         throw IndexOutOfBoundsException();
     146           0 :     SolarMutexGuard aGuard;
     147           0 :     if(!m_pResMgr.get())
     148             :         throw RuntimeException(
     149             :             OUString("resource manager not available"),
     150           0 :             Reference<XInterface>());
     151             : 
     152           0 :     const ResId aId(static_cast<sal_uInt16>(nIdx), *m_pResMgr);
     153           0 :     aId.SetRT(RSC_STRING);
     154             : 
     155           0 :     if(!m_pResMgr->IsAvailable(aId))
     156             :         throw RuntimeException(
     157             :             OUString("string resource for id not available"),
     158           0 :             Reference<XInterface>());
     159             : 
     160           0 :     return makeAny(aId.toString());
     161             : }
     162             : 
     163           0 : Any SAL_CALL ResourceStringListIndexAccess::getByIndex(sal_Int32 nIdx)
     164             :     throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
     165             : {
     166           0 :     if(nIdx > SAL_MAX_UINT16 || nIdx < 0)
     167           0 :         throw IndexOutOfBoundsException();
     168           0 :     SolarMutexGuard aGuard;
     169             : 
     170           0 :     if(!m_pResMgr.get())
     171             :         throw RuntimeException(
     172             :             OUString("resource manager not available"),
     173           0 :             Reference<XInterface>());
     174             : 
     175           0 :     const ResId aId(static_cast<sal_uInt16>(nIdx), *m_pResMgr);
     176           0 :     aId.SetRT(RSC_STRINGARRAY);
     177           0 :     if(!m_pResMgr->IsAvailable(aId))
     178             :         throw RuntimeException(
     179             :             OUString("string list resource for id not available"),
     180           0 :             Reference<XInterface>());
     181           0 :     const ResStringArray aStringList(aId);
     182           0 :     Sequence<PropertyValue> aPropList(aStringList.Count());
     183           0 :     for(sal_Int32 nCount = 0; nCount != aPropList.getLength(); ++nCount)
     184             :     {
     185           0 :         aPropList[nCount].Name = aStringList.GetString(nCount);
     186           0 :         aPropList[nCount].Handle = -1;
     187           0 :         aPropList[nCount].Value <<= aStringList.GetValue(nCount);
     188           0 :         aPropList[nCount].State = PropertyState_DIRECT_VALUE;
     189             :     }
     190           0 :     return makeAny(aPropList);
     191             : }
     192             : 
     193             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10