1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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>
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
)),
76 mpSupportedMacroItems(pSupportedMacroItems
),
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
,
93 IllegalArgumentException
,
94 NoSuchElementException
,
95 WrappedTargetException
,
98 sal_uInt16 nMacroID
= getMacroID(rName
);
102 throw NoSuchElementException();
103 if (rElement
.getValueType() != getElementType())
104 throw IllegalArgumentException();
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
)
119 NoSuchElementException
,
120 WrappedTargetException
,
123 sal_uInt16 nMacroID
= getMacroID(rName
);
127 throw NoSuchElementException();
129 // perform get (in subclass)
131 SvxMacro
aMacro( sEmpty
, sEmpty
);
132 getByName(aMacro
, nMacroID
);
133 getAnyFromMacro(aAny
, aMacro
);
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
);
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
;
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
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
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())
232 Sequence
<PropertyValue
> aSequence(3);
236 PropertyValue aTypeValue
;
237 aTypeValue
.Name
= sEventType
;
239 aTypeValue
.Value
= aTmp
;
240 aSequence
[0] = aTypeValue
;
243 PropertyValue aNameValue
;
244 aNameValue
.Name
= sMacroName
;
245 OUString
sNameTmp(rMacro
.GetMacName());
247 aNameValue
.Value
= aTmp
;
248 aSequence
[1] = aNameValue
;
251 PropertyValue aLibValue
;
252 aLibValue
.Name
= sLibrary
;
253 OUString
sLibTmp(rMacro
.GetLibName());
255 aLibValue
.Value
= aTmp
;
256 aSequence
[2] = aLibValue
;
259 bRetValueOK
= sal_True
;
265 Sequence
<PropertyValue
> aSequence(2);
269 PropertyValue aTypeValue
;
270 aTypeValue
.Name
= sEventType
;
272 aTypeValue
.Value
= aTmp
;
273 aSequence
[0] = aTypeValue
;
276 PropertyValue aNameValue
;
277 aNameValue
.Name
= sScript
;
278 OUString
sNameTmp(rMacro
.GetMacName());
280 aNameValue
.Value
= aTmp
;
281 aSequence
[1] = aNameValue
;
284 bRetValueOK
= sal_True
;
289 DBG_ERROR("not implemented");
292 // else: bRetValueOK not set
294 // if we don't have a return value, make an empty one
297 // create "None" macro
298 Sequence
<PropertyValue
> aSequence(1);
300 PropertyValue aKindValue
;
301 aKindValue
.Name
= sEventType
;
304 aKindValue
.Value
= aTmp
;
305 aSequence
[0] = aKindValue
;
308 bRetValueOK
= sal_True
;
313 void SvBaseEventDescriptor::getMacroFromAny(
316 throw ( IllegalArgumentException
)
319 Sequence
<PropertyValue
> aSequence
;
323 sal_Bool bTypeOK
= sal_False
;
324 sal_Bool bNone
= sal_False
; // true if EventType=="None"
325 enum ScriptType eType
= EXTENDED_STYPE
;
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
))
336 aValue
.Value
>>= sTmp
;
337 if (sTmp
.equals(sStarBasic
))
342 else if (sTmp
.equals(sJavaScript
))
347 else if (sTmp
.equals(sScript
))
349 eType
= EXTENDED_STYPE
;
352 else if (sTmp
.equals(sNone
))
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
378 // return empty macro
379 rMacro
= SvxMacro( sEmpty
, sEmpty
);
383 if (eType
== STARBASIC
)
385 // create macro and return
386 SvxMacro
aMacro(sMacroVal
, sLibVal
, eType
);
389 else if (eType
== EXTENDED_STYPE
)
391 SvxMacro
aMacro(sScriptVal
, sScript
);
396 // we can't process type: abort
397 // TODO: JavaScript macros
398 throw IllegalArgumentException();
404 // no valid type: abort
405 throw IllegalArgumentException();
417 SvEventDescriptor::SvEventDescriptor(
419 const SvEventDescription
* pSupportedMacroItems
) :
420 SvBaseEventDescriptor(pSupportedMacroItems
),
426 SvEventDescriptor::~SvEventDescriptor()
428 // automatically release xParentRef !
431 void SvEventDescriptor::replaceByName(
432 const sal_uInt16 nEvent
,
433 const SvxMacro
& rMacro
)
435 IllegalArgumentException
,
436 NoSuchElementException
,
437 WrappedTargetException
,
440 SvxMacroItem
aItem(getMacroItemWhich());
441 aItem
.SetMacroTable(getMacroItem().GetMacroTable());
442 aItem
.SetMacro(nEvent
, rMacro
);
446 void SvEventDescriptor::getByName(
448 const sal_uInt16 nEvent
)
450 NoSuchElementException
,
451 WrappedTargetException
,
454 const SvxMacroItem
& rItem
= getMacroItem();
455 if( rItem
.HasMacro( nEvent
) )
456 rMacro
= rItem
.GetMacro(nEvent
);
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
))
477 aMacros
= new SvxMacro
*[mnMacroItems
];
479 // ... and initialize
480 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
486 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
488 // delete contents of aMacros
489 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
491 if (NULL
!= aMacros
[i
])
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) )
507 return (mpSupportedMacroItems
[nIndex
].mnEvent
== nID
) ? nIndex
: -1;
510 OUString
SvDetachedEventDescriptor::getImplementationName()
511 throw( ::com::sun::star::uno::RuntimeException
)
517 void SvDetachedEventDescriptor::replaceByName(
518 const sal_uInt16 nEvent
,
519 const SvxMacro
& rMacro
)
521 IllegalArgumentException
,
522 NoSuchElementException
,
523 WrappedTargetException
,
526 sal_Int16 nIndex
= getIndex(nEvent
);
528 throw IllegalArgumentException();
530 aMacros
[nIndex
] = new SvxMacro(rMacro
.GetMacName(), rMacro
.GetLibName(),
531 rMacro
.GetScriptType() );
535 void SvDetachedEventDescriptor::getByName(
537 const sal_uInt16 nEvent
)
539 NoSuchElementException
,
540 WrappedTargetException
,
543 sal_Int16 nIndex
= getIndex(nEvent
);
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
);
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
);
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
);