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