xref: /aoo4110/main/svtools/source/uno/unoevent.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svtools.hxx"
26 
27 #include <com/sun/star/beans/PropertyValue.hpp>
28 #include <rtl/ustrbuf.hxx>
29 #include <tools/rtti.hxx>
30 #include <tools/solar.h>
31 #include <svtools/unoevent.hxx>
32 #include <svl/macitem.hxx>
33 
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 
37 using ::com::sun::star::container::NoSuchElementException;
38 using ::com::sun::star::container::XNameReplace;
39 using ::com::sun::star::lang::IllegalArgumentException;
40 using ::com::sun::star::lang::WrappedTargetException;
41 using ::com::sun::star::lang::XServiceInfo;
42 using ::com::sun::star::beans::PropertyValue;
43 using ::cppu::WeakImplHelper2;
44 using ::rtl::OUString;
45 using ::rtl::OUStringBuffer;
46 
47 
48 const sal_Char sAPI_ServiceName[] = "com.sun.star.container.XNameReplace";
49 const sal_Char sAPI_SvDetachedEventDescriptor[] = "SvDetachedEventDescriptor";
50 
51 //
52 // SvBaseEventDescriptor
53 //
54 
SvBaseEventDescriptor(const SvEventDescription * pSupportedMacroItems)55 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
56 		sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
57 		sMacroName(RTL_CONSTASCII_USTRINGPARAM("MacroName")),
58 		sLibrary(RTL_CONSTASCII_USTRINGPARAM("Library")),
59 		sStarBasic(RTL_CONSTASCII_USTRINGPARAM("StarBasic")),
60 		sJavaScript(RTL_CONSTASCII_USTRINGPARAM("JavaScript")),
61 		sScript(RTL_CONSTASCII_USTRINGPARAM("Script")),
62 		sNone(RTL_CONSTASCII_USTRINGPARAM("None")),
63 		sServiceName(RTL_CONSTASCII_USTRINGPARAM(sAPI_ServiceName)),
64 		sEmpty(),
65 		mpSupportedMacroItems(pSupportedMacroItems),
66 		mnMacroItems(0)
67 {
68 	DBG_ASSERT(pSupportedMacroItems != NULL, "Need a list of supported events!");
69 
70 	for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != 0; mnMacroItems++) ;
71 }
72 
73 
~SvBaseEventDescriptor()74 SvBaseEventDescriptor::~SvBaseEventDescriptor()
75 {
76 }
77 
replaceByName(const OUString & rName,const Any & rElement)78 void SvBaseEventDescriptor::replaceByName(
79 	const OUString& rName,
80 	const Any& rElement )
81 	throw(
82 		IllegalArgumentException,
83 		NoSuchElementException,
84 		WrappedTargetException,
85 		RuntimeException)
86 {
87 	sal_uInt16 nMacroID = getMacroID(rName);
88 
89 	// error checking
90 	if (0 == nMacroID)
91 		throw NoSuchElementException();
92 	if (rElement.getValueType() != getElementType())
93 		throw IllegalArgumentException();
94 
95 	// get sequence
96 	Sequence<PropertyValue> aSequence;
97 	rElement >>= aSequence;
98 
99 	// perform replace (in subclass)
100 	SvxMacro aMacro(sEmpty,sEmpty);
101 	getMacroFromAny(aMacro, rElement);
102 	replaceByName(nMacroID, aMacro);
103 }
104 
getByName(const OUString & rName)105 Any SvBaseEventDescriptor::getByName(
106 	const OUString& rName )
107 	throw(
108 		NoSuchElementException,
109 		WrappedTargetException,
110 		RuntimeException)
111 {
112 	sal_uInt16 nMacroID = getMacroID(rName);
113 
114 	// error checking
115 	if (0 == nMacroID)
116 		throw NoSuchElementException();
117 
118 	// perform get (in subclass)
119 	Any aAny;
120 	SvxMacro aMacro( sEmpty, sEmpty );
121 	getByName(aMacro, nMacroID);
122 	getAnyFromMacro(aAny, aMacro);
123 	return aAny;
124 }
125 
getElementNames()126 Sequence<OUString> SvBaseEventDescriptor::getElementNames()
127 	throw(RuntimeException)
128 {
129 	// create and fill sequence
130 	Sequence<OUString> aSequence(mnMacroItems);
131 	for( sal_Int16 i = 0; i < mnMacroItems; i++)
132 	{
133 		aSequence[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
134 	}
135 
136 	return aSequence;
137 }
138 
hasByName(const OUString & rName)139 sal_Bool SvBaseEventDescriptor::hasByName(
140 	const OUString& rName )
141 	throw(RuntimeException)
142 {
143 	sal_uInt16 nMacroID = getMacroID(rName);
144 	return (nMacroID != 0);
145 }
146 
getElementType()147 Type SvBaseEventDescriptor::getElementType()
148 	throw(RuntimeException)
149 {
150 	return ::getCppuType((Sequence<PropertyValue> *)0);
151 }
152 
hasElements()153 sal_Bool SvBaseEventDescriptor::hasElements()
154 	throw(RuntimeException)
155 {
156 	return mnMacroItems != 0;
157 }
158 
supportsService(const OUString & rServiceName)159 sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
160 	throw(RuntimeException)
161 {
162 	return sServiceName.equals(rServiceName);
163 }
164 
getSupportedServiceNames(void)165 Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames(void)
166 	throw(RuntimeException)
167 {
168 	Sequence<OUString> aSequence(1);
169 	aSequence[0] = sServiceName;
170 
171 	return aSequence;
172 }
173 
mapNameToEventID(const OUString & rName) const174 sal_uInt16 SvBaseEventDescriptor::mapNameToEventID(const OUString& rName) const
175 {
176 	// iterate over known event names
177 	for(sal_Int16 i = 0; i < mnMacroItems; i++)
178 	{
179 		if (0 == rName.compareToAscii(mpSupportedMacroItems[i].mpEventName))
180 		{
181 			return mpSupportedMacroItems[i].mnEvent;
182 		}
183 	}
184 
185 	// not found -> return zero
186 	return 0;
187 }
188 
mapEventIDToName(sal_uInt16 nPoolID) const189 OUString SvBaseEventDescriptor::mapEventIDToName(sal_uInt16 nPoolID) const
190 {
191 	// iterate over known event IDs
192 	for(sal_Int16 i = 0; i < mnMacroItems; i++)
193 	{
194 		if (nPoolID == mpSupportedMacroItems[i].mnEvent)
195 		{
196 			return OUString::createFromAscii(mpSupportedMacroItems[i].mpEventName);
197 		}
198 	}
199 
200 	// not found -> return empty string
201 	return OUString();
202 }
203 
getMacroID(const OUString & rName) const204 sal_uInt16 SvBaseEventDescriptor::getMacroID(const OUString& rName) const
205 {
206 	return mapNameToEventID(rName);
207 }
208 
getAnyFromMacro(Any & rAny,const SvxMacro & rMacro)209 void SvBaseEventDescriptor::getAnyFromMacro(Any& rAny,
210 									   const SvxMacro& rMacro)
211 {
212 	sal_Bool bRetValueOK = sal_False;	// do we have a ret value?
213 
214 	if (rMacro.HasMacro())
215 	{
216 		switch (rMacro.GetScriptType())
217 		{
218 			case STARBASIC:
219 			{
220 				// create sequence
221 				Sequence<PropertyValue> aSequence(3);
222 				Any aTmp;
223 
224 				// create type
225 				PropertyValue aTypeValue;
226 				aTypeValue.Name = sEventType;
227 				aTmp <<= sStarBasic;
228 				aTypeValue.Value = aTmp;
229 				aSequence[0] = aTypeValue;
230 
231 				// macro name
232 				PropertyValue aNameValue;
233 				aNameValue.Name = sMacroName;
234 				OUString sNameTmp(rMacro.GetMacName());
235 				aTmp <<= sNameTmp;
236 				aNameValue.Value = aTmp;
237 				aSequence[1] = aNameValue;
238 
239 				// library name
240 				PropertyValue aLibValue;
241 				aLibValue.Name = sLibrary;
242 				OUString sLibTmp(rMacro.GetLibName());
243 				aTmp <<= sLibTmp;
244 				aLibValue.Value = aTmp;
245 				aSequence[2] = aLibValue;
246 
247 				rAny <<= aSequence;
248 				bRetValueOK = sal_True;
249 				break;
250 			}
251 			case EXTENDED_STYPE:
252 			{
253 				// create sequence
254 				Sequence<PropertyValue> aSequence(2);
255 				Any aTmp;
256 
257 				// create type
258 				PropertyValue aTypeValue;
259 				aTypeValue.Name = sEventType;
260 				aTmp <<= sScript;
261 				aTypeValue.Value = aTmp;
262 				aSequence[0] = aTypeValue;
263 
264 				// macro name
265 				PropertyValue aNameValue;
266 				aNameValue.Name = sScript;
267 				OUString sNameTmp(rMacro.GetMacName());
268 				aTmp <<= sNameTmp;
269 				aNameValue.Value = aTmp;
270 				aSequence[1] = aNameValue;
271 
272 				rAny <<= aSequence;
273 				bRetValueOK = sal_True;
274 				break;
275                         }
276 			case JAVASCRIPT:
277 			default:
278 				DBG_ERROR("not implemented");
279 		}
280 	}
281 	// else: bRetValueOK not set
282 
283 	// if we don't have a return value, make an empty one
284 	if (! bRetValueOK)
285 	{
286 		// create "None" macro
287 		Sequence<PropertyValue> aSequence(1);
288 
289 		PropertyValue aKindValue;
290 		aKindValue.Name = sEventType;
291 		Any aTmp;
292 		aTmp <<= sNone;
293 		aKindValue.Value = aTmp;
294 		aSequence[0] = aKindValue;
295 
296 		rAny <<= aSequence;
297 		bRetValueOK = sal_True;
298 	}
299 }
300 
301 
getMacroFromAny(SvxMacro & rMacro,const Any & rAny)302 void SvBaseEventDescriptor::getMacroFromAny(
303 	SvxMacro& rMacro,
304 	const Any& rAny)
305 		throw ( IllegalArgumentException )
306 {
307 	// get sequence
308 	Sequence<PropertyValue> aSequence;
309 	rAny >>= aSequence;
310 
311 	// process ...
312 	sal_Bool bTypeOK = sal_False;
313 	sal_Bool bNone = sal_False;		// true if EventType=="None"
314 	enum ScriptType eType = EXTENDED_STYPE;
315 	OUString sScriptVal;
316 	OUString sMacroVal;
317 	OUString sLibVal;
318 	sal_Int32 nCount = aSequence.getLength();
319 	for (sal_Int32 i = 0; i < nCount; i++)
320 	{
321 		PropertyValue& aValue = aSequence[i];
322 		if (aValue.Name.equals(sEventType))
323 		{
324 			OUString sTmp;
325 			aValue.Value >>= sTmp;
326 			if (sTmp.equals(sStarBasic))
327 			{
328 				eType = STARBASIC;
329 				bTypeOK = sal_True;
330 			}
331 			else if (sTmp.equals(sJavaScript))
332 			{
333 				eType = JAVASCRIPT;
334 				bTypeOK = sal_True;
335 			}
336 			else if (sTmp.equals(sScript))
337 			{
338 				eType = EXTENDED_STYPE;
339 				bTypeOK = sal_True;
340 			}
341 			else if (sTmp.equals(sNone))
342 			{
343 				bNone = sal_True;
344 				bTypeOK = sal_True;
345 			}
346 			// else: unknown script type
347 		}
348 		else if (aValue.Name.equals(sMacroName))
349 		{
350 			aValue.Value >>= sMacroVal;
351 		}
352 		else if (aValue.Name.equals(sLibrary))
353 		{
354 			aValue.Value >>= sLibVal;
355 		}
356 		else if (aValue.Name.equals(sScript))
357 		{
358 			aValue.Value >>= sScriptVal;
359 		}
360 		// else: unknown PropertyValue -> ignore
361 	}
362 
363 	if (bTypeOK)
364 	{
365 		if (bNone)
366 		{
367 			// return empty macro
368 			rMacro = SvxMacro( sEmpty, sEmpty );
369 		}
370 		else
371 		{
372 			if (eType == STARBASIC)
373 			{
374 				// create macro and return
375 				SvxMacro aMacro(sMacroVal, sLibVal, eType);
376 				rMacro = aMacro;
377 			}
378 			else if (eType == EXTENDED_STYPE)
379 			{
380 				SvxMacro aMacro(sScriptVal, sScript);
381 				rMacro = aMacro;
382 			}
383 			else
384 			{
385 				// we can't process type: abort
386 				// TODO: JavaScript macros
387 				throw IllegalArgumentException();
388 			}
389 		}
390 	}
391 	else
392 	{
393 		// no valid type: abort
394 		throw IllegalArgumentException();
395 	}
396 }
397 
398 
399 
400 
401 //
402 // SvEventDescriptor
403 //
404 
405 
SvEventDescriptor(XInterface & rParent,const SvEventDescription * pSupportedMacroItems)406 SvEventDescriptor::SvEventDescriptor(
407 	XInterface& rParent,
408 	const SvEventDescription* pSupportedMacroItems) :
409 		SvBaseEventDescriptor(pSupportedMacroItems),
410 		xParentRef(&rParent)
411 {
412 }
413 
414 
~SvEventDescriptor()415 SvEventDescriptor::~SvEventDescriptor()
416 {
417 	// automatically release xParentRef !
418 }
419 
replaceByName(const sal_uInt16 nEvent,const SvxMacro & rMacro)420 void SvEventDescriptor::replaceByName(
421 	const sal_uInt16 nEvent,
422 	const SvxMacro& rMacro)
423 		throw(
424 			IllegalArgumentException,
425 			NoSuchElementException,
426 			WrappedTargetException,
427 			RuntimeException)
428 {
429 	SvxMacroItem aItem(getMacroItemWhich());
430 	aItem.SetMacroTable(getMacroItem().GetMacroTable());
431 	aItem.SetMacro(nEvent, rMacro);
432 	setMacroItem(aItem);
433 }
434 
getByName(SvxMacro & rMacro,const sal_uInt16 nEvent)435 void SvEventDescriptor::getByName(
436 	SvxMacro& rMacro,
437 	const sal_uInt16 nEvent )
438 		throw(
439 			NoSuchElementException,
440 			WrappedTargetException,
441 			RuntimeException)
442 {
443 	const SvxMacroItem& rItem = getMacroItem();
444 	if( rItem.HasMacro( nEvent ) )
445 		rMacro = rItem.GetMacro(nEvent);
446 	else
447 	{
448 		SvxMacro aEmptyMacro(sEmpty, sEmpty);
449 		rMacro = aEmptyMacro;
450 	}
451 }
452 
453 
454 
455 
456 //
457 // SvDetachedEventDescriptor
458 //
459 
SvDetachedEventDescriptor(const SvEventDescription * pSupportedMacroItems)460 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
461 	const SvEventDescription* pSupportedMacroItems) :
462 	SvBaseEventDescriptor(pSupportedMacroItems),
463 	sImplName(RTL_CONSTASCII_USTRINGPARAM(sAPI_SvDetachedEventDescriptor))
464 {
465 	// allocate aMacros
466 	aMacros = new SvxMacro*[mnMacroItems];
467 
468 	// ... and initialize
469 	for(sal_Int16 i = 0; i < mnMacroItems; i++)
470 	{
471 		aMacros[i] = NULL;
472 	}
473 }
474 
~SvDetachedEventDescriptor()475 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
476 {
477 	// delete contents of aMacros
478 	for(sal_Int16 i = 0; i < mnMacroItems; i++)
479 	{
480 		if (NULL != aMacros[i])
481 			delete aMacros[i];
482 	}
483 
484     delete [] aMacros;
485 }
486 
getIndex(const sal_uInt16 nID) const487 sal_Int16 SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID) const
488 {
489 	// iterate over supported events
490 	sal_Int16 nIndex = 0;
491 	while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
492 			(mpSupportedMacroItems[nIndex].mnEvent != 0)      )
493 	{
494 		nIndex++;
495 	}
496 	return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
497 }
498 
getImplementationName()499 OUString SvDetachedEventDescriptor::getImplementationName()
500 	throw( ::com::sun::star::uno::RuntimeException )
501 {
502 	return sImplName;
503 }
504 
505 
replaceByName(const sal_uInt16 nEvent,const SvxMacro & rMacro)506 void SvDetachedEventDescriptor::replaceByName(
507 	const sal_uInt16 nEvent,
508 	const SvxMacro& rMacro)
509 	throw(
510 		IllegalArgumentException,
511 		NoSuchElementException,
512 		WrappedTargetException,
513 		RuntimeException)
514 {
515 	sal_Int16 nIndex = getIndex(nEvent);
516 	if (-1 == nIndex)
517 		throw IllegalArgumentException();
518 
519 	aMacros[nIndex] = new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
520 								   rMacro.GetScriptType() );
521 }
522 
523 
getByName(SvxMacro & rMacro,const sal_uInt16 nEvent)524 void SvDetachedEventDescriptor::getByName(
525 	SvxMacro& rMacro,
526 	const sal_uInt16 nEvent )
527 	throw(
528 		NoSuchElementException,
529 		WrappedTargetException,
530 		RuntimeException)
531 {
532 	sal_Int16 nIndex = getIndex(nEvent);
533 	if (-1 == nIndex )
534 		throw NoSuchElementException();
535 
536 	if( aMacros[nIndex] )
537 		rMacro = (*aMacros[nIndex]);
538 }
539 
hasByName(const sal_uInt16 nEvent) const540 sal_Bool SvDetachedEventDescriptor::hasByName(
541 	const sal_uInt16 nEvent ) const		/// item ID of event
542 		throw(IllegalArgumentException)
543 {
544 	sal_Int16 nIndex = getIndex(nEvent);
545 	if (-1 == nIndex)
546 		throw IllegalArgumentException();
547 
548 	return (NULL == aMacros[nIndex]) ? sal_False : aMacros[nIndex]->HasMacro();
549 }
550 
551 
552 //
553 // SvMacroTableEventDescriptor
554 //
555 
SvMacroTableEventDescriptor(const SvEventDescription * pSupportedMacroItems)556 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
557 	SvDetachedEventDescriptor(pSupportedMacroItems)
558 {
559 }
560 
SvMacroTableEventDescriptor(const SvxMacroTableDtor & rMacroTable,const SvEventDescription * pSupportedMacroItems)561 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
562 	const SvxMacroTableDtor& rMacroTable,
563 	const SvEventDescription* pSupportedMacroItems) :
564 		SvDetachedEventDescriptor(pSupportedMacroItems)
565 {
566 	copyMacrosFromTable(rMacroTable);
567 }
568 
~SvMacroTableEventDescriptor()569 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
570 {
571 }
572 
copyMacrosFromTable(const SvxMacroTableDtor & rMacroTable)573 void SvMacroTableEventDescriptor::copyMacrosFromTable(
574 	const SvxMacroTableDtor& rMacroTable)
575 {
576 	for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
577 	{
578 		const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
579 		const SvxMacro* pMacro = rMacroTable.Get(nEvent);
580 		if (NULL != pMacro)
581 			replaceByName(nEvent, *pMacro);
582 	}
583 
584 }
585 
copyMacrosIntoTable(SvxMacroTableDtor & rMacroTable)586 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
587 	SvxMacroTableDtor& rMacroTable)
588 {
589 	for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
590 	{
591 		const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
592 		if (hasByName(nEvent))
593 		{
594 			SvxMacro* pMacro = new SvxMacro(sEmpty, sEmpty);
595 			getByName(*pMacro, nEvent);
596 			rMacroTable.Insert(nEvent, pMacro);
597 		}
598 	}
599 }
600 
601 
602 
603