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 .
20 #include <com/sun/star/beans/PropertyValue.hpp>
21 #include <cppuhelper/supportsservice.hxx>
22 #include <osl/diagnose.h>
23 #include <rtl/ustrbuf.hxx>
24 #include <tools/rtti.hxx>
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::SvBaseEventDescriptor( const SvEventDescription
* pSupportedMacroItems
) :
45 sEventType("EventType"),
46 sMacroName("MacroName"),
48 sStarBasic("StarBasic"),
49 sJavaScript("JavaScript"),
52 sServiceName(sAPI_ServiceName
),
54 mpSupportedMacroItems(pSupportedMacroItems
),
57 assert(pSupportedMacroItems
!= NULL
&& "Need a list of supported events!");
59 for( ; mpSupportedMacroItems
[mnMacroItems
].mnEvent
!= 0; mnMacroItems
++) ;
63 SvBaseEventDescriptor::~SvBaseEventDescriptor()
67 void SvBaseEventDescriptor::replaceByName(
68 const OUString
& rName
,
71 IllegalArgumentException
,
72 NoSuchElementException
,
73 WrappedTargetException
,
74 RuntimeException
, std::exception
)
76 sal_uInt16 nMacroID
= getMacroID(rName
);
80 throw NoSuchElementException();
81 if (rElement
.getValueType() != getElementType())
82 throw IllegalArgumentException();
85 Sequence
<PropertyValue
> aSequence
;
86 rElement
>>= aSequence
;
88 // perform replace (in subclass)
89 SvxMacro
aMacro(sEmpty
,sEmpty
);
90 getMacroFromAny(aMacro
, rElement
);
91 replaceByName(nMacroID
, aMacro
);
94 Any
SvBaseEventDescriptor::getByName(
95 const OUString
& rName
)
97 NoSuchElementException
,
98 WrappedTargetException
,
99 RuntimeException
, std::exception
)
101 sal_uInt16 nMacroID
= getMacroID(rName
);
105 throw NoSuchElementException();
107 // perform get (in subclass)
109 SvxMacro
aMacro( sEmpty
, sEmpty
);
110 getByName(aMacro
, nMacroID
);
111 getAnyFromMacro(aAny
, aMacro
);
115 Sequence
<OUString
> SvBaseEventDescriptor::getElementNames()
116 throw(RuntimeException
, std::exception
)
118 // create and fill sequence
119 Sequence
<OUString
> aSequence(mnMacroItems
);
120 for( sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
122 aSequence
[i
] = OUString::createFromAscii( mpSupportedMacroItems
[i
].mpEventName
);
128 sal_Bool
SvBaseEventDescriptor::hasByName(
129 const OUString
& rName
)
130 throw(RuntimeException
, std::exception
)
132 sal_uInt16 nMacroID
= getMacroID(rName
);
133 return (nMacroID
!= 0);
136 Type
SvBaseEventDescriptor::getElementType()
137 throw(RuntimeException
, std::exception
)
139 return cppu::UnoType
<Sequence
<PropertyValue
>>::get();
142 sal_Bool
SvBaseEventDescriptor::hasElements()
143 throw(RuntimeException
, std::exception
)
145 return mnMacroItems
!= 0;
148 sal_Bool
SvBaseEventDescriptor::supportsService(const OUString
& rServiceName
)
149 throw(RuntimeException
, std::exception
)
151 return cppu::supportsService(this, rServiceName
);
154 Sequence
<OUString
> SvBaseEventDescriptor::getSupportedServiceNames()
155 throw(RuntimeException
, std::exception
)
157 Sequence
<OUString
> aSequence(1);
158 aSequence
[0] = sServiceName
;
163 sal_uInt16
SvBaseEventDescriptor::mapNameToEventID(const OUString
& rName
) const
165 // iterate over known event names
166 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
168 if( rName
.equalsAscii(mpSupportedMacroItems
[i
].mpEventName
))
170 return mpSupportedMacroItems
[i
].mnEvent
;
174 // not found -> return zero
178 sal_uInt16
SvBaseEventDescriptor::getMacroID(const OUString
& rName
) const
180 return mapNameToEventID(rName
);
183 void SvBaseEventDescriptor::getAnyFromMacro(Any
& rAny
,
184 const SvxMacro
& rMacro
)
186 bool bRetValueOK
= false; // do we have a ret value?
188 if (rMacro
.HasMacro())
190 switch (rMacro
.GetScriptType())
195 Sequence
<PropertyValue
> aSequence(3);
199 PropertyValue aTypeValue
;
200 aTypeValue
.Name
= sEventType
;
202 aTypeValue
.Value
= aTmp
;
203 aSequence
[0] = aTypeValue
;
206 PropertyValue aNameValue
;
207 aNameValue
.Name
= sMacroName
;
208 OUString
sNameTmp(rMacro
.GetMacName());
210 aNameValue
.Value
= aTmp
;
211 aSequence
[1] = aNameValue
;
214 PropertyValue aLibValue
;
215 aLibValue
.Name
= sLibrary
;
216 OUString
sLibTmp(rMacro
.GetLibName());
218 aLibValue
.Value
= aTmp
;
219 aSequence
[2] = aLibValue
;
228 Sequence
<PropertyValue
> aSequence(2);
232 PropertyValue aTypeValue
;
233 aTypeValue
.Name
= sEventType
;
235 aTypeValue
.Value
= aTmp
;
236 aSequence
[0] = aTypeValue
;
239 PropertyValue aNameValue
;
240 aNameValue
.Name
= sScript
;
241 OUString
sNameTmp(rMacro
.GetMacName());
243 aNameValue
.Value
= aTmp
;
244 aSequence
[1] = aNameValue
;
252 OSL_FAIL("not implemented");
255 // else: bRetValueOK not set
257 // if we don't have a return value, make an empty one
260 // create "None" macro
261 Sequence
<PropertyValue
> aSequence(1);
263 PropertyValue aKindValue
;
264 aKindValue
.Name
= sEventType
;
267 aKindValue
.Value
= aTmp
;
268 aSequence
[0] = aKindValue
;
276 void SvBaseEventDescriptor::getMacroFromAny(
279 throw ( IllegalArgumentException
)
282 Sequence
<PropertyValue
> aSequence
;
286 bool bTypeOK
= false;
287 bool bNone
= false; // true if EventType=="None"
288 enum ScriptType eType
= EXTENDED_STYPE
;
292 sal_Int32 nCount
= aSequence
.getLength();
293 for (sal_Int32 i
= 0; i
< nCount
; i
++)
295 PropertyValue
& aValue
= aSequence
[i
];
296 if (aValue
.Name
.equals(sEventType
))
299 aValue
.Value
>>= sTmp
;
300 if (sTmp
.equals(sStarBasic
))
305 else if (sTmp
.equals(sJavaScript
))
310 else if (sTmp
.equals(sScript
))
312 eType
= EXTENDED_STYPE
;
315 else if (sTmp
.equals(sNone
))
320 // else: unknown script type
322 else if (aValue
.Name
.equals(sMacroName
))
324 aValue
.Value
>>= sMacroVal
;
326 else if (aValue
.Name
.equals(sLibrary
))
328 aValue
.Value
>>= sLibVal
;
330 else if (aValue
.Name
.equals(sScript
))
332 aValue
.Value
>>= sScriptVal
;
334 // else: unknown PropertyValue -> ignore
341 // return empty macro
342 rMacro
= SvxMacro( sEmpty
, sEmpty
);
346 if (eType
== STARBASIC
)
348 // create macro and return
349 SvxMacro
aMacro(sMacroVal
, sLibVal
, eType
);
352 else if (eType
== EXTENDED_STYPE
)
354 SvxMacro
aMacro(sScriptVal
, sScript
);
359 // we can't process type: abort
360 // TODO: JavaScript macros
361 throw IllegalArgumentException();
367 // no valid type: abort
368 throw IllegalArgumentException();
373 SvEventDescriptor::SvEventDescriptor(
375 const SvEventDescription
* pSupportedMacroItems
) :
376 SvBaseEventDescriptor(pSupportedMacroItems
),
382 SvEventDescriptor::~SvEventDescriptor()
384 // automatically release xParentRef !
387 void SvEventDescriptor::replaceByName(
388 const sal_uInt16 nEvent
,
389 const SvxMacro
& rMacro
)
391 IllegalArgumentException
,
392 NoSuchElementException
,
393 WrappedTargetException
,
396 SvxMacroItem
aItem(getMacroItemWhich());
397 aItem
.SetMacroTable(getMacroItem().GetMacroTable());
398 aItem
.SetMacro(nEvent
, rMacro
);
402 void SvEventDescriptor::getByName(
404 const sal_uInt16 nEvent
)
406 NoSuchElementException
,
407 WrappedTargetException
,
410 const SvxMacroItem
& rItem
= getMacroItem();
411 if( rItem
.HasMacro( nEvent
) )
412 rMacro
= rItem
.GetMacro(nEvent
);
415 SvxMacro
aEmptyMacro(sEmpty
, sEmpty
);
416 rMacro
= aEmptyMacro
;
421 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
422 const SvEventDescription
* pSupportedMacroItems
) :
423 SvBaseEventDescriptor(pSupportedMacroItems
),
424 sImplName(sAPI_SvDetachedEventDescriptor
)
427 aMacros
= new SvxMacro
*[mnMacroItems
];
429 // ... and initialize
430 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
436 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
438 // delete contents of aMacros
439 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
441 if (NULL
!= aMacros
[i
])
448 sal_Int16
SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID
) const
450 // iterate over supported events
451 sal_Int16 nIndex
= 0;
452 while ( (mpSupportedMacroItems
[nIndex
].mnEvent
!= nID
) &&
453 (mpSupportedMacroItems
[nIndex
].mnEvent
!= 0) )
457 return (mpSupportedMacroItems
[nIndex
].mnEvent
== nID
) ? nIndex
: -1;
460 OUString
SvDetachedEventDescriptor::getImplementationName()
461 throw( ::com::sun::star::uno::RuntimeException
, std::exception
)
467 void SvDetachedEventDescriptor::replaceByName(
468 const sal_uInt16 nEvent
,
469 const SvxMacro
& rMacro
)
471 IllegalArgumentException
,
472 NoSuchElementException
,
473 WrappedTargetException
,
476 sal_Int16 nIndex
= getIndex(nEvent
);
478 throw IllegalArgumentException();
480 aMacros
[nIndex
] = new SvxMacro(rMacro
.GetMacName(), rMacro
.GetLibName(),
481 rMacro
.GetScriptType() );
485 void SvDetachedEventDescriptor::getByName(
487 const sal_uInt16 nEvent
)
489 NoSuchElementException
,
490 WrappedTargetException
,
493 sal_Int16 nIndex
= getIndex(nEvent
);
495 throw NoSuchElementException();
497 if( aMacros
[nIndex
] )
498 rMacro
= (*aMacros
[nIndex
]);
501 bool SvDetachedEventDescriptor::hasByName(
502 const sal_uInt16 nEvent
) const /// item ID of event
503 throw(IllegalArgumentException
)
505 sal_Int16 nIndex
= getIndex(nEvent
);
507 throw IllegalArgumentException();
509 return (NULL
!= aMacros
[nIndex
]) && aMacros
[nIndex
]->HasMacro();
513 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription
* pSupportedMacroItems
) :
514 SvDetachedEventDescriptor(pSupportedMacroItems
)
518 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
519 const SvxMacroTableDtor
& rMacroTable
,
520 const SvEventDescription
* pSupportedMacroItems
) :
521 SvDetachedEventDescriptor(pSupportedMacroItems
)
523 copyMacrosFromTable(rMacroTable
);
526 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
530 void SvMacroTableEventDescriptor::copyMacrosFromTable(
531 const SvxMacroTableDtor
& rMacroTable
)
533 for(sal_Int16 i
= 0; mpSupportedMacroItems
[i
].mnEvent
!= 0; i
++)
535 const sal_uInt16 nEvent
= mpSupportedMacroItems
[i
].mnEvent
;
536 const SvxMacro
* pMacro
= rMacroTable
.Get(nEvent
);
538 replaceByName(nEvent
, *pMacro
);
543 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
544 SvxMacroTableDtor
& rMacroTable
)
546 for(sal_Int16 i
= 0; mpSupportedMacroItems
[i
].mnEvent
!= 0; i
++)
548 const sal_uInt16 nEvent
= mpSupportedMacroItems
[i
].mnEvent
;
549 if (hasByName(nEvent
))
551 SvxMacro
& rMacro
= rMacroTable
.Insert(nEvent
, SvxMacro(sEmpty
, sEmpty
));
552 getByName(rMacro
, nEvent
);
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */