tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / svtools / source / uno / unoevent.cxx
blobcfb33a0296a07e9e2706371f7d03d714724e9789
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 OUString sAPI_ServiceName = u"com.sun.star.container.XNameReplace"_ustr;
40 constexpr OUString sEventType = u"EventType"_ustr;
41 constexpr OUString sMacroName = u"MacroName"_ustr;
42 constexpr OUString sLibrary = u"Library"_ustr;
43 constexpr OUString sStarBasic = u"StarBasic"_ustr;
44 constexpr OUString sScript = u"Script"_ustr;
45 constexpr OUString sNone = u"None"_ustr;
47 namespace {
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())
57 case STARBASIC:
59 // create sequence
60 Sequence<PropertyValue> aSequence(
61 // create type
62 { comphelper::makePropertyValue(sEventType, sStarBasic),
63 // macro name
64 comphelper::makePropertyValue(sMacroName, rMacro.GetMacName()),
65 // library name
66 comphelper::makePropertyValue(sLibrary, rMacro.GetLibName()) });
68 rAny <<= aSequence;
69 bRetValueOK = true;
70 break;
72 case EXTENDED_STYPE:
74 // create sequence
75 Sequence<PropertyValue> aSequence(
76 // create type
77 { comphelper::makePropertyValue(sEventType, sScript),
78 // macro name
79 comphelper::makePropertyValue(sScript, rMacro.GetMacName()) });
81 rAny <<= aSequence;
82 bRetValueOK = true;
83 break;
85 case JAVASCRIPT:
86 default:
87 OSL_FAIL("not implemented");
90 // else: bRetValueOK not set
92 // if we don't have a return value, make an empty one
93 if ( bRetValueOK)
94 return;
96 // create "None" macro
97 Sequence<PropertyValue> aSequence{ comphelper::makePropertyValue(sEventType, sNone) };
98 rAny <<= aSequence;
101 /// @throws IllegalArgumentException
102 void getMacroFromAny(
103 SvxMacro& rMacro,
104 const Any& rAny)
106 // get sequence
107 Sequence<PropertyValue> aSequence;
108 rAny >>= aSequence;
110 // process ...
111 bool bTypeOK = false;
112 bool bNone = false; // true if EventType=="None"
113 enum ScriptType eType = EXTENDED_STYPE;
114 OUString sScriptVal;
115 OUString sMacroVal;
116 OUString sLibVal;
117 for (const PropertyValue& aValue : aSequence)
119 if (aValue.Name == sEventType)
121 OUString sTmp;
122 aValue.Value >>= sTmp;
123 if (sTmp == sStarBasic)
125 eType = STARBASIC;
126 bTypeOK = true;
128 else if (sTmp == "JavaScript")
130 eType = JAVASCRIPT;
131 bTypeOK = true;
133 else if (sTmp == sScript)
135 eType = EXTENDED_STYPE;
136 bTypeOK = true;
138 else if (sTmp == sNone)
140 bNone = true;
141 bTypeOK = true;
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
160 if (!bTypeOK)
162 // no valid type: abort
163 throw IllegalArgumentException();
166 if (bNone)
168 // return empty macro
169 rMacro = SvxMacro( u""_ustr, u""_ustr );
171 else
173 if (eType == STARBASIC)
175 // create macro and return
176 rMacro = SvxMacro(sMacroVal, sLibVal, eType);
178 else if (eType == EXTENDED_STYPE)
180 rMacro = SvxMacro(sScriptVal, sScript);
182 else
184 // we can't process type: abort
185 // TODO: JavaScript macros
186 throw IllegalArgumentException();
193 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
194 mpSupportedMacroItems(pSupportedMacroItems),
195 mnMacroItems(0)
197 assert(pSupportedMacroItems != nullptr && "Need a list of supported events!");
199 for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != SvMacroItemId::NONE; mnMacroItems++) ;
203 SvBaseEventDescriptor::~SvBaseEventDescriptor()
207 void SvBaseEventDescriptor::replaceByName(
208 const OUString& rName,
209 const Any& rElement )
211 SvMacroItemId nMacroID = getMacroID(rName);
213 // error checking
214 if (SvMacroItemId::NONE == nMacroID)
215 throw NoSuchElementException();
216 if (rElement.getValueType() != getElementType())
217 throw IllegalArgumentException();
219 // get sequence
220 Sequence<PropertyValue> aSequence;
221 rElement >>= aSequence;
223 // perform replace (in subclass)
224 SvxMacro aMacro(u""_ustr,u""_ustr);
225 getMacroFromAny(aMacro, rElement);
226 replaceByName(nMacroID, aMacro);
229 Any SvBaseEventDescriptor::getByName(
230 const OUString& rName )
232 SvMacroItemId nMacroID = getMacroID(rName);
234 // error checking
235 if (SvMacroItemId::NONE == nMacroID)
236 throw NoSuchElementException();
238 // perform get (in subclass)
239 Any aAny;
240 SvxMacro aMacro( u""_ustr, u""_ustr );
241 getByName(aMacro, nMacroID);
242 getAnyFromMacro(aAny, aMacro);
243 return aAny;
246 Sequence<OUString> SvBaseEventDescriptor::getElementNames()
248 // create and fill sequence
249 Sequence<OUString> aSequence(mnMacroItems);
250 auto aSequenceRange = asNonConstRange(aSequence);
251 for( sal_Int16 i = 0; i < mnMacroItems; i++)
253 aSequenceRange[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
256 return aSequence;
259 sal_Bool SvBaseEventDescriptor::hasByName(
260 const OUString& rName )
262 SvMacroItemId nMacroID = getMacroID(rName);
263 return (nMacroID != SvMacroItemId::NONE);
266 Type SvBaseEventDescriptor::getElementType()
268 return cppu::UnoType<Sequence<PropertyValue>>::get();
271 sal_Bool SvBaseEventDescriptor::hasElements()
273 return mnMacroItems != 0;
276 sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
278 return cppu::supportsService(this, rServiceName);
281 Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames()
283 return { sAPI_ServiceName };
286 SvMacroItemId SvBaseEventDescriptor::mapNameToEventID(std::u16string_view rName) const
288 // iterate over known event names
289 for(sal_Int16 i = 0; i < mnMacroItems; i++)
291 if( o3tl::equalsAscii(rName, mpSupportedMacroItems[i].mpEventName))
293 return mpSupportedMacroItems[i].mnEvent;
297 // not found -> return zero
298 return SvMacroItemId::NONE;
301 SvMacroItemId SvBaseEventDescriptor::getMacroID(std::u16string_view rName) const
303 return mapNameToEventID(rName);
306 SvEventDescriptor::SvEventDescriptor(
307 XInterface& rParent,
308 const SvEventDescription* pSupportedMacroItems) :
309 SvBaseEventDescriptor(pSupportedMacroItems),
310 xParentRef(&rParent)
315 SvEventDescriptor::~SvEventDescriptor()
317 // automatically release xParentRef !
320 void SvEventDescriptor::replaceByName(
321 const SvMacroItemId nEvent,
322 const SvxMacro& rMacro)
324 SvxMacroItem aItem(getMacroItemWhich());
325 aItem.SetMacroTable(getMacroItem().GetMacroTable());
326 aItem.SetMacro(nEvent, rMacro);
327 setMacroItem(aItem);
330 void SvEventDescriptor::getByName(
331 SvxMacro& rMacro,
332 const SvMacroItemId nEvent )
334 const SvxMacroItem& rItem = getMacroItem();
335 if (rItem.HasMacro(nEvent))
336 rMacro = rItem.GetMacro(nEvent);
337 else
338 rMacro = SvxMacro(u""_ustr, u""_ustr);
341 SvDetachedEventDescriptor::SvDetachedEventDescriptor(
342 const SvEventDescription* pSupportedMacroItems) :
343 SvBaseEventDescriptor(pSupportedMacroItems)
345 aMacros.resize(mnMacroItems);
348 SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
352 sal_Int16 SvDetachedEventDescriptor::getIndex(const SvMacroItemId nID) const
354 // iterate over supported events
355 sal_Int16 nIndex = 0;
356 while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
357 (mpSupportedMacroItems[nIndex].mnEvent != SvMacroItemId::NONE) )
359 nIndex++;
361 return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
364 OUString SvDetachedEventDescriptor::getImplementationName()
366 return u"SvDetachedEventDescriptor"_ustr;
370 void SvDetachedEventDescriptor::replaceByName(
371 const SvMacroItemId nEvent,
372 const SvxMacro& rMacro)
374 sal_Int16 nIndex = getIndex(nEvent);
375 if (-1 == nIndex)
376 throw IllegalArgumentException();
378 aMacros[nIndex].reset( new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
379 rMacro.GetScriptType() ) );
383 void SvDetachedEventDescriptor::getByName(
384 SvxMacro& rMacro,
385 const SvMacroItemId nEvent )
387 sal_Int16 nIndex = getIndex(nEvent);
388 if (-1 == nIndex )
389 throw NoSuchElementException();
391 if( aMacros[nIndex] )
392 rMacro = *aMacros[nIndex];
395 bool SvDetachedEventDescriptor::hasById(
396 const SvMacroItemId nEvent ) const /// item ID of event
398 sal_Int16 nIndex = getIndex(nEvent);
399 if (-1 == nIndex)
400 throw IllegalArgumentException();
402 return (nullptr != aMacros[nIndex]) && aMacros[nIndex]->HasMacro();
406 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
407 SvDetachedEventDescriptor(pSupportedMacroItems)
411 SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
412 const SvxMacroTableDtor& rMacroTable,
413 const SvEventDescription* pSupportedMacroItems) :
414 SvDetachedEventDescriptor(pSupportedMacroItems)
416 assert(mpSupportedMacroItems);
417 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != SvMacroItemId::NONE; i++)
419 const SvMacroItemId nEvent = mpSupportedMacroItems[i].mnEvent;
420 const SvxMacro* pMacro = rMacroTable.Get(nEvent);
421 if (nullptr != pMacro)
422 replaceByName(nEvent, *pMacro);
426 SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
430 void SvMacroTableEventDescriptor::copyMacrosIntoTable(
431 SvxMacroTableDtor& rMacroTable)
433 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != SvMacroItemId::NONE; i++)
435 const SvMacroItemId nEvent = mpSupportedMacroItems[i].mnEvent;
436 if (hasById(nEvent))
438 SvxMacro& rMacro = rMacroTable.Insert(nEvent, SvxMacro(u""_ustr, u""_ustr));
439 getByName(rMacro, nEvent);
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */