Bump version to 5.0-14
[LibreOffice.git] / svtools / source / uno / unoevent.cxx
blobb1baf3e903d711d5dbc22def2f5164ccc416e4d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/beans/PropertyValue.hpp>
21 #include <cppuhelper/supportsservice.hxx>
22 #include <osl/diagnose.h>
23 #include <rtl/ustrbuf.hxx>
24 #include <tools/rtti.hxx>
25 #include <svtools/unoevent.hxx>
26 #include <svl/macitem.hxx>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
31 using ::com::sun::star::container::NoSuchElementException;
32 using ::com::sun::star::container::XNameReplace;
33 using ::com::sun::star::lang::IllegalArgumentException;
34 using ::com::sun::star::lang::WrappedTargetException;
35 using ::com::sun::star::lang::XServiceInfo;
36 using ::com::sun::star::beans::PropertyValue;
37 using ::cppu::WeakImplHelper2;
40 const sal_Char sAPI_ServiceName[] = "com.sun.star.container.XNameReplace";
41 const sal_Char sAPI_SvDetachedEventDescriptor[] = "SvDetachedEventDescriptor";
44 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
45 sEventType("EventType"),
46 sMacroName("MacroName"),
47 sLibrary("Library"),
48 sStarBasic("StarBasic"),
49 sJavaScript("JavaScript"),
50 sScript("Script"),
51 sNone("None"),
52 sServiceName(sAPI_ServiceName),
53 sEmpty(),
54 mpSupportedMacroItems(pSupportedMacroItems),
55 mnMacroItems(0)
57 assert(pSupportedMacroItems != NULL && "Need a list of supported events!");
59 for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != 0; mnMacroItems++) ;
63 SvBaseEventDescriptor::~SvBaseEventDescriptor()
67 void SvBaseEventDescriptor::replaceByName(
68 const OUString& rName,
69 const Any& rElement )
70 throw(
71 IllegalArgumentException,
72 NoSuchElementException,
73 WrappedTargetException,
74 RuntimeException, std::exception)
76 sal_uInt16 nMacroID = getMacroID(rName);
78 // error checking
79 if (0 == nMacroID)
80 throw NoSuchElementException();
81 if (rElement.getValueType() != getElementType())
82 throw IllegalArgumentException();
84 // get sequence
85 Sequence<PropertyValue> aSequence;
86 rElement >>= aSequence;
88 // perform replace (in subclass)
89 SvxMacro aMacro(sEmpty,sEmpty);
90 getMacroFromAny(aMacro, rElement);
91 replaceByName(nMacroID, aMacro);
94 Any SvBaseEventDescriptor::getByName(
95 const OUString& rName )
96 throw(
97 NoSuchElementException,
98 WrappedTargetException,
99 RuntimeException, std::exception)
101 sal_uInt16 nMacroID = getMacroID(rName);
103 // error checking
104 if (0 == nMacroID)
105 throw NoSuchElementException();
107 // perform get (in subclass)
108 Any aAny;
109 SvxMacro aMacro( sEmpty, sEmpty );
110 getByName(aMacro, nMacroID);
111 getAnyFromMacro(aAny, aMacro);
112 return aAny;
115 Sequence<OUString> SvBaseEventDescriptor::getElementNames()
116 throw(RuntimeException, std::exception)
118 // create and fill sequence
119 Sequence<OUString> aSequence(mnMacroItems);
120 for( sal_Int16 i = 0; i < mnMacroItems; i++)
122 aSequence[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
125 return aSequence;
128 sal_Bool SvBaseEventDescriptor::hasByName(
129 const OUString& rName )
130 throw(RuntimeException, std::exception)
132 sal_uInt16 nMacroID = getMacroID(rName);
133 return (nMacroID != 0);
136 Type SvBaseEventDescriptor::getElementType()
137 throw(RuntimeException, std::exception)
139 return cppu::UnoType<Sequence<PropertyValue>>::get();
142 sal_Bool SvBaseEventDescriptor::hasElements()
143 throw(RuntimeException, std::exception)
145 return mnMacroItems != 0;
148 sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
149 throw(RuntimeException, std::exception)
151 return cppu::supportsService(this, rServiceName);
154 Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames()
155 throw(RuntimeException, std::exception)
157 Sequence<OUString> aSequence(1);
158 aSequence[0] = sServiceName;
160 return aSequence;
163 sal_uInt16 SvBaseEventDescriptor::mapNameToEventID(const OUString& rName) const
165 // iterate over known event names
166 for(sal_Int16 i = 0; i < mnMacroItems; i++)
168 if( rName.equalsAscii(mpSupportedMacroItems[i].mpEventName))
170 return mpSupportedMacroItems[i].mnEvent;
174 // not found -> return zero
175 return 0;
178 sal_uInt16 SvBaseEventDescriptor::getMacroID(const OUString& rName) const
180 return mapNameToEventID(rName);
183 void SvBaseEventDescriptor::getAnyFromMacro(Any& rAny,
184 const SvxMacro& rMacro)
186 bool bRetValueOK = false; // do we have a ret value?
188 if (rMacro.HasMacro())
190 switch (rMacro.GetScriptType())
192 case STARBASIC:
194 // create sequence
195 Sequence<PropertyValue> aSequence(3);
196 Any aTmp;
198 // create type
199 PropertyValue aTypeValue;
200 aTypeValue.Name = sEventType;
201 aTmp <<= sStarBasic;
202 aTypeValue.Value = aTmp;
203 aSequence[0] = aTypeValue;
205 // macro name
206 PropertyValue aNameValue;
207 aNameValue.Name = sMacroName;
208 OUString sNameTmp(rMacro.GetMacName());
209 aTmp <<= sNameTmp;
210 aNameValue.Value = aTmp;
211 aSequence[1] = aNameValue;
213 // library name
214 PropertyValue aLibValue;
215 aLibValue.Name = sLibrary;
216 OUString sLibTmp(rMacro.GetLibName());
217 aTmp <<= sLibTmp;
218 aLibValue.Value = aTmp;
219 aSequence[2] = aLibValue;
221 rAny <<= aSequence;
222 bRetValueOK = true;
223 break;
225 case EXTENDED_STYPE:
227 // create sequence
228 Sequence<PropertyValue> aSequence(2);
229 Any aTmp;
231 // create type
232 PropertyValue aTypeValue;
233 aTypeValue.Name = sEventType;
234 aTmp <<= sScript;
235 aTypeValue.Value = aTmp;
236 aSequence[0] = aTypeValue;
238 // macro name
239 PropertyValue aNameValue;
240 aNameValue.Name = sScript;
241 OUString sNameTmp(rMacro.GetMacName());
242 aTmp <<= sNameTmp;
243 aNameValue.Value = aTmp;
244 aSequence[1] = aNameValue;
246 rAny <<= aSequence;
247 bRetValueOK = true;
248 break;
250 case JAVASCRIPT:
251 default:
252 OSL_FAIL("not implemented");
255 // else: bRetValueOK not set
257 // if we don't have a return value, make an empty one
258 if (! bRetValueOK)
260 // create "None" macro
261 Sequence<PropertyValue> aSequence(1);
263 PropertyValue aKindValue;
264 aKindValue.Name = sEventType;
265 Any aTmp;
266 aTmp <<= sNone;
267 aKindValue.Value = aTmp;
268 aSequence[0] = aKindValue;
270 rAny <<= aSequence;
271 bRetValueOK = true;
276 void SvBaseEventDescriptor::getMacroFromAny(
277 SvxMacro& rMacro,
278 const Any& rAny)
279 throw ( IllegalArgumentException )
281 // get sequence
282 Sequence<PropertyValue> aSequence;
283 rAny >>= aSequence;
285 // process ...
286 bool bTypeOK = false;
287 bool bNone = false; // true if EventType=="None"
288 enum ScriptType eType = EXTENDED_STYPE;
289 OUString sScriptVal;
290 OUString sMacroVal;
291 OUString sLibVal;
292 sal_Int32 nCount = aSequence.getLength();
293 for (sal_Int32 i = 0; i < nCount; i++)
295 PropertyValue& aValue = aSequence[i];
296 if (aValue.Name.equals(sEventType))
298 OUString sTmp;
299 aValue.Value >>= sTmp;
300 if (sTmp.equals(sStarBasic))
302 eType = STARBASIC;
303 bTypeOK = true;
305 else if (sTmp.equals(sJavaScript))
307 eType = JAVASCRIPT;
308 bTypeOK = true;
310 else if (sTmp.equals(sScript))
312 eType = EXTENDED_STYPE;
313 bTypeOK = true;
315 else if (sTmp.equals(sNone))
317 bNone = true;
318 bTypeOK = true;
320 // else: unknown script type
322 else if (aValue.Name.equals(sMacroName))
324 aValue.Value >>= sMacroVal;
326 else if (aValue.Name.equals(sLibrary))
328 aValue.Value >>= sLibVal;
330 else if (aValue.Name.equals(sScript))
332 aValue.Value >>= sScriptVal;
334 // else: unknown PropertyValue -> ignore
337 if (bTypeOK)
339 if (bNone)
341 // return empty macro
342 rMacro = SvxMacro( sEmpty, sEmpty );
344 else
346 if (eType == STARBASIC)
348 // create macro and return
349 SvxMacro aMacro(sMacroVal, sLibVal, eType);
350 rMacro = aMacro;
352 else if (eType == EXTENDED_STYPE)
354 SvxMacro aMacro(sScriptVal, sScript);
355 rMacro = aMacro;
357 else
359 // we can't process type: abort
360 // TODO: JavaScript macros
361 throw IllegalArgumentException();
365 else
367 // no valid type: abort
368 throw IllegalArgumentException();
373 SvEventDescriptor::SvEventDescriptor(
374 XInterface& rParent,
375 const SvEventDescription* pSupportedMacroItems) :
376 SvBaseEventDescriptor(pSupportedMacroItems),
377 xParentRef(&rParent)
382 SvEventDescriptor::~SvEventDescriptor()
384 // automatically release xParentRef !
387 void SvEventDescriptor::replaceByName(
388 const sal_uInt16 nEvent,
389 const SvxMacro& rMacro)
390 throw(
391 IllegalArgumentException,
392 NoSuchElementException,
393 WrappedTargetException,
394 RuntimeException)
396 SvxMacroItem aItem(getMacroItemWhich());
397 aItem.SetMacroTable(getMacroItem().GetMacroTable());
398 aItem.SetMacro(nEvent, rMacro);
399 setMacroItem(aItem);
402 void SvEventDescriptor::getByName(
403 SvxMacro& rMacro,
404 const sal_uInt16 nEvent )
405 throw(
406 NoSuchElementException,
407 WrappedTargetException,
408 RuntimeException)
410 const SvxMacroItem& rItem = getMacroItem();
411 if( rItem.HasMacro( nEvent ) )
412 rMacro = rItem.GetMacro(nEvent);
413 else
415 SvxMacro aEmptyMacro(sEmpty, sEmpty);
416 rMacro = aEmptyMacro;
421 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
422 const SvEventDescription* pSupportedMacroItems) :
423 SvBaseEventDescriptor(pSupportedMacroItems),
424 sImplName(sAPI_SvDetachedEventDescriptor)
426 // allocate aMacros
427 aMacros = new SvxMacro*[mnMacroItems];
429 // ... and initialize
430 for(sal_Int16 i = 0; i < mnMacroItems; i++)
432 aMacros[i] = NULL;
436 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
438 // delete contents of aMacros
439 for(sal_Int16 i = 0; i < mnMacroItems; i++)
441 if (NULL != aMacros[i])
442 delete aMacros[i];
445 delete [] aMacros;
448 sal_Int16 SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID) const
450 // iterate over supported events
451 sal_Int16 nIndex = 0;
452 while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
453 (mpSupportedMacroItems[nIndex].mnEvent != 0) )
455 nIndex++;
457 return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
460 OUString SvDetachedEventDescriptor::getImplementationName()
461 throw( ::com::sun::star::uno::RuntimeException, std::exception )
463 return sImplName;
467 void SvDetachedEventDescriptor::replaceByName(
468 const sal_uInt16 nEvent,
469 const SvxMacro& rMacro)
470 throw(
471 IllegalArgumentException,
472 NoSuchElementException,
473 WrappedTargetException,
474 RuntimeException)
476 sal_Int16 nIndex = getIndex(nEvent);
477 if (-1 == nIndex)
478 throw IllegalArgumentException();
480 aMacros[nIndex] = new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
481 rMacro.GetScriptType() );
485 void SvDetachedEventDescriptor::getByName(
486 SvxMacro& rMacro,
487 const sal_uInt16 nEvent )
488 throw(
489 NoSuchElementException,
490 WrappedTargetException,
491 RuntimeException)
493 sal_Int16 nIndex = getIndex(nEvent);
494 if (-1 == nIndex )
495 throw NoSuchElementException();
497 if( aMacros[nIndex] )
498 rMacro = (*aMacros[nIndex]);
501 bool SvDetachedEventDescriptor::hasByName(
502 const sal_uInt16 nEvent ) const /// item ID of event
503 throw(IllegalArgumentException)
505 sal_Int16 nIndex = getIndex(nEvent);
506 if (-1 == nIndex)
507 throw IllegalArgumentException();
509 return (NULL != aMacros[nIndex]) && aMacros[nIndex]->HasMacro();
513 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
514 SvDetachedEventDescriptor(pSupportedMacroItems)
518 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
519 const SvxMacroTableDtor& rMacroTable,
520 const SvEventDescription* pSupportedMacroItems) :
521 SvDetachedEventDescriptor(pSupportedMacroItems)
523 copyMacrosFromTable(rMacroTable);
526 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
530 void SvMacroTableEventDescriptor::copyMacrosFromTable(
531 const SvxMacroTableDtor& rMacroTable)
533 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
535 const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
536 const SvxMacro* pMacro = rMacroTable.Get(nEvent);
537 if (NULL != pMacro)
538 replaceByName(nEvent, *pMacro);
543 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
544 SvxMacroTableDtor& rMacroTable)
546 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
548 const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
549 if (hasByName(nEvent))
551 SvxMacro& rMacro = rMacroTable.Insert(nEvent, SvxMacro(sEmpty, sEmpty));
552 getByName(rMacro, nEvent);
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */