LCOV - code coverage report
Current view: top level - svx/source/unodraw - UnoNameItemTable.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 92 136 67.6 %
Date: 2014-04-11 Functions: 12 15 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include <set>
      22             : #include <svl/itempool.hxx>
      23             : #include <svl/itemset.hxx>
      24             : #include <svl/style.hxx>
      25             : #include <cppuhelper/supportsservice.hxx>
      26             : 
      27             : #include <svx/svdmodel.hxx>
      28             : #include "UnoNameItemTable.hxx"
      29             : #include <osl/mutex.hxx>
      30             : #include <vcl/svapp.hxx>
      31             : 
      32             : #include "svx/unoapi.hxx"
      33             : 
      34             : using namespace ::com::sun::star;
      35             : using namespace ::rtl;
      36             : using namespace ::cppu;
      37             : 
      38        1479 : SvxUnoNameItemTable::SvxUnoNameItemTable( SdrModel* pModel, sal_uInt16 nWhich, sal_uInt8 nMemberId ) throw()
      39             : : mpModel( pModel ),
      40             :   mpModelPool( pModel ? &pModel->GetItemPool() : NULL ),
      41        1479 :   mnWhich( nWhich ), mnMemberId( nMemberId )
      42             : {
      43        1479 :     if( pModel )
      44        1479 :         StartListening( *pModel );
      45        1479 : }
      46             : 
      47        1708 : SvxUnoNameItemTable::~SvxUnoNameItemTable() throw()
      48             : {
      49         854 :     if( mpModel )
      50         854 :         EndListening( *mpModel );
      51         854 :     dispose();
      52         854 : }
      53             : 
      54         542 : bool SvxUnoNameItemTable::isValid( const NameOrIndex* pItem ) const
      55             : {
      56         542 :     return pItem && !pItem->GetName().isEmpty();
      57             : }
      58             : 
      59        1864 : void SvxUnoNameItemTable::dispose()
      60             : {
      61        1864 :     ItemPoolVector::iterator aIter = maItemSetVector.begin();
      62        1864 :     const ItemPoolVector::iterator aEnd = maItemSetVector.end();
      63             : 
      64        3815 :     while( aIter != aEnd )
      65             :     {
      66          87 :         delete (*aIter++);
      67             :     }
      68             : 
      69        1864 :     maItemSetVector.clear();
      70        1864 : }
      71             : 
      72      296371 : void SvxUnoNameItemTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
      73             : {
      74      296371 :     const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
      75             : 
      76      296371 :     if( pSdrHint && HINT_MODELCLEARED == pSdrHint->GetKind() )
      77         940 :         dispose();
      78      296371 : }
      79             : 
      80           0 : sal_Bool SAL_CALL SvxUnoNameItemTable::supportsService( const  OUString& ServiceName ) throw(uno::RuntimeException, std::exception)
      81             : {
      82           0 :     return cppu::supportsService(this, ServiceName);
      83             : }
      84             : 
      85         131 : void SAL_CALL SvxUnoNameItemTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
      86             : {
      87         131 :     SfxItemSet* mpInSet = new SfxItemSet( *mpModelPool, mnWhich, mnWhich );
      88         131 :     maItemSetVector.push_back( mpInSet );
      89             : 
      90         131 :     NameOrIndex* pNewItem = createItem();
      91         131 :     pNewItem->SetName( aName );
      92         131 :     pNewItem->PutValue( aElement, mnMemberId );
      93         131 :     mpInSet->Put( *pNewItem, mnWhich );
      94         131 :     delete pNewItem;
      95         131 : }
      96             : 
      97             : // XNameContainer
      98         131 : void SAL_CALL SvxUnoNameItemTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
      99             :     throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
     100             : {
     101         131 :     SolarMutexGuard aGuard;
     102             : 
     103         131 :     if( hasByName( aApiName ) )
     104           0 :         throw container::ElementExistException();
     105             : 
     106         262 :     OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
     107             : 
     108         262 :     ImplInsertByName( aName, aElement );
     109         131 : }
     110             : 
     111             : 
     112             : 
     113          70 : void SAL_CALL SvxUnoNameItemTable::removeByName( const OUString& aApiName )
     114             :     throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
     115             : {
     116          70 :     SolarMutexGuard aGuard;
     117             : 
     118             :     // a little quickfix for 2.0 to let applications clear api
     119             :     // created items that are not used
     120          70 :     if ( aApiName == "~clear~" )
     121             :     {
     122          70 :         dispose();
     123          70 :         return;
     124             :     }
     125             : 
     126           0 :     OUString sName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
     127             : 
     128           0 :     ItemPoolVector::iterator aIter = maItemSetVector.begin();
     129           0 :     const ItemPoolVector::iterator aEnd = maItemSetVector.end();
     130             : 
     131             :     NameOrIndex *pItem;
     132             : 
     133           0 :     while( aIter != aEnd )
     134             :     {
     135           0 :         pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
     136           0 :         if (sName.equals(pItem->GetName()))
     137             :         {
     138           0 :             delete (*aIter);
     139           0 :             maItemSetVector.erase( aIter );
     140           0 :             return;
     141             :         }
     142           0 :         ++aIter;
     143             :     }
     144             : 
     145           0 :     if (!hasByName(sName))
     146           0 :         throw container::NoSuchElementException();
     147             : }
     148             : 
     149             : // XNameReplace
     150           0 : void SAL_CALL SvxUnoNameItemTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
     151             :     throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
     152             : {
     153           0 :     SolarMutexGuard aGuard;
     154             : 
     155           0 :     OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
     156             : 
     157           0 :     ItemPoolVector::iterator aIter = maItemSetVector.begin();
     158           0 :     const ItemPoolVector::iterator aEnd = maItemSetVector.end();
     159             : 
     160             :     NameOrIndex *pItem;
     161             : 
     162           0 :     while( aIter != aEnd )
     163             :     {
     164           0 :         pItem = (NameOrIndex *)&((*aIter)->Get( mnWhich ) );
     165           0 :         if (aName.equals(pItem->GetName()))
     166             :         {
     167           0 :             NameOrIndex* pNewItem = createItem();
     168           0 :             pNewItem->SetName(aName);
     169           0 :             if( !pNewItem->PutValue( aElement, mnMemberId ) || !isValid( pNewItem ) )
     170           0 :                 throw lang::IllegalArgumentException();
     171             : 
     172           0 :             (*aIter)->Put( *pNewItem );
     173           0 :             return;
     174             :         }
     175           0 :         ++aIter;
     176             :     }
     177             : 
     178             :     // if it is not in our own sets, modify the pool!
     179           0 :     bool bFound = false;
     180             : 
     181             :     sal_uInt32 nSurrogate;
     182           0 :     sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
     183           0 :     for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
     184             :     {
     185           0 :         pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate);
     186           0 :         if (pItem && aName.equals(pItem->GetName()))
     187             :         {
     188           0 :             pItem->PutValue( aElement, mnMemberId );
     189           0 :             bFound = true;
     190           0 :             break;
     191             :         }
     192             :     }
     193             : 
     194           0 :     if( bFound )
     195           0 :         ImplInsertByName( aName, aElement );
     196             :     else
     197           0 :         throw container::NoSuchElementException();
     198             : 
     199           0 :     if( !hasByName( aName ) )
     200           0 :         throw container::NoSuchElementException();
     201             : }
     202             : 
     203             : // XNameAccess
     204          17 : uno::Any SAL_CALL SvxUnoNameItemTable::getByName( const OUString& aApiName )
     205             :     throw( container::NoSuchElementException,  lang::WrappedTargetException, uno::RuntimeException, std::exception)
     206             : {
     207          17 :     SolarMutexGuard aGuard;
     208             : 
     209          34 :     OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
     210             : 
     211          17 :     uno::Any aAny;
     212             : 
     213          17 :     if (mpModelPool && !aName.isEmpty())
     214             :     {
     215             :         NameOrIndex *pItem;
     216             :         sal_uInt32 nSurrogate;
     217             : 
     218          17 :         sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
     219          20 :         for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
     220             :         {
     221          20 :             pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
     222             : 
     223          20 :             if (isValid(pItem) && aName.equals(pItem->GetName()))
     224             :             {
     225          17 :                 pItem->QueryValue( aAny, mnMemberId );
     226          34 :                 return aAny;
     227             :             }
     228             :         }
     229             :     }
     230             : 
     231          17 :     throw container::NoSuchElementException();
     232             : }
     233             : 
     234          11 : uno::Sequence< OUString > SAL_CALL SvxUnoNameItemTable::getElementNames(  )
     235             :     throw( uno::RuntimeException, std::exception )
     236             : {
     237          11 :     SolarMutexGuard aGuard;
     238             : 
     239          22 :     std::set< OUString > aNameSet;
     240             : 
     241             :     NameOrIndex *pItem;
     242             : 
     243          11 :     const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
     244             :     sal_uInt32 nSurrogate;
     245          26 :     for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
     246             :     {
     247          15 :         pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
     248             : 
     249          15 :         if( !isValid( pItem ) )
     250           2 :             continue;
     251             : 
     252          13 :         OUString aApiName = SvxUnogetApiNameForItem(mnWhich, pItem->GetName());
     253          13 :         aNameSet.insert(aApiName);
     254          13 :     }
     255             : 
     256          11 :     uno::Sequence< OUString > aSeq( aNameSet.size() );
     257          11 :     OUString* pNames = aSeq.getArray();
     258             : 
     259          11 :     std::set< OUString >::iterator aIter( aNameSet.begin() );
     260          11 :     const std::set< OUString >::iterator aEnd( aNameSet.end() );
     261             : 
     262          35 :     while( aIter != aEnd )
     263             :     {
     264          13 :         *pNames++ = *aIter++;
     265             :     }
     266             : 
     267          22 :     return aSeq;
     268             : }
     269             : 
     270         311 : sal_Bool SAL_CALL SvxUnoNameItemTable::hasByName( const OUString& aApiName )
     271             :     throw( uno::RuntimeException, std::exception )
     272             : {
     273         311 :     SolarMutexGuard aGuard;
     274             : 
     275         622 :     OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
     276             : 
     277         311 :     if (aName.isEmpty())
     278           0 :         return sal_False;
     279             : 
     280             :     sal_uInt32 nSurrogate;
     281             : 
     282             :     const NameOrIndex *pItem;
     283             : 
     284         311 :     sal_uInt32 nCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
     285         716 :     for( nSurrogate = 0; nSurrogate < nCount; nSurrogate++ )
     286             :     {
     287         408 :         pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
     288         408 :         if (isValid(pItem) && aName.equals(pItem->GetName()))
     289           3 :             return sal_True;
     290             :     }
     291             : 
     292         619 :     return sal_False;
     293             : }
     294             : 
     295        1160 : sal_Bool SAL_CALL SvxUnoNameItemTable::hasElements(  )
     296             :     throw( uno::RuntimeException, std::exception )
     297             : {
     298        1160 :     SolarMutexGuard aGuard;
     299             : 
     300             :     const NameOrIndex *pItem;
     301             : 
     302             :     sal_uInt32 nSurrogate;
     303        1160 :     const sal_uInt32 nSurrogateCount = mpModelPool ? mpModelPool->GetItemCount2( mnWhich ) : 0;
     304        1248 :     for( nSurrogate = 0; nSurrogate < nSurrogateCount; nSurrogate++ )
     305             :     {
     306          99 :         pItem = (NameOrIndex*)mpModelPool->GetItem2( mnWhich, nSurrogate );
     307             : 
     308          99 :         if( isValid( pItem ) )
     309          11 :             return sal_True;
     310             :     }
     311             : 
     312        1149 :     return sal_False;
     313             : }
     314             : 
     315             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10