merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / uno / unoevent.cxx
blob4bc0addccfcee0c346049eca6b447419968f2f97
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unoevent.cxx,v $
10 * $Revision: 1.13.136.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #endif
37 #include <rtl/ustrbuf.hxx>
40 #include <tools/rtti.hxx>
41 #include <tools/solar.h>
42 #include "unoevent.hxx"
43 #include <svtools/macitem.hxx>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
48 using ::com::sun::star::container::NoSuchElementException;
49 using ::com::sun::star::container::XNameReplace;
50 using ::com::sun::star::lang::IllegalArgumentException;
51 using ::com::sun::star::lang::WrappedTargetException;
52 using ::com::sun::star::lang::XServiceInfo;
53 using ::com::sun::star::beans::PropertyValue;
54 using ::cppu::WeakImplHelper2;
55 using ::rtl::OUString;
56 using ::rtl::OUStringBuffer;
59 const sal_Char sAPI_ServiceName[] = "com.sun.star.container.XNameReplace";
60 const sal_Char sAPI_SvDetachedEventDescriptor[] = "SvDetachedEventDescriptor";
63 // SvBaseEventDescriptor
66 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
67 sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
68 sMacroName(RTL_CONSTASCII_USTRINGPARAM("MacroName")),
69 sLibrary(RTL_CONSTASCII_USTRINGPARAM("Library")),
70 sStarBasic(RTL_CONSTASCII_USTRINGPARAM("StarBasic")),
71 sJavaScript(RTL_CONSTASCII_USTRINGPARAM("JavaScript")),
72 sScript(RTL_CONSTASCII_USTRINGPARAM("Script")),
73 sNone(RTL_CONSTASCII_USTRINGPARAM("None")),
74 sServiceName(RTL_CONSTASCII_USTRINGPARAM(sAPI_ServiceName)),
75 sEmpty(),
76 mpSupportedMacroItems(pSupportedMacroItems),
77 mnMacroItems(0)
79 DBG_ASSERT(pSupportedMacroItems != NULL, "Need a list of supported events!");
81 for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != 0; mnMacroItems++) ;
85 SvBaseEventDescriptor::~SvBaseEventDescriptor()
89 void SvBaseEventDescriptor::replaceByName(
90 const OUString& rName,
91 const Any& rElement )
92 throw(
93 IllegalArgumentException,
94 NoSuchElementException,
95 WrappedTargetException,
96 RuntimeException)
98 sal_uInt16 nMacroID = getMacroID(rName);
100 // error checking
101 if (0 == nMacroID)
102 throw NoSuchElementException();
103 if (rElement.getValueType() != getElementType())
104 throw IllegalArgumentException();
106 // get sequence
107 Sequence<PropertyValue> aSequence;
108 rElement >>= aSequence;
110 // perform replace (in subclass)
111 SvxMacro aMacro(sEmpty,sEmpty);
112 getMacroFromAny(aMacro, rElement);
113 replaceByName(nMacroID, aMacro);
116 Any SvBaseEventDescriptor::getByName(
117 const OUString& rName )
118 throw(
119 NoSuchElementException,
120 WrappedTargetException,
121 RuntimeException)
123 sal_uInt16 nMacroID = getMacroID(rName);
125 // error checking
126 if (0 == nMacroID)
127 throw NoSuchElementException();
129 // perform get (in subclass)
130 Any aAny;
131 SvxMacro aMacro( sEmpty, sEmpty );
132 getByName(aMacro, nMacroID);
133 getAnyFromMacro(aAny, aMacro);
134 return aAny;
137 Sequence<OUString> SvBaseEventDescriptor::getElementNames()
138 throw(RuntimeException)
140 // create and fill sequence
141 Sequence<OUString> aSequence(mnMacroItems);
142 for( sal_Int16 i = 0; i < mnMacroItems; i++)
144 aSequence[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
147 return aSequence;
150 sal_Bool SvBaseEventDescriptor::hasByName(
151 const OUString& rName )
152 throw(RuntimeException)
154 sal_uInt16 nMacroID = getMacroID(rName);
155 return (nMacroID != 0);
158 Type SvBaseEventDescriptor::getElementType()
159 throw(RuntimeException)
161 return ::getCppuType((Sequence<PropertyValue> *)0);
164 sal_Bool SvBaseEventDescriptor::hasElements()
165 throw(RuntimeException)
167 return mnMacroItems != 0;
170 sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
171 throw(RuntimeException)
173 return sServiceName.equals(rServiceName);
176 Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames(void)
177 throw(RuntimeException)
179 Sequence<OUString> aSequence(1);
180 aSequence[0] = sServiceName;
182 return aSequence;
185 sal_uInt16 SvBaseEventDescriptor::mapNameToEventID(const OUString& rName) const
187 // iterate over known event names
188 for(sal_Int16 i = 0; i < mnMacroItems; i++)
190 if (0 == rName.compareToAscii(mpSupportedMacroItems[i].mpEventName))
192 return mpSupportedMacroItems[i].mnEvent;
196 // not found -> return zero
197 return 0;
200 OUString SvBaseEventDescriptor::mapEventIDToName(sal_uInt16 nPoolID) const
202 // iterate over known event IDs
203 for(sal_Int16 i = 0; i < mnMacroItems; i++)
205 if (nPoolID == mpSupportedMacroItems[i].mnEvent)
207 return OUString::createFromAscii(mpSupportedMacroItems[i].mpEventName);
211 // not found -> return empty string
212 return OUString();
215 sal_uInt16 SvBaseEventDescriptor::getMacroID(const OUString& rName) const
217 return mapNameToEventID(rName);
220 void SvBaseEventDescriptor::getAnyFromMacro(Any& rAny,
221 const SvxMacro& rMacro)
223 sal_Bool bRetValueOK = sal_False; // do we have a ret value?
225 if (rMacro.HasMacro())
227 switch (rMacro.GetScriptType())
229 case STARBASIC:
231 // create sequence
232 Sequence<PropertyValue> aSequence(3);
233 Any aTmp;
235 // create type
236 PropertyValue aTypeValue;
237 aTypeValue.Name = sEventType;
238 aTmp <<= sStarBasic;
239 aTypeValue.Value = aTmp;
240 aSequence[0] = aTypeValue;
242 // macro name
243 PropertyValue aNameValue;
244 aNameValue.Name = sMacroName;
245 OUString sNameTmp(rMacro.GetMacName());
246 aTmp <<= sNameTmp;
247 aNameValue.Value = aTmp;
248 aSequence[1] = aNameValue;
250 // library name
251 PropertyValue aLibValue;
252 aLibValue.Name = sLibrary;
253 OUString sLibTmp(rMacro.GetLibName());
254 aTmp <<= sLibTmp;
255 aLibValue.Value = aTmp;
256 aSequence[2] = aLibValue;
258 rAny <<= aSequence;
259 bRetValueOK = sal_True;
260 break;
262 case EXTENDED_STYPE:
264 // create sequence
265 Sequence<PropertyValue> aSequence(2);
266 Any aTmp;
268 // create type
269 PropertyValue aTypeValue;
270 aTypeValue.Name = sEventType;
271 aTmp <<= sScript;
272 aTypeValue.Value = aTmp;
273 aSequence[0] = aTypeValue;
275 // macro name
276 PropertyValue aNameValue;
277 aNameValue.Name = sScript;
278 OUString sNameTmp(rMacro.GetMacName());
279 aTmp <<= sNameTmp;
280 aNameValue.Value = aTmp;
281 aSequence[1] = aNameValue;
283 rAny <<= aSequence;
284 bRetValueOK = sal_True;
285 break;
287 case JAVASCRIPT:
288 default:
289 DBG_ERROR("not implemented");
292 // else: bRetValueOK not set
294 // if we don't have a return value, make an empty one
295 if (! bRetValueOK)
297 // create "None" macro
298 Sequence<PropertyValue> aSequence(1);
300 PropertyValue aKindValue;
301 aKindValue.Name = sEventType;
302 Any aTmp;
303 aTmp <<= sNone;
304 aKindValue.Value = aTmp;
305 aSequence[0] = aKindValue;
307 rAny <<= aSequence;
308 bRetValueOK = sal_True;
313 void SvBaseEventDescriptor::getMacroFromAny(
314 SvxMacro& rMacro,
315 const Any& rAny)
316 throw ( IllegalArgumentException )
318 // get sequence
319 Sequence<PropertyValue> aSequence;
320 rAny >>= aSequence;
322 // process ...
323 sal_Bool bTypeOK = sal_False;
324 sal_Bool bNone = sal_False; // true if EventType=="None"
325 enum ScriptType eType = EXTENDED_STYPE;
326 OUString sScriptVal;
327 OUString sMacroVal;
328 OUString sLibVal;
329 sal_Int32 nCount = aSequence.getLength();
330 for (sal_Int32 i = 0; i < nCount; i++)
332 PropertyValue& aValue = aSequence[i];
333 if (aValue.Name.equals(sEventType))
335 OUString sTmp;
336 aValue.Value >>= sTmp;
337 if (sTmp.equals(sStarBasic))
339 eType = STARBASIC;
340 bTypeOK = sal_True;
342 else if (sTmp.equals(sJavaScript))
344 eType = JAVASCRIPT;
345 bTypeOK = sal_True;
347 else if (sTmp.equals(sScript))
349 eType = EXTENDED_STYPE;
350 bTypeOK = sal_True;
352 else if (sTmp.equals(sNone))
354 bNone = sal_True;
355 bTypeOK = sal_True;
357 // else: unknown script type
359 else if (aValue.Name.equals(sMacroName))
361 aValue.Value >>= sMacroVal;
363 else if (aValue.Name.equals(sLibrary))
365 aValue.Value >>= sLibVal;
367 else if (aValue.Name.equals(sScript))
369 aValue.Value >>= sScriptVal;
371 // else: unknown PropertyValue -> ignore
374 if (bTypeOK)
376 if (bNone)
378 // return empty macro
379 rMacro = SvxMacro( sEmpty, sEmpty );
381 else
383 if (eType == STARBASIC)
385 // create macro and return
386 SvxMacro aMacro(sMacroVal, sLibVal, eType);
387 rMacro = aMacro;
389 else if (eType == EXTENDED_STYPE)
391 SvxMacro aMacro(sScriptVal, sScript);
392 rMacro = aMacro;
394 else
396 // we can't process type: abort
397 // TODO: JavaScript macros
398 throw IllegalArgumentException();
402 else
404 // no valid type: abort
405 throw IllegalArgumentException();
413 // SvEventDescriptor
417 SvEventDescriptor::SvEventDescriptor(
418 XInterface& rParent,
419 const SvEventDescription* pSupportedMacroItems) :
420 SvBaseEventDescriptor(pSupportedMacroItems),
421 xParentRef(&rParent)
426 SvEventDescriptor::~SvEventDescriptor()
428 // automatically release xParentRef !
431 void SvEventDescriptor::replaceByName(
432 const sal_uInt16 nEvent,
433 const SvxMacro& rMacro)
434 throw(
435 IllegalArgumentException,
436 NoSuchElementException,
437 WrappedTargetException,
438 RuntimeException)
440 SvxMacroItem aItem(getMacroItemWhich());
441 aItem.SetMacroTable(getMacroItem().GetMacroTable());
442 aItem.SetMacro(nEvent, rMacro);
443 setMacroItem(aItem);
446 void SvEventDescriptor::getByName(
447 SvxMacro& rMacro,
448 const sal_uInt16 nEvent )
449 throw(
450 NoSuchElementException,
451 WrappedTargetException,
452 RuntimeException)
454 const SvxMacroItem& rItem = getMacroItem();
455 if( rItem.HasMacro( nEvent ) )
456 rMacro = rItem.GetMacro(nEvent);
457 else
459 SvxMacro aEmptyMacro(sEmpty, sEmpty);
460 rMacro = aEmptyMacro;
468 // SvDetachedEventDescriptor
471 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
472 const SvEventDescription* pSupportedMacroItems) :
473 SvBaseEventDescriptor(pSupportedMacroItems),
474 sImplName(RTL_CONSTASCII_USTRINGPARAM(sAPI_SvDetachedEventDescriptor))
476 // allocate aMacros
477 aMacros = new SvxMacro*[mnMacroItems];
479 // ... and initialize
480 for(sal_Int16 i = 0; i < mnMacroItems; i++)
482 aMacros[i] = NULL;
486 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
488 // delete contents of aMacros
489 for(sal_Int16 i = 0; i < mnMacroItems; i++)
491 if (NULL != aMacros[i])
492 delete aMacros[i];
495 delete [] aMacros;
498 sal_Int16 SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID) const
500 // iterate over supported events
501 sal_Int16 nIndex = 0;
502 while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
503 (mpSupportedMacroItems[nIndex].mnEvent != 0) )
505 nIndex++;
507 return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
510 OUString SvDetachedEventDescriptor::getImplementationName()
511 throw( ::com::sun::star::uno::RuntimeException )
513 return sImplName;
517 void SvDetachedEventDescriptor::replaceByName(
518 const sal_uInt16 nEvent,
519 const SvxMacro& rMacro)
520 throw(
521 IllegalArgumentException,
522 NoSuchElementException,
523 WrappedTargetException,
524 RuntimeException)
526 sal_Int16 nIndex = getIndex(nEvent);
527 if (-1 == nIndex)
528 throw IllegalArgumentException();
530 aMacros[nIndex] = new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
531 rMacro.GetScriptType() );
535 void SvDetachedEventDescriptor::getByName(
536 SvxMacro& rMacro,
537 const sal_uInt16 nEvent )
538 throw(
539 NoSuchElementException,
540 WrappedTargetException,
541 RuntimeException)
543 sal_Int16 nIndex = getIndex(nEvent);
544 if (-1 == nIndex )
545 throw NoSuchElementException();
547 if( aMacros[nIndex] )
548 rMacro = (*aMacros[nIndex]);
551 sal_Bool SvDetachedEventDescriptor::hasByName(
552 const sal_uInt16 nEvent ) const /// item ID of event
553 throw(IllegalArgumentException)
555 sal_Int16 nIndex = getIndex(nEvent);
556 if (-1 == nIndex)
557 throw IllegalArgumentException();
559 return (NULL == aMacros[nIndex]) ? sal_False : aMacros[nIndex]->HasMacro();
564 // SvMacroTableEventDescriptor
567 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
568 SvDetachedEventDescriptor(pSupportedMacroItems)
572 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
573 const SvxMacroTableDtor& rMacroTable,
574 const SvEventDescription* pSupportedMacroItems) :
575 SvDetachedEventDescriptor(pSupportedMacroItems)
577 copyMacrosFromTable(rMacroTable);
580 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
584 void SvMacroTableEventDescriptor::copyMacrosFromTable(
585 const SvxMacroTableDtor& rMacroTable)
587 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
589 const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
590 const SvxMacro* pMacro = rMacroTable.Get(nEvent);
591 if (NULL != pMacro)
592 replaceByName(nEvent, *pMacro);
597 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
598 SvxMacroTableDtor& rMacroTable)
600 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
602 const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
603 if (hasByName(nEvent))
605 SvxMacro* pMacro = new SvxMacro(sEmpty, sEmpty);
606 getByName(*pMacro, nEvent);
607 rMacroTable.Insert(nEvent, pMacro);