android: Update app-specific/MIME type icons
[LibreOffice.git] / svtools / source / uno / unoevent.cxx
blob30f4cbecbdb94cdc635780f489fbba1c4e5da1cb
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 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";
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, OUString(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, OUString(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, OUString(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 : std::as_const(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( "", "" );
171 else
173 if (eType == STARBASIC)
175 // create macro and return
176 SvxMacro aMacro(sMacroVal, sLibVal, eType);
177 rMacro = aMacro;
179 else if (eType == EXTENDED_STYPE)
181 SvxMacro aMacro(sScriptVal, sScript);
182 rMacro = aMacro;
184 else
186 // we can't process type: abort
187 // TODO: JavaScript macros
188 throw IllegalArgumentException();
195 SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
196 mpSupportedMacroItems(pSupportedMacroItems),
197 mnMacroItems(0)
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);
215 // error checking
216 if (SvMacroItemId::NONE == nMacroID)
217 throw NoSuchElementException();
218 if (rElement.getValueType() != getElementType())
219 throw IllegalArgumentException();
221 // get sequence
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);
236 // error checking
237 if (SvMacroItemId::NONE == nMacroID)
238 throw NoSuchElementException();
240 // perform get (in subclass)
241 Any aAny;
242 SvxMacro aMacro( "", "" );
243 getByName(aMacro, nMacroID);
244 getAnyFromMacro(aAny, aMacro);
245 return aAny;
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 );
258 return aSequence;
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(
309 XInterface& rParent,
310 const SvEventDescription* pSupportedMacroItems) :
311 SvBaseEventDescriptor(pSupportedMacroItems),
312 xParentRef(&rParent)
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);
329 setMacroItem(aItem);
332 void SvEventDescriptor::getByName(
333 SvxMacro& rMacro,
334 const SvMacroItemId nEvent )
336 const SvxMacroItem& rItem = getMacroItem();
337 if( rItem.HasMacro( nEvent ) )
338 rMacro = rItem.GetMacro(nEvent);
339 else
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) )
364 nIndex++;
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);
380 if (-1 == nIndex)
381 throw IllegalArgumentException();
383 aMacros[nIndex].reset( new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
384 rMacro.GetScriptType() ) );
388 void SvDetachedEventDescriptor::getByName(
389 SvxMacro& rMacro,
390 const SvMacroItemId nEvent )
392 sal_Int16 nIndex = getIndex(nEvent);
393 if (-1 == nIndex )
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);
404 if (-1 == nIndex)
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;
441 if (hasById(nEvent))
443 SvxMacro& rMacro = rMacroTable.Insert(nEvent, SvxMacro("", ""));
444 getByName(rMacro, nEvent);
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */