LibreOffice Module xmloff (master) 1
eventimport.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 "eventimport.hxx"
21#include <com/sun/star/script/XEventAttacherManager.hpp>
22#include <com/sun/star/beans/PropertyValue.hpp>
23#include <osl/diagnose.h>
24#include "strings.hxx"
25
26namespace xmloff
27{
28
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::beans;
31 using namespace ::com::sun::star::script;
32 using namespace ::com::sun::star::container;
33
34 //= OFormEventsImportContext
36 :XMLEventsImportContext(_rImport)
37 ,m_rEventAttacher(_rEventAttacher)
38 {
39 }
40
42 {
43 Sequence< ScriptEventDescriptor > aTranslated(m_aCollectEvents.size());
44 ScriptEventDescriptor* pTranslated = aTranslated.getArray();
45
46 // loop through the collected events and translate them
47 sal_Int32 nSeparatorPos = -1;
48 for ( const auto& rEvent : m_aCollectEvents )
49 {
50 // the name of the event is built from ListenerType::EventMethod
51 nSeparatorPos = rEvent.first.indexOf(EVENT_NAME_SEPARATOR);
52 OSL_ENSURE(-1 != nSeparatorPos, "OFormEventsImportContext::EndElement: invalid (unrecognized) event name!");
53 pTranslated->ListenerType = rEvent.first.copy(0, nSeparatorPos);
54 pTranslated->EventMethod = rEvent.first.copy(nSeparatorPos + sizeof(EVENT_NAME_SEPARATOR) - 1);
55
56 OUString sLibrary;
57
58 // the local macro name and the event type are specified as properties
59 const PropertyValue* pEventDescription = rEvent.second.getConstArray();
60 const PropertyValue* pEventDescriptionEnd = pEventDescription + rEvent.second.getLength();
61 for (;pEventDescription != pEventDescriptionEnd; ++pEventDescription)
62 {
63 if (pEventDescription->Name == EVENT_LOCALMACRONAME ||
64 pEventDescription->Name == EVENT_SCRIPTURL)
65 pEventDescription->Value >>= pTranslated->ScriptCode;
66 else if (pEventDescription->Name == EVENT_TYPE)
67 pEventDescription->Value >>= pTranslated->ScriptType;
68 else if (pEventDescription->Name == EVENT_LIBRARY)
69 pEventDescription->Value >>= sLibrary;
70 }
71
72 if (pTranslated->ScriptType == EVENT_STARBASIC)
73 {
76
77 if ( !sLibrary.isEmpty() )
78 {
79 // for StarBasic, the library is prepended
80 sLibrary += ":";
81 }
82 sLibrary += pTranslated->ScriptCode;
83 pTranslated->ScriptCode = sLibrary;
84 }
85
86 ++pTranslated;
87 }
88
89 // register the events
91 }
92
93 //= ODefaultEventAttacherManager
94
96 {
97 }
98
99 void ODefaultEventAttacherManager::registerEvents(const Reference< XPropertySet >& _rxElement,
100 const Sequence< ScriptEventDescriptor >& _rEvents)
101 {
102 OSL_ENSURE(m_aEvents.end() == m_aEvents.find(_rxElement),
103 "ODefaultEventAttacherManager::registerEvents: already have events for this object!");
104 // for the moment, only remember the script events
105 m_aEvents[_rxElement] = _rEvents;
106 }
107
108 void ODefaultEventAttacherManager::setEvents(const Reference< XIndexAccess >& _rxContainer)
109 {
110 Reference< XEventAttacherManager > xEventManager(_rxContainer, UNO_QUERY);
111 if (!xEventManager.is())
112 {
113 OSL_FAIL("ODefaultEventAttacherManager::setEvents: invalid argument!");
114 return;
115 }
116
117 // loop through all elements
118 sal_Int32 nCount = _rxContainer->getCount();
119 Reference< XPropertySet > xCurrent;
120 MapPropertySet2ScriptSequence::const_iterator aRegisteredEventsPos;
121 for (sal_Int32 i=0; i<nCount; ++i)
122 {
123 xCurrent.set(_rxContainer->getByIndex(i), css::uno::UNO_QUERY);
124 if (xCurrent.is())
125 {
126 aRegisteredEventsPos = m_aEvents.find(xCurrent);
127 if (m_aEvents.end() != aRegisteredEventsPos)
128 xEventManager->registerScriptEvents(i, aRegisteredEventsPos->second);
129 }
130 }
131 }
132
133} // namespace xmloff
134
135/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Import <script:events> element.
std::vector< EventNameValuesPair > m_aCollectEvents
virtual void registerEvents(const css::uno::Sequence< css::script::ScriptEventDescriptor > &_rEvents)=0
MapPropertySet2ScriptSequence m_aEvents
Definition: eventimport.hxx:58
virtual void registerEvents(const css::uno::Reference< css::beans::XPropertySet > &_rxElement, const css::uno::Sequence< css::script::ScriptEventDescriptor > &_rEvents) override
Definition: eventimport.cxx:99
void setEvents(const css::uno::Reference< css::container::XIndexAccess > &_rxContainer)
OFormEventsImportContext(SvXMLImport &_rImport, IEventAttacher &_rEventAttacher)
Definition: eventimport.cxx:35
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
Definition: eventimport.cxx:41
int nCount
int i
constexpr OUStringLiteral EVENT_APPLICATION
Definition: strings.hxx:204
constexpr OUStringLiteral EVENT_LIBRARY
Definition: strings.hxx:199
constexpr OUStringLiteral EVENT_TYPE
Definition: strings.hxx:198
constexpr OUStringLiteral EVENT_SCRIPTURL
Definition: strings.hxx:201
constexpr OUStringLiteral EVENT_STAROFFICE
Definition: strings.hxx:202
constexpr OUStringLiteral EVENT_LOCALMACRONAME
Definition: strings.hxx:200
#define EVENT_STARBASIC
Definition: strings.hxx:203
#define EVENT_NAME_SEPARATOR
Definition: strings.hxx:197
constexpr OUStringLiteral sLibrary