LCOV - code coverage report
Current view: top level - framework/source/recording - dispatchrecorder.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 136 210 64.8 %
Date: 2014-04-11 Functions: 20 25 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             : #include <recording/dispatchrecorder.hxx>
      21             : #include <com/sun/star/frame/DispatchStatement.hpp>
      22             : #include <com/sun/star/script/Converter.hpp>
      23             : #include <services.h>
      24             : #include <vcl/svapp.hxx>
      25             : #include <comphelper/processfactory.hxx>
      26             : 
      27             : using namespace ::com::sun::star::uno;
      28             : 
      29             : namespace framework{
      30             : 
      31             : // used to mark a dispatch as comment (mostly it indicates an error) Changing of this wdefine will impact all using of such comments ...
      32             : #define REM_AS_COMMENT    "rem "
      33             : 
      34             : //  XInterface, XTypeProvider, XServiceInfo
      35             : 
      36          54 : DEFINE_XSERVICEINFO_MULTISERVICE_2(
      37             :     DispatchRecorder,
      38             :     ::cppu::OWeakObject,
      39             :     OUString("com.sun.star.frame.DispatchRecorder"),
      40             :     OUString("com.sun.star.comp.framework.DispatchRecorder"))
      41             : 
      42           3 : DEFINE_INIT_SERVICE(
      43             :     DispatchRecorder,
      44             :     {
      45             :     }
      46             : )
      47             : 
      48             : #include <typelib/typedescription.h>
      49             : 
      50           0 : void flatten_struct_members(
      51             :     ::std::vector< Any > * vec, void const * data,
      52             :     typelib_CompoundTypeDescription * pTD )
      53             :     SAL_THROW(())
      54             : {
      55           0 :     if (pTD->pBaseTypeDescription)
      56             :     {
      57           0 :         flatten_struct_members( vec, data, pTD->pBaseTypeDescription );
      58             :     }
      59           0 :     for ( sal_Int32 nPos = 0; nPos < pTD->nMembers; ++nPos )
      60             :     {
      61             :         vec->push_back(
      62           0 :             Any( (char const *)data + pTD->pMemberOffsets[ nPos ], pTD->ppTypeRefs[ nPos ] ) );
      63             :     }
      64           0 : }
      65             : 
      66           0 : Sequence< Any > make_seq_out_of_struct(
      67             :     Any const & val )
      68             :     SAL_THROW( (RuntimeException) )
      69             : {
      70           0 :     Type const & type = val.getValueType();
      71           0 :     TypeClass eTypeClass = type.getTypeClass();
      72           0 :     if (TypeClass_STRUCT != eTypeClass && TypeClass_EXCEPTION != eTypeClass)
      73             :     {
      74             :         throw RuntimeException(
      75           0 :             type.getTypeName() +
      76           0 :             OUString( "is no struct or exception!" ),
      77           0 :             Reference< XInterface >() );
      78             :     }
      79           0 :     typelib_TypeDescription * pTD = 0;
      80           0 :     TYPELIB_DANGER_GET( &pTD, type.getTypeLibType() );
      81             :     OSL_ASSERT( pTD );
      82           0 :     if (! pTD)
      83             :     {
      84             :         throw RuntimeException(
      85           0 :             OUString( "cannot get type descr of type " ) +
      86           0 :             type.getTypeName(),
      87           0 :             Reference< XInterface >() );
      88             :     }
      89             : 
      90           0 :     ::std::vector< Any > vec;
      91           0 :     vec.reserve( ((typelib_CompoundTypeDescription *)pTD)->nMembers ); // good guess
      92           0 :     flatten_struct_members( &vec, val.getValue(), (typelib_CompoundTypeDescription *)pTD );
      93           0 :     TYPELIB_DANGER_RELEASE( pTD );
      94           0 :     return Sequence< Any >( &vec[ 0 ], vec.size() );
      95             : }
      96             : 
      97           3 : DispatchRecorder::DispatchRecorder( const css::uno::Reference< css::uno::XComponentContext >& xContext )
      98             :     : m_nRecordingID(0)
      99           3 :     , m_xConverter(css::script::Converter::create(xContext))
     100             : {
     101           3 : }
     102             : 
     103           6 : DispatchRecorder::~DispatchRecorder()
     104             : {
     105           6 : }
     106             : 
     107             : // generate header
     108           8 : void SAL_CALL DispatchRecorder::startRecording( const css::uno::Reference< css::frame::XFrame >& ) throw( css::uno::RuntimeException, std::exception )
     109             : {
     110             :     /* SAFE{ */
     111             :     /* } */
     112           8 : }
     113             : 
     114           3 : void SAL_CALL DispatchRecorder::recordDispatch( const css::util::URL& aURL,
     115             :                                                 const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException, std::exception )
     116             : {
     117           3 :     OUString aTarget;
     118             : 
     119           6 :     com::sun::star::frame::DispatchStatement aStatement( aURL.Complete, aTarget, lArguments, 0, sal_False );
     120           6 :     m_aStatements.push_back( aStatement );
     121           3 : }
     122             : 
     123           2 : void SAL_CALL  DispatchRecorder::recordDispatchAsComment( const css::util::URL& aURL,
     124             :                                                           const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException, std::exception )
     125             : {
     126           2 :     OUString aTarget;
     127             : 
     128             :     // last parameter must be set to true -> it's a comment
     129           4 :         com::sun::star::frame::DispatchStatement aStatement( aURL.Complete, aTarget, lArguments, 0, sal_True );
     130           4 :     m_aStatements.push_back( aStatement );
     131           2 : }
     132             : 
     133           8 : void SAL_CALL DispatchRecorder::endRecording() throw( css::uno::RuntimeException, std::exception )
     134             : {
     135           8 :     SolarMutexGuard g;
     136           8 :     m_aStatements.clear();
     137           8 : }
     138             : 
     139           8 : OUString SAL_CALL DispatchRecorder::getRecordedMacro() throw( css::uno::RuntimeException, std::exception )
     140             : {
     141           8 :     SolarMutexGuard g;
     142             : 
     143           8 :     if ( m_aStatements.empty() )
     144           4 :         return OUString();
     145             : 
     146           8 :     OUStringBuffer aScriptBuffer;
     147           4 :     aScriptBuffer.ensureCapacity(10000);
     148           4 :     m_nRecordingID = 1;
     149             : 
     150           4 :     aScriptBuffer.appendAscii("rem ----------------------------------------------------------------------\n");
     151           4 :     aScriptBuffer.appendAscii("rem define variables\n");
     152           4 :     aScriptBuffer.appendAscii("dim document   as object\n");
     153           4 :     aScriptBuffer.appendAscii("dim dispatcher as object\n");
     154           4 :     aScriptBuffer.appendAscii("rem ----------------------------------------------------------------------\n");
     155           4 :     aScriptBuffer.appendAscii("rem get access to the document\n");
     156           4 :     aScriptBuffer.appendAscii("document   = ThisComponent.CurrentController.Frame\n");
     157           4 :     aScriptBuffer.appendAscii("dispatcher = createUnoService(\"com.sun.star.frame.DispatchHelper\")\n\n");
     158             : 
     159           4 :     std::vector< com::sun::star::frame::DispatchStatement>::iterator p;
     160           8 :     for ( p = m_aStatements.begin(); p != m_aStatements.end(); ++p )
     161           4 :         implts_recordMacro( p->aCommand, p->aArgs, p->bIsComment, aScriptBuffer );
     162           8 :     OUString sScript = aScriptBuffer.makeStringAndClear();
     163          12 :     return sScript;
     164             : }
     165             : 
     166           3 : void SAL_CALL DispatchRecorder::AppendToBuffer( css::uno::Any aValue, OUStringBuffer& aArgumentBuffer )
     167             : {
     168             :     // if value == bool
     169           3 :     if (aValue.getValueTypeClass() == css::uno::TypeClass_STRUCT )
     170             :     {
     171             :         // structs are recorded as arrays, convert to "Sequence of any"
     172           0 :         Sequence< Any > aSeq = make_seq_out_of_struct( aValue );
     173           0 :         aArgumentBuffer.appendAscii("Array(");
     174           0 :         for ( sal_Int32 nAny=0; nAny<aSeq.getLength(); nAny++ )
     175             :         {
     176           0 :             AppendToBuffer( aSeq[nAny], aArgumentBuffer );
     177           0 :             if ( nAny+1 < aSeq.getLength() )
     178             :                 // not last argument
     179           0 :                 aArgumentBuffer.appendAscii(",");
     180             :         }
     181             : 
     182           0 :         aArgumentBuffer.appendAscii(")");
     183             :     }
     184           3 :     else if (aValue.getValueTypeClass() == css::uno::TypeClass_SEQUENCE )
     185             :     {
     186             :         // convert to "Sequence of any"
     187           0 :         css::uno::Sequence < css::uno::Any > aSeq;
     188           0 :         css::uno::Any aNew;
     189           0 :         try { aNew = m_xConverter->convertTo( aValue, ::getCppuType((const css::uno::Sequence < css::uno::Any >*)0) ); }
     190           0 :         catch (const css::uno::Exception&) {}
     191             : 
     192           0 :         aNew >>= aSeq;
     193           0 :         aArgumentBuffer.appendAscii("Array(");
     194           0 :         for ( sal_Int32 nAny=0; nAny<aSeq.getLength(); nAny++ )
     195             :         {
     196           0 :             AppendToBuffer( aSeq[nAny], aArgumentBuffer );
     197           0 :             if ( nAny+1 < aSeq.getLength() )
     198             :                 // not last argument
     199           0 :                 aArgumentBuffer.appendAscii(",");
     200             :         }
     201             : 
     202           0 :         aArgumentBuffer.appendAscii(")");
     203             :     }
     204           3 :     else if (aValue.getValueTypeClass() == css::uno::TypeClass_STRING )
     205             :     {
     206             :         // strings need \"
     207           3 :         OUString sVal;
     208           3 :         aValue >>= sVal;
     209             : 
     210             :         // encode non printable characters or '"' by using the CHR$ function
     211           3 :         if ( !sVal.isEmpty() )
     212             :         {
     213           3 :             const sal_Unicode* pChars = sVal.getStr();
     214           3 :             bool bInString = false;
     215         105 :             for ( sal_Int32 nChar=0; nChar<sVal.getLength(); nChar ++ )
     216             :             {
     217         102 :                 if ( pChars[nChar] < 32 || pChars[nChar] == '"' )
     218             :                 {
     219             :                     // problematic character detected
     220           0 :                     if ( bInString )
     221             :                     {
     222             :                         // close current string
     223           0 :                         aArgumentBuffer.appendAscii("\"");
     224           0 :                         bInString = false;
     225             :                     }
     226             : 
     227           0 :                     if ( nChar>0 )
     228             :                         // if this is not the first character, parts of the string have already been added
     229           0 :                         aArgumentBuffer.appendAscii("+");
     230             : 
     231             :                     // add the character constant
     232           0 :                     aArgumentBuffer.appendAscii("CHR$(");
     233           0 :                     aArgumentBuffer.append( (sal_Int32) pChars[nChar] );
     234           0 :                     aArgumentBuffer.appendAscii(")");
     235             :                 }
     236             :                 else
     237             :                 {
     238         102 :                     if ( !bInString )
     239             :                     {
     240           3 :                         if ( nChar>0 )
     241             :                             // if this is not the first character, parts of the string have already been added
     242           0 :                             aArgumentBuffer.appendAscii("+");
     243             : 
     244             :                         // start a new string
     245           3 :                         aArgumentBuffer.appendAscii("\"");
     246           3 :                         bInString = true;
     247             :                     }
     248             : 
     249         102 :                     aArgumentBuffer.append( pChars[nChar] );
     250             :                 }
     251             :             }
     252             : 
     253             :             // close string
     254           3 :             if ( bInString )
     255           3 :                 aArgumentBuffer.appendAscii("\"");
     256             :         }
     257             :         else
     258           0 :             aArgumentBuffer.appendAscii("\"\"");
     259             :     }
     260           0 :     else if (aValue.getValueType() == getCppuCharType())
     261             :     {
     262             :         // character variables are recorded as strings, back conversion must be handled in client code
     263           0 :         sal_Unicode nVal = *((sal_Unicode*)aValue.getValue());
     264           0 :         aArgumentBuffer.appendAscii("\"");
     265           0 :         if ( (sal_Unicode(nVal) == '\"') )
     266             :             // encode \" to \"\"
     267           0 :             aArgumentBuffer.append((sal_Unicode)nVal);
     268           0 :         aArgumentBuffer.append((sal_Unicode)nVal);
     269           0 :         aArgumentBuffer.appendAscii("\"");
     270             :     }
     271             :     else
     272             :     {
     273           0 :         css::uno::Any aNew;
     274             :         try
     275             :         {
     276           0 :             aNew = m_xConverter->convertToSimpleType( aValue, css::uno::TypeClass_STRING );
     277             :         }
     278           0 :         catch (const css::script::CannotConvertException&) {}
     279           0 :         catch (const css::uno::Exception&) {}
     280           0 :         OUString sVal;
     281           0 :         aNew >>= sVal;
     282             : 
     283           0 :         if (aValue.getValueTypeClass() == css::uno::TypeClass_ENUM )
     284             :         {
     285           0 :             OUString aName = aValue.getValueType().getTypeName();
     286           0 :             aArgumentBuffer.append( aName );
     287           0 :             aArgumentBuffer.appendAscii(".");
     288             :         }
     289             : 
     290           0 :         aArgumentBuffer.append(sVal);
     291             :     }
     292           3 : }
     293             : 
     294           4 : void SAL_CALL DispatchRecorder::implts_recordMacro( const OUString& aURL,
     295             :                                                     const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
     296             :                                                           bool bAsComment, OUStringBuffer& aScriptBuffer )
     297             : {
     298           4 :     OUStringBuffer aArgumentBuffer(1000);
     299           8 :     OUString       sArrayName;
     300             :     // this value is used to name the arrays of aArgumentBuffer
     301           4 :     sArrayName = "args" + OUString::number(m_nRecordingID);
     302             : 
     303           4 :     aScriptBuffer.appendAscii("rem ----------------------------------------------------------------------\n");
     304             : 
     305           4 :     sal_Int32 nLength = lArguments.getLength();
     306           4 :     sal_Int32 nValidArgs = 0;
     307           7 :     for( sal_Int32 i=0; i<nLength; ++i )
     308             :     {
     309           3 :         if(!lArguments[i].Value.hasValue())
     310           0 :             continue;
     311             : 
     312           3 :         OUStringBuffer sValBuffer(100);
     313             :         try
     314             :         {
     315           3 :             AppendToBuffer(lArguments[i].Value, sValBuffer);
     316             :         }
     317           0 :         catch(const css::uno::Exception&)
     318             :         {
     319           0 :             sValBuffer.setLength(0);
     320             :         }
     321           3 :         if (sValBuffer.isEmpty())
     322           0 :             continue;
     323             : 
     324             :         {
     325             :             // add arg().Name
     326           3 :             if(bAsComment)
     327           1 :                 aArgumentBuffer.appendAscii(REM_AS_COMMENT);
     328           3 :             aArgumentBuffer.append     (sArrayName);
     329           3 :             aArgumentBuffer.appendAscii("(");
     330           3 :             aArgumentBuffer.append     (nValidArgs);
     331           3 :             aArgumentBuffer.appendAscii(").Name = \"");
     332           3 :             aArgumentBuffer.append     (lArguments[i].Name);
     333           3 :             aArgumentBuffer.appendAscii("\"\n");
     334             : 
     335             :             // add arg().Value
     336           3 :             if(bAsComment)
     337           1 :                 aArgumentBuffer.appendAscii(REM_AS_COMMENT);
     338           3 :             aArgumentBuffer.append     (sArrayName);
     339           3 :             aArgumentBuffer.appendAscii("(");
     340           3 :             aArgumentBuffer.append     (nValidArgs);
     341           3 :             aArgumentBuffer.appendAscii(").Value = ");
     342           3 :             aArgumentBuffer.append     (sValBuffer.makeStringAndClear());
     343           3 :             aArgumentBuffer.appendAscii("\n");
     344             : 
     345           3 :             ++nValidArgs;
     346             :         }
     347           3 :     }
     348             : 
     349             :     // if aArgumentBuffer exist - pack it into the aScriptBuffer
     350           4 :     if(nValidArgs>0)
     351             :     {
     352           3 :         if(bAsComment)
     353           1 :             aScriptBuffer.appendAscii(REM_AS_COMMENT);
     354           3 :         aScriptBuffer.appendAscii("dim ");
     355           3 :         aScriptBuffer.append     (sArrayName);
     356           3 :         aScriptBuffer.appendAscii("(");
     357           3 :         aScriptBuffer.append     ((sal_Int32)(nValidArgs-1)); // 0 based!
     358           3 :         aScriptBuffer.appendAscii(") as new com.sun.star.beans.PropertyValue\n");
     359           3 :         aScriptBuffer.append     (aArgumentBuffer.makeStringAndClear());
     360           3 :         aScriptBuffer.appendAscii("\n");
     361             :     }
     362             : 
     363             :     // add code for dispatches
     364           4 :     if(bAsComment)
     365           2 :         aScriptBuffer.appendAscii(REM_AS_COMMENT);
     366           4 :     aScriptBuffer.appendAscii("dispatcher.executeDispatch(document, \"");
     367           4 :     aScriptBuffer.append     (aURL);
     368           4 :     aScriptBuffer.appendAscii("\", \"\", 0, ");
     369           4 :     if(nValidArgs<1)
     370           1 :         aScriptBuffer.appendAscii("Array()");
     371             :     else
     372             :     {
     373           3 :         aScriptBuffer.append( sArrayName.getStr() );
     374           3 :         aScriptBuffer.appendAscii("()");
     375             :     }
     376           4 :     aScriptBuffer.appendAscii(")\n\n");
     377             : 
     378             :     /* SAFE { */
     379           8 :     m_nRecordingID++;
     380             :     /* } */
     381           4 : }
     382             : 
     383           1 : com::sun::star::uno::Type SAL_CALL DispatchRecorder::getElementType() throw (::com::sun::star::uno::RuntimeException, std::exception)
     384             : {
     385           1 :     return ::getCppuType((const com::sun::star::frame::DispatchStatement *)NULL);
     386             : }
     387             : 
     388           1 : sal_Bool SAL_CALL DispatchRecorder::hasElements()  throw (::com::sun::star::uno::RuntimeException, std::exception)
     389             : {
     390           1 :     return (! m_aStatements.empty());
     391             : }
     392             : 
     393           2 : sal_Int32 SAL_CALL DispatchRecorder::getCount() throw (::com::sun::star::uno::RuntimeException, std::exception)
     394             : {
     395           2 :     return m_aStatements.size();
     396             : }
     397             : 
     398           6 : com::sun::star::uno::Any SAL_CALL DispatchRecorder::getByIndex(sal_Int32 idx)  throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
     399             : {
     400           6 :     if (idx >= (sal_Int32)m_aStatements.size()) {
     401             :         throw com::sun::star::lang::IndexOutOfBoundsException(
     402             :             OUString( "Dispatch recorder out of bounds" ),
     403           1 :                     Reference< XInterface >() );
     404             : 
     405             :     }
     406             : 
     407           5 :     Any element(&m_aStatements[idx],
     408          10 :         ::getCppuType((const com::sun::star::frame::DispatchStatement *)NULL));
     409             : 
     410           5 :     return element;
     411             : }
     412             : 
     413           4 : void SAL_CALL DispatchRecorder::replaceByIndex(sal_Int32 idx, const com::sun::star::uno::Any& element) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
     414             : {
     415           4 :     if (element.getValueType() !=
     416           4 :         ::getCppuType((const com::sun::star::frame::DispatchStatement *)NULL)) {
     417             :                         throw com::sun::star::lang::IllegalArgumentException(
     418             :                         OUString( "Illegal argument in dispatch recorder" ),
     419           1 :                         Reference< XInterface >(), 2 );
     420             :     }
     421             : 
     422           3 :     if (idx >= (sal_Int32)m_aStatements.size()) {
     423             :                 throw com::sun::star::lang::IndexOutOfBoundsException(
     424             :                         OUString( "Dispatch recorder out of bounds" ),
     425           1 :                         Reference< XInterface >() );
     426             : 
     427             :         }
     428             : 
     429             :     com::sun::star::frame::DispatchStatement *pStatement;
     430             : 
     431           2 :     pStatement = (com::sun::star::frame::DispatchStatement *)element.getValue();
     432             : 
     433             :     com::sun::star::frame::DispatchStatement aStatement(
     434             :         pStatement->aCommand,
     435             :         pStatement->aTarget,
     436             :         pStatement->aArgs,
     437             :         pStatement->nFlags,
     438           2 :         pStatement->bIsComment);
     439             : 
     440           2 :     m_aStatements[idx] = aStatement;
     441           2 : }
     442             : 
     443             : } // namespace framework
     444             : 
     445             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10