1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"),
51 sStarBasic("StarBasic"),
52 sJavaScript("JavaScript"),
55 sServiceName(sAPI_ServiceName
),
57 mpSupportedMacroItems(pSupportedMacroItems
),
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
,
74 IllegalArgumentException
,
75 NoSuchElementException
,
76 WrappedTargetException
,
79 sal_uInt16 nMacroID
= getMacroID(rName
);
83 throw NoSuchElementException();
84 if (rElement
.getValueType() != getElementType())
85 throw IllegalArgumentException();
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
)
100 NoSuchElementException
,
101 WrappedTargetException
,
104 sal_uInt16 nMacroID
= getMacroID(rName
);
108 throw NoSuchElementException();
110 // perform get (in subclass)
112 SvxMacro
aMacro( sEmpty
, sEmpty
);
113 getByName(aMacro
, nMacroID
);
114 getAnyFromMacro(aAny
, aMacro
);
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
);
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
;
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
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())
198 Sequence
<PropertyValue
> aSequence(3);
202 PropertyValue aTypeValue
;
203 aTypeValue
.Name
= sEventType
;
205 aTypeValue
.Value
= aTmp
;
206 aSequence
[0] = aTypeValue
;
209 PropertyValue aNameValue
;
210 aNameValue
.Name
= sMacroName
;
211 OUString
sNameTmp(rMacro
.GetMacName());
213 aNameValue
.Value
= aTmp
;
214 aSequence
[1] = aNameValue
;
217 PropertyValue aLibValue
;
218 aLibValue
.Name
= sLibrary
;
219 OUString
sLibTmp(rMacro
.GetLibName());
221 aLibValue
.Value
= aTmp
;
222 aSequence
[2] = aLibValue
;
225 bRetValueOK
= sal_True
;
231 Sequence
<PropertyValue
> aSequence(2);
235 PropertyValue aTypeValue
;
236 aTypeValue
.Name
= sEventType
;
238 aTypeValue
.Value
= aTmp
;
239 aSequence
[0] = aTypeValue
;
242 PropertyValue aNameValue
;
243 aNameValue
.Name
= sScript
;
244 OUString
sNameTmp(rMacro
.GetMacName());
246 aNameValue
.Value
= aTmp
;
247 aSequence
[1] = aNameValue
;
250 bRetValueOK
= sal_True
;
255 OSL_FAIL("not implemented");
258 // else: bRetValueOK not set
260 // if we don't have a return value, make an empty one
263 // create "None" macro
264 Sequence
<PropertyValue
> aSequence(1);
266 PropertyValue aKindValue
;
267 aKindValue
.Name
= sEventType
;
270 aKindValue
.Value
= aTmp
;
271 aSequence
[0] = aKindValue
;
274 bRetValueOK
= sal_True
;
279 void SvBaseEventDescriptor::getMacroFromAny(
282 throw ( IllegalArgumentException
)
285 Sequence
<PropertyValue
> aSequence
;
289 sal_Bool bTypeOK
= sal_False
;
290 sal_Bool bNone
= sal_False
; // true if EventType=="None"
291 enum ScriptType eType
= EXTENDED_STYPE
;
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
))
302 aValue
.Value
>>= sTmp
;
303 if (sTmp
.equals(sStarBasic
))
308 else if (sTmp
.equals(sJavaScript
))
313 else if (sTmp
.equals(sScript
))
315 eType
= EXTENDED_STYPE
;
318 else if (sTmp
.equals(sNone
))
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
344 // return empty macro
345 rMacro
= SvxMacro( sEmpty
, sEmpty
);
349 if (eType
== STARBASIC
)
351 // create macro and return
352 SvxMacro
aMacro(sMacroVal
, sLibVal
, eType
);
355 else if (eType
== EXTENDED_STYPE
)
357 SvxMacro
aMacro(sScriptVal
, sScript
);
362 // we can't process type: abort
363 // TODO: JavaScript macros
364 throw IllegalArgumentException();
370 // no valid type: abort
371 throw IllegalArgumentException();
383 SvEventDescriptor::SvEventDescriptor(
385 const SvEventDescription
* pSupportedMacroItems
) :
386 SvBaseEventDescriptor(pSupportedMacroItems
),
392 SvEventDescriptor::~SvEventDescriptor()
394 // automatically release xParentRef !
397 void SvEventDescriptor::replaceByName(
398 const sal_uInt16 nEvent
,
399 const SvxMacro
& rMacro
)
401 IllegalArgumentException
,
402 NoSuchElementException
,
403 WrappedTargetException
,
406 SvxMacroItem
aItem(getMacroItemWhich());
407 aItem
.SetMacroTable(getMacroItem().GetMacroTable());
408 aItem
.SetMacro(nEvent
, rMacro
);
412 void SvEventDescriptor::getByName(
414 const sal_uInt16 nEvent
)
416 NoSuchElementException
,
417 WrappedTargetException
,
420 const SvxMacroItem
& rItem
= getMacroItem();
421 if( rItem
.HasMacro( nEvent
) )
422 rMacro
= rItem
.GetMacro(nEvent
);
425 SvxMacro
aEmptyMacro(sEmpty
, sEmpty
);
426 rMacro
= aEmptyMacro
;
434 // SvDetachedEventDescriptor
437 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
438 const SvEventDescription
* pSupportedMacroItems
) :
439 SvBaseEventDescriptor(pSupportedMacroItems
),
440 sImplName(sAPI_SvDetachedEventDescriptor
)
443 aMacros
= new SvxMacro
*[mnMacroItems
];
445 // ... and initialize
446 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
452 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
454 // delete contents of aMacros
455 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
457 if (NULL
!= aMacros
[i
])
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) )
473 return (mpSupportedMacroItems
[nIndex
].mnEvent
== nID
) ? nIndex
: -1;
476 OUString
SvDetachedEventDescriptor::getImplementationName()
477 throw( ::com::sun::star::uno::RuntimeException
)
483 void SvDetachedEventDescriptor::replaceByName(
484 const sal_uInt16 nEvent
,
485 const SvxMacro
& rMacro
)
487 IllegalArgumentException
,
488 NoSuchElementException
,
489 WrappedTargetException
,
492 sal_Int16 nIndex
= getIndex(nEvent
);
494 throw IllegalArgumentException();
496 aMacros
[nIndex
] = new SvxMacro(rMacro
.GetMacName(), rMacro
.GetLibName(),
497 rMacro
.GetScriptType() );
501 void SvDetachedEventDescriptor::getByName(
503 const sal_uInt16 nEvent
)
505 NoSuchElementException
,
506 WrappedTargetException
,
509 sal_Int16 nIndex
= getIndex(nEvent
);
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
);
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
);
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: */