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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
15 #include <cppuhelper/implbase.hxx>
16 #include <osl/mutex.hxx>
17 #include <tools/rcid.h>
18 #include <tools/resary.hxx>
19 #include <tools/resmgr.hxx>
20 #include <vcl/svapp.hxx>
22 using namespace ::extensions::resource
;
23 using namespace ::com::sun::star::uno
;
24 using namespace ::com::sun::star::lang
;
25 using namespace ::com::sun::star::beans
;
26 using namespace ::com::sun::star::container
;
30 std::shared_ptr
<ResMgr
> GetResMgr(Sequence
<Any
> const& rArgs
)
32 if(rArgs
.getLength()!=1)
33 return std::shared_ptr
<ResMgr
>();
35 rArgs
[0] >>= sFilename
;
36 SolarMutexGuard aGuard
;
37 const OString
sEncName(OUStringToOString(sFilename
, osl_getThreadTextEncoding()));
38 return std::shared_ptr
<ResMgr
>(ResMgr::CreateResMgr(sEncName
.getStr()));
41 class ResourceStringIndexAccess
: public cppu::WeakImplHelper
< css::container::XIndexAccess
>
44 explicit ResourceStringIndexAccess(const std::shared_ptr
<ResMgr
>& pResMgr
)
47 OSL_ENSURE(m_pResMgr
, "no resource manager given");
51 virtual ::sal_Int32 SAL_CALL
getCount( ) override
52 { return m_pResMgr
.get() ? SAL_MAX_UINT16
: 0; };
53 virtual css::uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) override
;
55 virtual sal_Bool SAL_CALL
hasElements( ) override
56 { return static_cast<bool>(m_pResMgr
.get()); };
57 virtual css::uno::Type SAL_CALL
getElementType( ) override
58 { return ::cppu::UnoType
<OUString
>::get(); };
60 // m_pResMgr should never be NULL
61 const std::shared_ptr
<ResMgr
> m_pResMgr
;
65 ResourceIndexAccess::ResourceIndexAccess(Sequence
<Any
> const& rArgs
, Reference
<XComponentContext
> const&)
66 : m_pResMgr(GetResMgr(rArgs
))
69 Any SAL_CALL
ResourceIndexAccess::getByName(const OUString
& aName
)
71 const Sequence
<OUString
> aNames(getElementNames());
72 Reference
<XIndexAccess
> xResult
;
73 switch(std::find(aNames
.begin(), aNames
.end(), aName
) - aNames
.begin())
76 xResult
.set(new ResourceStringIndexAccess(m_pResMgr
));
79 throw NoSuchElementException();
81 return makeAny(xResult
);
84 Sequence
<OUString
> SAL_CALL
ResourceIndexAccess::getElementNames( )
86 static Sequence
<OUString
> aResult
;
87 if( aResult
.getLength() == 0)
90 aResult
[0] = "String";
95 sal_Bool SAL_CALL
ResourceIndexAccess::hasByName(const OUString
& aName
)
97 const Sequence
<OUString
> aNames(getElementNames());
98 return (std::find(aNames
.begin(), aNames
.end(), aName
) != aNames
.end());
101 Any SAL_CALL
ResourceStringIndexAccess::getByIndex(sal_Int32 nIdx
)
103 if(nIdx
> SAL_MAX_UINT16
|| nIdx
< 0)
104 throw IndexOutOfBoundsException();
105 SolarMutexGuard aGuard
;
107 throw RuntimeException("resource manager not available");
109 const ResId
aId(static_cast<sal_uInt16
>(nIdx
), *m_pResMgr
);
110 aId
.SetRT(RSC_STRING
);
112 if(!m_pResMgr
->IsAvailable(aId
))
113 throw RuntimeException("string resource for id not available");
115 return makeAny(aId
.toString());
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */