bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / config / evntconf.cxx
blob20c09d9b0d3c3616a1fc2c1f34e8ed8429ce6354
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <boost/scoped_ptr.hpp>
23 #include <vcl/msgbox.hxx>
24 #include <svl/lstner.hxx>
25 #include <basic/basmgr.hxx>
26 #include <basic/sbmod.hxx>
27 #include <basic/sbx.hxx>
28 #include <sot/storage.hxx>
29 #include <unotools/securityoptions.hxx>
31 #include <rtl/ustring.h>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <framework/eventsconfiguration.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <sfx2/evntconf.hxx>
37 #include <sfx2/docfile.hxx>
38 #include <sfx2/app.hxx>
39 #include <sfx2/objsh.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include <sfx2/sfxresid.hxx>
42 #include "eventsupplier.hxx"
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/container/XNameReplace.hpp>
46 #include <com/sun/star/document/XEventsSupplier.hpp>
47 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
48 #include <com/sun/star/uno/Sequence.hxx>
49 #include <com/sun/star/uno/Reference.hxx>
52 TYPEINIT1(SfxEventNamesItem, SfxPoolItem);
54 using namespace com::sun::star;
56 SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& rTbl )
58 DelDtor();
59 for ( size_t i = 0, n = rTbl.size(); i < n; ++i )
61 SfxEventName* pTmp = rTbl.at( i );
62 SfxEventName* pNew = new SfxEventName( *pTmp );
63 aEventNamesList.push_back( pNew );
65 return *this;
68 void SfxEventNamesList::DelDtor()
70 for ( size_t i = 0, n = aEventNamesList.size(); i < n; ++i )
71 delete aEventNamesList[ i ];
72 aEventNamesList.clear();
75 bool SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const
77 DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
79 const SfxEventNamesList& rOwn = aEventsList;
80 const SfxEventNamesList& rOther = static_cast<const SfxEventNamesItem&>( rAttr ).aEventsList;
82 if ( rOwn.size() != rOther.size() )
83 return false;
85 for ( size_t nNo = 0, nCnt = rOwn.size(); nNo < nCnt; ++nNo )
87 const SfxEventName *pOwn = rOwn.at( nNo );
88 const SfxEventName *pOther = rOther.at( nNo );
89 if ( pOwn->mnId != pOther->mnId ||
90 pOwn->maEventName != pOther->maEventName ||
91 pOwn->maUIName != pOther->maUIName )
92 return false;
95 return true;
99 bool SfxEventNamesItem::GetPresentation( SfxItemPresentation,
100 SfxMapUnit,
101 SfxMapUnit,
102 OUString &rText,
103 const IntlWrapper* ) const
105 rText.clear();
106 return false;
109 SfxPoolItem* SfxEventNamesItem::Clone( SfxItemPool *) const
111 return new SfxEventNamesItem(*this);
114 SfxPoolItem* SfxEventNamesItem::Create(SvStream &, sal_uInt16) const
116 OSL_FAIL("not streamable!");
117 return new SfxEventNamesItem(Which());
120 SvStream& SfxEventNamesItem::Store(SvStream &rStream, sal_uInt16 ) const
122 OSL_FAIL("not streamable!");
123 return rStream;
126 sal_uInt16 SfxEventNamesItem::GetVersion( sal_uInt16 ) const
128 OSL_FAIL("not streamable!");
129 return 0;
132 void SfxEventNamesItem::AddEvent( const OUString& rName, const OUString& rUIName, sal_uInt16 nID )
134 aEventsList.push_back( new SfxEventName( nID, rName, !rUIName.isEmpty() ? rUIName : rName ) );
141 uno::Any CreateEventData_Impl( const SvxMacro *pMacro )
144 This function converts a SvxMacro into an Any containing three
145 properties. These properties are EventType and Script. Possible
146 values for EventType ar StarBasic, JavaScript, ...
147 The Script property should contain the URL to the macro and looks
148 like "macro://./standard.module1.main()"
150 If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
151 can delete an event binding.
153 uno::Any aEventData;
155 if ( pMacro )
157 if ( pMacro->GetScriptType() == STARBASIC )
159 uno::Sequence < beans::PropertyValue > aProperties(3);
160 beans::PropertyValue *pValues = aProperties.getArray();
162 OUString aType(STAR_BASIC );
163 OUString aLib = pMacro->GetLibName();
164 OUString aMacro = pMacro->GetMacName();
166 pValues[ 0 ].Name = PROP_EVENT_TYPE;
167 pValues[ 0 ].Value <<= aType;
169 pValues[ 1 ].Name = PROP_LIBRARY;
170 pValues[ 1 ].Value <<= aLib;
172 pValues[ 2 ].Name = PROP_MACRO_NAME;
173 pValues[ 2 ].Value <<= aMacro;
175 aEventData <<= aProperties;
177 else if ( pMacro->GetScriptType() == EXTENDED_STYPE )
179 uno::Sequence < beans::PropertyValue > aProperties(2);
180 beans::PropertyValue *pValues = aProperties.getArray();
182 OUString aLib = pMacro->GetLibName();
183 OUString aMacro = pMacro->GetMacName();
185 pValues[ 0 ].Name = PROP_EVENT_TYPE;
186 pValues[ 0 ].Value <<= aLib;
188 pValues[ 1 ].Name = PROP_SCRIPT;
189 pValues[ 1 ].Value <<= aMacro;
191 aEventData <<= aProperties;
193 else if ( pMacro->GetScriptType() == JAVASCRIPT )
195 uno::Sequence < beans::PropertyValue > aProperties(2);
196 beans::PropertyValue *pValues = aProperties.getArray();
198 OUString aMacro = pMacro->GetMacName();
200 pValues[ 0 ].Name = PROP_EVENT_TYPE;
201 pValues[ 0 ].Value <<= OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT);
203 pValues[ 1 ].Name = PROP_MACRO_NAME;
204 pValues[ 1 ].Value <<= aMacro;
206 aEventData <<= aProperties;
208 else
210 SAL_WARN( "sfx.config", "CreateEventData_Impl(): ScriptType not supported!");
213 else
215 uno::Sequence < beans::PropertyValue > aProperties;
216 aEventData <<= aProperties;
219 return aEventData;
223 void PropagateEvent_Impl( SfxObjectShell *pDoc, const OUString& aEventName, const SvxMacro* pMacro )
225 uno::Reference < document::XEventsSupplier > xSupplier;
226 if ( pDoc )
228 xSupplier = uno::Reference < document::XEventsSupplier >( pDoc->GetModel(), uno::UNO_QUERY );
230 else
232 xSupplier = uno::Reference < document::XEventsSupplier >
233 ( frame::theGlobalEventBroadcaster::get(::comphelper::getProcessComponentContext()),
234 uno::UNO_QUERY );
237 if ( xSupplier.is() )
239 uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents();
240 if ( !aEventName.isEmpty() )
242 uno::Any aEventData = CreateEventData_Impl( pMacro );
246 xEvents->replaceByName( aEventName, aEventData );
248 catch( const ::com::sun::star::lang::IllegalArgumentException& )
250 SAL_WARN( "sfx.config", "PropagateEvents_Impl: caught IllegalArgumentException" );
252 catch( const ::com::sun::star::container::NoSuchElementException& )
254 SAL_WARN( "sfx.config", "PropagateEvents_Impl: caught NoSuchElementException" );
257 else {
258 DBG_WARNING( "PropagateEvents_Impl: Got unknown event" );
264 void SfxEventConfiguration::ConfigureEvent( const OUString& aName, const SvxMacro& rMacro, SfxObjectShell *pDoc )
266 boost::scoped_ptr<SvxMacro> pMacro;
267 if ( rMacro.HasMacro() )
268 pMacro.reset( new SvxMacro( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() ) );
269 PropagateEvent_Impl( pDoc ? pDoc : 0, aName, pMacro.get() );
273 SvxMacro* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any& rElement, SfxObjectShell* pDoc, bool bBlowUp )
275 return SfxEvents_Impl::ConvertToMacro( rElement, pDoc, bBlowUp );
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */