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 ::cppu::UnoType<OUString>::get(); };
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 cppu::UnoType<Sequence<PropertyValue>>::get(); };
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 0 : throw RuntimeException("resource manager could not get initialized");
97 0 : return xResult;
98 : }
99 :
100 0 : Any SAL_CALL ResourceIndexAccess::getByName(const OUString& aName)
101 : throw (NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
102 : {
103 0 : const Sequence<OUString> aNames(getElementNames());
104 0 : Reference<XIndexAccess> xResult;
105 0 : switch(::std::find(aNames.begin(), aNames.end(), aName) - aNames.begin())
106 : {
107 : case 0:
108 0 : xResult = Reference<XIndexAccess>(new ResourceStringIndexAccess(m_pResMgr));
109 0 : break;
110 : case 1:
111 0 : xResult = Reference<XIndexAccess>(new ResourceStringListIndexAccess(m_pResMgr));
112 0 : break;
113 : default:
114 0 : throw NoSuchElementException();
115 : }
116 0 : return makeAny(xResult);
117 : }
118 :
119 0 : Sequence<OUString> SAL_CALL ResourceIndexAccess::getElementNames( )
120 : throw (RuntimeException, std::exception)
121 : {
122 0 : static Sequence<OUString> aResult;
123 0 : if( aResult.getLength() == 0)
124 : {
125 0 : aResult.realloc(2);
126 0 : aResult[0] = "String";
127 0 : aResult[1] = "StringList";
128 : }
129 0 : return aResult;
130 : }
131 :
132 0 : sal_Bool SAL_CALL ResourceIndexAccess::hasByName(const OUString& aName)
133 : throw (RuntimeException, std::exception)
134 : {
135 0 : const Sequence<OUString> aNames(getElementNames());
136 0 : return (::std::find(aNames.begin(), aNames.end(), aName) != aNames.end());
137 : }
138 :
139 0 : Any SAL_CALL ResourceStringIndexAccess::getByIndex(sal_Int32 nIdx)
140 : throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
141 : {
142 0 : if(nIdx > SAL_MAX_UINT16 || nIdx < 0)
143 0 : throw IndexOutOfBoundsException();
144 0 : SolarMutexGuard aGuard;
145 0 : if(!m_pResMgr.get())
146 0 : throw RuntimeException("resource manager not available");
147 :
148 0 : const ResId aId(static_cast<sal_uInt16>(nIdx), *m_pResMgr);
149 0 : aId.SetRT(RSC_STRING);
150 :
151 0 : if(!m_pResMgr->IsAvailable(aId))
152 0 : throw RuntimeException("string resource for id not available");
153 :
154 0 : return makeAny(aId.toString());
155 : }
156 :
157 0 : Any SAL_CALL ResourceStringListIndexAccess::getByIndex(sal_Int32 nIdx)
158 : throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
159 : {
160 0 : if(nIdx > SAL_MAX_UINT16 || nIdx < 0)
161 0 : throw IndexOutOfBoundsException();
162 0 : SolarMutexGuard aGuard;
163 :
164 0 : if(!m_pResMgr.get())
165 0 : throw RuntimeException("resource manager not available");
166 :
167 0 : const ResId aId(static_cast<sal_uInt16>(nIdx), *m_pResMgr);
168 0 : aId.SetRT(RSC_STRINGARRAY);
169 0 : if(!m_pResMgr->IsAvailable(aId))
170 0 : throw RuntimeException("string list resource for id not available");
171 0 : const ResStringArray aStringList(aId);
172 0 : Sequence<PropertyValue> aPropList(aStringList.Count());
173 0 : for(sal_Int32 nCount = 0; nCount != aPropList.getLength(); ++nCount)
174 : {
175 0 : aPropList[nCount].Name = aStringList.GetString(nCount);
176 0 : aPropList[nCount].Handle = -1;
177 0 : aPropList[nCount].Value <<= aStringList.GetValue(nCount);
178 0 : aPropList[nCount].State = PropertyState_DIRECT_VALUE;
179 : }
180 0 : return makeAny(aPropList);
181 : }
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|