1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: UnoNameItemTable.cxx,v $
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"
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>
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()
54 mpModelPool( pModel
? &pModel
->GetItemPool() : NULL
),
55 mnWhich( nWhich
), mnMemberId( nMemberId
)
58 StartListening( *pModel
);
61 SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
64 EndListening( *mpModel
);
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
)
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() )
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
)
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
);
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();
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~") ) )
149 SvxUnogetInternalNameForItem( mnWhich
, aApiName
, Name
);
151 ItemPoolVector::iterator aIter
= maItemSetVector
.begin();
152 const ItemPoolVector::iterator aEnd
= maItemSetVector
.end();
155 const String
aSearchName( Name
);
157 while( aIter
!= aEnd
)
159 pItem
= (NameOrIndex
*)&((*aIter
)->Get( mnWhich
) );
160 if( pItem
->GetName() == aSearchName
)
163 maItemSetVector
.erase( aIter
);
169 if( !hasByName( Name
) )
170 throw container::NoSuchElementException();
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() );
180 SvxUnogetInternalNameForItem( mnWhich
, aApiName
, aName
);
182 ItemPoolVector::iterator aIter
= maItemSetVector
.begin();
183 const ItemPoolVector::iterator aEnd
= maItemSetVector
.end();
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
);
204 // if it is not in our own sets, modify the pool!
205 sal_Bool bFound
= sal_False
;
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
);
221 ImplInsertByName( aName
, aElement
);
223 throw container::NoSuchElementException();
225 if( !hasByName( aName
) )
226 throw container::NoSuchElementException();
230 uno::Any SAL_CALL
SvxUnoNameItemTable::getByName( const OUString
& aApiName
)
231 throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
233 OGuard
aGuard( Application::GetSolarMutex() );
236 SvxUnogetInternalNameForItem( mnWhich
, aApiName
, aName
);
240 if( mpModelPool
&& aName
.Len() != 0 )
242 const String
aSearchName( aName
);
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
);
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
;
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
) )
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
++;
299 sal_Bool SAL_CALL
SvxUnoNameItemTable::hasByName( const OUString
& aApiName
)
300 throw( uno::RuntimeException
)
302 OGuard
aGuard( Application::GetSolarMutex() );
305 SvxUnogetInternalNameForItem( mnWhich
, aApiName
, aName
);
307 if( aName
.Len() == 0 )
310 const String
aSearchName( aName
);
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
) )
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
) )