LCOV - code coverage report
Current view: top level - svtools/source/uno - unoevent.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 91 232 39.2 %
Date: 2014-04-11 Functions: 20 34 58.8 %
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             : #include <com/sun/star/beans/PropertyValue.hpp>
      21             : #include <cppuhelper/supportsservice.hxx>
      22             : #include <rtl/ustrbuf.hxx>
      23             : #include <tools/rtti.hxx>
      24             : #include <svtools/unoevent.hxx>
      25             : #include <svl/macitem.hxx>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : using namespace ::com::sun::star::uno;
      29             : 
      30             : using ::com::sun::star::container::NoSuchElementException;
      31             : using ::com::sun::star::container::XNameReplace;
      32             : using ::com::sun::star::lang::IllegalArgumentException;
      33             : using ::com::sun::star::lang::WrappedTargetException;
      34             : using ::com::sun::star::lang::XServiceInfo;
      35             : using ::com::sun::star::beans::PropertyValue;
      36             : using ::cppu::WeakImplHelper2;
      37             : 
      38             : 
      39             : const sal_Char sAPI_ServiceName[] = "com.sun.star.container.XNameReplace";
      40             : const sal_Char sAPI_SvDetachedEventDescriptor[] = "SvDetachedEventDescriptor";
      41             : 
      42             : 
      43          35 : SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
      44             :         sEventType("EventType"),
      45             :         sMacroName("MacroName"),
      46             :         sLibrary("Library"),
      47             :         sStarBasic("StarBasic"),
      48             :         sJavaScript("JavaScript"),
      49             :         sScript("Script"),
      50             :         sNone("None"),
      51             :         sServiceName(sAPI_ServiceName),
      52             :         sEmpty(),
      53             :         mpSupportedMacroItems(pSupportedMacroItems),
      54          35 :         mnMacroItems(0)
      55             : {
      56             :     DBG_ASSERT(pSupportedMacroItems != NULL, "Need a list of supported events!");
      57             : 
      58          35 :     for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != 0; mnMacroItems++) ;
      59          35 : }
      60             : 
      61             : 
      62          35 : SvBaseEventDescriptor::~SvBaseEventDescriptor()
      63             : {
      64          35 : }
      65             : 
      66           0 : void SvBaseEventDescriptor::replaceByName(
      67             :     const OUString& rName,
      68             :     const Any& rElement )
      69             :     throw(
      70             :         IllegalArgumentException,
      71             :         NoSuchElementException,
      72             :         WrappedTargetException,
      73             :         RuntimeException, std::exception)
      74             : {
      75           0 :     sal_uInt16 nMacroID = getMacroID(rName);
      76             : 
      77             :     // error checking
      78           0 :     if (0 == nMacroID)
      79           0 :         throw NoSuchElementException();
      80           0 :     if (rElement.getValueType() != getElementType())
      81           0 :         throw IllegalArgumentException();
      82             : 
      83             :     // get sequence
      84           0 :     Sequence<PropertyValue> aSequence;
      85           0 :     rElement >>= aSequence;
      86             : 
      87             :     // perform replace (in subclass)
      88           0 :     SvxMacro aMacro(sEmpty,sEmpty);
      89           0 :     getMacroFromAny(aMacro, rElement);
      90           0 :     replaceByName(nMacroID, aMacro);
      91           0 : }
      92             : 
      93         216 : Any SvBaseEventDescriptor::getByName(
      94             :     const OUString& rName )
      95             :     throw(
      96             :         NoSuchElementException,
      97             :         WrappedTargetException,
      98             :         RuntimeException, std::exception)
      99             : {
     100         216 :     sal_uInt16 nMacroID = getMacroID(rName);
     101             : 
     102             :     // error checking
     103         216 :     if (0 == nMacroID)
     104           0 :         throw NoSuchElementException();
     105             : 
     106             :     // perform get (in subclass)
     107         216 :     Any aAny;
     108         432 :     SvxMacro aMacro( sEmpty, sEmpty );
     109         216 :     getByName(aMacro, nMacroID);
     110         216 :     getAnyFromMacro(aAny, aMacro);
     111         432 :     return aAny;
     112             : }
     113             : 
     114          27 : Sequence<OUString> SvBaseEventDescriptor::getElementNames()
     115             :     throw(RuntimeException, std::exception)
     116             : {
     117             :     // create and fill sequence
     118          27 :     Sequence<OUString> aSequence(mnMacroItems);
     119         243 :     for( sal_Int16 i = 0; i < mnMacroItems; i++)
     120             :     {
     121         216 :         aSequence[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
     122             :     }
     123             : 
     124          27 :     return aSequence;
     125             : }
     126             : 
     127           0 : sal_Bool SvBaseEventDescriptor::hasByName(
     128             :     const OUString& rName )
     129             :     throw(RuntimeException, std::exception)
     130             : {
     131           0 :     sal_uInt16 nMacroID = getMacroID(rName);
     132           0 :     return (nMacroID != 0);
     133             : }
     134             : 
     135           0 : Type SvBaseEventDescriptor::getElementType()
     136             :     throw(RuntimeException, std::exception)
     137             : {
     138           0 :     return ::getCppuType((Sequence<PropertyValue> *)0);
     139             : }
     140             : 
     141           0 : sal_Bool SvBaseEventDescriptor::hasElements()
     142             :     throw(RuntimeException, std::exception)
     143             : {
     144           0 :     return mnMacroItems != 0;
     145             : }
     146             : 
     147           0 : sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
     148             :     throw(RuntimeException, std::exception)
     149             : {
     150           0 :     return cppu::supportsService(this, rServiceName);
     151             : }
     152             : 
     153           0 : Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames(void)
     154             :     throw(RuntimeException, std::exception)
     155             : {
     156           0 :     Sequence<OUString> aSequence(1);
     157           0 :     aSequence[0] = sServiceName;
     158             : 
     159           0 :     return aSequence;
     160             : }
     161             : 
     162         216 : sal_uInt16 SvBaseEventDescriptor::mapNameToEventID(const OUString& rName) const
     163             : {
     164             :     // iterate over known event names
     165        1086 :     for(sal_Int16 i = 0; i < mnMacroItems; i++)
     166             :     {
     167        1086 :         if( rName.equalsAscii(mpSupportedMacroItems[i].mpEventName))
     168             :         {
     169         216 :             return mpSupportedMacroItems[i].mnEvent;
     170             :         }
     171             :     }
     172             : 
     173             :     // not found -> return zero
     174           0 :     return 0;
     175             : }
     176             : 
     177         216 : sal_uInt16 SvBaseEventDescriptor::getMacroID(const OUString& rName) const
     178             : {
     179         216 :     return mapNameToEventID(rName);
     180             : }
     181             : 
     182         216 : void SvBaseEventDescriptor::getAnyFromMacro(Any& rAny,
     183             :                                        const SvxMacro& rMacro)
     184             : {
     185         216 :     sal_Bool bRetValueOK = sal_False;   // do we have a ret value?
     186             : 
     187         216 :     if (rMacro.HasMacro())
     188             :     {
     189           0 :         switch (rMacro.GetScriptType())
     190             :         {
     191             :             case STARBASIC:
     192             :             {
     193             :                 // create sequence
     194           0 :                 Sequence<PropertyValue> aSequence(3);
     195           0 :                 Any aTmp;
     196             : 
     197             :                 // create type
     198           0 :                 PropertyValue aTypeValue;
     199           0 :                 aTypeValue.Name = sEventType;
     200           0 :                 aTmp <<= sStarBasic;
     201           0 :                 aTypeValue.Value = aTmp;
     202           0 :                 aSequence[0] = aTypeValue;
     203             : 
     204             :                 // macro name
     205           0 :                 PropertyValue aNameValue;
     206           0 :                 aNameValue.Name = sMacroName;
     207           0 :                 OUString sNameTmp(rMacro.GetMacName());
     208           0 :                 aTmp <<= sNameTmp;
     209           0 :                 aNameValue.Value = aTmp;
     210           0 :                 aSequence[1] = aNameValue;
     211             : 
     212             :                 // library name
     213           0 :                 PropertyValue aLibValue;
     214           0 :                 aLibValue.Name = sLibrary;
     215           0 :                 OUString sLibTmp(rMacro.GetLibName());
     216           0 :                 aTmp <<= sLibTmp;
     217           0 :                 aLibValue.Value = aTmp;
     218           0 :                 aSequence[2] = aLibValue;
     219             : 
     220           0 :                 rAny <<= aSequence;
     221           0 :                 bRetValueOK = sal_True;
     222           0 :                 break;
     223             :             }
     224             :             case EXTENDED_STYPE:
     225             :             {
     226             :                 // create sequence
     227           0 :                 Sequence<PropertyValue> aSequence(2);
     228           0 :                 Any aTmp;
     229             : 
     230             :                 // create type
     231           0 :                 PropertyValue aTypeValue;
     232           0 :                 aTypeValue.Name = sEventType;
     233           0 :                 aTmp <<= sScript;
     234           0 :                 aTypeValue.Value = aTmp;
     235           0 :                 aSequence[0] = aTypeValue;
     236             : 
     237             :                 // macro name
     238           0 :                 PropertyValue aNameValue;
     239           0 :                 aNameValue.Name = sScript;
     240           0 :                 OUString sNameTmp(rMacro.GetMacName());
     241           0 :                 aTmp <<= sNameTmp;
     242           0 :                 aNameValue.Value = aTmp;
     243           0 :                 aSequence[1] = aNameValue;
     244             : 
     245           0 :                 rAny <<= aSequence;
     246           0 :                 bRetValueOK = sal_True;
     247           0 :                 break;
     248             :                         }
     249             :             case JAVASCRIPT:
     250             :             default:
     251             :                 OSL_FAIL("not implemented");
     252             :         }
     253             :     }
     254             :     // else: bRetValueOK not set
     255             : 
     256             :     // if we don't have a return value, make an empty one
     257         216 :     if (! bRetValueOK)
     258             :     {
     259             :         // create "None" macro
     260         216 :         Sequence<PropertyValue> aSequence(1);
     261             : 
     262         432 :         PropertyValue aKindValue;
     263         216 :         aKindValue.Name = sEventType;
     264         432 :         Any aTmp;
     265         216 :         aTmp <<= sNone;
     266         216 :         aKindValue.Value = aTmp;
     267         216 :         aSequence[0] = aKindValue;
     268             : 
     269         216 :         rAny <<= aSequence;
     270         432 :         bRetValueOK = sal_True;
     271             :     }
     272         216 : }
     273             : 
     274             : 
     275           0 : void SvBaseEventDescriptor::getMacroFromAny(
     276             :     SvxMacro& rMacro,
     277             :     const Any& rAny)
     278             :         throw ( IllegalArgumentException )
     279             : {
     280             :     // get sequence
     281           0 :     Sequence<PropertyValue> aSequence;
     282           0 :     rAny >>= aSequence;
     283             : 
     284             :     // process ...
     285           0 :     sal_Bool bTypeOK = sal_False;
     286           0 :     sal_Bool bNone = sal_False;     // true if EventType=="None"
     287           0 :     enum ScriptType eType = EXTENDED_STYPE;
     288           0 :     OUString sScriptVal;
     289           0 :     OUString sMacroVal;
     290           0 :     OUString sLibVal;
     291           0 :     sal_Int32 nCount = aSequence.getLength();
     292           0 :     for (sal_Int32 i = 0; i < nCount; i++)
     293             :     {
     294           0 :         PropertyValue& aValue = aSequence[i];
     295           0 :         if (aValue.Name.equals(sEventType))
     296             :         {
     297           0 :             OUString sTmp;
     298           0 :             aValue.Value >>= sTmp;
     299           0 :             if (sTmp.equals(sStarBasic))
     300             :             {
     301           0 :                 eType = STARBASIC;
     302           0 :                 bTypeOK = sal_True;
     303             :             }
     304           0 :             else if (sTmp.equals(sJavaScript))
     305             :             {
     306           0 :                 eType = JAVASCRIPT;
     307           0 :                 bTypeOK = sal_True;
     308             :             }
     309           0 :             else if (sTmp.equals(sScript))
     310             :             {
     311           0 :                 eType = EXTENDED_STYPE;
     312           0 :                 bTypeOK = sal_True;
     313             :             }
     314           0 :             else if (sTmp.equals(sNone))
     315             :             {
     316           0 :                 bNone = sal_True;
     317           0 :                 bTypeOK = sal_True;
     318           0 :             }
     319             :             // else: unknown script type
     320             :         }
     321           0 :         else if (aValue.Name.equals(sMacroName))
     322             :         {
     323           0 :             aValue.Value >>= sMacroVal;
     324             :         }
     325           0 :         else if (aValue.Name.equals(sLibrary))
     326             :         {
     327           0 :             aValue.Value >>= sLibVal;
     328             :         }
     329           0 :         else if (aValue.Name.equals(sScript))
     330             :         {
     331           0 :             aValue.Value >>= sScriptVal;
     332             :         }
     333             :         // else: unknown PropertyValue -> ignore
     334             :     }
     335             : 
     336           0 :     if (bTypeOK)
     337             :     {
     338           0 :         if (bNone)
     339             :         {
     340             :             // return empty macro
     341           0 :             rMacro = SvxMacro( sEmpty, sEmpty );
     342             :         }
     343             :         else
     344             :         {
     345           0 :             if (eType == STARBASIC)
     346             :             {
     347             :                 // create macro and return
     348           0 :                 SvxMacro aMacro(sMacroVal, sLibVal, eType);
     349           0 :                 rMacro = aMacro;
     350             :             }
     351           0 :             else if (eType == EXTENDED_STYPE)
     352             :             {
     353           0 :                 SvxMacro aMacro(sScriptVal, sScript);
     354           0 :                 rMacro = aMacro;
     355             :             }
     356             :             else
     357             :             {
     358             :                 // we can't process type: abort
     359             :                 // TODO: JavaScript macros
     360           0 :                 throw IllegalArgumentException();
     361             :             }
     362             :         }
     363             :     }
     364             :     else
     365             :     {
     366             :         // no valid type: abort
     367           0 :         throw IllegalArgumentException();
     368           0 :     }
     369           0 : }
     370             : 
     371             : 
     372          27 : SvEventDescriptor::SvEventDescriptor(
     373             :     XInterface& rParent,
     374             :     const SvEventDescription* pSupportedMacroItems) :
     375             :         SvBaseEventDescriptor(pSupportedMacroItems),
     376          27 :         xParentRef(&rParent)
     377             : {
     378          27 : }
     379             : 
     380             : 
     381          27 : SvEventDescriptor::~SvEventDescriptor()
     382             : {
     383             :     // automatically release xParentRef !
     384          27 : }
     385             : 
     386           0 : void SvEventDescriptor::replaceByName(
     387             :     const sal_uInt16 nEvent,
     388             :     const SvxMacro& rMacro)
     389             :         throw(
     390             :             IllegalArgumentException,
     391             :             NoSuchElementException,
     392             :             WrappedTargetException,
     393             :             RuntimeException)
     394             : {
     395           0 :     SvxMacroItem aItem(getMacroItemWhich());
     396           0 :     aItem.SetMacroTable(getMacroItem().GetMacroTable());
     397           0 :     aItem.SetMacro(nEvent, rMacro);
     398           0 :     setMacroItem(aItem);
     399           0 : }
     400             : 
     401         216 : void SvEventDescriptor::getByName(
     402             :     SvxMacro& rMacro,
     403             :     const sal_uInt16 nEvent )
     404             :         throw(
     405             :             NoSuchElementException,
     406             :             WrappedTargetException,
     407             :             RuntimeException)
     408             : {
     409         216 :     const SvxMacroItem& rItem = getMacroItem();
     410         216 :     if( rItem.HasMacro( nEvent ) )
     411           0 :         rMacro = rItem.GetMacro(nEvent);
     412             :     else
     413             :     {
     414         216 :         SvxMacro aEmptyMacro(sEmpty, sEmpty);
     415         216 :         rMacro = aEmptyMacro;
     416             :     }
     417         216 : }
     418             : 
     419             : 
     420           8 : SvDetachedEventDescriptor::SvDetachedEventDescriptor(
     421             :     const SvEventDescription* pSupportedMacroItems) :
     422             :     SvBaseEventDescriptor(pSupportedMacroItems),
     423           8 :     sImplName(sAPI_SvDetachedEventDescriptor)
     424             : {
     425             :     // allocate aMacros
     426           8 :     aMacros = new SvxMacro*[mnMacroItems];
     427             : 
     428             :     // ... and initialize
     429          21 :     for(sal_Int16 i = 0; i < mnMacroItems; i++)
     430             :     {
     431          13 :         aMacros[i] = NULL;
     432             :     }
     433           8 : }
     434             : 
     435          16 : SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
     436             : {
     437             :     // delete contents of aMacros
     438          21 :     for(sal_Int16 i = 0; i < mnMacroItems; i++)
     439             :     {
     440          13 :         if (NULL != aMacros[i])
     441           0 :             delete aMacros[i];
     442             :     }
     443             : 
     444           8 :     delete [] aMacros;
     445           8 : }
     446             : 
     447           2 : sal_Int16 SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID) const
     448             : {
     449             :     // iterate over supported events
     450           2 :     sal_Int16 nIndex = 0;
     451           6 :     while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
     452           1 :             (mpSupportedMacroItems[nIndex].mnEvent != 0)      )
     453             :     {
     454           1 :         nIndex++;
     455             :     }
     456           2 :     return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
     457             : }
     458             : 
     459           0 : OUString SvDetachedEventDescriptor::getImplementationName()
     460             :     throw( ::com::sun::star::uno::RuntimeException, std::exception )
     461             : {
     462           0 :     return sImplName;
     463             : }
     464             : 
     465             : 
     466           0 : void SvDetachedEventDescriptor::replaceByName(
     467             :     const sal_uInt16 nEvent,
     468             :     const SvxMacro& rMacro)
     469             :     throw(
     470             :         IllegalArgumentException,
     471             :         NoSuchElementException,
     472             :         WrappedTargetException,
     473             :         RuntimeException)
     474             : {
     475           0 :     sal_Int16 nIndex = getIndex(nEvent);
     476           0 :     if (-1 == nIndex)
     477           0 :         throw IllegalArgumentException();
     478             : 
     479           0 :     aMacros[nIndex] = new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
     480           0 :                                    rMacro.GetScriptType() );
     481           0 : }
     482             : 
     483             : 
     484           0 : void SvDetachedEventDescriptor::getByName(
     485             :     SvxMacro& rMacro,
     486             :     const sal_uInt16 nEvent )
     487             :     throw(
     488             :         NoSuchElementException,
     489             :         WrappedTargetException,
     490             :         RuntimeException)
     491             : {
     492           0 :     sal_Int16 nIndex = getIndex(nEvent);
     493           0 :     if (-1 == nIndex )
     494           0 :         throw NoSuchElementException();
     495             : 
     496           0 :     if( aMacros[nIndex] )
     497           0 :         rMacro = (*aMacros[nIndex]);
     498           0 : }
     499             : 
     500           2 : sal_Bool SvDetachedEventDescriptor::hasByName(
     501             :     const sal_uInt16 nEvent ) const     /// item ID of event
     502             :         throw(IllegalArgumentException)
     503             : {
     504           2 :     sal_Int16 nIndex = getIndex(nEvent);
     505           2 :     if (-1 == nIndex)
     506           0 :         throw IllegalArgumentException();
     507             : 
     508           2 :     return (NULL == aMacros[nIndex]) ? sal_False : aMacros[nIndex]->HasMacro();
     509             : }
     510             : 
     511             : 
     512           4 : SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
     513           4 :     SvDetachedEventDescriptor(pSupportedMacroItems)
     514             : {
     515           4 : }
     516             : 
     517           1 : SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
     518             :     const SvxMacroTableDtor& rMacroTable,
     519             :     const SvEventDescription* pSupportedMacroItems) :
     520           1 :         SvDetachedEventDescriptor(pSupportedMacroItems)
     521             : {
     522           1 :     copyMacrosFromTable(rMacroTable);
     523           1 : }
     524             : 
     525          10 : SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
     526             : {
     527          10 : }
     528             : 
     529           1 : void SvMacroTableEventDescriptor::copyMacrosFromTable(
     530             :     const SvxMacroTableDtor& rMacroTable)
     531             : {
     532           3 :     for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
     533             :     {
     534           2 :         const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
     535           2 :         const SvxMacro* pMacro = rMacroTable.Get(nEvent);
     536           2 :         if (NULL != pMacro)
     537           0 :             replaceByName(nEvent, *pMacro);
     538             :     }
     539             : 
     540           1 : }
     541             : 
     542           1 : void SvMacroTableEventDescriptor::copyMacrosIntoTable(
     543             :     SvxMacroTableDtor& rMacroTable)
     544             : {
     545           3 :     for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
     546             :     {
     547           2 :         const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
     548           2 :         if (hasByName(nEvent))
     549             :         {
     550           0 :             SvxMacro& rMacro = rMacroTable.Insert(nEvent, SvxMacro(sEmpty, sEmpty));
     551           0 :             getByName(rMacro, nEvent);
     552             :         }
     553             :     }
     554           1 : }
     555             : 
     556             : 
     557             : 
     558             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10