bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / resource / ResourceIndexAccess.cxx
blob77cdc21d5e6d232af69a3229b285aeb7a524f8cc
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 <osl/mutex.hxx>
15 #include <tools/rcid.h>
16 #include <tools/resary.hxx>
17 #include <tools/resmgr.hxx>
18 #include <vcl/svapp.hxx>
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;
26 namespace
28 static ::boost::shared_ptr<ResMgr> GetResMgr(Sequence<Any> const& rArgs)
30 if(rArgs.getLength()!=1)
31 return ::boost::shared_ptr<ResMgr>();
32 OUString sFilename;
33 rArgs[0] >>= sFilename;
34 SolarMutexGuard aGuard;
35 const OString sEncName(OUStringToOString(sFilename, osl_getThreadTextEncoding()));
36 return ::boost::shared_ptr<ResMgr>(ResMgr::CreateResMgr(sEncName.getStr()));
39 class ResourceIndexAccessBase : public cppu::WeakImplHelper1< ::com::sun::star::container::XIndexAccess>
41 public:
42 ResourceIndexAccessBase( ::boost::shared_ptr<ResMgr> pResMgr)
43 : m_pResMgr(pResMgr)
45 OSL_ENSURE(m_pResMgr, "no resource manager given");
48 // XIndexAccess
49 virtual ::sal_Int32 SAL_CALL getCount( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
50 { return m_pResMgr.get() ? SAL_MAX_UINT16 : 0; };
51 // XElementAccess
52 virtual sal_Bool SAL_CALL hasElements( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
53 { return static_cast<bool>(m_pResMgr.get()); };
55 protected:
56 // m_pResMgr should never be NULL
57 const ::boost::shared_ptr<ResMgr> m_pResMgr;
60 class ResourceStringIndexAccess : public ResourceIndexAccessBase
62 public:
63 ResourceStringIndexAccess( ::boost::shared_ptr<ResMgr> pResMgr)
64 : 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 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
69 { return ::cppu::UnoType<OUString>::get(); };
72 class ResourceStringListIndexAccess : public ResourceIndexAccessBase
74 public:
75 ResourceStringListIndexAccess( ::boost::shared_ptr<ResMgr> pResMgr)
76 : 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 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
81 { return cppu::UnoType<Sequence<PropertyValue>>::get(); };
85 ResourceIndexAccess::ResourceIndexAccess(Sequence<Any> const& rArgs, Reference<XComponentContext> const&)
86 : m_pResMgr(GetResMgr(rArgs))
87 {};
89 Reference<XInterface> initResourceIndexAccess(ResourceIndexAccess* pResourceIndexAccess)
91 Reference<XInterface> xResult(static_cast<cppu::OWeakObject*>(pResourceIndexAccess));
92 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("resource manager could not get initialized");
97 return xResult;
100 Any SAL_CALL ResourceIndexAccess::getByName(const OUString& aName)
101 throw (NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
103 const Sequence<OUString> aNames(getElementNames());
104 Reference<XIndexAccess> xResult;
105 switch(::std::find(aNames.begin(), aNames.end(), aName) - aNames.begin())
107 case 0:
108 xResult = Reference<XIndexAccess>(new ResourceStringIndexAccess(m_pResMgr));
109 break;
110 case 1:
111 xResult = Reference<XIndexAccess>(new ResourceStringListIndexAccess(m_pResMgr));
112 break;
113 default:
114 throw NoSuchElementException();
116 return makeAny(xResult);
119 Sequence<OUString> SAL_CALL ResourceIndexAccess::getElementNames( )
120 throw (RuntimeException, std::exception)
122 static Sequence<OUString> aResult;
123 if( aResult.getLength() == 0)
125 aResult.realloc(2);
126 aResult[0] = "String";
127 aResult[1] = "StringList";
129 return aResult;
132 sal_Bool SAL_CALL ResourceIndexAccess::hasByName(const OUString& aName)
133 throw (RuntimeException, std::exception)
135 const Sequence<OUString> aNames(getElementNames());
136 return (::std::find(aNames.begin(), aNames.end(), aName) != aNames.end());
139 Any SAL_CALL ResourceStringIndexAccess::getByIndex(sal_Int32 nIdx)
140 throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
142 if(nIdx > SAL_MAX_UINT16 || nIdx < 0)
143 throw IndexOutOfBoundsException();
144 SolarMutexGuard aGuard;
145 if(!m_pResMgr.get())
146 throw RuntimeException("resource manager not available");
148 const ResId aId(static_cast<sal_uInt16>(nIdx), *m_pResMgr);
149 aId.SetRT(RSC_STRING);
151 if(!m_pResMgr->IsAvailable(aId))
152 throw RuntimeException("string resource for id not available");
154 return makeAny(aId.toString());
157 Any SAL_CALL ResourceStringListIndexAccess::getByIndex(sal_Int32 nIdx)
158 throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
160 if(nIdx > SAL_MAX_UINT16 || nIdx < 0)
161 throw IndexOutOfBoundsException();
162 SolarMutexGuard aGuard;
164 if(!m_pResMgr.get())
165 throw RuntimeException("resource manager not available");
167 const ResId aId(static_cast<sal_uInt16>(nIdx), *m_pResMgr);
168 aId.SetRT(RSC_STRINGARRAY);
169 if(!m_pResMgr->IsAvailable(aId))
170 throw RuntimeException("string list resource for id not available");
171 const ResStringArray aStringList(aId);
172 Sequence<PropertyValue> aPropList(aStringList.Count());
173 for(sal_Int32 nCount = 0; nCount != aPropList.getLength(); ++nCount)
175 aPropList[nCount].Name = aStringList.GetString(nCount);
176 aPropList[nCount].Handle = -1;
177 aPropList[nCount].Value <<= aStringList.GetValue(nCount);
178 aPropList[nCount].State = PropertyState_DIRECT_VALUE;
180 return makeAny(aPropList);
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */