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