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>
22 #include <comphelper/propertyvalue.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <o3tl/string_view.hxx>
25 #include <osl/diagnose.h>
26 #include <sfx2/event.hxx>
27 #include <svtools/unoevent.hxx>
28 #include <svl/macitem.hxx>
30 using namespace ::com::sun::star
;
31 using namespace css::uno
;
33 using css::container::NoSuchElementException
;
34 using css::container::XNameReplace
;
35 using css::lang::IllegalArgumentException
;
36 using css::beans::PropertyValue
;
39 constexpr OUStringLiteral sAPI_ServiceName
= u
"com.sun.star.container.XNameReplace";
40 constexpr OUStringLiteral sEventType
= u
"EventType";
41 constexpr OUStringLiteral sMacroName
= u
"MacroName";
42 constexpr OUStringLiteral sLibrary
= u
"Library";
43 constexpr OUStringLiteral sStarBasic
= u
"StarBasic";
44 constexpr OUStringLiteral sScript
= u
"Script";
45 constexpr OUStringLiteral sNone
= u
"None";
49 void getAnyFromMacro(Any
& rAny
, const SvxMacro
& rMacro
)
51 bool bRetValueOK
= false; // do we have a ret value?
53 if (rMacro
.HasMacro())
55 switch (rMacro
.GetScriptType())
60 Sequence
<PropertyValue
> aSequence(
62 { comphelper::makePropertyValue(sEventType
, OUString(sStarBasic
)),
64 comphelper::makePropertyValue(sMacroName
, rMacro
.GetMacName()),
66 comphelper::makePropertyValue(sLibrary
, rMacro
.GetLibName()) });
75 Sequence
<PropertyValue
> aSequence(
77 { comphelper::makePropertyValue(sEventType
, OUString(sScript
)),
79 comphelper::makePropertyValue(sScript
, rMacro
.GetMacName()) });
87 OSL_FAIL("not implemented");
90 // else: bRetValueOK not set
92 // if we don't have a return value, make an empty one
96 // create "None" macro
97 Sequence
<PropertyValue
> aSequence
{ comphelper::makePropertyValue(sEventType
, OUString(sNone
)) };
101 /// @throws IllegalArgumentException
102 void getMacroFromAny(
107 Sequence
<PropertyValue
> aSequence
;
111 bool bTypeOK
= false;
112 bool bNone
= false; // true if EventType=="None"
113 enum ScriptType eType
= EXTENDED_STYPE
;
117 for (const PropertyValue
& aValue
: std::as_const(aSequence
))
119 if (aValue
.Name
== sEventType
)
122 aValue
.Value
>>= sTmp
;
123 if (sTmp
== sStarBasic
)
128 else if (sTmp
== "JavaScript")
133 else if (sTmp
== sScript
)
135 eType
= EXTENDED_STYPE
;
138 else if (sTmp
== sNone
)
143 // else: unknown script type
145 else if (aValue
.Name
== sMacroName
)
147 aValue
.Value
>>= sMacroVal
;
149 else if (aValue
.Name
== sLibrary
)
151 aValue
.Value
>>= sLibVal
;
153 else if (aValue
.Name
== sScript
)
155 aValue
.Value
>>= sScriptVal
;
157 // else: unknown PropertyValue -> ignore
162 // no valid type: abort
163 throw IllegalArgumentException();
168 // return empty macro
169 rMacro
= SvxMacro( "", "" );
173 if (eType
== STARBASIC
)
175 // create macro and return
176 SvxMacro
aMacro(sMacroVal
, sLibVal
, eType
);
179 else if (eType
== EXTENDED_STYPE
)
181 SvxMacro
aMacro(sScriptVal
, sScript
);
186 // we can't process type: abort
187 // TODO: JavaScript macros
188 throw IllegalArgumentException();
195 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription
* pSupportedMacroItems
) :
196 mpSupportedMacroItems(pSupportedMacroItems
),
199 assert(pSupportedMacroItems
!= nullptr && "Need a list of supported events!");
201 for( ; mpSupportedMacroItems
[mnMacroItems
].mnEvent
!= SvMacroItemId::NONE
; mnMacroItems
++) ;
205 SvBaseEventDescriptor::~SvBaseEventDescriptor()
209 void SvBaseEventDescriptor::replaceByName(
210 const OUString
& rName
,
211 const Any
& rElement
)
213 SvMacroItemId nMacroID
= getMacroID(rName
);
216 if (SvMacroItemId::NONE
== nMacroID
)
217 throw NoSuchElementException();
218 if (rElement
.getValueType() != getElementType())
219 throw IllegalArgumentException();
222 Sequence
<PropertyValue
> aSequence
;
223 rElement
>>= aSequence
;
225 // perform replace (in subclass)
226 SvxMacro
aMacro("","");
227 getMacroFromAny(aMacro
, rElement
);
228 replaceByName(nMacroID
, aMacro
);
231 Any
SvBaseEventDescriptor::getByName(
232 const OUString
& rName
)
234 SvMacroItemId nMacroID
= getMacroID(rName
);
237 if (SvMacroItemId::NONE
== nMacroID
)
238 throw NoSuchElementException();
240 // perform get (in subclass)
242 SvxMacro
aMacro( "", "" );
243 getByName(aMacro
, nMacroID
);
244 getAnyFromMacro(aAny
, aMacro
);
248 Sequence
<OUString
> SvBaseEventDescriptor::getElementNames()
250 // create and fill sequence
251 Sequence
<OUString
> aSequence(mnMacroItems
);
252 auto aSequenceRange
= asNonConstRange(aSequence
);
253 for( sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
255 aSequenceRange
[i
] = OUString::createFromAscii( mpSupportedMacroItems
[i
].mpEventName
);
261 sal_Bool
SvBaseEventDescriptor::hasByName(
262 const OUString
& rName
)
264 SvMacroItemId nMacroID
= getMacroID(rName
);
265 return (nMacroID
!= SvMacroItemId::NONE
);
268 Type
SvBaseEventDescriptor::getElementType()
270 return cppu::UnoType
<Sequence
<PropertyValue
>>::get();
273 sal_Bool
SvBaseEventDescriptor::hasElements()
275 return mnMacroItems
!= 0;
278 sal_Bool
SvBaseEventDescriptor::supportsService(const OUString
& rServiceName
)
280 return cppu::supportsService(this, rServiceName
);
283 Sequence
<OUString
> SvBaseEventDescriptor::getSupportedServiceNames()
285 return { sAPI_ServiceName
};
288 SvMacroItemId
SvBaseEventDescriptor::mapNameToEventID(std::u16string_view rName
) const
290 // iterate over known event names
291 for(sal_Int16 i
= 0; i
< mnMacroItems
; i
++)
293 if( o3tl::equalsAscii(rName
, mpSupportedMacroItems
[i
].mpEventName
))
295 return mpSupportedMacroItems
[i
].mnEvent
;
299 // not found -> return zero
300 return SvMacroItemId::NONE
;
303 SvMacroItemId
SvBaseEventDescriptor::getMacroID(std::u16string_view rName
) const
305 return mapNameToEventID(rName
);
308 SvEventDescriptor::SvEventDescriptor(
310 const SvEventDescription
* pSupportedMacroItems
) :
311 SvBaseEventDescriptor(pSupportedMacroItems
),
317 SvEventDescriptor::~SvEventDescriptor()
319 // automatically release xParentRef !
322 void SvEventDescriptor::replaceByName(
323 const SvMacroItemId nEvent
,
324 const SvxMacro
& rMacro
)
326 SvxMacroItem
aItem(getMacroItemWhich());
327 aItem
.SetMacroTable(getMacroItem().GetMacroTable());
328 aItem
.SetMacro(nEvent
, rMacro
);
332 void SvEventDescriptor::getByName(
334 const SvMacroItemId nEvent
)
336 const SvxMacroItem
& rItem
= getMacroItem();
337 if( rItem
.HasMacro( nEvent
) )
338 rMacro
= rItem
.GetMacro(nEvent
);
341 SvxMacro
aEmptyMacro("", "");
342 rMacro
= aEmptyMacro
;
346 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
347 const SvEventDescription
* pSupportedMacroItems
) :
348 SvBaseEventDescriptor(pSupportedMacroItems
)
350 aMacros
.resize(mnMacroItems
);
353 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
357 sal_Int16
SvDetachedEventDescriptor::getIndex(const SvMacroItemId nID
) const
359 // iterate over supported events
360 sal_Int16 nIndex
= 0;
361 while ( (mpSupportedMacroItems
[nIndex
].mnEvent
!= nID
) &&
362 (mpSupportedMacroItems
[nIndex
].mnEvent
!= SvMacroItemId::NONE
) )
366 return (mpSupportedMacroItems
[nIndex
].mnEvent
== nID
) ? nIndex
: -1;
369 OUString
SvDetachedEventDescriptor::getImplementationName()
371 return "SvDetachedEventDescriptor";
375 void SvDetachedEventDescriptor::replaceByName(
376 const SvMacroItemId nEvent
,
377 const SvxMacro
& rMacro
)
379 sal_Int16 nIndex
= getIndex(nEvent
);
381 throw IllegalArgumentException();
383 aMacros
[nIndex
].reset( new SvxMacro(rMacro
.GetMacName(), rMacro
.GetLibName(),
384 rMacro
.GetScriptType() ) );
388 void SvDetachedEventDescriptor::getByName(
390 const SvMacroItemId nEvent
)
392 sal_Int16 nIndex
= getIndex(nEvent
);
394 throw NoSuchElementException();
396 if( aMacros
[nIndex
] )
397 rMacro
= *aMacros
[nIndex
];
400 bool SvDetachedEventDescriptor::hasById(
401 const SvMacroItemId nEvent
) const /// item ID of event
403 sal_Int16 nIndex
= getIndex(nEvent
);
405 throw IllegalArgumentException();
407 return (nullptr != aMacros
[nIndex
]) && aMacros
[nIndex
]->HasMacro();
411 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription
* pSupportedMacroItems
) :
412 SvDetachedEventDescriptor(pSupportedMacroItems
)
416 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
417 const SvxMacroTableDtor
& rMacroTable
,
418 const SvEventDescription
* pSupportedMacroItems
) :
419 SvDetachedEventDescriptor(pSupportedMacroItems
)
421 assert(mpSupportedMacroItems
);
422 for(sal_Int16 i
= 0; mpSupportedMacroItems
[i
].mnEvent
!= SvMacroItemId::NONE
; i
++)
424 const SvMacroItemId nEvent
= mpSupportedMacroItems
[i
].mnEvent
;
425 const SvxMacro
* pMacro
= rMacroTable
.Get(nEvent
);
426 if (nullptr != pMacro
)
427 replaceByName(nEvent
, *pMacro
);
431 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
435 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
436 SvxMacroTableDtor
& rMacroTable
)
438 for(sal_Int16 i
= 0; mpSupportedMacroItems
[i
].mnEvent
!= SvMacroItemId::NONE
; i
++)
440 const SvMacroItemId nEvent
= mpSupportedMacroItems
[i
].mnEvent
;
443 SvxMacro
& rMacro
= rMacroTable
.Insert(nEvent
, SvxMacro("", ""));
444 getByName(rMacro
, nEvent
);
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */