Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / resource / ResourceIndexAccess.cxx
blobbebd3af63e2aec17e06618776773f8a7b13126c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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;
28 namespace
30 std::shared_ptr<ResMgr> GetResMgr(Sequence<Any> const& rArgs)
32 if(rArgs.getLength()!=1)
33 return std::shared_ptr<ResMgr>();
34 OUString sFilename;
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>
43 public:
44 explicit ResourceStringIndexAccess(const std::shared_ptr<ResMgr>& pResMgr)
45 : m_pResMgr(pResMgr)
47 OSL_ENSURE(m_pResMgr, "no resource manager given");
50 // XIndexAccess
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;
54 // XElementAccess
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(); };
59 private:
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))
67 {};
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())
75 case 0:
76 xResult.set(new ResourceStringIndexAccess(m_pResMgr));
77 break;
78 default:
79 throw NoSuchElementException();
81 return makeAny(xResult);
84 Sequence<OUString> SAL_CALL ResourceIndexAccess::getElementNames( )
86 static Sequence<OUString> aResult;
87 if( aResult.getLength() == 0)
89 aResult.realloc(1);
90 aResult[0] = "String";
92 return aResult;
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;
106 if(!m_pResMgr.get())
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: */