Line data Source code
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 "strings.hxx"
24 :
25 : namespace xmloff
26 : {
27 :
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::com::sun::star::beans;
30 : using namespace ::com::sun::star::script;
31 : using namespace ::com::sun::star::container;
32 :
33 : //= OFormEventsImportContext
34 8 : OFormEventsImportContext::OFormEventsImportContext(SvXMLImport& _rImport, sal_uInt16 _nPrefix, const OUString& _rLocalName, IEventAttacher& _rEventAttacher)
35 : :XMLEventsImportContext(_rImport, _nPrefix, _rLocalName)
36 8 : ,m_rEventAttacher(_rEventAttacher)
37 : {
38 8 : }
39 :
40 8 : void OFormEventsImportContext::EndElement()
41 : {
42 8 : Sequence< ScriptEventDescriptor > aTranslated(aCollectEvents.size());
43 8 : ScriptEventDescriptor* pTranslated = aTranslated.getArray();
44 :
45 : // loop through the collected events and translate them
46 : const PropertyValue* pEventDescription;
47 : const PropertyValue* pEventDescriptionEnd;
48 8 : sal_Int32 nSeparatorPos = -1;
49 48 : for ( EventsVector::const_iterator aEvent = aCollectEvents.begin();
50 32 : aEvent != aCollectEvents.end();
51 : ++aEvent, ++pTranslated
52 : )
53 : {
54 : // the name of the event is built from ListenerType::EventMethod
55 8 : nSeparatorPos = aEvent->first.indexOf(EVENT_NAME_SEPARATOR);
56 : OSL_ENSURE(-1 != nSeparatorPos, "OFormEventsImportContext::EndElement: invalid (unrecognized) event name!");
57 8 : pTranslated->ListenerType = aEvent->first.copy(0, nSeparatorPos);
58 8 : pTranslated->EventMethod = aEvent->first.copy(nSeparatorPos + sizeof(EVENT_NAME_SEPARATOR) - 1);
59 :
60 8 : OUString sLibrary;
61 :
62 : // the local macro name and the event type are specified as properties
63 8 : pEventDescription = aEvent->second.getConstArray();
64 8 : pEventDescriptionEnd = pEventDescription + aEvent->second.getLength();
65 27 : for (;pEventDescription != pEventDescriptionEnd; ++pEventDescription)
66 : {
67 70 : if ((pEventDescription->Name.equals(EVENT_LOCALMACRONAME)) ||
68 51 : (pEventDescription->Name.equals(EVENT_SCRIPTURL)))
69 8 : pEventDescription->Value >>= pTranslated->ScriptCode;
70 11 : else if (pEventDescription->Name.equals(EVENT_TYPE))
71 8 : pEventDescription->Value >>= pTranslated->ScriptType;
72 3 : else if (pEventDescription->Name.equals(EVENT_LIBRARY))
73 3 : pEventDescription->Value >>= sLibrary;
74 : }
75 :
76 8 : if (pTranslated->ScriptType.equals(EVENT_STARBASIC))
77 : {
78 3 : if (sLibrary.equals(EVENT_STAROFFICE))
79 0 : sLibrary = EVENT_APPLICATION;
80 :
81 3 : if ( !sLibrary.isEmpty() )
82 : {
83 : // for StarBasic, the library is prepended
84 3 : sal_Unicode cLibSeparator = ':';
85 3 : sLibrary += OUString( &cLibSeparator, 1 );
86 : }
87 3 : sLibrary += pTranslated->ScriptCode;
88 3 : pTranslated->ScriptCode = sLibrary;
89 : }
90 8 : }
91 :
92 : // register the events
93 8 : m_rEventAttacher.registerEvents(aTranslated);
94 :
95 8 : XMLEventsImportContext::EndElement();
96 8 : }
97 :
98 : //= ODefaultEventAttacherManager
99 :
100 632 : ODefaultEventAttacherManager::~ODefaultEventAttacherManager()
101 : {
102 632 : }
103 :
104 8 : void ODefaultEventAttacherManager::registerEvents(const Reference< XPropertySet >& _rxElement,
105 : const Sequence< ScriptEventDescriptor >& _rEvents)
106 : {
107 : OSL_ENSURE(m_aEvents.end() == m_aEvents.find(_rxElement),
108 : "ODefaultEventAttacherManager::registerEvents: already have events for this object!");
109 : // for the moment, only remember the script events
110 8 : m_aEvents[_rxElement] = _rEvents;
111 8 : }
112 :
113 18 : void ODefaultEventAttacherManager::setEvents(const Reference< XIndexAccess >& _rxContainer)
114 : {
115 18 : Reference< XEventAttacherManager > xEventManager(_rxContainer, UNO_QUERY);
116 18 : if (!xEventManager.is())
117 : {
118 : OSL_FAIL("ODefaultEventAttacherManager::setEvents: invalid argument!");
119 18 : return;
120 : }
121 :
122 : // loop through all elements
123 18 : sal_Int32 nCount = _rxContainer->getCount();
124 36 : Reference< XPropertySet > xCurrent;
125 18 : MapPropertySet2ScriptSequence::const_iterator aRegisteredEventsPos;
126 56 : for (sal_Int32 i=0; i<nCount; ++i)
127 : {
128 38 : xCurrent.set(_rxContainer->getByIndex(i), css::uno::UNO_QUERY);
129 38 : if (xCurrent.is())
130 : {
131 38 : aRegisteredEventsPos = m_aEvents.find(xCurrent);
132 38 : if (m_aEvents.end() != aRegisteredEventsPos)
133 8 : xEventManager->registerScriptEvents(i, aRegisteredEventsPos->second);
134 : }
135 18 : }
136 : }
137 :
138 : } // namespace xmloff
139 :
140 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|