1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <set>
28cdf0e10cSrcweir #include <svl/itempool.hxx>
29cdf0e10cSrcweir #include <svl/itemset.hxx>
30cdf0e10cSrcweir #include <svl/style.hxx>
31cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <svx/svdmodel.hxx>
34cdf0e10cSrcweir #include "UnoNameItemTable.hxx"
35cdf0e10cSrcweir #include <vos/mutex.hxx>
36cdf0e10cSrcweir #include <vcl/svapp.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "svx/unoapi.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star;
41cdf0e10cSrcweir using namespace ::rtl;
42cdf0e10cSrcweir using namespace ::cppu;
43cdf0e10cSrcweir using namespace ::vos;
44cdf0e10cSrcweir 
SvxUnoNameItemTable(SdrModel * pModel,sal_uInt16 nWhich,sal_uInt8 nMemberId)45cdf0e10cSrcweir SvxUnoNameItemTable::SvxUnoNameItemTable( SdrModel* pModel, sal_uInt16 nWhich, sal_uInt8 nMemberId ) throw()
46cdf0e10cSrcweir : mpModel( pModel ),
47cdf0e10cSrcweir   mpModelPool( pModel ? &pModel->GetItemPool() : NULL ),
48cdf0e10cSrcweir   mnWhich( nWhich ), mnMemberId( nMemberId )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	if( pModel )
51cdf0e10cSrcweir 		StartListening( *pModel );
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
~SvxUnoNameItemTable()54cdf0e10cSrcweir SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	if( mpModel )
57cdf0e10cSrcweir 		EndListening( *mpModel );
58cdf0e10cSrcweir 	dispose();
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
isValid(const NameOrIndex * pItem) const61cdf0e10cSrcweir bool SvxUnoNameItemTable::isValid( const NameOrIndex* pItem ) const
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	return pItem && (pItem->GetName().Len() != 0);
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
dispose()66cdf0e10cSrcweir void SvxUnoNameItemTable::dispose()
67cdf0e10cSrcweir {
68cdf0e10cSrcweir 	ItemPoolVector::iterator aIter = maItemSetVector.begin();
69cdf0e10cSrcweir 	const ItemPoolVector::iterator aEnd = maItemSetVector.end();
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	while( aIter != aEnd )
72cdf0e10cSrcweir 	{
73cdf0e10cSrcweir 		delete (*aIter++);
74cdf0e10cSrcweir 	}
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	maItemSetVector.clear();
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
Notify(SfxBroadcaster &,const SfxHint & rHint)79cdf0e10cSrcweir void SvxUnoNameItemTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	if( pSdrHint && HINT_MODELCLEARED == pSdrHint->GetKind() )
84cdf0e10cSrcweir 		dispose();
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
supportsService(const OUString & ServiceName)87cdf0e10cSrcweir sal_Bool SAL_CALL SvxUnoNameItemTable::supportsService( const  OUString& ServiceName ) throw(uno::RuntimeException)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
90cdf0e10cSrcweir     const OUString * pArray = aSNL.getConstArray();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
93cdf0e10cSrcweir         if( pArray[i] == ServiceName )
94cdf0e10cSrcweir             return sal_True;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     return sal_False;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
ImplInsertByName(const OUString & aName,const uno::Any & aElement)99cdf0e10cSrcweir void SAL_CALL SvxUnoNameItemTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	SfxItemSet* mpInSet = new SfxItemSet( *mpModelPool, mnWhich, mnWhich );
102cdf0e10cSrcweir 	maItemSetVector.push_back( mpInSet );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	NameOrIndex* pNewItem = createItem();
105cdf0e10cSrcweir 	pNewItem->SetName( String( aName ) );
106cdf0e10cSrcweir 	pNewItem->PutValue( aElement, mnMemberId );
107cdf0e10cSrcweir 	mpInSet->Put( *pNewItem, mnWhich );
108cdf0e10cSrcweir 	delete pNewItem;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir // XNameContainer
insertByName(const OUString & aApiName,const uno::Any & aElement)112cdf0e10cSrcweir void SAL_CALL SvxUnoNameItemTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
113cdf0e10cSrcweir 	throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	if( hasByName( aApiName ) )
118cdf0e10cSrcweir 		throw container::ElementExistException();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	String aName;
121cdf0e10cSrcweir 	SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	ImplInsertByName( aName, aElement );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
removeByName(const OUString & aApiName)128cdf0e10cSrcweir void SAL_CALL SvxUnoNameItemTable::removeByName( const OUString& aApiName )
129cdf0e10cSrcweir 	throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	// a little quickfix for 2.0 to let applications clear api
134cdf0e10cSrcweir 	// created items that are not used
135cdf0e10cSrcweir 	if( aApiName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("~clear~") ) )
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		dispose();
138cdf0e10cSrcweir 		return;
139cdf0e10cSrcweir 	}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	String Name;
142cdf0e10cSrcweir 	SvxUnogetInternalNameForItem( mnWhich, aApiName, Name );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	ItemPoolVector::iterator aIter = maItemSetVector.begin();
145cdf0e10cSrcweir 	const ItemPoolVector::iterator aEnd = maItemSetVector.end();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	NameOrIndex *pItem;
148cdf0e10cSrcweir 	const String aSearchName( Name );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	while( aIter != aEnd )
151cdf0e10cSrcweir 	{
152cdf0e10cSrcweir 		pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
153cdf0e10cSrcweir 		if( pItem->GetName() == aSearchName )
154cdf0e10cSrcweir 		{
155cdf0e10cSrcweir 			delete (*aIter);
156cdf0e10cSrcweir 			maItemSetVector.erase( aIter );
157cdf0e10cSrcweir 			return;
158cdf0e10cSrcweir 		}
159cdf0e10cSrcweir 		aIter++;
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	if( !hasByName( Name ) )
163cdf0e10cSrcweir 		throw container::NoSuchElementException();
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir // XNameReplace
replaceByName(const OUString & aApiName,const uno::Any & aElement)167cdf0e10cSrcweir void SAL_CALL SvxUnoNameItemTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
168cdf0e10cSrcweir 	throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	String aName;
173cdf0e10cSrcweir 	SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	ItemPoolVector::iterator aIter = maItemSetVector.begin();
176cdf0e10cSrcweir 	const ItemPoolVector::iterator aEnd = maItemSetVector.end();
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	NameOrIndex *pItem;
179cdf0e10cSrcweir 	const String aSearchName( aName );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	while( aIter != aEnd )
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
184cdf0e10cSrcweir 		if( pItem->GetName() == aSearchName )
185cdf0e10cSrcweir 		{
186cdf0e10cSrcweir 			NameOrIndex* pNewItem = createItem();
187cdf0e10cSrcweir 			pNewItem->SetName( aSearchName );
188cdf0e10cSrcweir 			if( !pNewItem->PutValue( aElement, mnMemberId ) || !isValid( pNewItem ) )
189cdf0e10cSrcweir 				throw lang::IllegalArgumentException();
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			(*aIter)->Put( *pNewItem );
192cdf0e10cSrcweir 			return;
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 		aIter++;
195cdf0e10cSrcweir 	}
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	// if it is not in our own sets, modify the pool!
198cdf0e10cSrcweir 	sal_Bool bFound = sal_False;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	sal_uInt32 nSurrogate;
201cdf0e10cSrcweir 	sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
202cdf0e10cSrcweir 	for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
203cdf0e10cSrcweir 	{
204cdf0e10cSrcweir 		pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate);
205cdf0e10cSrcweir 		if( pItem && pItem->GetName() == aSearchName )
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			pItem->PutValue( aElement, mnMemberId );
208cdf0e10cSrcweir 			bFound = sal_True;
209cdf0e10cSrcweir 			break;
210cdf0e10cSrcweir 		}
211cdf0e10cSrcweir 	}
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	if( bFound )
214cdf0e10cSrcweir 		ImplInsertByName( aName, aElement );
215cdf0e10cSrcweir 	else
216cdf0e10cSrcweir 		throw container::NoSuchElementException();
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	if( !hasByName( aName ) )
219cdf0e10cSrcweir 		throw container::NoSuchElementException();
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir // XNameAccess
getByName(const OUString & aApiName)223cdf0e10cSrcweir uno::Any SAL_CALL SvxUnoNameItemTable::getByName( const OUString& aApiName )
224cdf0e10cSrcweir 	throw( container::NoSuchElementException,  lang::WrappedTargetException, uno::RuntimeException)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	String aName;
229cdf0e10cSrcweir 	SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 	uno::Any aAny;
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	if( mpModelPool && aName.Len() != 0 )
234cdf0e10cSrcweir 	{
235cdf0e10cSrcweir 		const String aSearchName( aName );
236cdf0e10cSrcweir 		NameOrIndex *pItem;
237cdf0e10cSrcweir 		sal_uInt32 nSurrogate;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
240cdf0e10cSrcweir 		for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
241cdf0e10cSrcweir 		{
242cdf0e10cSrcweir 			pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 			if( isValid( pItem ) && (pItem->GetName() == aSearchName) )
245cdf0e10cSrcweir 			{
246cdf0e10cSrcweir 				pItem->QueryValue( aAny, mnMemberId );
247cdf0e10cSrcweir 				return aAny;
248cdf0e10cSrcweir 			}
249cdf0e10cSrcweir 		}
250cdf0e10cSrcweir 	}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	throw container::NoSuchElementException();
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
getElementNames()255cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL SvxUnoNameItemTable::getElementNames(  )
256cdf0e10cSrcweir 	throw( uno::RuntimeException )
257cdf0e10cSrcweir {
258cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	std::set< OUString, comphelper::UStringLess > aNameSet;
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	NameOrIndex *pItem;
263cdf0e10cSrcweir 	OUString aApiName;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
266cdf0e10cSrcweir 	sal_uInt32 nSurrogate;
267cdf0e10cSrcweir 	for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
268cdf0e10cSrcweir 	{
269cdf0e10cSrcweir 		pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 		if( !isValid( pItem ) )
272cdf0e10cSrcweir 			continue;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 		SvxUnogetApiNameForItem( mnWhich, pItem->GetName(), aApiName );
275cdf0e10cSrcweir 		aNameSet.insert( aApiName );
276cdf0e10cSrcweir 	}
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	uno::Sequence< OUString > aSeq( aNameSet.size() );
279cdf0e10cSrcweir 	OUString* pNames = aSeq.getArray();
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 	std::set< OUString, comphelper::UStringLess >::iterator aIter( aNameSet.begin() );
282cdf0e10cSrcweir 	const std::set< OUString, comphelper::UStringLess >::iterator aEnd( aNameSet.end() );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 	while( aIter != aEnd )
285cdf0e10cSrcweir 	{
286cdf0e10cSrcweir 		*pNames++ = *aIter++;
287cdf0e10cSrcweir 	}
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	return aSeq;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
hasByName(const OUString & aApiName)292cdf0e10cSrcweir sal_Bool SAL_CALL SvxUnoNameItemTable::hasByName( const OUString& aApiName )
293cdf0e10cSrcweir 	throw( uno::RuntimeException )
294cdf0e10cSrcweir {
295cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	String aName;
298cdf0e10cSrcweir 	SvxUnogetInternalNameForItem( mnWhich, aApiName, aName );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	if( aName.Len() == 0 )
301cdf0e10cSrcweir 		return sal_False;
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 	const String aSearchName( aName );
304cdf0e10cSrcweir 	sal_uInt32 nSurrogate;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	const NameOrIndex *pItem;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
309cdf0e10cSrcweir 	for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
310cdf0e10cSrcweir 	{
311cdf0e10cSrcweir 		pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
312cdf0e10cSrcweir 		if( isValid( pItem ) && (pItem->GetName() == aSearchName) )
313cdf0e10cSrcweir 			return sal_True;
314cdf0e10cSrcweir 	}
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 	return sal_False;
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
hasElements()319cdf0e10cSrcweir sal_Bool SAL_CALL SvxUnoNameItemTable::hasElements(  )
320cdf0e10cSrcweir 	throw( uno::RuntimeException )
321cdf0e10cSrcweir {
322cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 	const NameOrIndex *pItem;
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 	sal_uInt32 nSurrogate;
327cdf0e10cSrcweir 	const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
328cdf0e10cSrcweir 	for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
329cdf0e10cSrcweir 	{
330cdf0e10cSrcweir 		pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 		if( isValid( pItem ) )
333cdf0e10cSrcweir 			return sal_True;
334cdf0e10cSrcweir 	}
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 	return sal_False;
337cdf0e10cSrcweir }
338