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 <tools/debug.hxx>
22 : #include <com/sun/star/document/XEventsSupplier.hpp>
23 : #include <com/sun/star/container/XNameReplace.hpp>
24 : #include <com/sun/star/presentation/AnimationSpeed.hpp>
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
27 : #include <com/sun/star/presentation/ClickAction.hpp>
28 : #include <tools/urlobj.hxx>
29 : #include <comphelper/extract.hxx>
30 :
31 : #include <sax/tools/converter.hxx>
32 :
33 : #include <xmloff/xmltoken.hxx>
34 : #include <xmloff/xmlimp.hxx>
35 : #include "xmloff/xmlnmspe.hxx"
36 : #include <xmloff/xmluconv.hxx>
37 : #include <xmloff/nmspmap.hxx>
38 : #include "eventimp.hxx"
39 : #include "anim.hxx"
40 :
41 :
42 : using namespace ::std;
43 : using namespace ::cppu;
44 : using namespace ::com::sun::star;
45 : using namespace ::com::sun::star::xml;
46 : using namespace ::com::sun::star::xml::sax;
47 : using namespace ::com::sun::star::uno;
48 : using namespace ::com::sun::star::drawing;
49 : using namespace ::com::sun::star::beans;
50 : using namespace ::com::sun::star::lang;
51 : using namespace ::com::sun::star::document;
52 : using namespace ::com::sun::star::container;
53 : using namespace ::com::sun::star::presentation;
54 : using namespace ::xmloff::token;
55 :
56 : ///////////////////////////////////////////////////////////////////////
57 :
58 : SvXMLEnumMapEntry const aXML_EventActions_EnumMap[] =
59 : {
60 : { XML_NONE, ClickAction_NONE },
61 : { XML_PREVIOUS_PAGE, ClickAction_PREVPAGE },
62 : { XML_NEXT_PAGE, ClickAction_NEXTPAGE },
63 : { XML_FIRST_PAGE, ClickAction_FIRSTPAGE },
64 : { XML_LAST_PAGE, ClickAction_LASTPAGE },
65 : { XML_HIDE, ClickAction_INVISIBLE },
66 : { XML_STOP, ClickAction_STOPPRESENTATION },
67 : { XML_EXECUTE, ClickAction_PROGRAM },
68 : { XML_SHOW, ClickAction_BOOKMARK },
69 : { XML_SHOW, ClickAction_DOCUMENT },
70 : { XML_EXECUTE_MACRO, ClickAction_MACRO },
71 : { XML_VERB, ClickAction_VERB },
72 : { XML_FADE_OUT, ClickAction_VANISH },
73 : { XML_SOUND, ClickAction_SOUND },
74 : { XML_TOKEN_INVALID, 0 }
75 : };
76 :
77 : ///////////////////////////////////////////////////////////////////////
78 :
79 : class SdXMLEventContext : public SvXMLImportContext
80 : {
81 : private:
82 : com::sun::star::uno::Reference< com::sun::star::drawing::XShape > mxShape;
83 :
84 : public:
85 : TYPEINFO();
86 :
87 : SdXMLEventContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList>& xAttrList, const Reference< XShape >& rxShape );
88 : virtual ~SdXMLEventContext();
89 :
90 : virtual SvXMLImportContext * CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList>& xAttrList );
91 : virtual void EndElement();
92 :
93 : sal_Bool mbValid;
94 : sal_Bool mbScript;
95 : ClickAction meClickAction;
96 : XMLEffect meEffect;
97 : XMLEffectDirection meDirection;
98 : sal_Int16 mnStartScale;
99 : AnimationSpeed meSpeed;
100 : sal_Int32 mnVerb;
101 : OUString msSoundURL;
102 : sal_Bool mbPlayFull;
103 : OUString msMacroName;
104 : OUString msBookmark;
105 : OUString msLanguage;
106 : };
107 :
108 : ///////////////////////////////////////////////////////////////////////
109 :
110 : class XMLEventSoundContext : public SvXMLImportContext
111 : {
112 : SdXMLEventContext* mpParent;
113 :
114 : public:
115 : TYPEINFO();
116 :
117 : XMLEventSoundContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, SdXMLEventContext* pParent );
118 : virtual ~XMLEventSoundContext();
119 : };
120 :
121 0 : TYPEINIT1( XMLEventSoundContext, SvXMLImportContext );
122 :
123 0 : XMLEventSoundContext::XMLEventSoundContext( SvXMLImport& rImp, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, SdXMLEventContext* pParent )
124 0 : : SvXMLImportContext( rImp, nPrfx, rLocalName ), mpParent( pParent )
125 : {
126 0 : if( mpParent && nPrfx == XML_NAMESPACE_PRESENTATION && IsXMLToken( rLocalName, XML_SOUND ) )
127 : {
128 0 : const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
129 0 : for(sal_Int16 i=0; i < nAttrCount; i++)
130 : {
131 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
132 0 : OUString aAttrLocalName;
133 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aAttrLocalName );
134 0 : OUString sValue = xAttrList->getValueByIndex( i );
135 :
136 0 : switch( nAttrPrefix )
137 : {
138 : case XML_NAMESPACE_XLINK:
139 0 : if( IsXMLToken( aAttrLocalName, XML_HREF ) )
140 : {
141 0 : mpParent->msSoundURL = rImp.GetAbsoluteReference(sValue);
142 : }
143 0 : break;
144 : case XML_NAMESPACE_PRESENTATION:
145 0 : if( IsXMLToken( aAttrLocalName, XML_PLAY_FULL ) )
146 : {
147 0 : mpParent->mbPlayFull = IsXMLToken( sValue, XML_TRUE );
148 : }
149 : }
150 0 : }
151 : }
152 0 : }
153 :
154 0 : XMLEventSoundContext::~XMLEventSoundContext()
155 : {
156 0 : }
157 :
158 : ///////////////////////////////////////////////////////////////////////
159 :
160 0 : TYPEINIT1( SdXMLEventContext, SvXMLImportContext );
161 :
162 0 : SdXMLEventContext::SdXMLEventContext( SvXMLImport& rImp, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XShape >& rxShape )
163 : : SvXMLImportContext(rImp, nPrfx, rLocalName),
164 : mxShape( rxShape ), mbScript( sal_False ), meClickAction( ClickAction_NONE ),
165 : meEffect( EK_none ), meDirection( ED_none ), mnStartScale( 100 ),
166 0 : meSpeed( AnimationSpeed_MEDIUM ), mnVerb(0), mbPlayFull( sal_False )
167 : {
168 0 : static const OUString sXMLClickName( "click" );
169 :
170 0 : if( nPrfx == XML_NAMESPACE_PRESENTATION && IsXMLToken( rLocalName, XML_EVENT_LISTENER ) )
171 : {
172 0 : mbValid = sal_True;
173 : }
174 0 : else if( nPrfx == XML_NAMESPACE_SCRIPT && IsXMLToken( rLocalName, XML_EVENT_LISTENER ) )
175 : {
176 0 : mbScript = sal_True;
177 0 : mbValid = sal_True;
178 : }
179 : else
180 : {
181 0 : return;
182 : }
183 :
184 : // read attributes
185 0 : OUString sEventName;
186 0 : const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
187 0 : for(sal_Int16 i=0; (i < nAttrCount) && mbValid; i++)
188 : {
189 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
190 0 : OUString aAttrLocalName;
191 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aAttrLocalName );
192 0 : OUString sValue = xAttrList->getValueByIndex( i );
193 :
194 0 : switch( nAttrPrefix )
195 : {
196 : case XML_NAMESPACE_PRESENTATION:
197 0 : if( IsXMLToken( aAttrLocalName, XML_ACTION ) )
198 : {
199 : sal_uInt16 eEnum;
200 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_EventActions_EnumMap ) )
201 0 : meClickAction = (ClickAction)eEnum;
202 : }
203 0 : if( IsXMLToken( aAttrLocalName, XML_EFFECT ) )
204 : {
205 : sal_uInt16 eEnum;
206 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_AnimationEffect_EnumMap ) )
207 0 : meEffect = (XMLEffect)eEnum;
208 : }
209 0 : else if( IsXMLToken( aAttrLocalName, XML_DIRECTION ) )
210 : {
211 : sal_uInt16 eEnum;
212 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_AnimationDirection_EnumMap ) )
213 0 : meDirection = (XMLEffectDirection)eEnum;
214 : }
215 0 : else if( IsXMLToken( aAttrLocalName, XML_START_SCALE ) )
216 : {
217 : sal_Int32 nScale;
218 0 : if (::sax::Converter::convertPercent( nScale, sValue ))
219 0 : mnStartScale = (sal_Int16)nScale;
220 : }
221 0 : else if( IsXMLToken( aAttrLocalName, XML_SPEED ) )
222 : {
223 : sal_uInt16 eEnum;
224 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_AnimationSpeed_EnumMap ) )
225 0 : meSpeed = (AnimationSpeed)eEnum;
226 : }
227 0 : else if( IsXMLToken( aAttrLocalName, XML_VERB ) )
228 : {
229 0 : ::sax::Converter::convertNumber( mnVerb, sValue );
230 : }
231 0 : break;
232 :
233 : case XML_NAMESPACE_SCRIPT:
234 0 : if( IsXMLToken( aAttrLocalName, XML_EVENT_NAME ) )
235 : {
236 0 : sEventName = sValue;
237 : sal_uInt16 nScriptPrefix =
238 0 : GetImport().GetNamespaceMap().GetKeyByAttrName( sValue, &sEventName );
239 0 : mbValid = XML_NAMESPACE_DOM == nScriptPrefix && sEventName == sXMLClickName;
240 : }
241 0 : else if( IsXMLToken( aAttrLocalName, XML_LANGUAGE ) )
242 : {
243 : // language is not evaluated!
244 0 : OUString aScriptLanguage;
245 0 : msLanguage = sValue;
246 0 : sal_uInt16 nScriptPrefix = rImp.GetNamespaceMap().
247 0 : GetKeyByAttrName( msLanguage, &aScriptLanguage );
248 0 : if( XML_NAMESPACE_OOO == nScriptPrefix )
249 0 : msLanguage = aScriptLanguage;
250 : }
251 0 : else if( IsXMLToken( aAttrLocalName, XML_MACRO_NAME ) )
252 : {
253 0 : msMacroName = sValue;
254 : }
255 0 : break;
256 :
257 : case XML_NAMESPACE_XLINK:
258 0 : if( IsXMLToken( aAttrLocalName, XML_HREF ) )
259 : {
260 0 : if ( mbScript )
261 : {
262 0 : msMacroName = sValue;
263 : }
264 : else
265 : {
266 : const OUString &rTmp =
267 0 : rImp.GetAbsoluteReference(sValue);
268 : INetURLObject::translateToInternal( rTmp, msBookmark,
269 : INetURLObject::DECODE_UNAMBIGUOUS,
270 0 : RTL_TEXTENCODING_UTF8 );
271 : }
272 : }
273 0 : break;
274 : }
275 0 : }
276 :
277 0 : if( mbValid )
278 0 : mbValid = !sEventName.isEmpty();
279 : }
280 :
281 0 : SdXMLEventContext::~SdXMLEventContext()
282 : {
283 0 : }
284 :
285 0 : SvXMLImportContext * SdXMLEventContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList>& xAttrList )
286 : {
287 0 : return new XMLEventSoundContext( GetImport(), nPrefix, rLocalName, xAttrList, this );
288 : }
289 :
290 0 : void SdXMLEventContext::EndElement()
291 : {
292 0 : if( !mbValid )
293 0 : return;
294 :
295 : do
296 : {
297 0 : Reference< XEventsSupplier > xEventsSupplier( mxShape, UNO_QUERY );
298 0 : if( !xEventsSupplier.is() )
299 0 : break;
300 :
301 0 : Reference< XNameReplace > xEvents( xEventsSupplier->getEvents() );
302 : DBG_ASSERT( xEvents.is(), "XEventsSupplier::getEvents() returned NULL" );
303 0 : if( !xEvents.is() )
304 0 : break;
305 :
306 0 : OUString sAPIEventName;
307 0 : uno::Sequence< beans::PropertyValue > aProperties;
308 :
309 0 : sAPIEventName = OUString( "OnClick" );
310 :
311 0 : if( mbScript )
312 0 : meClickAction = ClickAction_MACRO;
313 :
314 0 : sal_Int32 nPropertyCount = 2;
315 0 : switch( meClickAction )
316 : {
317 : case ClickAction_NONE:
318 : case ClickAction_PREVPAGE:
319 : case ClickAction_NEXTPAGE:
320 : case ClickAction_FIRSTPAGE:
321 : case ClickAction_LASTPAGE:
322 : case ClickAction_INVISIBLE:
323 : case ClickAction_STOPPRESENTATION:
324 0 : break;
325 : case ClickAction_PROGRAM:
326 : case ClickAction_VERB:
327 : case ClickAction_BOOKMARK:
328 : case ClickAction_DOCUMENT:
329 0 : nPropertyCount += 1;
330 0 : break;
331 : case ClickAction_MACRO:
332 0 : if ( msLanguage.equalsIgnoreAsciiCase("starbasic") )
333 0 : nPropertyCount += 1;
334 0 : break;
335 :
336 : case ClickAction_SOUND:
337 0 : nPropertyCount += 2;
338 0 : break;
339 :
340 : case ClickAction_VANISH:
341 0 : nPropertyCount += 4;
342 0 : break;
343 : default:
344 0 : break;
345 : }
346 :
347 0 : aProperties.realloc( nPropertyCount );
348 0 : beans::PropertyValue* pProperties = aProperties.getArray();
349 :
350 0 : if( ClickAction_MACRO == meClickAction )
351 : {
352 0 : if ( msLanguage.equalsIgnoreAsciiCase("starbasic") )
353 : {
354 0 : OUString sLibrary;
355 0 : const OUString& rApp = GetXMLToken( XML_APPLICATION );
356 0 : const OUString& rDoc = GetXMLToken( XML_DOCUMENT );
357 0 : if( msMacroName.getLength() > rApp.getLength()+1 &&
358 0 : msMacroName.copy(0,rApp.getLength()).equalsIgnoreAsciiCase( rApp ) &&
359 0 : ':' == msMacroName[rApp.getLength()] )
360 : {
361 0 : sLibrary = "StarOffice";
362 0 : msMacroName = msMacroName.copy( rApp.getLength()+1 );
363 : }
364 0 : else if( msMacroName.getLength() > rDoc.getLength()+1 &&
365 0 : msMacroName.copy(0,rDoc.getLength()).equalsIgnoreAsciiCase( rDoc ) &&
366 0 : ':' == msMacroName[rDoc.getLength()] )
367 : {
368 0 : sLibrary = rDoc;
369 0 : msMacroName = msMacroName.copy( rDoc.getLength()+1 );
370 : }
371 :
372 0 : pProperties->Name = OUString( "EventType" );
373 0 : pProperties->Handle = -1;
374 0 : pProperties->Value <<= OUString( "StarBasic" );
375 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
376 0 : pProperties++;
377 :
378 0 : pProperties->Name = OUString( "MacroName" );
379 0 : pProperties->Handle = -1;
380 0 : pProperties->Value <<= msMacroName;
381 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
382 0 : pProperties++;
383 :
384 0 : pProperties->Name = OUString( "Library" );
385 0 : pProperties->Handle = -1;
386 0 : pProperties->Value <<= sLibrary;
387 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
388 : }
389 : else
390 : {
391 0 : pProperties->Name =
392 0 : OUString( "EventType" );
393 0 : pProperties->Handle = -1;
394 0 : pProperties->Value <<= OUString( "Script" );
395 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
396 0 : pProperties++;
397 :
398 0 : pProperties->Name = OUString( "Script" );
399 0 : pProperties->Handle = -1;
400 0 : pProperties->Value <<= msMacroName;
401 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
402 : }
403 : }
404 : else
405 : {
406 0 : pProperties->Name = OUString( "EventType" );
407 0 : pProperties->Handle = -1;
408 0 : pProperties->Value <<= OUString( "Presentation" );
409 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
410 0 : pProperties++;
411 :
412 : // ClickAction_BOOKMARK and ClickAction_DOCUMENT share the same xml event
413 : // so check here if its realy a bookmark or maybe a document
414 0 : if( meClickAction == ClickAction_BOOKMARK )
415 : {
416 0 : if( !msBookmark.startsWith( "#" ) )
417 0 : meClickAction = ClickAction_DOCUMENT;
418 : }
419 :
420 0 : pProperties->Name = OUString( "ClickAction" );
421 0 : pProperties->Handle = -1;
422 0 : pProperties->Value <<= meClickAction;
423 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
424 0 : pProperties++;
425 :
426 0 : switch( meClickAction )
427 : {
428 : case ClickAction_NONE:
429 : case ClickAction_PREVPAGE:
430 : case ClickAction_NEXTPAGE:
431 : case ClickAction_FIRSTPAGE:
432 : case ClickAction_LASTPAGE:
433 : case ClickAction_INVISIBLE:
434 : case ClickAction_STOPPRESENTATION:
435 0 : break;
436 :
437 : case ClickAction_BOOKMARK:
438 0 : msBookmark = msBookmark.copy(1);
439 :
440 : // Note: no break here!!!
441 :
442 : case ClickAction_DOCUMENT:
443 : case ClickAction_PROGRAM:
444 0 : pProperties->Name = OUString( "Bookmark" );
445 0 : pProperties->Handle = -1;
446 0 : pProperties->Value <<= msBookmark;
447 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
448 0 : break;
449 :
450 : case ClickAction_VANISH:
451 0 : pProperties->Name = OUString( "Effect" );
452 0 : pProperties->Handle = -1;
453 0 : pProperties->Value <<= ImplSdXMLgetEffect( meEffect, meDirection, mnStartScale, sal_True );
454 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
455 0 : pProperties++;
456 :
457 0 : pProperties->Name = OUString( "Speed" );
458 0 : pProperties->Handle = -1;
459 0 : pProperties->Value <<= meSpeed;
460 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
461 0 : pProperties++;
462 :
463 : // NOTE: no break here!!!
464 :
465 : case ClickAction_SOUND:
466 0 : pProperties->Name = OUString( "SoundURL" );
467 0 : pProperties->Handle = -1;
468 0 : pProperties->Value <<= msSoundURL;
469 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
470 0 : pProperties++;
471 :
472 0 : pProperties->Name = OUString( "PlayFull" );
473 0 : pProperties->Handle = -1;
474 0 : pProperties->Value = ::cppu::bool2any(mbPlayFull);
475 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
476 0 : break;
477 :
478 : case ClickAction_VERB:
479 0 : pProperties->Name = OUString( "Verb" );
480 0 : pProperties->Handle = -1;
481 0 : pProperties->Value <<= mnVerb;
482 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
483 0 : break;
484 : case ClickAction_MACRO:
485 : OSL_FAIL("xmloff::SdXMLEventContext::EndElement(), ClickAction_MACRO must be handled in different if case");
486 0 : break;
487 : default:
488 0 : break;
489 : }
490 : }
491 0 : xEvents->replaceByName( sAPIEventName, uno::Any( aProperties ) );
492 :
493 : } while(0);
494 : }
495 :
496 : ///////////////////////////////////////////////////////////////////////
497 :
498 0 : TYPEINIT1( SdXMLEventsContext, SvXMLImportContext );
499 :
500 0 : SdXMLEventsContext::SdXMLEventsContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName,
501 : const Reference< XAttributeList>&, const Reference< XShape >& rxShape)
502 0 : : SvXMLImportContext(rImport, nPrfx, rLocalName), mxShape( rxShape )
503 : {
504 0 : }
505 :
506 0 : SdXMLEventsContext::~SdXMLEventsContext()
507 : {
508 0 : }
509 :
510 0 : SvXMLImportContext * SdXMLEventsContext::CreateChildContext( sal_uInt16 nPrfx, const OUString& rLocalName,
511 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList )
512 : {
513 0 : return new SdXMLEventContext( GetImport(), nPrfx, rLocalName, xAttrList, mxShape );
514 : }
515 :
516 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|