1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir #include "eventimport.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/script/XEventAttacherManager.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
33*cdf0e10cSrcweir #include <comphelper/extract.hxx>
34*cdf0e10cSrcweir #include "strings.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //.........................................................................
37*cdf0e10cSrcweir namespace xmloff
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir //.........................................................................
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
42*cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
43*cdf0e10cSrcweir 	using namespace ::com::sun::star::script;
44*cdf0e10cSrcweir 	using namespace ::com::sun::star::container;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 	//=====================================================================
47*cdf0e10cSrcweir 	//= OFormEventsImportContext
48*cdf0e10cSrcweir 	//=====================================================================
49*cdf0e10cSrcweir 	//---------------------------------------------------------------------
50*cdf0e10cSrcweir 	OFormEventsImportContext::OFormEventsImportContext(SvXMLImport& _rImport, sal_uInt16 _nPrefix, const ::rtl::OUString& _rLocalName, IEventAttacher& _rEventAttacher)
51*cdf0e10cSrcweir 		:XMLEventsImportContext(_rImport, _nPrefix, _rLocalName)
52*cdf0e10cSrcweir 		,m_rEventAttacher(_rEventAttacher)
53*cdf0e10cSrcweir 	{
54*cdf0e10cSrcweir 	}
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	//---------------------------------------------------------------------
57*cdf0e10cSrcweir 	void OFormEventsImportContext::EndElement()
58*cdf0e10cSrcweir 	{
59*cdf0e10cSrcweir 		Sequence< ScriptEventDescriptor > aTranslated(aCollectEvents.size());
60*cdf0e10cSrcweir 		ScriptEventDescriptor* pTranslated = aTranslated.getArray();
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 		// loop through the collected events and translate them
63*cdf0e10cSrcweir 		const PropertyValue* pEventDescription;
64*cdf0e10cSrcweir 		const PropertyValue* pEventDescriptionEnd;
65*cdf0e10cSrcweir 		sal_Int32 nSeparatorPos = -1;
66*cdf0e10cSrcweir 		for	(	EventsVector::const_iterator aEvent = aCollectEvents.begin();
67*cdf0e10cSrcweir 				aEvent != aCollectEvents.end();
68*cdf0e10cSrcweir 				++aEvent, ++pTranslated
69*cdf0e10cSrcweir 			)
70*cdf0e10cSrcweir 		{
71*cdf0e10cSrcweir 			// the name of the event is built from ListenerType::EventMethod
72*cdf0e10cSrcweir 			nSeparatorPos = aEvent->first.indexOf(EVENT_NAME_SEPARATOR);
73*cdf0e10cSrcweir 			OSL_ENSURE(-1 != nSeparatorPos, "OFormEventsImportContext::EndElement: invalid (unrecognized) event name!");
74*cdf0e10cSrcweir 			pTranslated->ListenerType = aEvent->first.copy(0, nSeparatorPos);
75*cdf0e10cSrcweir 			pTranslated->EventMethod = aEvent->first.copy(nSeparatorPos + EVENT_NAME_SEPARATOR.length);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 			::rtl::OUString sLibrary;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 			// the local macro name and the event type are specified as properties
80*cdf0e10cSrcweir 			pEventDescription		=						aEvent->second.getConstArray();
81*cdf0e10cSrcweir 			pEventDescriptionEnd	=	pEventDescription + aEvent->second.getLength();
82*cdf0e10cSrcweir 			for (;pEventDescription != pEventDescriptionEnd; ++pEventDescription)
83*cdf0e10cSrcweir 			{
84*cdf0e10cSrcweir 				if ((0 == pEventDescription->Name.compareToAscii(EVENT_LOCALMACRONAME)) ||
85*cdf0e10cSrcweir 					(0 == pEventDescription->Name.compareToAscii(EVENT_SCRIPTURL)))
86*cdf0e10cSrcweir 					pEventDescription->Value >>= pTranslated->ScriptCode;
87*cdf0e10cSrcweir 				else if (0 == pEventDescription->Name.compareToAscii(EVENT_TYPE))
88*cdf0e10cSrcweir 					pEventDescription->Value >>= pTranslated->ScriptType;
89*cdf0e10cSrcweir 				else if ( 0 == pEventDescription->Name.compareToAscii( EVENT_LIBRARY ) )
90*cdf0e10cSrcweir 					pEventDescription->Value >>= sLibrary;
91*cdf0e10cSrcweir 			}
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 			if ( 0 == pTranslated->ScriptType.compareToAscii( EVENT_STARBASIC ) )
94*cdf0e10cSrcweir 			{
95*cdf0e10cSrcweir 				if ( 0 == sLibrary.compareToAscii( EVENT_STAROFFICE ) )
96*cdf0e10cSrcweir 					sLibrary = EVENT_APPLICATION;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 				if ( sLibrary.getLength() )
99*cdf0e10cSrcweir 				{
100*cdf0e10cSrcweir 					// for StarBasic, the library is prepended
101*cdf0e10cSrcweir 					sal_Unicode cLibSeparator = ':';
102*cdf0e10cSrcweir 					sLibrary += ::rtl::OUString( &cLibSeparator, 1 );
103*cdf0e10cSrcweir 				}
104*cdf0e10cSrcweir 				sLibrary += pTranslated->ScriptCode;
105*cdf0e10cSrcweir 				pTranslated->ScriptCode = sLibrary;
106*cdf0e10cSrcweir 			}
107*cdf0e10cSrcweir 		}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 		// register the events
110*cdf0e10cSrcweir 		m_rEventAttacher.registerEvents(aTranslated);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		XMLEventsImportContext::EndElement();
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	//=====================================================================
116*cdf0e10cSrcweir 	//= ODefaultEventAttacherManager
117*cdf0e10cSrcweir 	//=====================================================================
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	ODefaultEventAttacherManager::~ODefaultEventAttacherManager()
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	//-------------------------------------------------------------------------
124*cdf0e10cSrcweir 	void ODefaultEventAttacherManager::registerEvents(const Reference< XPropertySet >& _rxElement,
125*cdf0e10cSrcweir 		const Sequence< ScriptEventDescriptor >& _rEvents)
126*cdf0e10cSrcweir 	{
127*cdf0e10cSrcweir 		OSL_ENSURE(m_aEvents.end() == m_aEvents.find(_rxElement),
128*cdf0e10cSrcweir 			"ODefaultEventAttacherManager::registerEvents: already have events for this object!");
129*cdf0e10cSrcweir 		// for the moment, only remember the script events
130*cdf0e10cSrcweir 		m_aEvents[_rxElement] = _rEvents;
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	//-------------------------------------------------------------------------
134*cdf0e10cSrcweir 	void ODefaultEventAttacherManager::setEvents(const Reference< XIndexAccess >& _rxContainer)
135*cdf0e10cSrcweir 	{
136*cdf0e10cSrcweir 		Reference< XEventAttacherManager > xEventManager(_rxContainer, UNO_QUERY);
137*cdf0e10cSrcweir 		if (!xEventManager.is())
138*cdf0e10cSrcweir 		{
139*cdf0e10cSrcweir 			OSL_ENSURE(sal_False, "ODefaultEventAttacherManager::setEvents: invalid argument!");
140*cdf0e10cSrcweir 			return;
141*cdf0e10cSrcweir 		}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 		// loop through all elements
144*cdf0e10cSrcweir 		sal_Int32 nCount = _rxContainer->getCount();
145*cdf0e10cSrcweir 		Reference< XPropertySet > xCurrent;
146*cdf0e10cSrcweir 		ConstMapPropertySet2ScriptSequenceIterator aRegisteredEventsPos;
147*cdf0e10cSrcweir 		for (sal_Int32 i=0; i<nCount; ++i)
148*cdf0e10cSrcweir 		{
149*cdf0e10cSrcweir 			::cppu::extractInterface(xCurrent, _rxContainer->getByIndex(i));
150*cdf0e10cSrcweir 			if (xCurrent.is())
151*cdf0e10cSrcweir 			{
152*cdf0e10cSrcweir 				aRegisteredEventsPos = m_aEvents.find(xCurrent);
153*cdf0e10cSrcweir 				if (m_aEvents.end() != aRegisteredEventsPos)
154*cdf0e10cSrcweir 					xEventManager->registerScriptEvents(i, aRegisteredEventsPos->second);
155*cdf0e10cSrcweir 			}
156*cdf0e10cSrcweir 		}
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //.........................................................................
160*cdf0e10cSrcweir }	// namespace xmloff
161*cdf0e10cSrcweir //.........................................................................
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 
164