xref: /AOO41X/main/xmloff/source/forms/eventimport.cxx (revision 63bba73cc51e0afb45f8a8d578158724bb5afee8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include "eventimport.hxx"
27 #include <com/sun/star/script/XEventAttacherManager.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <comphelper/extract.hxx>
30 #include "strings.hxx"
31 
32 //.........................................................................
33 namespace xmloff
34 {
35 //.........................................................................
36 
37     using namespace ::com::sun::star::uno;
38     using namespace ::com::sun::star::beans;
39     using namespace ::com::sun::star::script;
40     using namespace ::com::sun::star::container;
41 
42     //=====================================================================
43     //= OFormEventsImportContext
44     //=====================================================================
45     //---------------------------------------------------------------------
OFormEventsImportContext(SvXMLImport & _rImport,sal_uInt16 _nPrefix,const::rtl::OUString & _rLocalName,IEventAttacher & _rEventAttacher)46     OFormEventsImportContext::OFormEventsImportContext(SvXMLImport& _rImport, sal_uInt16 _nPrefix, const ::rtl::OUString& _rLocalName, IEventAttacher& _rEventAttacher)
47         :XMLEventsImportContext(_rImport, _nPrefix, _rLocalName)
48         ,m_rEventAttacher(_rEventAttacher)
49     {
50     }
51 
52     //---------------------------------------------------------------------
EndElement()53     void OFormEventsImportContext::EndElement()
54     {
55         Sequence< ScriptEventDescriptor > aTranslated(aCollectEvents.size());
56         ScriptEventDescriptor* pTranslated = aTranslated.getArray();
57 
58         // loop through the collected events and translate them
59         const PropertyValue* pEventDescription;
60         const PropertyValue* pEventDescriptionEnd;
61         sal_Int32 nSeparatorPos = -1;
62         for (   EventsVector::const_iterator aEvent = aCollectEvents.begin();
63                 aEvent != aCollectEvents.end();
64                 ++aEvent, ++pTranslated
65             )
66         {
67             // the name of the event is built from ListenerType::EventMethod
68             nSeparatorPos = aEvent->first.indexOf(EVENT_NAME_SEPARATOR);
69             OSL_ENSURE(-1 != nSeparatorPos, "OFormEventsImportContext::EndElement: invalid (unrecognized) event name!");
70             pTranslated->ListenerType = aEvent->first.copy(0, nSeparatorPos);
71             pTranslated->EventMethod = aEvent->first.copy(nSeparatorPos + EVENT_NAME_SEPARATOR.length);
72 
73             ::rtl::OUString sLibrary;
74 
75             // the local macro name and the event type are specified as properties
76             pEventDescription       =                       aEvent->second.getConstArray();
77             pEventDescriptionEnd    =   pEventDescription + aEvent->second.getLength();
78             for (;pEventDescription != pEventDescriptionEnd; ++pEventDescription)
79             {
80                 if ((0 == pEventDescription->Name.compareToAscii(EVENT_LOCALMACRONAME)) ||
81                     (0 == pEventDescription->Name.compareToAscii(EVENT_SCRIPTURL)))
82                     pEventDescription->Value >>= pTranslated->ScriptCode;
83                 else if (0 == pEventDescription->Name.compareToAscii(EVENT_TYPE))
84                     pEventDescription->Value >>= pTranslated->ScriptType;
85                 else if ( 0 == pEventDescription->Name.compareToAscii( EVENT_LIBRARY ) )
86                     pEventDescription->Value >>= sLibrary;
87             }
88 
89             if ( 0 == pTranslated->ScriptType.compareToAscii( EVENT_STARBASIC ) )
90             {
91                 if ( 0 == sLibrary.compareToAscii( EVENT_STAROFFICE ) )
92                     sLibrary = EVENT_APPLICATION;
93 
94                 if ( sLibrary.getLength() )
95                 {
96                     // for StarBasic, the library is prepended
97                     sal_Unicode cLibSeparator = ':';
98                     sLibrary += ::rtl::OUString( &cLibSeparator, 1 );
99                 }
100                 sLibrary += pTranslated->ScriptCode;
101                 pTranslated->ScriptCode = sLibrary;
102             }
103         }
104 
105         // register the events
106         m_rEventAttacher.registerEvents(aTranslated);
107 
108         XMLEventsImportContext::EndElement();
109     }
110 
111     //=====================================================================
112     //= ODefaultEventAttacherManager
113     //=====================================================================
114 
~ODefaultEventAttacherManager()115     ODefaultEventAttacherManager::~ODefaultEventAttacherManager()
116     {
117     }
118 
119     //-------------------------------------------------------------------------
registerEvents(const Reference<XPropertySet> & _rxElement,const Sequence<ScriptEventDescriptor> & _rEvents)120     void ODefaultEventAttacherManager::registerEvents(const Reference< XPropertySet >& _rxElement,
121         const Sequence< ScriptEventDescriptor >& _rEvents)
122     {
123         OSL_ENSURE(m_aEvents.end() == m_aEvents.find(_rxElement),
124             "ODefaultEventAttacherManager::registerEvents: already have events for this object!");
125         // for the moment, only remember the script events
126         m_aEvents[_rxElement] = _rEvents;
127     }
128 
129     //-------------------------------------------------------------------------
setEvents(const Reference<XIndexAccess> & _rxContainer)130     void ODefaultEventAttacherManager::setEvents(const Reference< XIndexAccess >& _rxContainer)
131     {
132         Reference< XEventAttacherManager > xEventManager(_rxContainer, UNO_QUERY);
133         if (!xEventManager.is())
134         {
135             OSL_ENSURE(sal_False, "ODefaultEventAttacherManager::setEvents: invalid argument!");
136             return;
137         }
138 
139         // loop through all elements
140         sal_Int32 nCount = _rxContainer->getCount();
141         Reference< XPropertySet > xCurrent;
142         ConstMapPropertySet2ScriptSequenceIterator aRegisteredEventsPos;
143         for (sal_Int32 i=0; i<nCount; ++i)
144         {
145             ::cppu::extractInterface(xCurrent, _rxContainer->getByIndex(i));
146             if (xCurrent.is())
147             {
148                 aRegisteredEventsPos = m_aEvents.find(xCurrent);
149                 if (m_aEvents.end() != aRegisteredEventsPos)
150                     xEventManager->registerScriptEvents(i, aRegisteredEventsPos->second);
151             }
152         }
153     }
154 
155 //.........................................................................
156 }   // namespace xmloff
157 //.........................................................................
158 
159 
160