LibreOffice Module svtools (master) 1
unoevent.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/beans/PropertyValue.hpp>
21
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>
29
30using namespace ::com::sun::star;
31using namespace css::uno;
32
33using css::container::NoSuchElementException;
34using css::container::XNameReplace;
35using css::lang::IllegalArgumentException;
36using css::beans::PropertyValue;
37
38
39constexpr OUStringLiteral sAPI_ServiceName = u"com.sun.star.container.XNameReplace";
40constexpr OUStringLiteral sEventType = u"EventType";
41constexpr OUStringLiteral sMacroName = u"MacroName";
42constexpr OUStringLiteral sLibrary = u"Library";
43constexpr OUStringLiteral sStarBasic = u"StarBasic";
44constexpr OUStringLiteral sScript = u"Script";
45constexpr OUStringLiteral sNone = u"None";
46
47namespace {
48
49void getAnyFromMacro(Any& rAny, const SvxMacro& rMacro)
50{
51 bool bRetValueOK = false; // do we have a ret value?
52
53 if (rMacro.HasMacro())
54 {
55 switch (rMacro.GetScriptType())
56 {
57 case STARBASIC:
58 {
59 // create sequence
60 Sequence<PropertyValue> aSequence(
61 // create type
63 // macro name
65 // library name
67
68 rAny <<= aSequence;
69 bRetValueOK = true;
70 break;
71 }
72 case EXTENDED_STYPE:
73 {
74 // create sequence
75 Sequence<PropertyValue> aSequence(
76 // create type
78 // macro name
80
81 rAny <<= aSequence;
82 bRetValueOK = true;
83 break;
84 }
85 case JAVASCRIPT:
86 default:
87 OSL_FAIL("not implemented");
88 }
89 }
90 // else: bRetValueOK not set
91
92 // if we don't have a return value, make an empty one
93 if ( bRetValueOK)
94 return;
95
96 // create "None" macro
97 Sequence<PropertyValue> aSequence{ comphelper::makePropertyValue(sEventType, OUString(sNone)) };
98 rAny <<= aSequence;
99}
100
102void getMacroFromAny(
103 SvxMacro& rMacro,
104 const Any& rAny)
105{
106 // get sequence
107 Sequence<PropertyValue> aSequence;
108 rAny >>= aSequence;
109
110 // process ...
111 bool bTypeOK = false;
112 bool bNone = false; // true if EventType=="None"
114 OUString sScriptVal;
115 OUString sMacroVal;
116 OUString sLibVal;
117 for (const PropertyValue& aValue : std::as_const(aSequence))
118 {
119 if (aValue.Name == sEventType)
120 {
121 OUString sTmp;
122 aValue.Value >>= sTmp;
123 if (sTmp == sStarBasic)
124 {
126 bTypeOK = true;
127 }
128 else if (sTmp == "JavaScript")
129 {
131 bTypeOK = true;
132 }
133 else if (sTmp == sScript)
134 {
136 bTypeOK = true;
137 }
138 else if (sTmp == sNone)
139 {
140 bNone = true;
141 bTypeOK = true;
142 }
143 // else: unknown script type
144 }
145 else if (aValue.Name == sMacroName)
146 {
147 aValue.Value >>= sMacroVal;
148 }
149 else if (aValue.Name == sLibrary)
150 {
151 aValue.Value >>= sLibVal;
152 }
153 else if (aValue.Name == sScript)
154 {
155 aValue.Value >>= sScriptVal;
156 }
157 // else: unknown PropertyValue -> ignore
158 }
159
160 if (!bTypeOK)
161 {
162 // no valid type: abort
163 throw IllegalArgumentException();
164 }
165
166 if (bNone)
167 {
168 // return empty macro
169 rMacro = SvxMacro( "", "" );
170 }
171 else
172 {
173 if (eType == STARBASIC)
174 {
175 // create macro and return
176 SvxMacro aMacro(sMacroVal, sLibVal, eType);
177 rMacro = aMacro;
178 }
179 else if (eType == EXTENDED_STYPE)
180 {
181 SvxMacro aMacro(sScriptVal, sScript);
182 rMacro = aMacro;
183 }
184 else
185 {
186 // we can't process type: abort
187 // TODO: JavaScript macros
188 throw IllegalArgumentException();
189 }
190 }
191}
192
193}
194
196 mpSupportedMacroItems(pSupportedMacroItems),
197 mnMacroItems(0)
198{
199 assert(pSupportedMacroItems != nullptr && "Need a list of supported events!");
200
201 for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != SvMacroItemId::NONE; mnMacroItems++) ;
202}
203
204
206{
207}
208
210 const OUString& rName,
211 const Any& rElement )
212{
213 SvMacroItemId nMacroID = getMacroID(rName);
214
215 // error checking
216 if (SvMacroItemId::NONE == nMacroID)
217 throw NoSuchElementException();
218 if (rElement.getValueType() != getElementType())
219 throw IllegalArgumentException();
220
221 // get sequence
222 Sequence<PropertyValue> aSequence;
223 rElement >>= aSequence;
224
225 // perform replace (in subclass)
226 SvxMacro aMacro("","");
227 getMacroFromAny(aMacro, rElement);
228 replaceByName(nMacroID, aMacro);
229}
230
232 const OUString& rName )
233{
234 SvMacroItemId nMacroID = getMacroID(rName);
235
236 // error checking
237 if (SvMacroItemId::NONE == nMacroID)
238 throw NoSuchElementException();
239
240 // perform get (in subclass)
241 Any aAny;
242 SvxMacro aMacro( "", "" );
243 getByName(aMacro, nMacroID);
244 getAnyFromMacro(aAny, aMacro);
245 return aAny;
246}
247
249{
250 // create and fill sequence
251 Sequence<OUString> aSequence(mnMacroItems);
252 auto aSequenceRange = asNonConstRange(aSequence);
253 for( sal_Int16 i = 0; i < mnMacroItems; i++)
254 {
255 aSequenceRange[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
256 }
257
258 return aSequence;
259}
260
262 const OUString& rName )
263{
264 SvMacroItemId nMacroID = getMacroID(rName);
265 return (nMacroID != SvMacroItemId::NONE);
266}
267
269{
271}
272
274{
275 return mnMacroItems != 0;
276}
277
279{
280 return cppu::supportsService(this, rServiceName);
281}
282
284{
285 return { sAPI_ServiceName };
286}
287
289{
290 // iterate over known event names
291 for(sal_Int16 i = 0; i < mnMacroItems; i++)
292 {
293 if( o3tl::equalsAscii(rName, mpSupportedMacroItems[i].mpEventName))
294 {
296 }
297 }
298
299 // not found -> return zero
300 return SvMacroItemId::NONE;
301}
302
303SvMacroItemId SvBaseEventDescriptor::getMacroID(std::u16string_view rName) const
304{
305 return mapNameToEventID(rName);
306}
307
309 XInterface& rParent,
310 const SvEventDescription* pSupportedMacroItems) :
311 SvBaseEventDescriptor(pSupportedMacroItems),
312 xParentRef(&rParent)
313{
314}
315
316
318{
319 // automatically release xParentRef !
320}
321
323 const SvMacroItemId nEvent,
324 const SvxMacro& rMacro)
325{
327 aItem.SetMacroTable(getMacroItem().GetMacroTable());
328 aItem.SetMacro(nEvent, rMacro);
329 setMacroItem(aItem);
330}
331
333 SvxMacro& rMacro,
334 const SvMacroItemId nEvent )
335{
336 const SvxMacroItem& rItem = getMacroItem();
337 if( rItem.HasMacro( nEvent ) )
338 rMacro = rItem.GetMacro(nEvent);
339 else
340 {
341 SvxMacro aEmptyMacro("", "");
342 rMacro = aEmptyMacro;
343 }
344}
345
347 const SvEventDescription* pSupportedMacroItems) :
348 SvBaseEventDescriptor(pSupportedMacroItems)
349{
350 aMacros.resize(mnMacroItems);
351}
352
354{
355}
356
358{
359 // iterate over supported events
360 sal_Int16 nIndex = 0;
361 while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
362 (mpSupportedMacroItems[nIndex].mnEvent != SvMacroItemId::NONE) )
363 {
364 nIndex++;
365 }
366 return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
367}
368
370{
371 return "SvDetachedEventDescriptor";
372}
373
374
376 const SvMacroItemId nEvent,
377 const SvxMacro& rMacro)
378{
379 sal_Int16 nIndex = getIndex(nEvent);
380 if (-1 == nIndex)
381 throw IllegalArgumentException();
382
383 aMacros[nIndex].reset( new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
384 rMacro.GetScriptType() ) );
385}
386
387
389 SvxMacro& rMacro,
390 const SvMacroItemId nEvent )
391{
392 sal_Int16 nIndex = getIndex(nEvent);
393 if (-1 == nIndex )
394 throw NoSuchElementException();
395
396 if( aMacros[nIndex] )
397 rMacro = *aMacros[nIndex];
398}
399
401 const SvMacroItemId nEvent ) const
402{
403 sal_Int16 nIndex = getIndex(nEvent);
404 if (-1 == nIndex)
405 throw IllegalArgumentException();
406
407 return (nullptr != aMacros[nIndex]) && aMacros[nIndex]->HasMacro();
408}
409
410
412 SvDetachedEventDescriptor(pSupportedMacroItems)
413{
414}
415
417 const SvxMacroTableDtor& rMacroTable,
418 const SvEventDescription* pSupportedMacroItems) :
419 SvDetachedEventDescriptor(pSupportedMacroItems)
420{
421 assert(mpSupportedMacroItems);
422 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != SvMacroItemId::NONE; i++)
423 {
425 const SvxMacro* pMacro = rMacroTable.Get(nEvent);
426 if (nullptr != pMacro)
427 replaceByName(nEvent, *pMacro);
428 }
429}
430
432{
433}
434
436 SvxMacroTableDtor& rMacroTable)
437{
438 for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != SvMacroItemId::NONE; i++)
439 {
441 if (hasById(nEvent))
442 {
443 SvxMacro& rMacro = rMacroTable.Insert(nEvent, SvxMacro("", ""));
444 getByName(rMacro, nEvent);
445 }
446 }
447}
448
449
450/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvBaseEventDescriptor: Abstract class that implements the basics of an XNameReplace that is delivered...
Definition: unoevent.hxx:65
virtual sal_Bool SAL_CALL hasElements() override
Definition: unoevent.cxx:273
virtual css::uno::Type SAL_CALL getElementType() override
Definition: unoevent.cxx:268
virtual ~SvBaseEventDescriptor() override
Definition: unoevent.cxx:205
const SvEventDescription * mpSupportedMacroItems
last element is 0, 0
Definition: unoevent.hxx:68
virtual sal_Bool SAL_CALL hasByName(const OUString &rName) override
Definition: unoevent.cxx:261
SvMacroItemId mapNameToEventID(std::u16string_view rName) const
convert an API event name to the event ID as used by SvxMacroItem
Definition: unoevent.cxx:288
virtual void SAL_CALL replaceByName(const OUString &rName, const css::uno::Any &rElement) override
calls replaceByName(const sal_uInt16, const SvxMacro&)
virtual css::uno::Any SAL_CALL getByName(const OUString &rName) override
calls getByName(sal_uInt16)
Definition: unoevent.cxx:231
sal_Int16 mnMacroItems
Definition: unoevent.hxx:69
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: unoevent.cxx:278
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: unoevent.cxx:283
SvMacroItemId getMacroID(std::u16string_view rName) const
get the event ID for the name; return 0 if not supported
Definition: unoevent.cxx:303
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: unoevent.cxx:248
SvBaseEventDescriptor(const SvEventDescription *pSupportedMacroItems)
Definition: unoevent.cxx:195
SvDetachedEventDescriptor:
Definition: unoevent.hxx:206
virtual OUString SAL_CALL getImplementationName() override
must be implemented in subclass
Definition: unoevent.cxx:369
virtual ~SvDetachedEventDescriptor() override
Definition: unoevent.cxx:353
bool hasById(const SvMacroItemId nEvent) const
do we have an event? return true: we have a macro for the event return false: no macro; getByName() w...
Definition: unoevent.cxx:400
SvDetachedEventDescriptor(const SvEventDescription *pSupportedMacroItems)
Definition: unoevent.cxx:346
virtual void getByName(SvxMacro &rMacro, const SvMacroItemId nEvent) override
item ID of event
Definition: unoevent.cxx:388
virtual void replaceByName(const SvMacroItemId nEvent, const SvxMacro &rMacro) override
event (will be copied)
Definition: unoevent.cxx:375
std::vector< std::unique_ptr< SvxMacro > > aMacros
Definition: unoevent.hxx:208
sal_Int16 getIndex(const SvMacroItemId nID) const
Definition: unoevent.cxx:357
SvEventDescriptor(css::uno::XInterface &rParent, const SvEventDescription *pSupportedMacroItems)
Definition: unoevent.cxx:308
virtual void setMacroItem(const SvxMacroItem &rItem)=0
Set the SvxMacroItem at the parent.
virtual const SvxMacroItem & getMacroItem()=0
Get the SvxMacroItem from the parent.
virtual sal_uInt16 getMacroItemWhich() const =0
Get the SvxMacroItem Which Id needed for the current application must be implemented by subclass.
virtual ~SvEventDescriptor() override
Definition: unoevent.cxx:317
virtual void replaceByName(const SvMacroItemId nEvent, const SvxMacro &rMacro) override
event (will be copied)
Definition: unoevent.cxx:322
virtual void getByName(SvxMacro &rMacros, const SvMacroItemId nEvent) override
item ID of event
Definition: unoevent.cxx:332
SvMacroTableEventDescriptor(const SvEventDescription *pSupportedMacroItems)
Definition: unoevent.cxx:411
virtual ~SvMacroTableEventDescriptor() override
Definition: unoevent.cxx:431
void copyMacrosIntoTable(SvxMacroTableDtor &aFmt)
Definition: unoevent.cxx:435
const SvxMacro & GetMacro(SvMacroItemId nEvent) const
bool HasMacro(SvMacroItemId nEvent) const
void SetMacroTable(const SvxMacroTableDtor &rTbl)
void SetMacro(SvMacroItemId nEvent, const SvxMacro &)
SvxMacro & Insert(SvMacroItemId nEvent, const SvxMacro &rMacro)
const SvxMacro * Get(SvMacroItemId nEvent) const
const OUString & GetMacName() const
ScriptType GetScriptType() const
const OUString & GetLibName() const
bool HasMacro() const
float u
SvMacroItemId
DocumentType eType
sal_Int32 nIndex
ScriptType
EXTENDED_STYPE
JAVASCRIPT
STARBASIC
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
bool equalsAscii(std::u16string_view s1, std::string_view s2)
SvEventDescription: Description of a single event.
Definition: unoevent.hxx:42
SvMacroItemId mnEvent
Definition: unoevent.hxx:43
unsigned char sal_Bool
constexpr OUStringLiteral sScript
Definition: unoevent.cxx:44
constexpr OUStringLiteral sLibrary
Definition: unoevent.cxx:42
constexpr OUStringLiteral sAPI_ServiceName
Definition: unoevent.cxx:39
constexpr OUStringLiteral sStarBasic
Definition: unoevent.cxx:43
constexpr OUStringLiteral sMacroName
Definition: unoevent.cxx:41
constexpr OUStringLiteral sNone
Definition: unoevent.cxx:45
constexpr OUStringLiteral sEventType
Definition: unoevent.cxx:40