xref: /AOO41X/main/framework/source/recording/dispatchrecorder.cxx (revision 6d739b60ff8f4ed2134ae1442e284f9da90334b4)
1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_framework.hxx"
27cdf0e10cSrcweir #include <recording/dispatchrecorder.hxx>
28cdf0e10cSrcweir #include <com/sun/star/frame/DispatchStatement.hpp>
29cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
30cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
31cdf0e10cSrcweir #include <services.h>
32cdf0e10cSrcweir #include <vcl/svapp.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::com::sun::star::uno;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace framework{
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // used to mark a dispatch as comment (mostly it indicates an error) Changing of this wdefine will impact all using of such comments ...
39cdf0e10cSrcweir #define REM_AS_COMMENT    "rem "
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //*****************************************************************************************************************
42cdf0e10cSrcweir //  XInterface, XTypeProvider, XServiceInfo
43cdf0e10cSrcweir //*****************************************************************************************************************
DEFINE_XINTERFACE_6(DispatchRecorder,OWeakObject,DIRECT_INTERFACE (css::lang::XTypeProvider),DIRECT_INTERFACE (css::lang::XServiceInfo),DIRECT_INTERFACE (css::frame::XDispatchRecorder),DIRECT_INTERFACE (css::container::XIndexReplace),DIRECT_INTERFACE (css::container::XIndexAccess),DIRECT_INTERFACE (css::container::XElementAccess))44cdf0e10cSrcweir DEFINE_XINTERFACE_6(
45cdf0e10cSrcweir     DispatchRecorder,
46cdf0e10cSrcweir     OWeakObject,
47cdf0e10cSrcweir     DIRECT_INTERFACE(css::lang::XTypeProvider),
48cdf0e10cSrcweir     DIRECT_INTERFACE(css::lang::XServiceInfo),
49cdf0e10cSrcweir     DIRECT_INTERFACE(css::frame::XDispatchRecorder),
50cdf0e10cSrcweir     DIRECT_INTERFACE(css::container::XIndexReplace),
51cdf0e10cSrcweir     DIRECT_INTERFACE(css::container::XIndexAccess),
52cdf0e10cSrcweir     DIRECT_INTERFACE(css::container::XElementAccess))
53cdf0e10cSrcweir 
54cdf0e10cSrcweir DEFINE_XTYPEPROVIDER_6(
55cdf0e10cSrcweir     DispatchRecorder,
56cdf0e10cSrcweir     css::lang::XTypeProvider,
57cdf0e10cSrcweir     css::lang::XServiceInfo,
58cdf0e10cSrcweir     css::frame::XDispatchRecorder,
59cdf0e10cSrcweir     css::container::XIndexReplace,
60cdf0e10cSrcweir     css::container::XIndexAccess,
61cdf0e10cSrcweir     css::container::XElementAccess)
62cdf0e10cSrcweir 
63cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE(
64cdf0e10cSrcweir     DispatchRecorder,
65cdf0e10cSrcweir     ::cppu::OWeakObject,
66cdf0e10cSrcweir     SERVICENAME_DISPATCHRECORDER,
67cdf0e10cSrcweir     IMPLEMENTATIONNAME_DISPATCHRECORDER)
68cdf0e10cSrcweir 
69cdf0e10cSrcweir DEFINE_INIT_SERVICE(
70cdf0e10cSrcweir     DispatchRecorder,
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir )
74cdf0e10cSrcweir 
75cdf0e10cSrcweir #include <typelib/typedescription.h>
76cdf0e10cSrcweir 
77cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
78cdf0e10cSrcweir void flatten_struct_members(
79cdf0e10cSrcweir     ::std::vector< Any > * vec, void const * data,
80cdf0e10cSrcweir     typelib_CompoundTypeDescription * pTD )
81cdf0e10cSrcweir     SAL_THROW( () )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     if (pTD->pBaseTypeDescription)
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         flatten_struct_members( vec, data, pTD->pBaseTypeDescription );
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir     for ( sal_Int32 nPos = 0; nPos < pTD->nMembers; ++nPos )
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         vec->push_back(
90cdf0e10cSrcweir             Any( (char const *)data + pTD->pMemberOffsets[ nPos ], pTD->ppTypeRefs[ nPos ] ) );
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93cdf0e10cSrcweir //==================================================================================================
make_seq_out_of_struct(Any const & val)94cdf0e10cSrcweir Sequence< Any > make_seq_out_of_struct(
95cdf0e10cSrcweir     Any const & val )
96cdf0e10cSrcweir     SAL_THROW( (RuntimeException) )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     Type const & type = val.getValueType();
99cdf0e10cSrcweir     TypeClass eTypeClass = type.getTypeClass();
100cdf0e10cSrcweir     if (TypeClass_STRUCT != eTypeClass && TypeClass_EXCEPTION != eTypeClass)
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         throw RuntimeException(
103cdf0e10cSrcweir             type.getTypeName() +
104cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("is no struct or exception!") ),
105cdf0e10cSrcweir             Reference< XInterface >() );
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     typelib_TypeDescription * pTD = 0;
108cdf0e10cSrcweir     TYPELIB_DANGER_GET( &pTD, type.getTypeLibType() );
109cdf0e10cSrcweir     OSL_ASSERT( pTD );
110cdf0e10cSrcweir     if (! pTD)
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         throw RuntimeException(
113cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get type descr of type ") ) +
114cdf0e10cSrcweir             type.getTypeName(),
115cdf0e10cSrcweir             Reference< XInterface >() );
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     ::std::vector< Any > vec;
119cdf0e10cSrcweir     vec.reserve( ((typelib_CompoundTypeDescription *)pTD)->nMembers ); // good guess
120cdf0e10cSrcweir     flatten_struct_members( &vec, val.getValue(), (typelib_CompoundTypeDescription *)pTD );
121cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE( pTD );
122cdf0e10cSrcweir     return Sequence< Any >( &vec[ 0 ], vec.size() );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir //***********************************************************************
DispatchRecorder(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR)126cdf0e10cSrcweir DispatchRecorder::DispatchRecorder( const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR )
127cdf0e10cSrcweir         : ThreadHelpBase     ( &Application::GetSolarMutex() )
128cdf0e10cSrcweir         , ::cppu::OWeakObject(                               )
129cdf0e10cSrcweir         , m_xSMGR            ( xSMGR                         )
130cdf0e10cSrcweir         , m_xConverter( m_xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.script.Converter")), css::uno::UNO_QUERY )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //************************************************************************
~DispatchRecorder()135cdf0e10cSrcweir DispatchRecorder::~DispatchRecorder()
136cdf0e10cSrcweir {
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir //*************************************************************************
140cdf0e10cSrcweir // generate header
startRecording(const css::uno::Reference<css::frame::XFrame> &)141cdf0e10cSrcweir void SAL_CALL DispatchRecorder::startRecording( const css::uno::Reference< css::frame::XFrame >& ) throw( css::uno::RuntimeException )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     /* SAFE{ */
144cdf0e10cSrcweir     /* } */
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir //*************************************************************************
recordDispatch(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments)148cdf0e10cSrcweir void SAL_CALL DispatchRecorder::recordDispatch( const css::util::URL& aURL,
149cdf0e10cSrcweir                                                 const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir 	::rtl::OUString aTarget;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	com::sun::star::frame::DispatchStatement aStatement( aURL.Complete, aTarget, lArguments, 0, sal_False );
154cdf0e10cSrcweir 	m_aStatements.push_back( aStatement );
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir //*************************************************************************
recordDispatchAsComment(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments)158cdf0e10cSrcweir void SAL_CALL  DispatchRecorder::recordDispatchAsComment( const css::util::URL& aURL,
159cdf0e10cSrcweir                                                           const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException )
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	::rtl::OUString aTarget;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     // last parameter must be set to true -> it's a comment
164cdf0e10cSrcweir         com::sun::star::frame::DispatchStatement aStatement( aURL.Complete, aTarget, lArguments, 0, sal_True );
165cdf0e10cSrcweir 	m_aStatements.push_back( aStatement );
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir //*************************************************************************
endRecording()169cdf0e10cSrcweir void SAL_CALL DispatchRecorder::endRecording() throw( css::uno::RuntimeException )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     /* SAFE{ */
172cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
173cdf0e10cSrcweir 	m_aStatements.clear();
174cdf0e10cSrcweir     /* } */
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir //*************************************************************************
getRecordedMacro()178cdf0e10cSrcweir ::rtl::OUString SAL_CALL DispatchRecorder::getRecordedMacro() throw( css::uno::RuntimeException )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     /* SAFE{ */
181cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     if ( m_aStatements.empty() )
184cdf0e10cSrcweir         return ::rtl::OUString();
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     ::rtl::OUStringBuffer aScriptBuffer;
187cdf0e10cSrcweir     aScriptBuffer.ensureCapacity(10000);
188cdf0e10cSrcweir     m_nRecordingID = 1;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     aScriptBuffer.appendAscii("rem ----------------------------------------------------------------------\n");
191cdf0e10cSrcweir     aScriptBuffer.appendAscii("rem define variables\n");
192cdf0e10cSrcweir     aScriptBuffer.appendAscii("dim document   as object\n");
193cdf0e10cSrcweir     aScriptBuffer.appendAscii("dim dispatcher as object\n");
194cdf0e10cSrcweir     aScriptBuffer.appendAscii("rem ----------------------------------------------------------------------\n");
195cdf0e10cSrcweir     aScriptBuffer.appendAscii("rem get access to the document\n");
196cdf0e10cSrcweir     aScriptBuffer.appendAscii("document   = ThisComponent.CurrentController.Frame\n");
197cdf0e10cSrcweir     aScriptBuffer.appendAscii("dispatcher = createUnoService(\"com.sun.star.frame.DispatchHelper\")\n\n");
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     std::vector< com::sun::star::frame::DispatchStatement>::iterator p;
200cdf0e10cSrcweir 	for ( p = m_aStatements.begin(); p != m_aStatements.end(); p++ )
201cdf0e10cSrcweir         implts_recordMacro( p->aCommand, p->aArgs, p->bIsComment, aScriptBuffer );
202cdf0e10cSrcweir     ::rtl::OUString sScript = aScriptBuffer.makeStringAndClear();
203cdf0e10cSrcweir     return sScript;
204cdf0e10cSrcweir     /* } */
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir //*************************************************************************
AppendToBuffer(css::uno::Any aValue,::rtl::OUStringBuffer & aArgumentBuffer)208cdf0e10cSrcweir void SAL_CALL DispatchRecorder::AppendToBuffer( css::uno::Any aValue, ::rtl::OUStringBuffer& aArgumentBuffer )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     // if value == bool
211cdf0e10cSrcweir     if (aValue.getValueTypeClass() == css::uno::TypeClass_STRUCT )
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir 		// structs are recorded as arrays, convert to "Sequence of any"
214cdf0e10cSrcweir         Sequence< Any > aSeq = make_seq_out_of_struct( aValue );
215cdf0e10cSrcweir         aArgumentBuffer.appendAscii("Array(");
216cdf0e10cSrcweir         for ( sal_Int32 nAny=0; nAny<aSeq.getLength(); nAny++ )
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             AppendToBuffer( aSeq[nAny], aArgumentBuffer );
219cdf0e10cSrcweir             if ( nAny+1 < aSeq.getLength() )
220cdf0e10cSrcweir                 // not last argument
221cdf0e10cSrcweir                 aArgumentBuffer.appendAscii(",");
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir         aArgumentBuffer.appendAscii(")");
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir     else if (aValue.getValueTypeClass() == css::uno::TypeClass_SEQUENCE )
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir 		// convert to "Sequence of any"
229cdf0e10cSrcweir         css::uno::Sequence < css::uno::Any > aSeq;
230cdf0e10cSrcweir         css::uno::Any aNew;
231cdf0e10cSrcweir         try { aNew = m_xConverter->convertTo( aValue, ::getCppuType((const css::uno::Sequence < css::uno::Any >*)0) ); }
232cdf0e10cSrcweir         catch (css::uno::Exception&) {}
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         aNew >>= aSeq;
235cdf0e10cSrcweir         aArgumentBuffer.appendAscii("Array(");
236cdf0e10cSrcweir         for ( sal_Int32 nAny=0; nAny<aSeq.getLength(); nAny++ )
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             AppendToBuffer( aSeq[nAny], aArgumentBuffer );
239cdf0e10cSrcweir             if ( nAny+1 < aSeq.getLength() )
240cdf0e10cSrcweir                 // not last argument
241cdf0e10cSrcweir                 aArgumentBuffer.appendAscii(",");
242cdf0e10cSrcweir         }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir         aArgumentBuffer.appendAscii(")");
245cdf0e10cSrcweir     }
246cdf0e10cSrcweir     else if (aValue.getValueTypeClass() == css::uno::TypeClass_STRING )
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir 		// strings need \"
249cdf0e10cSrcweir         ::rtl::OUString sVal;
250cdf0e10cSrcweir         aValue >>= sVal;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         // encode non printable characters or '"' by using the CHR$ function
253cdf0e10cSrcweir         if ( sVal.getLength() )
254cdf0e10cSrcweir         {
255cdf0e10cSrcweir             const sal_Unicode* pChars = sVal.getStr();
256cdf0e10cSrcweir             sal_Bool bInString = sal_False;
257cdf0e10cSrcweir             for ( sal_Int32 nChar=0; nChar<sVal.getLength(); nChar ++ )
258cdf0e10cSrcweir             {
259cdf0e10cSrcweir                 if ( pChars[nChar] < 32 || pChars[nChar] == '"' )
260cdf0e10cSrcweir                 {
261cdf0e10cSrcweir                     // problematic character detected
262cdf0e10cSrcweir                     if ( bInString )
263cdf0e10cSrcweir                     {
264cdf0e10cSrcweir                         // close current string
265cdf0e10cSrcweir                         aArgumentBuffer.appendAscii("\"");
266cdf0e10cSrcweir                         bInString = sal_False;
267cdf0e10cSrcweir                     }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir                     if ( nChar>0 )
270cdf0e10cSrcweir                         // if this is not the first character, parts of the string have already been added
271cdf0e10cSrcweir                         aArgumentBuffer.appendAscii("+");
272cdf0e10cSrcweir 
273cdf0e10cSrcweir                     // add the character constant
274cdf0e10cSrcweir                     aArgumentBuffer.appendAscii("CHR$(");
275cdf0e10cSrcweir                     aArgumentBuffer.append( (sal_Int32) pChars[nChar] );
276cdf0e10cSrcweir                     aArgumentBuffer.appendAscii(")");
277cdf0e10cSrcweir                 }
278cdf0e10cSrcweir                 else
279cdf0e10cSrcweir                 {
280cdf0e10cSrcweir                     if ( !bInString )
281cdf0e10cSrcweir                     {
282cdf0e10cSrcweir                         if ( nChar>0 )
283cdf0e10cSrcweir                             // if this is not the first character, parts of the string have already been added
284cdf0e10cSrcweir                             aArgumentBuffer.appendAscii("+");
285cdf0e10cSrcweir 
286cdf0e10cSrcweir                         // start a new string
287cdf0e10cSrcweir                         aArgumentBuffer.appendAscii("\"");
288cdf0e10cSrcweir                         bInString = sal_True;
289cdf0e10cSrcweir                     }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir                     aArgumentBuffer.append( pChars[nChar] );
292cdf0e10cSrcweir                 }
293cdf0e10cSrcweir             }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir             // close string
296cdf0e10cSrcweir             if ( bInString )
297cdf0e10cSrcweir                 aArgumentBuffer.appendAscii("\"");
298cdf0e10cSrcweir         }
299cdf0e10cSrcweir         else
300cdf0e10cSrcweir             aArgumentBuffer.appendAscii("\"\"");
301cdf0e10cSrcweir 	}
302cdf0e10cSrcweir     else if (aValue.getValueType() == getCppuCharType())
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir 		// character variables are recorded as strings, back conversion must be handled in client code
305cdf0e10cSrcweir         sal_Unicode nVal = *((sal_Unicode*)aValue.getValue());
306cdf0e10cSrcweir         aArgumentBuffer.appendAscii("\"");
307cdf0e10cSrcweir         if ( (sal_Unicode(nVal) == '\"') )
308cdf0e10cSrcweir             // encode \" to \"\"
309cdf0e10cSrcweir             aArgumentBuffer.append((sal_Unicode)nVal);
310cdf0e10cSrcweir         aArgumentBuffer.append((sal_Unicode)nVal);
311cdf0e10cSrcweir         aArgumentBuffer.appendAscii("\"");
312cdf0e10cSrcweir     }
313cdf0e10cSrcweir 	else
314cdf0e10cSrcweir 	{
315cdf0e10cSrcweir         css::uno::Any aNew;
316cdf0e10cSrcweir         try
317cdf0e10cSrcweir 		{
318cdf0e10cSrcweir 			aNew = m_xConverter->convertToSimpleType( aValue, css::uno::TypeClass_STRING );
319cdf0e10cSrcweir 		}
320cdf0e10cSrcweir         catch (css::script::CannotConvertException&) {}
321cdf0e10cSrcweir         catch (css::uno::Exception&) {}
322cdf0e10cSrcweir         ::rtl::OUString sVal;
323cdf0e10cSrcweir         aNew >>= sVal;
324cdf0e10cSrcweir 
325cdf0e10cSrcweir         if (aValue.getValueTypeClass() == css::uno::TypeClass_ENUM )
326cdf0e10cSrcweir         {
327cdf0e10cSrcweir             ::rtl::OUString aName = aValue.getValueType().getTypeName();
328cdf0e10cSrcweir             aArgumentBuffer.append( aName );
329cdf0e10cSrcweir             aArgumentBuffer.appendAscii(".");
330cdf0e10cSrcweir         }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         aArgumentBuffer.append(sVal);
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
implts_recordMacro(const::rtl::OUString & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments,sal_Bool bAsComment,::rtl::OUStringBuffer & aScriptBuffer)336cdf0e10cSrcweir void SAL_CALL DispatchRecorder::implts_recordMacro( const ::rtl::OUString& aURL,
337cdf0e10cSrcweir                                                     const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
338cdf0e10cSrcweir                                                           sal_Bool bAsComment, ::rtl::OUStringBuffer& aScriptBuffer )
339cdf0e10cSrcweir {
340cdf0e10cSrcweir     ::rtl::OUStringBuffer aArgumentBuffer(1000);
341cdf0e10cSrcweir     ::rtl::OUString       sArrayName;
342cdf0e10cSrcweir     // this value is used to name the arrays of aArgumentBuffer
343cdf0e10cSrcweir     sArrayName = ::rtl::OUString::createFromAscii("args");
344cdf0e10cSrcweir     sArrayName += ::rtl::OUString::valueOf((sal_Int32)m_nRecordingID);
345cdf0e10cSrcweir 
346cdf0e10cSrcweir     aScriptBuffer.appendAscii("rem ----------------------------------------------------------------------\n");
347cdf0e10cSrcweir 
348cdf0e10cSrcweir     sal_Int32 nLength = lArguments.getLength();
349cdf0e10cSrcweir     sal_Int32 nValidArgs = 0;
350cdf0e10cSrcweir     for( sal_Int32 i=0; i<nLength; ++i )
351cdf0e10cSrcweir     {
352cdf0e10cSrcweir         if(!lArguments[i].Value.hasValue())
353cdf0e10cSrcweir             continue;
354cdf0e10cSrcweir 
355cdf0e10cSrcweir         ::rtl::OUStringBuffer sValBuffer(100);
356cdf0e10cSrcweir         try
357cdf0e10cSrcweir         {
358cdf0e10cSrcweir             AppendToBuffer(lArguments[i].Value, sValBuffer);
359cdf0e10cSrcweir         }
360cdf0e10cSrcweir         catch(const css::uno::Exception&)
361cdf0e10cSrcweir         {
362cdf0e10cSrcweir             sValBuffer.setLength(0);
363cdf0e10cSrcweir         }
364cdf0e10cSrcweir         if (!sValBuffer.getLength())
365cdf0e10cSrcweir             continue;
366cdf0e10cSrcweir 
367cdf0e10cSrcweir         {
368cdf0e10cSrcweir             // add arg().Name
369cdf0e10cSrcweir             if(bAsComment)
370cdf0e10cSrcweir                 aArgumentBuffer.appendAscii(REM_AS_COMMENT);
371cdf0e10cSrcweir             aArgumentBuffer.append     (sArrayName);
372cdf0e10cSrcweir             aArgumentBuffer.appendAscii("(");
373cdf0e10cSrcweir             aArgumentBuffer.append     (nValidArgs);
374cdf0e10cSrcweir             aArgumentBuffer.appendAscii(").Name = \"");
375cdf0e10cSrcweir             aArgumentBuffer.append     (lArguments[i].Name);
376cdf0e10cSrcweir             aArgumentBuffer.appendAscii("\"\n");
377cdf0e10cSrcweir 
378cdf0e10cSrcweir             // add arg().Value
379cdf0e10cSrcweir             if(bAsComment)
380cdf0e10cSrcweir                 aArgumentBuffer.appendAscii(REM_AS_COMMENT);
381cdf0e10cSrcweir             aArgumentBuffer.append     (sArrayName);
382cdf0e10cSrcweir             aArgumentBuffer.appendAscii("(");
383cdf0e10cSrcweir             aArgumentBuffer.append     (nValidArgs);
384cdf0e10cSrcweir             aArgumentBuffer.appendAscii(").Value = ");
385cdf0e10cSrcweir             aArgumentBuffer.append     (sValBuffer.makeStringAndClear());
386cdf0e10cSrcweir             aArgumentBuffer.appendAscii("\n");
387cdf0e10cSrcweir 
388cdf0e10cSrcweir             ++nValidArgs;
389cdf0e10cSrcweir         }
390cdf0e10cSrcweir     }
391cdf0e10cSrcweir 
392cdf0e10cSrcweir     // if aArgumentBuffer exist - pack it into the aScriptBuffer
393cdf0e10cSrcweir     if(nValidArgs>0)
394cdf0e10cSrcweir     {
395cdf0e10cSrcweir         if(bAsComment)
396cdf0e10cSrcweir             aScriptBuffer.appendAscii(REM_AS_COMMENT);
397cdf0e10cSrcweir         aScriptBuffer.appendAscii("dim ");
398cdf0e10cSrcweir         aScriptBuffer.append     (sArrayName);
399cdf0e10cSrcweir         aScriptBuffer.appendAscii("(");
400cdf0e10cSrcweir         aScriptBuffer.append     ((sal_Int32)(nValidArgs-1)); // 0 based!
401cdf0e10cSrcweir         aScriptBuffer.appendAscii(") as new com.sun.star.beans.PropertyValue\n");
402cdf0e10cSrcweir         aScriptBuffer.append     (aArgumentBuffer.makeStringAndClear());
403cdf0e10cSrcweir         aScriptBuffer.appendAscii("\n");
404cdf0e10cSrcweir     }
405cdf0e10cSrcweir 
406cdf0e10cSrcweir     // add code for dispatches
407cdf0e10cSrcweir     if(bAsComment)
408cdf0e10cSrcweir         aScriptBuffer.appendAscii(REM_AS_COMMENT);
409cdf0e10cSrcweir     aScriptBuffer.appendAscii("dispatcher.executeDispatch(document, \"");
410cdf0e10cSrcweir     aScriptBuffer.append     (aURL);
411cdf0e10cSrcweir     aScriptBuffer.appendAscii("\", \"\", 0, ");
412cdf0e10cSrcweir     if(nValidArgs<1)
413cdf0e10cSrcweir         aScriptBuffer.appendAscii("Array()");
414cdf0e10cSrcweir     else
415cdf0e10cSrcweir     {
416cdf0e10cSrcweir         aScriptBuffer.append( sArrayName.getStr() );
417cdf0e10cSrcweir         aScriptBuffer.appendAscii("()");
418cdf0e10cSrcweir     }
419cdf0e10cSrcweir     aScriptBuffer.appendAscii(")\n\n");
420cdf0e10cSrcweir 
421cdf0e10cSrcweir     /* SAFE { */
422cdf0e10cSrcweir     m_nRecordingID++;
423cdf0e10cSrcweir     /* } */
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
getElementType()426cdf0e10cSrcweir com::sun::star::uno::Type SAL_CALL DispatchRecorder::getElementType() throw (::com::sun::star::uno::RuntimeException)
427cdf0e10cSrcweir {
428cdf0e10cSrcweir 	return ::getCppuType((const com::sun::star::frame::DispatchStatement *)NULL);
429cdf0e10cSrcweir }
430cdf0e10cSrcweir 
hasElements()431cdf0e10cSrcweir sal_Bool SAL_CALL DispatchRecorder::hasElements()  throw (::com::sun::star::uno::RuntimeException)
432cdf0e10cSrcweir {
433cdf0e10cSrcweir 	return (! m_aStatements.empty());
434cdf0e10cSrcweir }
435cdf0e10cSrcweir 
getCount()436cdf0e10cSrcweir sal_Int32 SAL_CALL DispatchRecorder::getCount() throw (::com::sun::star::uno::RuntimeException)
437cdf0e10cSrcweir {
438cdf0e10cSrcweir 	return m_aStatements.size();
439cdf0e10cSrcweir }
440cdf0e10cSrcweir 
getByIndex(sal_Int32 idx)441cdf0e10cSrcweir 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)
442cdf0e10cSrcweir {
443cdf0e10cSrcweir     if (idx >= (sal_Int32)m_aStatements.size()) {
444cdf0e10cSrcweir 		throw com::sun::star::lang::IndexOutOfBoundsException(
445cdf0e10cSrcweir 			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
446cdf0e10cSrcweir 				"Dispatch recorder out of bounds") ),
447cdf0e10cSrcweir             		Reference< XInterface >() );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 	}
450cdf0e10cSrcweir 
451cdf0e10cSrcweir 	Any element(&m_aStatements[idx],
452cdf0e10cSrcweir 		::getCppuType((const com::sun::star::frame::DispatchStatement *)NULL));
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 	return element;
455cdf0e10cSrcweir }
456cdf0e10cSrcweir 
replaceByIndex(sal_Int32 idx,const com::sun::star::uno::Any & element)457cdf0e10cSrcweir 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)
458cdf0e10cSrcweir {
459cdf0e10cSrcweir 	if (element.getValueType() !=
460cdf0e10cSrcweir 	    ::getCppuType((const com::sun::star::frame::DispatchStatement *)NULL)) {
461cdf0e10cSrcweir 		                throw com::sun::star::lang::IllegalArgumentException(
462cdf0e10cSrcweir                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
463cdf0e10cSrcweir                                 "Illegal argument in dispatch recorder") ),
464cdf0e10cSrcweir                         Reference< XInterface >(), 2 );
465cdf0e10cSrcweir 	}
466cdf0e10cSrcweir 
467cdf0e10cSrcweir     if (idx >= (sal_Int32)m_aStatements.size()) {
468cdf0e10cSrcweir                 throw com::sun::star::lang::IndexOutOfBoundsException(
469cdf0e10cSrcweir                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
470cdf0e10cSrcweir                                 "Dispatch recorder out of bounds") ),
471cdf0e10cSrcweir                         Reference< XInterface >() );
472cdf0e10cSrcweir 
473cdf0e10cSrcweir         }
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	com::sun::star::frame::DispatchStatement *pStatement;
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	pStatement = (com::sun::star::frame::DispatchStatement *)element.getValue();
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 	com::sun::star::frame::DispatchStatement aStatement(
480cdf0e10cSrcweir 		pStatement->aCommand,
481cdf0e10cSrcweir 		pStatement->aTarget,
482cdf0e10cSrcweir 		pStatement->aArgs,
483cdf0e10cSrcweir 		pStatement->nFlags,
484cdf0e10cSrcweir 		pStatement->bIsComment);
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 	m_aStatements[idx] = aStatement;
487cdf0e10cSrcweir }
488cdf0e10cSrcweir 
489cdf0e10cSrcweir } // namespace framework
490