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