Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / config / evntconf.cxx
blobfd2e6fee2c71cacc903091a70ba573b42967d1d6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <boost/scoped_ptr.hpp>
32 #include <vcl/msgbox.hxx>
33 #include <svl/lstner.hxx>
34 #include <basic/basmgr.hxx>
35 #include <basic/sbmod.hxx>
36 #include <basic/sbx.hxx>
37 #include <sot/storage.hxx>
38 #include <unotools/securityoptions.hxx>
40 #include <rtl/ustring.h>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <framework/eventsconfiguration.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include <sfx2/evntconf.hxx>
46 #include <sfx2/docfile.hxx>
47 #include <sfx2/app.hxx>
48 #include <sfx2/objsh.hxx>
49 #include <sfx2/dispatch.hxx>
50 #include "sfx2/sfxresid.hxx"
51 #include "eventsupplier.hxx"
53 #include <com/sun/star/beans/PropertyValue.hpp>
54 #include <com/sun/star/container/XNameReplace.hpp>
55 #include <com/sun/star/document/XEventsSupplier.hpp>
56 #include <com/sun/star/uno/Sequence.hxx>
57 #include <com/sun/star/uno/Reference.hxx>
59 // -----------------------------------------------------------------------
60 TYPEINIT1(SfxEventHint, SfxHint);
61 TYPEINIT1(SfxEventNamesItem, SfxPoolItem);
62 TYPEINIT1(SfxViewEventHint, SfxEventHint);
64 using namespace com::sun::star;
66 SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& rTbl )
68 DelDtor();
69 for ( size_t i = 0, n = rTbl.size(); i < n; ++i )
71 SfxEventName* pTmp = rTbl.at( i );
72 SfxEventName* pNew = new SfxEventName( *pTmp );
73 aEventNamesList.push_back( pNew );
75 return *this;
78 void SfxEventNamesList::DelDtor()
80 for ( size_t i = 0, n = aEventNamesList.size(); i < n; ++i )
81 delete aEventNamesList[ i ];
82 aEventNamesList.clear();
85 int SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const
87 DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
89 const SfxEventNamesList& rOwn = aEventsList;
90 const SfxEventNamesList& rOther = ( (SfxEventNamesItem&) rAttr ).aEventsList;
92 if ( rOwn.size() != rOther.size() )
93 return sal_False;
95 for ( size_t nNo = 0, nCnt = rOwn.size(); nNo < nCnt; ++nNo )
97 const SfxEventName *pOwn = rOwn.at( nNo );
98 const SfxEventName *pOther = rOther.at( nNo );
99 if ( pOwn->mnId != pOther->mnId ||
100 pOwn->maEventName != pOther->maEventName ||
101 pOwn->maUIName != pOther->maUIName )
102 return sal_False;
105 return sal_True;
109 SfxItemPresentation SfxEventNamesItem::GetPresentation( SfxItemPresentation,
110 SfxMapUnit,
111 SfxMapUnit,
112 XubString &rText,
113 const IntlWrapper* ) const
115 rText.Erase();
116 return SFX_ITEM_PRESENTATION_NONE;
119 SfxPoolItem* SfxEventNamesItem::Clone( SfxItemPool *) const
121 return new SfxEventNamesItem(*this);
124 SfxPoolItem* SfxEventNamesItem::Create(SvStream &, sal_uInt16) const
126 OSL_FAIL("not streamable!");
127 return new SfxEventNamesItem(Which());
130 SvStream& SfxEventNamesItem::Store(SvStream &rStream, sal_uInt16 ) const
132 OSL_FAIL("not streamable!");
133 return rStream;
136 sal_uInt16 SfxEventNamesItem::GetVersion( sal_uInt16 ) const
138 OSL_FAIL("not streamable!");
139 return 0;
142 void SfxEventNamesItem::AddEvent( const String& rName, const String& rUIName, sal_uInt16 nID )
144 aEventsList.push_back( new SfxEventName( nID, rName, rUIName.Len() ? rUIName : rName ) );
148 //==========================================================================
150 //--------------------------------------------------------------------------
151 uno::Any CreateEventData_Impl( const SvxMacro *pMacro )
154 This function converts a SvxMacro into an Any containing three
155 properties. These properties are EventType and Script. Possible
156 values for EventType ar StarBasic, JavaScript, ...
157 The Script property should contain the URL to the macro and looks
158 like "macro://./standard.module1.main()"
160 If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
161 can delete an event binding.
163 uno::Any aEventData;
165 if ( pMacro )
167 if ( pMacro->GetScriptType() == STARBASIC )
169 uno::Sequence < beans::PropertyValue > aProperties(3);
170 beans::PropertyValue *pValues = aProperties.getArray();
172 ::rtl::OUString aType(STAR_BASIC );
173 ::rtl::OUString aLib = pMacro->GetLibName();
174 ::rtl::OUString aMacro = pMacro->GetMacName();
176 pValues[ 0 ].Name = ::rtl::OUString(PROP_EVENT_TYPE );
177 pValues[ 0 ].Value <<= aType;
179 pValues[ 1 ].Name = ::rtl::OUString(PROP_LIBRARY );
180 pValues[ 1 ].Value <<= aLib;
182 pValues[ 2 ].Name = ::rtl::OUString(PROP_MACRO_NAME );
183 pValues[ 2 ].Value <<= aMacro;
185 aEventData <<= aProperties;
187 else if ( pMacro->GetScriptType() == EXTENDED_STYPE )
189 uno::Sequence < beans::PropertyValue > aProperties(2);
190 beans::PropertyValue *pValues = aProperties.getArray();
192 ::rtl::OUString aLib = pMacro->GetLibName();
193 ::rtl::OUString aMacro = pMacro->GetMacName();
195 pValues[ 0 ].Name = ::rtl::OUString(PROP_EVENT_TYPE );
196 pValues[ 0 ].Value <<= aLib;
198 pValues[ 1 ].Name = ::rtl::OUString(PROP_SCRIPT );
199 pValues[ 1 ].Value <<= aMacro;
201 aEventData <<= aProperties;
203 else if ( pMacro->GetScriptType() == JAVASCRIPT )
205 uno::Sequence < beans::PropertyValue > aProperties(2);
206 beans::PropertyValue *pValues = aProperties.getArray();
208 ::rtl::OUString aMacro = pMacro->GetMacName();
210 pValues[ 0 ].Name = ::rtl::OUString(PROP_EVENT_TYPE );
211 pValues[ 0 ].Value <<= ::rtl::OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT);
213 pValues[ 1 ].Name = ::rtl::OUString(PROP_MACRO_NAME );
214 pValues[ 1 ].Value <<= aMacro;
216 aEventData <<= aProperties;
218 else
220 SAL_WARN( "sfx2.config", "CreateEventData_Impl(): ScriptType not supported!");
223 else
225 uno::Sequence < beans::PropertyValue > aProperties;
226 aEventData <<= aProperties;
229 return aEventData;
232 //--------------------------------------------------------------------------
233 void PropagateEvent_Impl( SfxObjectShell *pDoc, rtl::OUString aEventName, const SvxMacro* pMacro )
235 uno::Reference < document::XEventsSupplier > xSupplier;
236 if ( pDoc )
238 xSupplier = uno::Reference < document::XEventsSupplier >( pDoc->GetModel(), uno::UNO_QUERY );
240 else
242 xSupplier = uno::Reference < document::XEventsSupplier >
243 ( ::comphelper::getProcessServiceFactory()->createInstance(
244 rtl::OUString("com.sun.star.frame.GlobalEventBroadcaster")), uno::UNO_QUERY );
247 if ( xSupplier.is() )
249 uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents();
250 if ( !aEventName.isEmpty() )
252 uno::Any aEventData = CreateEventData_Impl( pMacro );
256 xEvents->replaceByName( aEventName, aEventData );
258 catch( const ::com::sun::star::lang::IllegalArgumentException& )
260 SAL_WARN( "sfx2.config", "PropagateEvents_Impl: caught IllegalArgumentException" );
262 catch( const ::com::sun::star::container::NoSuchElementException& )
264 SAL_WARN( "sfx2.config", "PropagateEvents_Impl: caught NoSuchElementException" );
267 else {
268 DBG_WARNING( "PropagateEvents_Impl: Got unkown event" );
273 //--------------------------------------------------------------------------------------------------------
274 void SfxEventConfiguration::ConfigureEvent( rtl::OUString aName, const SvxMacro& rMacro, SfxObjectShell *pDoc )
276 boost::scoped_ptr<SvxMacro> pMacro;
277 if ( rMacro.HasMacro() )
278 pMacro.reset( new SvxMacro( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() ) );
279 PropagateEvent_Impl( pDoc ? pDoc : 0, aName, pMacro.get() );
282 // -------------------------------------------------------------------------------------------------------
283 SvxMacro* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any& rElement, SfxObjectShell* pDoc, sal_Bool bBlowUp )
285 return SfxEvents_Impl::ConvertToMacro( rElement, pDoc, bBlowUp );
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */