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 :
29 : #include <sax/tools/converter.hxx>
30 :
31 : #include <xmloff/xmltoken.hxx>
32 : #include <xmloff/xmlimp.hxx>
33 : #include <xmloff/xmlnmspe.hxx>
34 : #include <xmloff/xmluconv.hxx>
35 : #include <xmloff/nmspmap.hxx>
36 : #include "eventimp.hxx"
37 : #include "anim.hxx"
38 :
39 : using namespace ::std;
40 : using namespace ::cppu;
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star::xml;
43 : using namespace ::com::sun::star::xml::sax;
44 : using namespace ::com::sun::star::uno;
45 : using namespace ::com::sun::star::drawing;
46 : using namespace ::com::sun::star::beans;
47 : using namespace ::com::sun::star::lang;
48 : using namespace ::com::sun::star::document;
49 : using namespace ::com::sun::star::container;
50 : using namespace ::com::sun::star::presentation;
51 : using namespace ::xmloff::token;
52 :
53 : SvXMLEnumMapEntry const aXML_EventActions_EnumMap[] =
54 : {
55 : { XML_NONE, ClickAction_NONE },
56 : { XML_PREVIOUS_PAGE, ClickAction_PREVPAGE },
57 : { XML_NEXT_PAGE, ClickAction_NEXTPAGE },
58 : { XML_FIRST_PAGE, ClickAction_FIRSTPAGE },
59 : { XML_LAST_PAGE, ClickAction_LASTPAGE },
60 : { XML_HIDE, ClickAction_INVISIBLE },
61 : { XML_STOP, ClickAction_STOPPRESENTATION },
62 : { XML_EXECUTE, ClickAction_PROGRAM },
63 : { XML_SHOW, ClickAction_BOOKMARK },
64 : { XML_SHOW, ClickAction_DOCUMENT },
65 : { XML_EXECUTE_MACRO, ClickAction_MACRO },
66 : { XML_VERB, ClickAction_VERB },
67 : { XML_FADE_OUT, ClickAction_VANISH },
68 : { XML_SOUND, ClickAction_SOUND },
69 : { XML_TOKEN_INVALID, 0 }
70 : };
71 :
72 : class SdXMLEventContext : public SvXMLImportContext
73 : {
74 : private:
75 : com::sun::star::uno::Reference< com::sun::star::drawing::XShape > mxShape;
76 :
77 : public:
78 : TYPEINFO_OVERRIDE();
79 :
80 : SdXMLEventContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList>& xAttrList, const Reference< XShape >& rxShape );
81 : virtual ~SdXMLEventContext();
82 :
83 : virtual SvXMLImportContext * CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList>& xAttrList ) SAL_OVERRIDE;
84 : virtual void EndElement() SAL_OVERRIDE;
85 :
86 : bool mbValid;
87 : bool mbScript;
88 : ClickAction meClickAction;
89 : XMLEffect meEffect;
90 : XMLEffectDirection meDirection;
91 : sal_Int16 mnStartScale;
92 : AnimationSpeed meSpeed;
93 : sal_Int32 mnVerb;
94 : OUString msSoundURL;
95 : bool mbPlayFull;
96 : OUString msMacroName;
97 : OUString msBookmark;
98 : OUString msLanguage;
99 : };
100 :
101 : class XMLEventSoundContext : public SvXMLImportContext
102 : {
103 : SdXMLEventContext* mpParent;
104 :
105 : public:
106 : TYPEINFO_OVERRIDE();
107 :
108 : XMLEventSoundContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, SdXMLEventContext* pParent );
109 : virtual ~XMLEventSoundContext();
110 : };
111 :
112 0 : TYPEINIT1( XMLEventSoundContext, SvXMLImportContext );
113 :
114 0 : XMLEventSoundContext::XMLEventSoundContext( SvXMLImport& rImp, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, SdXMLEventContext* pParent )
115 0 : : SvXMLImportContext( rImp, nPrfx, rLocalName ), mpParent( pParent )
116 : {
117 0 : if( mpParent && nPrfx == XML_NAMESPACE_PRESENTATION && IsXMLToken( rLocalName, XML_SOUND ) )
118 : {
119 0 : const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
120 0 : for(sal_Int16 i=0; i < nAttrCount; i++)
121 : {
122 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
123 0 : OUString aAttrLocalName;
124 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aAttrLocalName );
125 0 : OUString sValue = xAttrList->getValueByIndex( i );
126 :
127 0 : switch( nAttrPrefix )
128 : {
129 : case XML_NAMESPACE_XLINK:
130 0 : if( IsXMLToken( aAttrLocalName, XML_HREF ) )
131 : {
132 0 : mpParent->msSoundURL = rImp.GetAbsoluteReference(sValue);
133 : }
134 0 : break;
135 : case XML_NAMESPACE_PRESENTATION:
136 0 : if( IsXMLToken( aAttrLocalName, XML_PLAY_FULL ) )
137 : {
138 0 : mpParent->mbPlayFull = IsXMLToken( sValue, XML_TRUE );
139 : }
140 : }
141 0 : }
142 : }
143 0 : }
144 :
145 0 : XMLEventSoundContext::~XMLEventSoundContext()
146 : {
147 0 : }
148 :
149 0 : TYPEINIT1( SdXMLEventContext, SvXMLImportContext );
150 :
151 0 : SdXMLEventContext::SdXMLEventContext( SvXMLImport& rImp, sal_uInt16 nPrfx, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XShape >& rxShape )
152 : : SvXMLImportContext(rImp, nPrfx, rLocalName)
153 : , mxShape(rxShape), mbValid(false), mbScript(false)
154 : , meClickAction(ClickAction_NONE), meEffect(EK_none)
155 : , meDirection(ED_none), mnStartScale(100), meSpeed(AnimationSpeed_MEDIUM)
156 0 : , mnVerb(0), mbPlayFull(false)
157 : {
158 0 : static const OUString sXMLClickName( "click" );
159 :
160 0 : if( nPrfx == XML_NAMESPACE_PRESENTATION && IsXMLToken( rLocalName, XML_EVENT_LISTENER ) )
161 : {
162 0 : mbValid = true;
163 : }
164 0 : else if( nPrfx == XML_NAMESPACE_SCRIPT && IsXMLToken( rLocalName, XML_EVENT_LISTENER ) )
165 : {
166 0 : mbScript = true;
167 0 : mbValid = true;
168 : }
169 : else
170 : {
171 0 : return;
172 : }
173 :
174 : // read attributes
175 0 : OUString sEventName;
176 0 : const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
177 0 : for(sal_Int16 i=0; (i < nAttrCount) && mbValid; i++)
178 : {
179 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
180 0 : OUString aAttrLocalName;
181 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aAttrLocalName );
182 0 : OUString sValue = xAttrList->getValueByIndex( i );
183 :
184 0 : switch( nAttrPrefix )
185 : {
186 : case XML_NAMESPACE_PRESENTATION:
187 0 : if( IsXMLToken( aAttrLocalName, XML_ACTION ) )
188 : {
189 : sal_uInt16 eEnum;
190 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_EventActions_EnumMap ) )
191 0 : meClickAction = (ClickAction)eEnum;
192 : }
193 0 : if( IsXMLToken( aAttrLocalName, XML_EFFECT ) )
194 : {
195 : sal_uInt16 eEnum;
196 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_AnimationEffect_EnumMap ) )
197 0 : meEffect = (XMLEffect)eEnum;
198 : }
199 0 : else if( IsXMLToken( aAttrLocalName, XML_DIRECTION ) )
200 : {
201 : sal_uInt16 eEnum;
202 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_AnimationDirection_EnumMap ) )
203 0 : meDirection = (XMLEffectDirection)eEnum;
204 : }
205 0 : else if( IsXMLToken( aAttrLocalName, XML_START_SCALE ) )
206 : {
207 : sal_Int32 nScale;
208 0 : if (::sax::Converter::convertPercent( nScale, sValue ))
209 0 : mnStartScale = (sal_Int16)nScale;
210 : }
211 0 : else if( IsXMLToken( aAttrLocalName, XML_SPEED ) )
212 : {
213 : sal_uInt16 eEnum;
214 0 : if( SvXMLUnitConverter::convertEnum( eEnum, sValue, aXML_AnimationSpeed_EnumMap ) )
215 0 : meSpeed = (AnimationSpeed)eEnum;
216 : }
217 0 : else if( IsXMLToken( aAttrLocalName, XML_VERB ) )
218 : {
219 0 : ::sax::Converter::convertNumber( mnVerb, sValue );
220 : }
221 0 : break;
222 :
223 : case XML_NAMESPACE_SCRIPT:
224 0 : if( IsXMLToken( aAttrLocalName, XML_EVENT_NAME ) )
225 : {
226 0 : sEventName = sValue;
227 : sal_uInt16 nScriptPrefix =
228 0 : GetImport().GetNamespaceMap().GetKeyByAttrName( sValue, &sEventName );
229 0 : mbValid = XML_NAMESPACE_DOM == nScriptPrefix && sEventName == sXMLClickName;
230 : }
231 0 : else if( IsXMLToken( aAttrLocalName, XML_LANGUAGE ) )
232 : {
233 : // language is not evaluated!
234 0 : OUString aScriptLanguage;
235 0 : msLanguage = sValue;
236 0 : sal_uInt16 nScriptPrefix = rImp.GetNamespaceMap().
237 0 : GetKeyByAttrName( msLanguage, &aScriptLanguage );
238 0 : if( XML_NAMESPACE_OOO == nScriptPrefix )
239 0 : msLanguage = aScriptLanguage;
240 : }
241 0 : else if( IsXMLToken( aAttrLocalName, XML_MACRO_NAME ) )
242 : {
243 0 : msMacroName = sValue;
244 : }
245 0 : break;
246 :
247 : case XML_NAMESPACE_XLINK:
248 0 : if( IsXMLToken( aAttrLocalName, XML_HREF ) )
249 : {
250 0 : if ( mbScript )
251 : {
252 0 : msMacroName = sValue;
253 : }
254 : else
255 : {
256 : const OUString &rTmp =
257 0 : rImp.GetAbsoluteReference(sValue);
258 : INetURLObject::translateToInternal( rTmp, msBookmark,
259 : INetURLObject::DECODE_UNAMBIGUOUS,
260 0 : RTL_TEXTENCODING_UTF8 );
261 : }
262 : }
263 0 : break;
264 : }
265 0 : }
266 :
267 0 : if( mbValid )
268 0 : mbValid = !sEventName.isEmpty();
269 : }
270 :
271 0 : SdXMLEventContext::~SdXMLEventContext()
272 : {
273 0 : }
274 :
275 0 : SvXMLImportContext * SdXMLEventContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList>& xAttrList )
276 : {
277 0 : return new XMLEventSoundContext( GetImport(), nPrefix, rLocalName, xAttrList, this );
278 : }
279 :
280 0 : void SdXMLEventContext::EndElement()
281 : {
282 0 : if( !mbValid )
283 0 : return;
284 :
285 : do
286 : {
287 0 : Reference< XEventsSupplier > xEventsSupplier( mxShape, UNO_QUERY );
288 0 : if( !xEventsSupplier.is() )
289 0 : break;
290 :
291 0 : Reference< XNameReplace > xEvents( xEventsSupplier->getEvents() );
292 : DBG_ASSERT( xEvents.is(), "XEventsSupplier::getEvents() returned NULL" );
293 0 : if( !xEvents.is() )
294 0 : break;
295 :
296 0 : OUString sAPIEventName;
297 0 : uno::Sequence< beans::PropertyValue > aProperties;
298 :
299 0 : sAPIEventName = "OnClick";
300 :
301 0 : if( mbScript )
302 0 : meClickAction = ClickAction_MACRO;
303 :
304 0 : sal_Int32 nPropertyCount = 2;
305 0 : switch( meClickAction )
306 : {
307 : case ClickAction_NONE:
308 : case ClickAction_PREVPAGE:
309 : case ClickAction_NEXTPAGE:
310 : case ClickAction_FIRSTPAGE:
311 : case ClickAction_LASTPAGE:
312 : case ClickAction_INVISIBLE:
313 : case ClickAction_STOPPRESENTATION:
314 0 : break;
315 : case ClickAction_PROGRAM:
316 : case ClickAction_VERB:
317 : case ClickAction_BOOKMARK:
318 : case ClickAction_DOCUMENT:
319 0 : nPropertyCount += 1;
320 0 : break;
321 : case ClickAction_MACRO:
322 0 : if ( msLanguage.equalsIgnoreAsciiCase("starbasic") )
323 0 : nPropertyCount += 1;
324 0 : break;
325 :
326 : case ClickAction_SOUND:
327 0 : nPropertyCount += 2;
328 0 : break;
329 :
330 : case ClickAction_VANISH:
331 0 : nPropertyCount += 4;
332 0 : break;
333 : default:
334 0 : break;
335 : }
336 :
337 0 : aProperties.realloc( nPropertyCount );
338 0 : beans::PropertyValue* pProperties = aProperties.getArray();
339 :
340 0 : if( ClickAction_MACRO == meClickAction )
341 : {
342 0 : if ( msLanguage.equalsIgnoreAsciiCase("starbasic") )
343 : {
344 0 : OUString sLibrary;
345 0 : const OUString& rApp = GetXMLToken( XML_APPLICATION );
346 0 : const OUString& rDoc = GetXMLToken( XML_DOCUMENT );
347 0 : if( msMacroName.getLength() > rApp.getLength()+1 &&
348 0 : msMacroName.copy(0,rApp.getLength()).equalsIgnoreAsciiCase( rApp ) &&
349 0 : ':' == msMacroName[rApp.getLength()] )
350 : {
351 0 : sLibrary = "StarOffice";
352 0 : msMacroName = msMacroName.copy( rApp.getLength()+1 );
353 : }
354 0 : else if( msMacroName.getLength() > rDoc.getLength()+1 &&
355 0 : msMacroName.copy(0,rDoc.getLength()).equalsIgnoreAsciiCase( rDoc ) &&
356 0 : ':' == msMacroName[rDoc.getLength()] )
357 : {
358 0 : sLibrary = rDoc;
359 0 : msMacroName = msMacroName.copy( rDoc.getLength()+1 );
360 : }
361 :
362 0 : pProperties->Name = "EventType";
363 0 : pProperties->Handle = -1;
364 0 : pProperties->Value <<= OUString( "StarBasic" );
365 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
366 0 : pProperties++;
367 :
368 0 : pProperties->Name = "MacroName";
369 0 : pProperties->Handle = -1;
370 0 : pProperties->Value <<= msMacroName;
371 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
372 0 : pProperties++;
373 :
374 0 : pProperties->Name = "Library";
375 0 : pProperties->Handle = -1;
376 0 : pProperties->Value <<= sLibrary;
377 0 : pProperties->State = beans::PropertyState_DIRECT_VALUE;
378 : }
379 : else
380 : {
381 0 : pProperties->Name =
382 0 : OUString( "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: */
|