bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / unodraw / UnoNameItemTable.cxx
blob3b44653f575d44934618606a25eb52b3dd5b9c11
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <set>
22 #include <svl/itempool.hxx>
23 #include <svl/itemset.hxx>
24 #include <svl/style.hxx>
25 #include <comphelper/stl_types.hxx>
27 #include <svx/svdmodel.hxx>
28 #include "UnoNameItemTable.hxx"
29 #include <osl/mutex.hxx>
30 #include <vcl/svapp.hxx>
32 #include "svx/unoapi.hxx"
34 using namespace ::com::sun::star;
35 using namespace ::rtl;
36 using namespace ::cppu;
38 SvxUnoNameItemTable::SvxUnoNameItemTable( SdrModel* pModel, sal_uInt16 nWhich, sal_uInt8 nMemberId ) throw()
39 : mpModel( pModel ),
40 mpModelPool( pModel ? &pModel->GetItemPool() : NULL ),
41 mnWhich( nWhich ), mnMemberId( nMemberId )
43 if( pModel )
44 StartListening( *pModel );
47 SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
49 if( mpModel )
50 EndListening( *mpModel );
51 dispose();
54 bool SvxUnoNameItemTable::isValid( const NameOrIndex* pItem ) const
56 return pItem && (pItem->GetName().Len() != 0);
59 void SvxUnoNameItemTable::dispose()
61 ItemPoolVector::iterator aIter = maItemSetVector.begin();
62 const ItemPoolVector::iterator aEnd = maItemSetVector.end();
64 while( aIter != aEnd )
66 delete (*aIter++);
69 maItemSetVector.clear();
72 void SvxUnoNameItemTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
74 const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
76 if( pSdrHint && HINT_MODELCLEARED == pSdrHint->GetKind() )
77 dispose();
80 sal_Bool SAL_CALL SvxUnoNameItemTable::supportsService( const OUString& ServiceName ) throw(uno::RuntimeException)
82 uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
83 const OUString * pArray = aSNL.getConstArray();
85 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
86 if( pArray[i] == ServiceName )
87 return sal_True;
89 return sal_False;
92 void SAL_CALL SvxUnoNameItemTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
94 SfxItemSet* mpInSet = new SfxItemSet( *mpModelPool, mnWhich, mnWhich );
95 maItemSetVector.push_back( mpInSet );
97 NameOrIndex* pNewItem = createItem();
98 pNewItem->SetName( String( aName ) );
99 pNewItem->PutValue( aElement, mnMemberId );
100 mpInSet->Put( *pNewItem, mnWhich );
101 delete pNewItem;
104 // XNameContainer
105 void SAL_CALL SvxUnoNameItemTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
106 throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
108 SolarMutexGuard aGuard;
110 if( hasByName( aApiName ) )
111 throw container::ElementExistException();
113 OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
115 ImplInsertByName( aName, aElement );
120 void SAL_CALL SvxUnoNameItemTable::removeByName( const OUString& aApiName )
121 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
123 SolarMutexGuard aGuard;
125 // a little quickfix for 2.0 to let applications clear api
126 // created items that are not used
127 if ( aApiName == "~clear~" )
129 dispose();
130 return;
133 OUString sName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
135 ItemPoolVector::iterator aIter = maItemSetVector.begin();
136 const ItemPoolVector::iterator aEnd = maItemSetVector.end();
138 NameOrIndex *pItem;
140 while( aIter != aEnd )
142 pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
143 if (sName.equals(pItem->GetName()))
145 delete (*aIter);
146 maItemSetVector.erase( aIter );
147 return;
149 ++aIter;
152 if (!hasByName(sName))
153 throw container::NoSuchElementException();
156 // XNameReplace
157 void SAL_CALL SvxUnoNameItemTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
158 throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
160 SolarMutexGuard aGuard;
162 OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
164 ItemPoolVector::iterator aIter = maItemSetVector.begin();
165 const ItemPoolVector::iterator aEnd = maItemSetVector.end();
167 NameOrIndex *pItem;
169 while( aIter != aEnd )
171 pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
172 if (aName.equals(pItem->GetName()))
174 NameOrIndex* pNewItem = createItem();
175 pNewItem->SetName(aName);
176 if( !pNewItem->PutValue( aElement, mnMemberId ) || !isValid( pNewItem ) )
177 throw lang::IllegalArgumentException();
179 (*aIter)->Put( *pNewItem );
180 return;
182 ++aIter;
185 // if it is not in our own sets, modify the pool!
186 bool bFound = false;
188 sal_uInt32 nSurrogate;
189 sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
190 for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
192 pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate);
193 if (pItem && aName.equals(pItem->GetName()))
195 pItem->PutValue( aElement, mnMemberId );
196 bFound = true;
197 break;
201 if( bFound )
202 ImplInsertByName( aName, aElement );
203 else
204 throw container::NoSuchElementException();
206 if( !hasByName( aName ) )
207 throw container::NoSuchElementException();
210 // XNameAccess
211 uno::Any SAL_CALL SvxUnoNameItemTable::getByName( const OUString& aApiName )
212 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
214 SolarMutexGuard aGuard;
216 OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
218 uno::Any aAny;
220 if (mpModelPool && !aName.isEmpty())
222 NameOrIndex *pItem;
223 sal_uInt32 nSurrogate;
225 sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
226 for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
228 pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
230 if (isValid(pItem) && aName.equals(pItem->GetName()))
232 pItem->QueryValue( aAny, mnMemberId );
233 return aAny;
238 throw container::NoSuchElementException();
241 uno::Sequence< OUString > SAL_CALL SvxUnoNameItemTable::getElementNames( )
242 throw( uno::RuntimeException )
244 SolarMutexGuard aGuard;
246 std::set< OUString, comphelper::UStringLess > aNameSet;
248 NameOrIndex *pItem;
250 const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
251 sal_uInt32 nSurrogate;
252 for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
254 pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
256 if( !isValid( pItem ) )
257 continue;
259 OUString aApiName = SvxUnogetApiNameForItem(mnWhich, pItem->GetName());
260 aNameSet.insert(aApiName);
263 uno::Sequence< OUString > aSeq( aNameSet.size() );
264 OUString* pNames = aSeq.getArray();
266 std::set< OUString, comphelper::UStringLess >::iterator aIter( aNameSet.begin() );
267 const std::set< OUString, comphelper::UStringLess >::iterator aEnd( aNameSet.end() );
269 while( aIter != aEnd )
271 *pNames++ = *aIter++;
274 return aSeq;
277 sal_Bool SAL_CALL SvxUnoNameItemTable::hasByName( const OUString& aApiName )
278 throw( uno::RuntimeException )
280 SolarMutexGuard aGuard;
282 OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
284 if (aName.isEmpty())
285 return sal_False;
287 sal_uInt32 nSurrogate;
289 const NameOrIndex *pItem;
291 sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
292 for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
294 pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
295 if (isValid(pItem) && aName.equals(pItem->GetName()))
296 return sal_True;
299 return sal_False;
302 sal_Bool SAL_CALL SvxUnoNameItemTable::hasElements( )
303 throw( uno::RuntimeException )
305 SolarMutexGuard aGuard;
307 const NameOrIndex *pItem;
309 sal_uInt32 nSurrogate;
310 const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
311 for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
313 pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
315 if( isValid( pItem ) )
316 return sal_True;
319 return sal_False;
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */