1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <ResourceIndexAccess.hxx>
12 #include <com/sun/star/container/XIndexAccess.hpp>
13 #include <com/sun/star/beans/PropertyValue.hpp>
14 #include <comphelper/stlunosequence.hxx>
15 #include <osl/mutex.hxx>
16 #include <tools/rcid.h>
17 #include <tools/resary.hxx>
18 #include <tools/resmgr.hxx>
19 #include <vcl/svapp.hxx>
21 using namespace ::extensions::resource
;
22 using namespace ::com::sun::star::uno
;
23 using namespace ::com::sun::star::lang
;
24 using namespace ::com::sun::star::beans
;
25 using namespace ::com::sun::star::container
;
27 using ::comphelper::stl_begin
;
28 using ::comphelper::stl_end
;
32 static ::boost::shared_ptr
<ResMgr
> GetResMgr(Sequence
<Any
> const& rArgs
)
34 if(rArgs
.getLength()!=1)
35 return ::boost::shared_ptr
<ResMgr
>();
37 rArgs
[0] >>= sFilename
;
38 SolarMutexGuard aGuard
;
39 const OString
sEncName(OUStringToOString(sFilename
, osl_getThreadTextEncoding()));
40 return ::boost::shared_ptr
<ResMgr
>(ResMgr::CreateResMgr(sEncName
.getStr()));
43 class ResourceIndexAccessBase
: public cppu::WeakImplHelper1
< ::com::sun::star::container::XIndexAccess
>
46 ResourceIndexAccessBase( ::boost::shared_ptr
<ResMgr
> pResMgr
)
49 OSL_ENSURE(m_pResMgr
, "no resource manager given");
53 virtual ::sal_Int32 SAL_CALL
getCount( ) throw (::com::sun::star::uno::RuntimeException
)
54 { return m_pResMgr
.get() ? SAL_MAX_UINT16
: 0; };
56 virtual ::sal_Bool SAL_CALL
hasElements( ) throw (::com::sun::star::uno::RuntimeException
)
57 { return static_cast<bool>(m_pResMgr
.get()); };
60 // m_pResMgr should never be NULL
61 const ::boost::shared_ptr
<ResMgr
> m_pResMgr
;
64 class ResourceStringIndexAccess
: public ResourceIndexAccessBase
67 ResourceStringIndexAccess( ::boost::shared_ptr
<ResMgr
> pResMgr
)
68 : ResourceIndexAccessBase(pResMgr
) {}
70 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
);
72 virtual ::com::sun::star::uno::Type SAL_CALL
getElementType( ) throw (::com::sun::star::uno::RuntimeException
)
73 { return ::getCppuType(static_cast< OUString
*>(0)); };
76 class ResourceStringListIndexAccess
: public ResourceIndexAccessBase
79 ResourceStringListIndexAccess( ::boost::shared_ptr
<ResMgr
> pResMgr
)
80 : ResourceIndexAccessBase(pResMgr
) {}
82 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
);
84 virtual ::com::sun::star::uno::Type SAL_CALL
getElementType( ) throw (::com::sun::star::uno::RuntimeException
)
85 { return ::getCppuType(static_cast<Sequence
<PropertyValue
> * >(0)); };
89 ResourceIndexAccess::ResourceIndexAccess(Sequence
<Any
> const& rArgs
, Reference
<XComponentContext
> const&)
90 : m_pResMgr(GetResMgr(rArgs
))
93 Reference
<XInterface
> initResourceIndexAccess(ResourceIndexAccess
* pResourceIndexAccess
)
95 Reference
<XInterface
> xResult(static_cast<cppu::OWeakObject
*>(pResourceIndexAccess
));
96 if(!pResourceIndexAccess
->hasElements())
97 // xResult does not help the client to analyse the problem
98 // and will crash on getByIndex calls, better just give back an empty Reference
99 // so that such ResourceStringIndexAccess instances are never release into the wild
100 throw RuntimeException(
101 OUString("resource manager could not get initialized"),
102 /* xResult */ Reference
<XInterface
>());
106 Any SAL_CALL
ResourceIndexAccess::getByName(const OUString
& aName
)
107 throw (NoSuchElementException
, WrappedTargetException
, RuntimeException
)
109 const Sequence
<OUString
> aNames(getElementNames());
110 Reference
<XIndexAccess
> xResult
;
111 switch(::std::find(stl_begin(aNames
), stl_end(aNames
), aName
)-stl_begin(aNames
))
114 xResult
= Reference
<XIndexAccess
>(new ResourceStringIndexAccess(m_pResMgr
));
117 xResult
= Reference
<XIndexAccess
>(new ResourceStringListIndexAccess(m_pResMgr
));
120 throw NoSuchElementException();
122 return makeAny(xResult
);
125 Sequence
<OUString
> SAL_CALL
ResourceIndexAccess::getElementNames( )
126 throw (RuntimeException
)
128 static Sequence
<OUString
> aResult
;
129 if( aResult
.getLength() == 0)
132 aResult
[0] = OUString("String");
133 aResult
[1] = OUString("StringList");
138 ::sal_Bool SAL_CALL
ResourceIndexAccess::hasByName(const OUString
& aName
)
139 throw (RuntimeException
)
141 const Sequence
<OUString
> aNames(getElementNames());
142 return (::std::find(stl_begin(aNames
), stl_end(aNames
), aName
) != stl_end(aNames
));
145 Any SAL_CALL
ResourceStringIndexAccess::getByIndex(sal_Int32 nIdx
)
146 throw (IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
148 if(nIdx
> SAL_MAX_UINT16
|| nIdx
< 0)
149 throw IndexOutOfBoundsException();
150 SolarMutexGuard aGuard
;
152 throw RuntimeException(
153 OUString("resource manager not available"),
154 Reference
<XInterface
>());
156 const ResId
aId(static_cast<sal_uInt16
>(nIdx
), *m_pResMgr
);
157 aId
.SetRT(RSC_STRING
);
159 if(!m_pResMgr
->IsAvailable(aId
))
160 throw RuntimeException(
161 OUString("string resource for id not available"),
162 Reference
<XInterface
>());
164 return makeAny(OUString(String(aId
)));
167 Any SAL_CALL
ResourceStringListIndexAccess::getByIndex(sal_Int32 nIdx
)
168 throw (IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
170 if(nIdx
> SAL_MAX_UINT16
|| nIdx
< 0)
171 throw IndexOutOfBoundsException();
172 SolarMutexGuard aGuard
;
175 throw RuntimeException(
176 OUString("resource manager not available"),
177 Reference
<XInterface
>());
179 const ResId
aId(static_cast<sal_uInt16
>(nIdx
), *m_pResMgr
);
180 aId
.SetRT(RSC_STRINGARRAY
);
181 if(!m_pResMgr
->IsAvailable(aId
))
182 throw RuntimeException(
183 OUString("string list resource for id not available"),
184 Reference
<XInterface
>());
185 const ResStringArray
aStringList(aId
);
186 Sequence
<PropertyValue
> aPropList(aStringList
.Count());
187 for(sal_Int32 nCount
= 0; nCount
!= aPropList
.getLength(); ++nCount
)
189 aPropList
[nCount
].Name
= aStringList
.GetString(nCount
);
190 aPropList
[nCount
].Handle
= -1;
191 aPropList
[nCount
].Value
<<= aStringList
.GetValue(nCount
);
192 aPropList
[nCount
].State
= PropertyState_DIRECT_VALUE
;
194 return makeAny(aPropList
);
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */