Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <set>
31 : : #include <svl/itempool.hxx>
32 : : #include <svl/itemset.hxx>
33 : : #include <svl/style.hxx>
34 : : #include <comphelper/stl_types.hxx>
35 : :
36 : : #include <svx/svdmodel.hxx>
37 : : #include "UnoNameItemTable.hxx"
38 : : #include <osl/mutex.hxx>
39 : : #include <vcl/svapp.hxx>
40 : :
41 : : #include "svx/unoapi.hxx"
42 : :
43 : : using namespace ::com::sun::star;
44 : : using namespace ::rtl;
45 : : using namespace ::cppu;
46 : :
47 : 479 : SvxUnoNameItemTable::SvxUnoNameItemTable( SdrModel* pModel, sal_uInt16 nWhich, sal_uInt8 nMemberId ) throw()
48 : : : mpModel( pModel ),
49 : : mpModelPool( pModel ? &pModel->GetItemPool() : NULL ),
50 [ + - ][ + - ]: 479 : mnWhich( nWhich ), mnMemberId( nMemberId )
[ + - ]
51 : : {
52 [ + - ]: 479 : if( pModel )
53 [ + - ]: 479 : StartListening( *pModel );
54 : 479 : }
55 : :
56 [ + - ]: 476 : SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
57 : : {
58 [ + - ]: 476 : if( mpModel )
59 [ + - ]: 476 : EndListening( *mpModel );
60 [ + - ]: 476 : dispose();
61 [ - + ]: 476 : }
62 : :
63 : 314 : bool SvxUnoNameItemTable::isValid( const NameOrIndex* pItem ) const
64 : : {
65 [ + - ][ + - ]: 314 : return pItem && (pItem->GetName().Len() != 0);
[ + + ][ + - ]
[ # # ]
66 : : }
67 : :
68 : 983 : void SvxUnoNameItemTable::dispose()
69 : : {
70 : 983 : ItemPoolVector::iterator aIter = maItemSetVector.begin();
71 : 983 : const ItemPoolVector::iterator aEnd = maItemSetVector.end();
72 : :
73 [ + - ][ + + ]: 1069 : while( aIter != aEnd )
74 : : {
75 [ + - ][ + - ]: 86 : delete (*aIter++);
[ + - ][ + - ]
76 : : }
77 : :
78 : 983 : maItemSetVector.clear();
79 : 983 : }
80 : :
81 : 473805 : void SvxUnoNameItemTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
82 : : {
83 [ + - ][ + + ]: 473805 : const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
84 : :
85 [ + + ][ + + ]: 473805 : if( pSdrHint && HINT_MODELCLEARED == pSdrHint->GetKind() )
[ + + ]
86 : 452 : dispose();
87 : 473805 : }
88 : :
89 : 0 : sal_Bool SAL_CALL SvxUnoNameItemTable::supportsService( const OUString& ServiceName ) throw(uno::RuntimeException)
90 : : {
91 [ # # ]: 0 : uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
92 : 0 : const OUString * pArray = aSNL.getConstArray();
93 : :
94 [ # # ]: 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
95 [ # # ]: 0 : if( pArray[i] == ServiceName )
96 : 0 : return sal_True;
97 : :
98 [ # # ]: 0 : return sal_False;
99 : : }
100 : :
101 : 98 : void SAL_CALL SvxUnoNameItemTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
102 : : {
103 [ + - ][ + - ]: 98 : SfxItemSet* mpInSet = new SfxItemSet( *mpModelPool, mnWhich, mnWhich );
104 [ + - ]: 98 : maItemSetVector.push_back( mpInSet );
105 : :
106 : 98 : NameOrIndex* pNewItem = createItem();
107 [ + - ][ + - ]: 98 : pNewItem->SetName( String( aName ) );
[ + - ]
108 [ + - ]: 98 : pNewItem->PutValue( aElement, mnMemberId );
109 [ + - ]: 98 : mpInSet->Put( *pNewItem, mnWhich );
110 [ + - ][ + - ]: 98 : delete pNewItem;
111 : 98 : }
112 : :
113 : : // XNameContainer
114 : 98 : void SAL_CALL SvxUnoNameItemTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
115 : : throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
116 : : {
117 [ + - ]: 98 : SolarMutexGuard aGuard;
118 : :
119 [ + - ][ - + ]: 98 : if( hasByName( aApiName ) )
120 [ # # ]: 0 : throw container::ElementExistException();
121 : :
122 [ + - ]: 98 : String aName;
123 : 98 : SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
124 : :
125 [ + - ][ + - ]: 98 : ImplInsertByName( aName, aElement );
[ + - ][ + - ]
126 : 98 : }
127 : :
128 : :
129 : :
130 : 55 : void SAL_CALL SvxUnoNameItemTable::removeByName( const OUString& aApiName )
131 : : throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
132 : : {
133 [ + - ]: 55 : SolarMutexGuard aGuard;
134 : :
135 : : // a little quickfix for 2.0 to let applications clear api
136 : : // created items that are not used
137 [ + - ]: 55 : if ( aApiName == "~clear~" )
138 : : {
139 [ + - ]: 55 : dispose();
140 : : return;
141 : : }
142 : :
143 [ # # ]: 0 : String Name;
144 : 0 : SvxUnogetInternalNameForItem( mnWhich, aApiName, Name );
145 : :
146 : 0 : ItemPoolVector::iterator aIter = maItemSetVector.begin();
147 : 0 : const ItemPoolVector::iterator aEnd = maItemSetVector.end();
148 : :
149 : : NameOrIndex *pItem;
150 [ # # ]: 0 : const String aSearchName( Name );
151 : :
152 [ # # ][ # # ]: 0 : while( aIter != aEnd )
153 : : {
154 [ # # ][ # # ]: 0 : pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
155 [ # # ][ # # ]: 0 : if( pItem->GetName() == aSearchName )
[ # # ][ # # ]
156 : : {
157 [ # # ][ # # ]: 0 : delete (*aIter);
[ # # ]
158 [ # # ]: 0 : maItemSetVector.erase( aIter );
159 : : return;
160 : : }
161 [ # # ]: 0 : ++aIter;
162 : : }
163 : :
164 [ # # ][ # # ]: 0 : if( !hasByName( Name ) )
[ # # ]
165 [ # # ][ # # ]: 55 : throw container::NoSuchElementException();
[ # # ][ # # ]
[ # # ][ + - ]
[ - + ]
166 : : }
167 : :
168 : : // XNameReplace
169 : 0 : void SAL_CALL SvxUnoNameItemTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
170 : : throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
171 : : {
172 [ # # ]: 0 : SolarMutexGuard aGuard;
173 : :
174 [ # # ]: 0 : String aName;
175 : 0 : SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
176 : :
177 : 0 : ItemPoolVector::iterator aIter = maItemSetVector.begin();
178 : 0 : const ItemPoolVector::iterator aEnd = maItemSetVector.end();
179 : :
180 : : NameOrIndex *pItem;
181 [ # # ]: 0 : const String aSearchName( aName );
182 : :
183 [ # # ][ # # ]: 0 : while( aIter != aEnd )
184 : : {
185 [ # # ][ # # ]: 0 : pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
186 [ # # ][ # # ]: 0 : if( pItem->GetName() == aSearchName )
[ # # ][ # # ]
187 : : {
188 : 0 : NameOrIndex* pNewItem = createItem();
189 [ # # ]: 0 : pNewItem->SetName( aSearchName );
190 [ # # ][ # # ]: 0 : if( !pNewItem->PutValue( aElement, mnMemberId ) || !isValid( pNewItem ) )
[ # # ][ # # ]
[ # # ]
191 [ # # ]: 0 : throw lang::IllegalArgumentException();
192 : :
193 [ # # ][ # # ]: 0 : (*aIter)->Put( *pNewItem );
194 : 0 : return;
195 : : }
196 [ # # ]: 0 : ++aIter;
197 : : }
198 : :
199 : : // if it is not in our own sets, modify the pool!
200 : 0 : sal_Bool bFound = sal_False;
201 : :
202 : : sal_uInt32 nSurrogate;
203 [ # # ][ # # ]: 0 : sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
204 [ # # ]: 0 : for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
205 : : {
206 [ # # ]: 0 : pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate);
207 [ # # ][ # # ]: 0 : if( pItem && pItem->GetName() == aSearchName )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
208 : : {
209 [ # # ]: 0 : pItem->PutValue( aElement, mnMemberId );
210 : 0 : bFound = sal_True;
211 : 0 : break;
212 : : }
213 : : }
214 : :
215 [ # # ]: 0 : if( bFound )
216 [ # # ][ # # ]: 0 : ImplInsertByName( aName, aElement );
217 : : else
218 [ # # ]: 0 : throw container::NoSuchElementException();
219 : :
220 [ # # ][ # # ]: 0 : if( !hasByName( aName ) )
[ # # ]
221 [ # # ][ # # ]: 0 : throw container::NoSuchElementException();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
222 : : }
223 : :
224 : : // XNameAccess
225 : 0 : uno::Any SAL_CALL SvxUnoNameItemTable::getByName( const OUString& aApiName )
226 : : throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
227 : : {
228 [ # # ]: 0 : SolarMutexGuard aGuard;
229 : :
230 [ # # ]: 0 : String aName;
231 : 0 : SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
232 : :
233 : 0 : uno::Any aAny;
234 : :
235 [ # # ][ # # ]: 0 : if( mpModelPool && aName.Len() != 0 )
[ # # ]
236 : : {
237 [ # # ]: 0 : const String aSearchName( aName );
238 : : NameOrIndex *pItem;
239 : : sal_uInt32 nSurrogate;
240 : :
241 [ # # ][ # # ]: 0 : sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
242 [ # # ]: 0 : for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
243 : : {
244 [ # # ]: 0 : pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
245 : :
246 [ # # ][ # # ]: 0 : if( isValid( pItem ) && (pItem->GetName() == aSearchName) )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
[ # # # # ]
247 : : {
248 [ # # ]: 0 : pItem->QueryValue( aAny, mnMemberId );
249 : 0 : return aAny;
250 : : }
251 [ # # ][ # # ]: 0 : }
252 : : }
253 : :
254 [ # # ][ # # ]: 0 : throw container::NoSuchElementException();
[ # # ]
255 : : }
256 : :
257 : 0 : uno::Sequence< OUString > SAL_CALL SvxUnoNameItemTable::getElementNames( )
258 : : throw( uno::RuntimeException )
259 : : {
260 [ # # ]: 0 : SolarMutexGuard aGuard;
261 : :
262 [ # # ]: 0 : std::set< OUString, comphelper::UStringLess > aNameSet;
263 : :
264 : : NameOrIndex *pItem;
265 : 0 : OUString aApiName;
266 : :
267 [ # # ][ # # ]: 0 : const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
268 : : sal_uInt32 nSurrogate;
269 [ # # ]: 0 : for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
270 : : {
271 [ # # ]: 0 : pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
272 : :
273 [ # # ][ # # ]: 0 : if( !isValid( pItem ) )
274 : 0 : continue;
275 : :
276 [ # # ][ # # ]: 0 : SvxUnogetApiNameForItem( mnWhich, pItem->GetName(), aApiName );
277 [ # # ]: 0 : aNameSet.insert( aApiName );
278 : : }
279 : :
280 [ # # ]: 0 : uno::Sequence< OUString > aSeq( aNameSet.size() );
281 [ # # ]: 0 : OUString* pNames = aSeq.getArray();
282 : :
283 : 0 : std::set< OUString, comphelper::UStringLess >::iterator aIter( aNameSet.begin() );
284 : 0 : const std::set< OUString, comphelper::UStringLess >::iterator aEnd( aNameSet.end() );
285 : :
286 [ # # ]: 0 : while( aIter != aEnd )
287 : : {
288 : 0 : *pNames++ = *aIter++;
289 : : }
290 : :
291 [ # # ]: 0 : return aSeq;
292 : : }
293 : :
294 : 196 : sal_Bool SAL_CALL SvxUnoNameItemTable::hasByName( const OUString& aApiName )
295 : : throw( uno::RuntimeException )
296 : : {
297 [ + - ]: 196 : SolarMutexGuard aGuard;
298 : :
299 [ + - ]: 196 : String aName;
300 : 196 : SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
301 : :
302 [ - + ]: 196 : if( aName.Len() == 0 )
303 : 0 : return sal_False;
304 : :
305 [ + - ]: 196 : const String aSearchName( aName );
306 : : sal_uInt32 nSurrogate;
307 : :
308 : : const NameOrIndex *pItem;
309 : :
310 [ + - ][ + - ]: 196 : sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
311 [ + + ]: 462 : for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
312 : : {
313 [ + - ]: 266 : pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
314 [ + - ][ + + ]: 266 : if( isValid( pItem ) && (pItem->GetName() == aSearchName) )
[ + - ][ + - ]
[ - + ][ + + ]
[ + - ]
[ - + # # ]
315 : 0 : return sal_True;
316 : : }
317 : :
318 [ + - ][ + - ]: 196 : return sal_False;
[ + - ]
319 : : }
320 : :
321 : 230 : sal_Bool SAL_CALL SvxUnoNameItemTable::hasElements( )
322 : : throw( uno::RuntimeException )
323 : : {
324 [ + - ]: 230 : SolarMutexGuard aGuard;
325 : :
326 : : const NameOrIndex *pItem;
327 : :
328 : : sal_uInt32 nSurrogate;
329 [ + - ][ + - ]: 230 : const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
330 [ + + ]: 278 : for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
331 : : {
332 [ + - ]: 48 : pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
333 : :
334 [ + - ][ - + ]: 48 : if( isValid( pItem ) )
335 : 0 : return sal_True;
336 : : }
337 : :
338 [ + - ]: 230 : return sal_False;
339 : : }
340 : :
341 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|