1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include "comphelper_module.hxx"
32 
33 #include <com/sun/star/container/XIndexContainer.hpp>
34 #include <com/sun/star/uno/Sequence.h>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <cppuhelper/implbase2.hxx>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/uno/XComponentContext.hpp>
39 
40 
41 #ifndef __SGI_STL_VECTOR
42 #include <vector>
43 #endif
44 
45 using namespace com::sun::star;
46 
47 typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
48 
49 class IndexedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XIndexContainer, lang::XServiceInfo >
50 {
51 public:
52 	IndexedPropertyValuesContainer() throw();
53 	virtual ~IndexedPropertyValuesContainer() throw();
54 
55 	// XIndexContainer
56 	virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
57 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
58 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
59 	virtual void SAL_CALL removeByIndex( sal_Int32 nIndex )
60 		throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
61 			::com::sun::star::uno::RuntimeException);
62 
63     // XIndexReplace
64 	virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
65 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
66 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
67 
68     // XIndexAccess
69 	virtual sal_Int32 SAL_CALL getCount(  )
70 		throw(::com::sun::star::uno::RuntimeException);
71 	virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex )
72 		throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
73 			::com::sun::star::uno::RuntimeException);
74 
75 	// XElementAccess
76 	virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
77 		throw(::com::sun::star::uno::RuntimeException);
78 	virtual sal_Bool SAL_CALL hasElements(  )
79 		throw(::com::sun::star::uno::RuntimeException);
80 
81 	//XServiceInfo
82 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
83 	virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
84 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
85 
86     // XServiceInfo - static versions (used for component registration)
87     static ::rtl::OUString SAL_CALL getImplementationName_static();
88     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
89     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
90 
91 private:
92 	IndexedPropertyValues maProperties;
93 };
94 
95 IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
96 {
97 }
98 
99 IndexedPropertyValuesContainer::~IndexedPropertyValuesContainer() throw()
100 {
101 }
102 
103 // XIndexContainer
104 void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
105 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
106 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
107 {
108 	sal_Int32 nSize(maProperties.size());
109 	if ((nSize >= nIndex) && (nIndex >= 0))
110 	{
111 		uno::Sequence<beans::PropertyValue> aProps;
112 		if (!(aElement >>= aProps))
113 			throw lang::IllegalArgumentException();
114 		if (nSize == nIndex)
115 			maProperties.push_back(aProps);
116 		else
117 		{
118 			IndexedPropertyValues::iterator aItr;
119 			if ((nIndex * 2) < nSize)
120 			{
121 				aItr = maProperties.begin();
122 				sal_Int32 i(0);
123 				while(i < nIndex)
124 				{
125 					i++;
126 					aItr++;
127 				}
128 			}
129 			else
130 			{
131 				aItr = maProperties.end();
132 				sal_Int32 i(nSize - 1);
133 				while(i > nIndex)
134 				{
135 					i--;
136 					aItr--;
137 				}
138 			}
139 			maProperties.insert(aItr, aProps);
140 		}
141 	}
142 	else
143 		throw lang::IndexOutOfBoundsException();
144 }
145 
146 void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
147 	throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
148 		::com::sun::star::uno::RuntimeException)
149 {
150 	sal_Int32 nSize(maProperties.size());
151 	if ((nIndex < nSize) && (nIndex >= 0))
152 	{
153 		IndexedPropertyValues::iterator aItr;
154 		if ((nIndex * 2) < nSize)
155 		{
156 			aItr = maProperties.begin();
157 			sal_Int32 i(0);
158 			while(i < nIndex)
159 			{
160 				i++;
161 				aItr++;
162 			}
163 		}
164 		else
165 		{
166 			aItr = maProperties.end();
167 			sal_Int32 i(nSize - 1);
168 			while(i > nIndex)
169 			{
170 				i--;
171 				aItr--;
172 			}
173 		}
174 		maProperties.erase(aItr);
175 	}
176 	else
177 		throw lang::IndexOutOfBoundsException();
178 }
179 
180 // XIndexReplace
181 void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
182 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
183 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
184 {
185 	sal_Int32 nSize(maProperties.size());
186 	if ((nIndex < nSize) && (nIndex >= 0))
187 	{
188 		uno::Sequence<beans::PropertyValue> aProps;
189 		if (!(aElement >>= aProps))
190 			throw lang::IllegalArgumentException();
191 		maProperties[nIndex] = aProps;
192 	}
193 	else
194 		throw lang::IndexOutOfBoundsException();
195 }
196 
197 // XIndexAccess
198 sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount(  )
199 	throw(::com::sun::star::uno::RuntimeException)
200 {
201 	return maProperties.size();
202 }
203 
204 ::com::sun::star::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
205 	throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
206 		::com::sun::star::uno::RuntimeException)
207 {
208 	sal_Int32 nSize(maProperties.size());
209 	if (!((nIndex < nSize) && (nIndex >= 0)))
210 		throw lang::IndexOutOfBoundsException();
211 
212 	uno::Any aAny;
213 	aAny <<= maProperties[nIndex];
214 	return aAny;
215 }
216 
217 // XElementAccess
218 ::com::sun::star::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType(  )
219 	throw(::com::sun::star::uno::RuntimeException)
220 {
221 	return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
222 }
223 
224 sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements(  )
225 	throw(::com::sun::star::uno::RuntimeException)
226 {
227 	return !maProperties.empty();
228 }
229 
230 //XServiceInfo
231 ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException)
232 {
233 	return getImplementationName_static();
234 }
235 
236 ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName_static(  )
237 {
238 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IndexedPropertyValuesContainer" ) );
239 }
240 
241 sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
242 {
243 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
244 	return aServiceName == ServiceName;
245 }
246 
247 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
248 {
249 	return getSupportedServiceNames_static();
250 }
251 
252 
253 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames_static(  )
254 {
255 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
256 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
257 	return aSeq;
258 }
259 
260 
261 uno::Reference< uno::XInterface > SAL_CALL IndexedPropertyValuesContainer::Create(
262                 const uno::Reference< uno::XComponentContext >&)
263 {
264 	return (cppu::OWeakObject*)new IndexedPropertyValuesContainer();
265 }
266 
267 void createRegistryInfo_IndexedPropertyValuesContainer()
268 {
269     static ::comphelper::module::OAutoRegistration< IndexedPropertyValuesContainer > aAutoRegistration;
270 }
271