GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svtools / source / uno / unoevent.cxx
blob9f02391f117bfaf95c84d794740cd09979738057
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 .
21 #include <com/sun/star/beans/PropertyValue.hpp>
22 #include <rtl/ustrbuf.hxx>
23 #include <tools/rtti.hxx>
24 #include <tools/solar.h>
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
47 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
48 sEventType("EventType"),
49 sMacroName("MacroName"),
50 sLibrary("Library"),
51 sStarBasic("StarBasic"),
52 sJavaScript("JavaScript"),
53 sScript("Script"),
54 sNone("None"),
55 sServiceName(sAPI_ServiceName),
56 sEmpty(),
57 mpSupportedMacroItems(pSupportedMacroItems),
58 mnMacroItems(0)
60 DBG_ASSERT(pSupportedMacroItems != NULL, "Need a list of supported events!");
62 for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != 0; mnMacroItems++) ;
66 SvBaseEventDescriptor::~SvBaseEventDescriptor()
70 void SvBaseEventDescriptor::replaceByName(
71 const OUString& rName,
72 const Any& rElement )
73 throw(
74 IllegalArgumentException,
75 NoSuchElementException,
76 WrappedTargetException,
77 RuntimeException)
79 sal_uInt16 nMacroID = getMacroID(rName);
81 // error checking
82 if (0 == nMacroID)
83 throw NoSuchElementException();
84 if (rElement.getValueType() != getElementType())
85 throw IllegalArgumentException();
87 // get sequence
88 Sequence<PropertyValue> aSequence;
89 rElement >>= aSequence;
91 // perform replace (in subclass)
92 SvxMacro aMacro(sEmpty,sEmpty);
93 getMacroFromAny(aMacro, rElement);
94 replaceByName(nMacroID, aMacro);
97 Any SvBaseEventDescriptor::getByName(
98 const OUString& rName )
99 throw(
100 NoSuchElementException,
101 WrappedTargetException,
102 RuntimeException)
104 sal_uInt16 nMacroID = getMacroID(rName);
106 // error checking
107 if (0 == nMacroID)
108 throw NoSuchElementException();
110 // perform get (in subclass)
111 Any aAny;
112 SvxMacro aMacro( sEmpty, sEmpty );
113 getByName(aMacro, nMacroID);
114 getAnyFromMacro(aAny, aMacro);
115 return aAny;
118 Sequence<OUString> SvBaseEventDescriptor::getElementNames()
119 throw(RuntimeException)
121 // create and fill sequence
122 Sequence<OUString> aSequence(mnMacroItems);
123 for( sal_Int16 i = 0; i < mnMacroItems; i++)
125 aSequence[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
128 return aSequence;
131 sal_Bool SvBaseEventDescriptor::hasByName(
132 const OUString& rName )
133 throw(RuntimeException)
135 sal_uInt16 nMacroID = getMacroID(rName);
136 return (nMacroID != 0);
139 Type SvBaseEventDescriptor::getElementType()
140 throw(RuntimeException)
142 return ::getCppuType((Sequence<PropertyValue> *)0);
145 sal_Bool SvBaseEventDescriptor::hasElements()
146 throw(RuntimeException)
148 return mnMacroItems != 0;
151 sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
152 throw(RuntimeException)
154 return sServiceName.equals(rServiceName);
157 Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames(void)
158 throw(RuntimeException)
160 Sequence<OUString> aSequence(1);
161 aSequence[0] = sServiceName;
163 return aSequence;
166 sal_uInt16 SvBaseEventDescriptor::mapNameToEventID(const OUString& rName) const
168 // iterate over known event names
169 for(sal_Int16 i = 0; i < mnMacroItems; i++)
171 if( rName.equalsAscii(mpSupportedMacroItems[i].mpEventName))
173 return mpSupportedMacroItems[i].mnEvent;
177 // not found -> return zero
178 return 0;
181 sal_uInt16 SvBaseEventDescriptor::getMacroID(const OUString& rName) const
183 return mapNameToEventID(rName);
186 void SvBaseEventDescriptor::getAnyFromMacro(Any& rAny,
187 const SvxMacro& rMacro)
189 sal_Bool bRetValueOK = sal_False; // do we have a ret value?
191 if (rMacro.HasMacro())
193 switch (rMacro.GetScriptType())
195 case STARBASIC:
197 // create sequence
198 Sequence<PropertyValue> aSequence(3);
199 Any aTmp;
201 // create type
202 PropertyValue aTypeValue;
203 aTypeValue.Name = sEventType;
204 aTmp <<= sStarBasic;
205 aTypeValue.Value = aTmp;
206 aSequence[0] = aTypeValue;
208 // macro name
209 PropertyValue aNameValue;
210 aNameValue.Name = sMacroName;
211 OUString sNameTmp(rMacro.GetMacName());
212 aTmp <<= sNameTmp;
213 aNameValue.Value = aTmp;
214 aSequence[1] = aNameValue;
216 // library name
217 PropertyValue aLibValue;
218 aLibValue.Name = sLibrary;
219 OUString sLibTmp(rMacro.GetLibName());
220 aTmp <<= sLibTmp;
221 aLibValue.Value = aTmp;
222 aSequence[2] = aLibValue;
224 rAny <<= aSequence;
225 bRetValueOK = sal_True;
226 break;
228 case EXTENDED_STYPE:
230 // create sequence
231 Sequence<PropertyValue> aSequence(2);
232 Any aTmp;
234 // create type
235 PropertyValue aTypeValue;
236 aTypeValue.Name = sEventType;
237 aTmp <<= sScript;
238 aTypeValue.Value = aTmp;
239 aSequence[0] = aTypeValue;
241 // macro name
242 PropertyValue aNameValue;
243 aNameValue.Name = sScript;
244 OUString sNameTmp(rMacro.GetMacName());
245 aTmp <<= sNameTmp;
246 aNameValue.Value = aTmp;
247 aSequence[1] = aNameValue;
249 rAny <<= aSequence;
250 bRetValueOK = sal_True;
251 break;
253 case JAVASCRIPT:
254 default:
255 OSL_FAIL("not implemented");
258 // else: bRetValueOK not set
260 // if we don't have a return value, make an empty one
261 if (! bRetValueOK)
263 // create "None" macro
264 Sequence<PropertyValue> aSequence(1);
266 PropertyValue aKindValue;
267 aKindValue.Name = sEventType;
268 Any aTmp;
269 aTmp <<= sNone;
270 aKindValue.Value = aTmp;
271 aSequence[0] = aKindValue;
273 rAny <<= aSequence;
274 bRetValueOK = sal_True;
279 void SvBaseEventDescriptor::getMacroFromAny(
280 SvxMacro& rMacro,
281 const Any& rAny)
282 throw ( IllegalArgumentException )
284 // get sequence
285 Sequence<PropertyValue> aSequence;
286 rAny >>= aSequence;
288 // process ...
289 sal_Bool bTypeOK = sal_False;
290 sal_Bool bNone = sal_False; // true if EventType=="None"
291 enum ScriptType eType = EXTENDED_STYPE;
292 OUString sScriptVal;
293 OUString sMacroVal;
294 OUString sLibVal;
295 sal_Int32 nCount = aSequence.getLength();
296 for (sal_Int32 i = 0; i < nCount; i++)
298 PropertyValue& aValue = aSequence[i];
299 if (aValue.Name.equals(sEventType))
301 OUString sTmp;
302 aValue.Value >>= sTmp;
303 if (sTmp.equals(sStarBasic))
305 eType = STARBASIC;
306 bTypeOK = sal_True;
308 else if (sTmp.equals(sJavaScript))
310 eType = JAVASCRIPT;
311 bTypeOK = sal_True;
313 else if (sTmp.equals(sScript))
315 eType = EXTENDED_STYPE;
316 bTypeOK = sal_True;
318 else if (sTmp.equals(sNone))
320 bNone = sal_True;
321 bTypeOK = sal_True;
323 // else: unknown script type
325 else if (aValue.Name.equals(sMacroName))
327 aValue.Value >>= sMacroVal;
329 else if (aValue.Name.equals(sLibrary))
331 aValue.Value >>= sLibVal;
333 else if (aValue.Name.equals(sScript))
335 aValue.Value >>= sScriptVal;
337 // else: unknown PropertyValue -> ignore
340 if (bTypeOK)
342 if (bNone)
344 // return empty macro
345 rMacro = SvxMacro( sEmpty, sEmpty );
347 else
349 if (eType == STARBASIC)
351 // create macro and return
352 SvxMacro aMacro(sMacroVal, sLibVal, eType);
353 rMacro = aMacro;
355 else if (eType == EXTENDED_STYPE)
357 SvxMacro aMacro(sScriptVal, sScript);
358 rMacro = aMacro;
360 else
362 // we can't process type: abort
363 // TODO: JavaScript macros
364 throw IllegalArgumentException();
368 else
370 // no valid type: abort
371 throw IllegalArgumentException();
379 // SvEventDescriptor
383 SvEventDescriptor::SvEventDescriptor(
384 XInterface& rParent,
385 const SvEventDescription* pSupportedMacroItems) :
386 SvBaseEventDescriptor(pSupportedMacroItems),
387 xParentRef(&rParent)
392 SvEventDescriptor::~SvEventDescriptor()
394 // automatically release xParentRef !
397 void SvEventDescriptor::replaceByName(
398 const sal_uInt16 nEvent,
399 const SvxMacro& rMacro)
400 throw(
401 IllegalArgumentException,
402 NoSuchElementException,
403 WrappedTargetException,
404 RuntimeException)
406 SvxMacroItem aItem(getMacroItemWhich());
407 aItem.SetMacroTable(getMacroItem().GetMacroTable());
408 aItem.SetMacro(nEvent, rMacro);
409 setMacroItem(aItem);
412 void SvEventDescriptor::getByName(
413 SvxMacro& rMacro,
414 const sal_uInt16 nEvent )
415 throw(
416 NoSuchElementException,
417 WrappedTargetException,
418 RuntimeException)
420 const SvxMacroItem& rItem = getMacroItem();
421 if( rItem.HasMacro( nEvent ) )
422 rMacro = rItem.GetMacro(nEvent);
423 else
425 SvxMacro aEmptyMacro(sEmpty, sEmpty);
426 rMacro = aEmptyMacro;
434 // SvDetachedEventDescriptor
437 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
438 const SvEventDescription* pSupportedMacroItems) :
439 SvBaseEventDescriptor(pSupportedMacroItems),
440 sImplName(sAPI_SvDetachedEventDescriptor)
442 // allocate aMacros
443 aMacros = new SvxMacro*[mnMacroItems];
445 // ... and initialize
446 for(sal_Int16 i = 0; i < mnMacroItems; i++)
448 aMacros[i] = NULL;
452 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
454 // delete contents of aMacros
455 for(sal_Int16 i = 0; i < mnMacroItems; i++)
457 if (NULL != aMacros[i])
458 delete aMacros[i];
461 delete [] aMacros;
464 sal_Int16 SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID) const
466 // iterate over supported events
467 sal_Int16 nIndex = 0;
468 while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
469 (mpSupportedMacroItems[nIndex].mnEvent != 0) )
471 nIndex++;
473 return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
476 OUString SvDetachedEventDescriptor::getImplementationName()
477 throw( ::com::sun::star::uno::RuntimeException )
479 return sImplName;
483 void SvDetachedEventDescriptor::replaceByName(
484 const sal_uInt16 nEvent,
485 const SvxMacro& rMacro)
486 throw(
487 IllegalArgumentException,
488 NoSuchElementException,
489 WrappedTargetException,
490 RuntimeException)
492 sal_Int16 nIndex = getIndex(nEvent);
493 if (-1 == nIndex)
494 throw IllegalArgumentException();
496 aMacros[nIndex] = new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
497 rMacro.GetScriptType() );
501 void SvDetachedEventDescriptor::getByName(
502 SvxMacro& rMacro,
503 const sal_uInt16 nEvent )
504 throw(
505 NoSuchElementException,
506 WrappedTargetException,
507 RuntimeException)
509 sal_Int16 nIndex = getIndex(nEvent);
510 if (-1 == nIndex )
511 throw NoSuchElementException();
513 if( aMacros[nIndex] )
514 rMacro = (*aMacros[nIndex]);
517 sal_Bool SvDetachedEventDescriptor::hasByName(
518 const sal_uInt16 nEvent ) const /// item ID of event
519 throw(IllegalArgumentException)
521 sal_Int16 nIndex = getIndex(nEvent);
522 if (-1 == nIndex)
523 throw IllegalArgumentException();
525 return (NULL == aMacros[nIndex]) ? sal_False : aMacros[nIndex]->HasMacro();
530 // SvMacroTableEventDescriptor
533 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
534 SvDetachedEventDescriptor(pSupportedMacroItems)
538 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
539 const SvxMacroTableDtor& rMacroTable,
540 const SvEventDescription* pSupportedMacroItems) :
541 SvDetachedEventDescriptor(pSupportedMacroItems)
543 copyMacrosFromTable(rMacroTable);
546 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
550 void SvMacroTableEventDescriptor::copyMacrosFromTable(
551 const SvxMacroTableDtor& rMacroTable)
553 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
555 const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
556 const SvxMacro* pMacro = rMacroTable.Get(nEvent);
557 if (NULL != pMacro)
558 replaceByName(nEvent, *pMacro);
563 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
564 SvxMacroTableDtor& rMacroTable)
566 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
568 const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
569 if (hasByName(nEvent))
571 SvxMacro& rMacro = rMacroTable.Insert(nEvent, SvxMacro(sEmpty, sEmpty));
572 getByName(rMacro, nEvent);
579 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */