Update ooo320-m1
[ooovba.git] / svx / source / unodraw / UnoNameItemTable.cxx
blobef1848cf3e313af2f3a8f4651a32743ef71033ac
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: UnoNameItemTable.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <set>
35 #include <svtools/itempool.hxx>
36 #include <svtools/itemset.hxx>
37 #include <svtools/style.hxx>
38 #include <comphelper/stl_types.hxx>
40 #include <svx/svdmodel.hxx>
41 #include "UnoNameItemTable.hxx"
42 #include <vos/mutex.hxx>
43 #include <vcl/svapp.hxx>
45 #include "unoapi.hxx"
47 using namespace ::com::sun::star;
48 using namespace ::rtl;
49 using namespace ::cppu;
50 using namespace ::vos;
52 SvxUnoNameItemTable::SvxUnoNameItemTable( SdrModel* pModel, USHORT nWhich, BYTE nMemberId ) throw()
53 : mpModel( pModel ),
54 mpModelPool( pModel ? &pModel->GetItemPool() : NULL ),
55 mnWhich( nWhich ), mnMemberId( nMemberId )
57 if( pModel )
58 StartListening( *pModel );
61 SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
63 if( mpModel )
64 EndListening( *mpModel );
65 dispose();
68 bool SvxUnoNameItemTable::isValid( const NameOrIndex* pItem ) const
70 return pItem && (pItem->GetName().Len() != 0);
73 void SvxUnoNameItemTable::dispose()
75 ItemPoolVector::iterator aIter = maItemSetVector.begin();
76 const ItemPoolVector::iterator aEnd = maItemSetVector.end();
78 while( aIter != aEnd )
80 delete (*aIter++);
83 maItemSetVector.clear();
86 void SvxUnoNameItemTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
88 const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
90 if( pSdrHint && HINT_MODELCLEARED == pSdrHint->GetKind() )
91 dispose();
94 sal_Bool SAL_CALL SvxUnoNameItemTable::supportsService( const OUString& ServiceName ) throw(uno::RuntimeException)
96 uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
97 const OUString * pArray = aSNL.getConstArray();
99 for( INT32 i = 0; i < aSNL.getLength(); i++ )
100 if( pArray[i] == ServiceName )
101 return TRUE;
103 return FALSE;
106 void SAL_CALL SvxUnoNameItemTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
108 SfxItemSet* mpInSet = new SfxItemSet( *mpModelPool, mnWhich, mnWhich );
109 maItemSetVector.push_back( mpInSet );
111 NameOrIndex* pNewItem = createItem();
112 pNewItem->SetName( String( aName ) );
113 pNewItem->PutValue( aElement, mnMemberId );
114 mpInSet->Put( *pNewItem, mnWhich );
115 delete pNewItem;
118 // XNameContainer
119 void SAL_CALL SvxUnoNameItemTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
120 throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
122 OGuard aGuard( Application::GetSolarMutex() );
124 if( hasByName( aApiName ) )
125 throw container::ElementExistException();
127 String aName;
128 SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
130 ImplInsertByName( aName, aElement );
135 void SAL_CALL SvxUnoNameItemTable::removeByName( const OUString& aApiName )
136 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
138 OGuard aGuard( Application::GetSolarMutex() );
140 // a little quickfix for 2.0 to let applications clear api
141 // created items that are not used
142 if( aApiName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("~clear~") ) )
144 dispose();
145 return;
148 String Name;
149 SvxUnogetInternalNameForItem( mnWhich, aApiName, Name );
151 ItemPoolVector::iterator aIter = maItemSetVector.begin();
152 const ItemPoolVector::iterator aEnd = maItemSetVector.end();
154 NameOrIndex *pItem;
155 const String aSearchName( Name );
157 while( aIter != aEnd )
159 pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
160 if( pItem->GetName() == aSearchName )
162 delete (*aIter);
163 maItemSetVector.erase( aIter );
164 return;
166 aIter++;
169 if( !hasByName( Name ) )
170 throw container::NoSuchElementException();
173 // XNameReplace
174 void SAL_CALL SvxUnoNameItemTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
175 throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
177 OGuard aGuard( Application::GetSolarMutex() );
179 String aName;
180 SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
182 ItemPoolVector::iterator aIter = maItemSetVector.begin();
183 const ItemPoolVector::iterator aEnd = maItemSetVector.end();
185 NameOrIndex *pItem;
186 const String aSearchName( aName );
188 while( aIter != aEnd )
190 pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
191 if( pItem->GetName() == aSearchName )
193 NameOrIndex* pNewItem = createItem();
194 pNewItem->SetName( aSearchName );
195 if( !pNewItem->PutValue( aElement, mnMemberId ) || !isValid( pNewItem ) )
196 throw lang::IllegalArgumentException();
198 (*aIter)->Put( *pNewItem );
199 return;
201 aIter++;
204 // if it is not in our own sets, modify the pool!
205 sal_Bool bFound = sal_False;
207 USHORT nSurrogate;
208 USHORT nCount = mpModelPool ? mpModelPool->GetItemCount( mnWhich ) : 0;
209 for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
211 pItem = (NameOrIndex*)mpModelPool->GetItem( mnWhich, nSurrogate);
212 if( pItem && pItem->GetName() == aSearchName )
214 pItem->PutValue( aElement, mnMemberId );
215 bFound = sal_True;
216 break;
220 if( bFound )
221 ImplInsertByName( aName, aElement );
222 else
223 throw container::NoSuchElementException();
225 if( !hasByName( aName ) )
226 throw container::NoSuchElementException();
229 // XNameAccess
230 uno::Any SAL_CALL SvxUnoNameItemTable::getByName( const OUString& aApiName )
231 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
233 OGuard aGuard( Application::GetSolarMutex() );
235 String aName;
236 SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
238 uno::Any aAny;
240 if( mpModelPool && aName.Len() != 0 )
242 const String aSearchName( aName );
243 NameOrIndex *pItem;
244 sal_Int32 nSurrogate;
246 sal_Int32 nSurrogateCount = mpModelPool ? (sal_Int32)mpModelPool->GetItemCount( mnWhich ) : 0;
247 for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
249 pItem = (NameOrIndex*)mpModelPool->GetItem( mnWhich, (USHORT)nSurrogate );
251 if( isValid( pItem ) && (pItem->GetName() == aSearchName) )
253 pItem->QueryValue( aAny, mnMemberId );
254 return aAny;
259 throw container::NoSuchElementException();
262 uno::Sequence< OUString > SAL_CALL SvxUnoNameItemTable::getElementNames( )
263 throw( uno::RuntimeException )
265 OGuard aGuard( Application::GetSolarMutex() );
267 std::set< OUString, comphelper::UStringLess > aNameSet;
269 NameOrIndex *pItem;
270 OUString aApiName;
272 const sal_Int32 nSurrogateCount = mpModelPool ? (sal_Int32)mpModelPool->GetItemCount( mnWhich ) : 0;
273 sal_Int32 nSurrogate;
274 for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
276 pItem = (NameOrIndex*)mpModelPool->GetItem( mnWhich, (USHORT)nSurrogate );
278 if( !isValid( pItem ) )
279 continue;
281 SvxUnogetApiNameForItem( mnWhich, pItem->GetName(), aApiName );
282 aNameSet.insert( aApiName );
285 uno::Sequence< OUString > aSeq( aNameSet.size() );
286 OUString* pNames = aSeq.getArray();
288 std::set< OUString, comphelper::UStringLess >::iterator aIter( aNameSet.begin() );
289 const std::set< OUString, comphelper::UStringLess >::iterator aEnd( aNameSet.end() );
291 while( aIter != aEnd )
293 *pNames++ = *aIter++;
296 return aSeq;
299 sal_Bool SAL_CALL SvxUnoNameItemTable::hasByName( const OUString& aApiName )
300 throw( uno::RuntimeException )
302 OGuard aGuard( Application::GetSolarMutex() );
304 String aName;
305 SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
307 if( aName.Len() == 0 )
308 return sal_False;
310 const String aSearchName( aName );
311 USHORT nSurrogate;
313 const NameOrIndex *pItem;
315 USHORT nCount = mpModelPool ? mpModelPool->GetItemCount( mnWhich ) : 0;
316 for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
318 pItem = (NameOrIndex*)mpModelPool->GetItem( mnWhich, nSurrogate );
319 if( isValid( pItem ) && (pItem->GetName() == aSearchName) )
320 return sal_True;
323 return sal_False;
326 sal_Bool SAL_CALL SvxUnoNameItemTable::hasElements( )
327 throw( uno::RuntimeException )
329 OGuard aGuard( Application::GetSolarMutex() );
331 const NameOrIndex *pItem;
333 sal_Int32 nSurrogate;
334 const sal_Int32 nSurrogateCount = mpModelPool ? (sal_Int32)mpModelPool->GetItemCount( mnWhich ) : 0;
335 for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
337 pItem = (NameOrIndex*)mpModelPool->GetItem( mnWhich, (USHORT)nSurrogate );
339 if( isValid( pItem ) )
340 return sal_True;
343 return sal_False;