xref: /trunk/main/svl/source/items/itemprop.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 
27 #include <svl/itemprop.hxx>
28 #include <svl/itempool.hxx>
29 #include <svl/itemset.hxx>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <hash_map>
32 /*************************************************************************
33     UNO III Implementation
34 *************************************************************************/
35 using namespace com::sun::star;
36 using namespace com::sun::star::beans;
37 using namespace com::sun::star::lang;
38 using namespace com::sun::star::uno;
39 
40 /*-- 16.02.2009 10:03:55---------------------------------------------------
41 
42   -----------------------------------------------------------------------*/
43 
44 struct equalOUString
45 {
operator ()equalOUString46   bool operator()(const ::rtl::OUString& r1, const ::rtl::OUString&  r2) const
47   {
48     return r1.equals( r2 );
49   }
50 };
51 
52 typedef ::std::hash_map< ::rtl::OUString,
53                                  SfxItemPropertySimpleEntry,
54                                  ::rtl::OUStringHash,
55                                  equalOUString > SfxItemPropertyHashMap_t;
56 
57 class SfxItemPropertyMap_Impl : public SfxItemPropertyHashMap_t
58 {
59 public:
60     mutable uno::Sequence< beans::Property > m_aPropSeq;
61 
SfxItemPropertyMap_Impl()62     SfxItemPropertyMap_Impl(){}
63     SfxItemPropertyMap_Impl( const SfxItemPropertyMap_Impl* pSource );
64 };
SfxItemPropertyMap_Impl(const SfxItemPropertyMap_Impl * pSource)65 SfxItemPropertyMap_Impl::SfxItemPropertyMap_Impl( const SfxItemPropertyMap_Impl* pSource )
66 {
67     this->SfxItemPropertyHashMap_t::operator=( *pSource );
68     m_aPropSeq = pSource->m_aPropSeq;
69 }
70 
71 /*-- 16.02.2009 10:03:51---------------------------------------------------
72 
73   -----------------------------------------------------------------------*/
SfxItemPropertyMap(const SfxItemPropertyMapEntry * pEntries)74 SfxItemPropertyMap::SfxItemPropertyMap( const SfxItemPropertyMapEntry* pEntries ) :
75     m_pImpl( new SfxItemPropertyMap_Impl )
76 {
77     while( pEntries->pName )
78     {
79         ::rtl::OUString sEntry(pEntries->pName, pEntries->nNameLen, RTL_TEXTENCODING_ASCII_US );
80         (*m_pImpl) [ sEntry ] = pEntries;
81         ++pEntries;
82     }
83 }
84 /*-- 16.02.2009 12:46:41---------------------------------------------------
85 
86   -----------------------------------------------------------------------*/
SfxItemPropertyMap(const SfxItemPropertyMap * pSource)87 SfxItemPropertyMap::SfxItemPropertyMap( const SfxItemPropertyMap* pSource ) :
88     m_pImpl( new SfxItemPropertyMap_Impl( pSource->m_pImpl ) )
89 {
90 }
91 /*-- 16.02.2009 10:03:51---------------------------------------------------
92 
93   -----------------------------------------------------------------------*/
~SfxItemPropertyMap()94 SfxItemPropertyMap::~SfxItemPropertyMap()
95 {
96     delete m_pImpl;
97 }
98 /*-- 16.02.2009 10:03:51---------------------------------------------------
99 
100   -----------------------------------------------------------------------*/
getByName(const::rtl::OUString & rName) const101 const SfxItemPropertySimpleEntry* SfxItemPropertyMap::getByName( const ::rtl::OUString &rName ) const
102 {
103     SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
104     if( aIter == m_pImpl->end() )
105         return 0;
106     return &aIter->second;
107 }
108 
109 /*-- 16.02.2009 10:44:24---------------------------------------------------
110 
111   -----------------------------------------------------------------------*/
getProperties() const112 uno::Sequence<beans::Property> SfxItemPropertyMap::getProperties() const
113 {
114     if( !m_pImpl->m_aPropSeq.getLength() )
115     {
116         m_pImpl->m_aPropSeq.realloc( m_pImpl->size() );
117         beans::Property* pPropArray = m_pImpl->m_aPropSeq.getArray();
118         sal_uInt32 n = 0;
119         SfxItemPropertyHashMap_t::const_iterator aIt = m_pImpl->begin();
120         while( aIt != m_pImpl->end() )
121         //for ( const SfxItemPropertyMap *pMap = _pMap; pMap->pName; ++pMap )
122         {
123             const SfxItemPropertySimpleEntry* pEntry = &(*aIt).second;
124             pPropArray[n].Name = (*aIt).first;
125             pPropArray[n].Handle = pEntry->nWID;
126             if(pEntry->pType)
127                 pPropArray[n].Type = *pEntry->pType;
128             pPropArray[n].Attributes =
129                 sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
130             n++;
131             ++aIt;
132         }
133     }
134 
135     return m_pImpl->m_aPropSeq;
136 }
137 /*-- 16.02.2009 11:04:31---------------------------------------------------
138 
139   -----------------------------------------------------------------------*/
getPropertyByName(const::rtl::OUString rName) const140 beans::Property SfxItemPropertyMap::getPropertyByName( const ::rtl::OUString rName ) const
141 {
142     SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
143     if( aIter == m_pImpl->end() )
144         throw UnknownPropertyException();
145     const SfxItemPropertySimpleEntry* pEntry = &aIter->second;
146     beans::Property aProp;
147     aProp.Name = rName;
148     aProp.Handle = pEntry->nWID;
149     if(pEntry->pType)
150         aProp.Type = *pEntry->pType;
151     aProp.Attributes = sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
152     return aProp;
153 }
154 /*-- 16.02.2009 11:09:16---------------------------------------------------
155 
156   -----------------------------------------------------------------------*/
hasPropertyByName(const::rtl::OUString & rName) const157 sal_Bool SfxItemPropertyMap::hasPropertyByName( const ::rtl::OUString& rName ) const
158 {
159     SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
160     return aIter != m_pImpl->end();
161 }
162 /*-- 16.02.2009 11:25:14---------------------------------------------------
163 
164   -----------------------------------------------------------------------*/
mergeProperties(const uno::Sequence<beans::Property> & rPropSeq)165 void SfxItemPropertyMap::mergeProperties( const uno::Sequence< beans::Property >& rPropSeq )
166 {
167     const beans::Property* pPropArray = rPropSeq.getConstArray();
168     sal_uInt32 nElements = rPropSeq.getLength();
169     for( sal_uInt32 nElement = 0; nElement < nElements; ++nElement )
170     {
171         SfxItemPropertySimpleEntry aTemp(
172             sal::static_int_cast< sal_Int16 >( pPropArray[nElement].Handle ), //nWID
173             &pPropArray[nElement].Type, //pType
174             pPropArray[nElement].Attributes, //nFlags
175             0 ); //nMemberId
176         (*m_pImpl)[pPropArray[nElement].Name] = aTemp;
177     }
178 }
179 /*-- 18.02.2009 12:04:42---------------------------------------------------
180 
181   -----------------------------------------------------------------------*/
getPropertyEntries() const182 PropertyEntryVector_t SfxItemPropertyMap::getPropertyEntries() const
183 {
184     PropertyEntryVector_t aRet;
185     aRet.reserve(m_pImpl->size());
186 
187     SfxItemPropertyHashMap_t::const_iterator aIt = m_pImpl->begin();
188     while( aIt != m_pImpl->end() )
189     {
190         const SfxItemPropertySimpleEntry* pEntry = &(*aIt).second;
191         aRet.push_back( SfxItemPropertyNamedEntry( (*aIt).first, * pEntry ) );
192         ++aIt;
193     }
194     return aRet;
195 }
196 /*-- 18.02.2009 15:11:06---------------------------------------------------
197 
198   -----------------------------------------------------------------------*/
getSize() const199 sal_uInt32 SfxItemPropertyMap::getSize() const
200 {
201     return m_pImpl->size();
202 }
203 /*-- 16.02.2009 13:44:54---------------------------------------------------
204 
205   -----------------------------------------------------------------------*/
~SfxItemPropertySet()206 SfxItemPropertySet::~SfxItemPropertySet()
207 {
208 }
209 /* -----------------------------21.02.00 11:26--------------------------------
210 
211  ---------------------------------------------------------------------------*/
FillItem(SfxItemSet &,sal_uInt16,sal_Bool) const212 sal_Bool SfxItemPropertySet::FillItem(SfxItemSet&, sal_uInt16, sal_Bool) const
213 {
214     return sal_False;
215 }
216 /* -----------------------------06.06.01 12:32--------------------------------
217 
218  ---------------------------------------------------------------------------*/
getPropertyValue(const SfxItemPropertySimpleEntry & rEntry,const SfxItemSet & rSet,Any & rAny) const219 void SfxItemPropertySet::getPropertyValue( const SfxItemPropertySimpleEntry& rEntry,
220             const SfxItemSet& rSet, Any& rAny ) const
221 {
222     // get the SfxPoolItem
223     const SfxPoolItem* pItem = 0;
224     SfxItemState eState = rSet.GetItemState( rEntry.nWID, sal_True, &pItem );
225     if(SFX_ITEM_SET != eState && SFX_WHICH_MAX > rEntry.nWID )
226         pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
227     // return item values as uno::Any
228     if(eState >= SFX_ITEM_DEFAULT && pItem)
229     {
230         pItem->QueryValue( rAny, rEntry.nMemberId );
231     }
232     else
233     {
234         SfxItemSet aSet(*rSet.GetPool(), rEntry.nWID, rEntry.nWID);
235         if(FillItem(aSet, rEntry.nWID, sal_True))
236         {
237             const SfxPoolItem& rItem = aSet.Get(rEntry.nWID);
238             rItem.QueryValue( rAny, rEntry.nMemberId );
239         }
240         else if(0 == (rEntry.nFlags & PropertyAttribute::MAYBEVOID))
241             throw RuntimeException();
242     }
243 
244 
245     // convert general SfxEnumItem values to specific values
246     if( rEntry.pType && TypeClass_ENUM == rEntry.pType->getTypeClass() &&
247          rAny.getValueTypeClass() == TypeClass_LONG )
248     {
249         sal_Int32 nTmp = *(sal_Int32*)rAny.getValue();
250         rAny.setValue( &nTmp, *rEntry.pType );
251     }
252 }
253 /* -----------------------------06.06.01 12:32--------------------------------
254 
255  ---------------------------------------------------------------------------*/
getPropertyValue(const rtl::OUString & rName,const SfxItemSet & rSet,Any & rAny) const256 void SfxItemPropertySet::getPropertyValue( const rtl::OUString &rName,
257             const SfxItemSet& rSet, Any& rAny ) const
258 {
259     // detect which-id
260     const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
261     if ( !pEntry )
262         throw UnknownPropertyException();
263     getPropertyValue( *pEntry,rSet, rAny );
264 }
265 /* -----------------------------21.02.00 11:26--------------------------------
266 
267  ---------------------------------------------------------------------------*/
getPropertyValue(const rtl::OUString & rName,const SfxItemSet & rSet) const268 Any SfxItemPropertySet::getPropertyValue( const rtl::OUString &rName,
269             const SfxItemSet& rSet ) const
270 {
271     Any aVal;
272     getPropertyValue( rName,rSet, aVal );
273     return aVal;
274 }
275 /* -----------------------------15.11.00 14:46--------------------------------
276 
277  ---------------------------------------------------------------------------*/
setPropertyValue(const SfxItemPropertySimpleEntry & rEntry,const Any & aVal,SfxItemSet & rSet) const278 void SfxItemPropertySet::setPropertyValue( const SfxItemPropertySimpleEntry& rEntry,
279                                             const Any& aVal,
280                                             SfxItemSet& rSet ) const
281 {
282     // get the SfxPoolItem
283     const SfxPoolItem* pItem = 0;
284     SfxPoolItem *pNewItem = 0;
285     SfxItemState eState = rSet.GetItemState( rEntry.nWID, sal_True, &pItem );
286     if(SFX_ITEM_SET != eState && SFX_WHICH_MAX > rEntry.nWID )
287         pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
288     //maybe there's another way to find an Item
289     if(eState < SFX_ITEM_DEFAULT)
290     {
291         SfxItemSet aSet(*rSet.GetPool(), rEntry.nWID, rEntry.nWID);
292         if(FillItem(aSet, rEntry.nWID, sal_False))
293         {
294             const SfxPoolItem &rItem = aSet.Get(rEntry.nWID);
295             pNewItem = rItem.Clone();
296         }
297     }
298     if(!pNewItem && pItem)
299     {
300         pNewItem = pItem->Clone();
301     }
302     if(pNewItem)
303     {
304         if( !pNewItem->PutValue( aVal, rEntry.nMemberId ) )
305         {
306             DELETEZ(pNewItem);
307             throw IllegalArgumentException();
308         }
309         // apply new item
310         rSet.Put( *pNewItem, rEntry.nWID );
311         delete pNewItem;
312     }
313 }
314 /* -----------------------------21.02.00 11:26--------------------------------
315 
316  ---------------------------------------------------------------------------*/
setPropertyValue(const rtl::OUString & rName,const Any & aVal,SfxItemSet & rSet) const317 void SfxItemPropertySet::setPropertyValue( const rtl::OUString &rName,
318                                             const Any& aVal,
319                                             SfxItemSet& rSet ) const
320 {
321     const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
322     if ( !pEntry )
323     {
324         throw UnknownPropertyException();
325     }
326     setPropertyValue(*pEntry, aVal, rSet);
327 }
328 /* -----------------------------21.02.00 11:26--------------------------------
329 
330  ---------------------------------------------------------------------------*/
getPropertyState(const SfxItemPropertySimpleEntry & rEntry,const SfxItemSet & rSet) const331 PropertyState SfxItemPropertySet::getPropertyState(const SfxItemPropertySimpleEntry& rEntry, const SfxItemSet& rSet) const
332                                     throw()
333 {
334     PropertyState eRet = PropertyState_DIRECT_VALUE;
335     sal_uInt16 nWhich = rEntry.nWID;
336 
337     // item state holen
338     SfxItemState eState = rSet.GetItemState( nWhich, sal_False );
339     // item-Wert als UnoAny zurueckgeben
340     if(eState == SFX_ITEM_DEFAULT)
341         eRet = PropertyState_DEFAULT_VALUE;
342     else if(eState < SFX_ITEM_DEFAULT)
343         eRet = PropertyState_AMBIGUOUS_VALUE;
344     return eRet;
345 }
getPropertyState(const rtl::OUString & rName,const SfxItemSet & rSet) const346 PropertyState   SfxItemPropertySet::getPropertyState(
347     const rtl::OUString& rName, const SfxItemSet& rSet) const
348 {
349     PropertyState eRet = PropertyState_DIRECT_VALUE;
350 
351     // which-id ermitteln
352     const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
353     if( !pEntry || !pEntry->nWID )
354     {
355         throw UnknownPropertyException();
356     }
357     sal_uInt16 nWhich = pEntry->nWID;
358 
359     // item holen
360     const SfxPoolItem* pItem = 0;
361     SfxItemState eState = rSet.GetItemState( nWhich, sal_False, &pItem );
362     if(!pItem && nWhich != rSet.GetPool()->GetSlotId(nWhich))
363         pItem = &rSet.GetPool()->GetDefaultItem(nWhich);
364     // item-Wert als UnoAny zurueckgeben
365     if(eState == SFX_ITEM_DEFAULT)
366         eRet = PropertyState_DEFAULT_VALUE;
367     else if(eState < SFX_ITEM_DEFAULT)
368         eRet = PropertyState_AMBIGUOUS_VALUE;
369     return eRet;
370 }
371 /* -----------------------------21.02.00 11:26--------------------------------
372 
373  ---------------------------------------------------------------------------*/
374 Reference<XPropertySetInfo>
getPropertySetInfo() const375     SfxItemPropertySet::getPropertySetInfo() const
376 {
377     if( !m_xInfo.is() )
378         m_xInfo = new SfxItemPropertySetInfo( &m_aMap );
379     return m_xInfo;
380 }
381 /*-- 16.02.2009 13:49:25---------------------------------------------------
382 
383   -----------------------------------------------------------------------*/
384 struct SfxItemPropertySetInfo_Impl
385 {
386     SfxItemPropertyMap*         m_pOwnMap;
387 };
388 /*-- 16.02.2009 13:49:24---------------------------------------------------
389 
390   -----------------------------------------------------------------------*/
SfxItemPropertySetInfo(const SfxItemPropertyMap * pMap)391 SfxItemPropertySetInfo::SfxItemPropertySetInfo(const SfxItemPropertyMap *pMap ) :
392     m_pImpl( new SfxItemPropertySetInfo_Impl )
393 {
394     m_pImpl->m_pOwnMap = new SfxItemPropertyMap( pMap );
395 }
396 /*-- 16.02.2009 13:49:25---------------------------------------------------
397 
398   -----------------------------------------------------------------------*/
SfxItemPropertySetInfo(const SfxItemPropertyMapEntry * pEntries)399 SfxItemPropertySetInfo::SfxItemPropertySetInfo(const SfxItemPropertyMapEntry *pEntries ) :
400     m_pImpl( new SfxItemPropertySetInfo_Impl )
401 {
402     m_pImpl->m_pOwnMap = new SfxItemPropertyMap( pEntries );
403 }
404 /* -----------------------------21.02.00 11:09--------------------------------
405 
406  ---------------------------------------------------------------------------*/
407 Sequence< Property > SAL_CALL
getProperties()408         SfxItemPropertySetInfo::getProperties(  )
409 {
410     return m_pImpl->m_pOwnMap->getProperties();
411 }
412 /*-- 16.02.2009 13:49:27---------------------------------------------------
413 
414   -----------------------------------------------------------------------*/
getMap() const415 const SfxItemPropertyMap* SfxItemPropertySetInfo::getMap() const
416 {
417     return m_pImpl->m_pOwnMap;
418 }
419 
420 /*-- 16.02.2009 12:43:36---------------------------------------------------
421 
422   -----------------------------------------------------------------------*/
~SfxItemPropertySetInfo()423 SfxItemPropertySetInfo::~SfxItemPropertySetInfo()
424 {
425     delete m_pImpl->m_pOwnMap;
426     delete m_pImpl;
427 }
428 /* -----------------------------21.02.00 11:27--------------------------------
429 
430  ---------------------------------------------------------------------------*/
431 Property SAL_CALL
getPropertyByName(const::rtl::OUString & rName)432         SfxItemPropertySetInfo::getPropertyByName( const ::rtl::OUString& rName )
433 {
434     return m_pImpl->m_pOwnMap->getPropertyByName( rName );
435 }
436 /* -----------------------------21.02.00 11:28--------------------------------
437 
438  ---------------------------------------------------------------------------*/
439 sal_Bool SAL_CALL
hasPropertyByName(const::rtl::OUString & rName)440         SfxItemPropertySetInfo::hasPropertyByName( const ::rtl::OUString& rName )
441 {
442     return m_pImpl->m_pOwnMap->hasPropertyByName( rName );
443 }
444 /* -----------------------------21.02.00 12:03--------------------------------
445 
446  ---------------------------------------------------------------------------*/
SfxExtItemPropertySetInfo(const SfxItemPropertyMapEntry * pMap,const Sequence<Property> & rPropSeq)447 SfxExtItemPropertySetInfo::SfxExtItemPropertySetInfo(
448                                 const SfxItemPropertyMapEntry *pMap,
449                                 const Sequence<Property>& rPropSeq ) :
450                 aExtMap( pMap )
451 {
452     aExtMap.mergeProperties( rPropSeq );
453 }
454 /*-- 16.02.2009 12:06:49---------------------------------------------------
455 
456   -----------------------------------------------------------------------*/
~SfxExtItemPropertySetInfo()457 SfxExtItemPropertySetInfo::~SfxExtItemPropertySetInfo()
458 {
459 }
460 /* -----------------------------21.02.00 12:03--------------------------------
461 
462  ---------------------------------------------------------------------------*/
463 Sequence< Property > SAL_CALL
getProperties()464         SfxExtItemPropertySetInfo::getProperties(  )
465 {
466     return aExtMap.getProperties();
467 }
468 /* -----------------------------21.02.00 12:03--------------------------------
469 
470  ---------------------------------------------------------------------------*/
471 Property SAL_CALL
getPropertyByName(const rtl::OUString & rPropertyName)472 SfxExtItemPropertySetInfo::getPropertyByName( const rtl::OUString& rPropertyName )
473 {
474     return aExtMap.getPropertyByName( rPropertyName );
475 }
476 /* -----------------------------21.02.00 12:03--------------------------------
477 
478  ---------------------------------------------------------------------------*/
479 sal_Bool SAL_CALL
hasPropertyByName(const rtl::OUString & rPropertyName)480 SfxExtItemPropertySetInfo::hasPropertyByName( const rtl::OUString& rPropertyName )
481 {
482     return aExtMap.hasPropertyByName( rPropertyName );
483 }
484