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

Generated by: LCOV version 1.11