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