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