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