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