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