1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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()
40 mpModelPool( pModel
? &pModel
->GetItemPool() : NULL
),
41 mnWhich( nWhich
), mnMemberId( nMemberId
)
44 StartListening( *pModel
);
47 SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
50 EndListening( *mpModel
);
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
)
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() )
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
)
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
);
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~" )
133 OUString sName
= SvxUnogetInternalNameForItem(mnWhich
, aApiName
);
135 ItemPoolVector::iterator aIter
= maItemSetVector
.begin();
136 const ItemPoolVector::iterator aEnd
= maItemSetVector
.end();
140 while( aIter
!= aEnd
)
142 pItem
= (NameOrIndex
*)&((*aIter
)->Get( mnWhich
) );
143 if (sName
.equals(pItem
->GetName()))
146 maItemSetVector
.erase( aIter
);
152 if (!hasByName(sName
))
153 throw container::NoSuchElementException();
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();
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
);
185 // if it is not in our own sets, modify the pool!
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
);
202 ImplInsertByName( aName
, aElement
);
204 throw container::NoSuchElementException();
206 if( !hasByName( aName
) )
207 throw container::NoSuchElementException();
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
);
220 if (mpModelPool
&& !aName
.isEmpty())
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
);
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
;
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
) )
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
++;
277 sal_Bool SAL_CALL
SvxUnoNameItemTable::hasByName( const OUString
& aApiName
)
278 throw( uno::RuntimeException
)
280 SolarMutexGuard aGuard
;
282 OUString aName
= SvxUnogetInternalNameForItem(mnWhich
, aApiName
);
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()))
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
) )
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */