nss: upgrade to release 3.73
[LibreOffice.git] / sfx2 / source / notify / eventsupplier.cxx
blob65a50da3ef75222b45e380441e845e03966b35dc
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 .
20 #include <com/sun/star/beans/PropertyValue.hpp>
22 #include <com/sun/star/document/XEmbeddedScripts.hpp>
23 #include <com/sun/star/document/XScriptInvocationContext.hpp>
24 #include <com/sun/star/util/URL.hpp>
25 #include <com/sun/star/frame/Desktop.hpp>
26 #include <com/sun/star/util/URLTransformer.hpp>
27 #include <com/sun/star/util/XURLTransformer.hpp>
28 #include <com/sun/star/uno/XInterface.hpp>
29 #include <tools/urlobj.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <svl/macitem.hxx>
32 #include <sfx2/objsh.hxx>
33 #include <sfx2/evntconf.hxx>
34 #include <unotools/eventcfg.hxx>
35 #include <sal/log.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <comphelper/namedvaluecollection.hxx>
39 #include <comphelper/sequence.hxx>
40 #include <eventsupplier.hxx>
42 #include <sfx2/app.hxx>
44 #include <sfx2/viewfrm.hxx>
45 #include <sfx2/frame.hxx>
46 #include <macroloader.hxx>
48 using namespace css;
49 using namespace ::com::sun::star;
53 // --- XNameReplace ---
55 void SAL_CALL SfxEvents_Impl::replaceByName( const OUString & aName, const uno::Any & rElement )
57 ::osl::MutexGuard aGuard( maMutex );
59 // find the event in the list and replace the data
60 auto nIndex = comphelper::findValue(maEventNames, aName);
61 if (nIndex != -1)
63 // check for correct type of the element
64 if ( !::comphelper::NamedValueCollection::canExtractFrom( rElement ) )
65 throw lang::IllegalArgumentException();
66 ::comphelper::NamedValueCollection const aEventDescriptor( rElement );
68 // create Configuration at first, creation might call this method also and that would overwrite everything
69 // we might have stored before!
70 if ( mpObjShell && !mpObjShell->IsLoading() )
71 mpObjShell->SetModified();
73 ::comphelper::NamedValueCollection aNormalizedDescriptor;
74 NormalizeMacro( aEventDescriptor, aNormalizedDescriptor, mpObjShell );
76 OUString sType;
77 if ( ( aNormalizedDescriptor.size() == 1 )
78 && !aNormalizedDescriptor.has( PROP_EVENT_TYPE ) //TODO
79 && ( aNormalizedDescriptor.get( PROP_EVENT_TYPE ) >>= sType )
80 && ( sType.isEmpty() )
83 // An empty event type means no binding. Therefore reset data
84 // to reflect that state.
85 // (that's for compatibility only. Nowadays, the Tools/Customize dialog should
86 // set an empty sequence to indicate the request for resetting the assignment.)
87 OSL_ENSURE( false, "legacy event assignment format detected" );
88 aNormalizedDescriptor.clear();
91 if ( !aNormalizedDescriptor.empty() )
93 maEventData[nIndex] <<= aNormalizedDescriptor.getPropertyValues();
95 else
97 maEventData[nIndex].clear();
99 return;
102 throw container::NoSuchElementException();
106 // --- XNameAccess ---
108 uno::Any SAL_CALL SfxEvents_Impl::getByName( const OUString& aName )
110 ::osl::MutexGuard aGuard( maMutex );
112 // find the event in the list and return the data
114 auto nIndex = comphelper::findValue(maEventNames, aName);
115 if (nIndex != -1)
116 return maEventData[nIndex];
118 throw container::NoSuchElementException();
122 uno::Sequence< OUString > SAL_CALL SfxEvents_Impl::getElementNames()
124 return maEventNames;
128 sal_Bool SAL_CALL SfxEvents_Impl::hasByName( const OUString& aName )
130 ::osl::MutexGuard aGuard( maMutex );
132 // find the event in the list and return the data
134 return comphelper::findValue(maEventNames, aName) != -1;
138 // --- XElementAccess ( parent of XNameAccess ) ---
140 uno::Type SAL_CALL SfxEvents_Impl::getElementType()
142 uno::Type aElementType = cppu::UnoType<uno::Sequence < beans::PropertyValue >>::get();
143 return aElementType;
147 sal_Bool SAL_CALL SfxEvents_Impl::hasElements()
149 ::osl::MutexGuard aGuard( maMutex );
151 return maEventNames.hasElements();
154 void SfxEvents_Impl::Execute( uno::Any const & aEventData, const document::DocumentEvent& aTrigger, SfxObjectShell* pDoc )
156 uno::Sequence < beans::PropertyValue > aProperties;
157 if ( !(aEventData >>= aProperties) )
158 return;
160 OUString aType;
161 OUString aScript;
162 OUString aLibrary;
163 OUString aMacroName;
165 if ( !aProperties.hasElements() )
166 return;
168 for ( const auto& rProp : std::as_const(aProperties) )
170 if ( rProp.Name == PROP_EVENT_TYPE )
171 rProp.Value >>= aType;
172 else if ( rProp.Name == PROP_SCRIPT )
173 rProp.Value >>= aScript;
174 else if ( rProp.Name == PROP_LIBRARY )
175 rProp.Value >>= aLibrary;
176 else if ( rProp.Name == PROP_MACRO_NAME )
177 rProp.Value >>= aMacroName;
178 else {
179 OSL_FAIL("Unknown property value!");
183 if (aType.isEmpty())
185 // Empty type means no active binding for the event. Just ignore do nothing.
186 return;
189 if (aScript.isEmpty())
190 return;
192 if (!pDoc)
193 pDoc = SfxObjectShell::Current();
195 if (pDoc && !SfxObjectShell::isScriptAccessAllowed(pDoc->GetModel()))
196 return;
198 if (aType == STAR_BASIC)
200 uno::Any aAny;
201 SfxMacroLoader::loadMacro( aScript, aAny, pDoc );
203 else if (aType == "Service" || aType == "Script")
205 util::URL aURL;
206 uno::Reference < util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
208 aURL.Complete = aScript;
209 xTrans->parseStrict( aURL );
211 bool bAllowed = !SfxObjectShell::UnTrustedScript(aURL.Complete);
213 if (bAllowed)
215 SfxViewFrame* pView = SfxViewFrame::GetFirst(pDoc);
217 uno::Reference
218 < frame::XDispatchProvider > xProv;
220 if ( pView != nullptr )
222 xProv = uno::Reference
223 < frame::XDispatchProvider > (
224 pView->GetFrame().GetFrameInterface(), uno::UNO_QUERY );
226 else
228 xProv = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
231 uno::Reference < frame::XDispatch > xDisp;
232 if ( xProv.is() )
233 xDisp = xProv->queryDispatch( aURL, OUString(), 0 );
235 if ( xDisp.is() )
237 beans::PropertyValue aEventParam;
238 aEventParam.Value <<= aTrigger;
239 uno::Sequence< beans::PropertyValue > aDispatchArgs( &aEventParam, 1 );
240 xDisp->dispatch( aURL, aDispatchArgs );
244 else
246 SAL_WARN( "sfx.notify", "notifyEvent(): Unsupported event type" );
251 // --- ::document::XEventListener ---
253 void SAL_CALL SfxEvents_Impl::notifyEvent( const document::EventObject& aEvent )
255 ::osl::ClearableMutexGuard aGuard( maMutex );
257 // get the event name, find the corresponding data, execute the data
259 auto nIndex = comphelper::findValue(maEventNames, aEvent.EventName);
260 if ( nIndex == -1 )
261 return;
263 uno::Any aEventData = maEventData[ nIndex ];
264 aGuard.clear();
265 Execute( aEventData, document::DocumentEvent(aEvent.Source, aEvent.EventName, nullptr, uno::Any()), mpObjShell );
269 // --- ::lang::XEventListener ---
271 void SAL_CALL SfxEvents_Impl::disposing( const lang::EventObject& /*Source*/ )
273 ::osl::MutexGuard aGuard( maMutex );
275 if ( mxBroadcaster.is() )
277 mxBroadcaster->removeEventListener( this );
278 mxBroadcaster = nullptr;
283 SfxEvents_Impl::SfxEvents_Impl( SfxObjectShell* pShell,
284 uno::Reference< document::XEventBroadcaster > const & xBroadcaster )
286 // get the list of supported events and store it
287 if ( pShell )
288 maEventNames = pShell->GetEventNames();
289 else
290 maEventNames = rtl::Reference<GlobalEventConfig>(new GlobalEventConfig)->getElementNames();
292 maEventData = uno::Sequence < uno::Any > ( maEventNames.getLength() );
294 mpObjShell = pShell;
295 mxBroadcaster = xBroadcaster;
297 if ( mxBroadcaster.is() )
298 mxBroadcaster->addEventListener( this );
302 SfxEvents_Impl::~SfxEvents_Impl()
307 std::unique_ptr<SvxMacro> SfxEvents_Impl::ConvertToMacro( const uno::Any& rElement, SfxObjectShell* pObjShell )
309 std::unique_ptr<SvxMacro> pMacro;
310 uno::Sequence < beans::PropertyValue > aProperties;
311 uno::Any aAny;
312 NormalizeMacro( rElement, aAny, pObjShell );
314 if ( aAny >>= aProperties )
316 OUString aType;
317 OUString aScriptURL;
318 OUString aLibrary;
319 OUString aMacroName;
321 if ( !aProperties.hasElements() )
322 return pMacro;
324 for ( const auto& rProp : std::as_const(aProperties) )
326 if ( rProp.Name == PROP_EVENT_TYPE )
327 rProp.Value >>= aType;
328 else if ( rProp.Name == PROP_SCRIPT )
329 rProp.Value >>= aScriptURL;
330 else if ( rProp.Name == PROP_LIBRARY )
331 rProp.Value >>= aLibrary;
332 else if ( rProp.Name == PROP_MACRO_NAME )
333 rProp.Value >>= aMacroName;
334 else {
335 OSL_FAIL("Unknown property value!");
339 // Get the type
340 ScriptType eType( STARBASIC );
341 if ( aType == STAR_BASIC )
342 eType = STARBASIC;
343 else if (aType == "Script" && !aScriptURL.isEmpty())
344 eType = EXTENDED_STYPE;
345 else if ( aType == SVX_MACRO_LANGUAGE_JAVASCRIPT )
346 eType = JAVASCRIPT;
347 else {
348 SAL_WARN( "sfx.notify", "ConvertToMacro: Unknown macro type" );
351 if ( !aMacroName.isEmpty() )
353 if ( aLibrary == "application" )
354 aLibrary = SfxGetpApp()->GetName();
355 else
356 aLibrary.clear();
357 pMacro.reset(new SvxMacro( aMacroName, aLibrary, eType ));
359 else if ( eType == EXTENDED_STYPE )
360 pMacro.reset(new SvxMacro( aScriptURL, aType ));
363 return pMacro;
366 void SfxEvents_Impl::NormalizeMacro( const uno::Any& rEvent, uno::Any& rRet, SfxObjectShell* pDoc )
368 const ::comphelper::NamedValueCollection aEventDescriptor( rEvent );
369 ::comphelper::NamedValueCollection aEventDescriptorOut;
371 NormalizeMacro( aEventDescriptor, aEventDescriptorOut, pDoc );
373 rRet <<= aEventDescriptorOut.getPropertyValues();
376 void SfxEvents_Impl::NormalizeMacro( const ::comphelper::NamedValueCollection& i_eventDescriptor,
377 ::comphelper::NamedValueCollection& o_normalizedDescriptor, SfxObjectShell* i_document )
379 SfxObjectShell* pDoc = i_document;
380 if ( !pDoc )
381 pDoc = SfxObjectShell::Current();
383 OUString aType = i_eventDescriptor.getOrDefault( PROP_EVENT_TYPE, OUString() );
384 OUString aScript = i_eventDescriptor.getOrDefault( PROP_SCRIPT, OUString() );
385 OUString aLibrary = i_eventDescriptor.getOrDefault( PROP_LIBRARY, OUString() );
386 OUString aMacroName = i_eventDescriptor.getOrDefault( PROP_MACRO_NAME, OUString() );
388 if ( !aType.isEmpty() )
389 o_normalizedDescriptor.put( PROP_EVENT_TYPE, aType );
390 if ( !aScript.isEmpty() )
391 o_normalizedDescriptor.put( PROP_SCRIPT, aScript );
393 if ( aType != STAR_BASIC )
394 return;
396 if ( !aScript.isEmpty() )
398 if ( aMacroName.isEmpty() || aLibrary.isEmpty() )
400 sal_Int32 nThirdSlashPos = aScript.indexOf( '/', 8 );
401 sal_Int32 nArgsPos = aScript.indexOf( '(' );
402 if ( ( nThirdSlashPos != -1 ) && ( nArgsPos == -1 || nThirdSlashPos < nArgsPos ) )
404 OUString aBasMgrName( INetURLObject::decode( aScript.copy( 8, nThirdSlashPos-8 ), INetURLObject::DecodeMechanism::WithCharset ) );
405 if (pDoc && aBasMgrName == ".")
406 aLibrary = pDoc->GetTitle();
407 else
408 aLibrary = SfxGetpApp()->GetName();
410 // Get the macro name
411 aMacroName = aScript.copy( nThirdSlashPos+1, nArgsPos - nThirdSlashPos - 1 );
413 else
415 SAL_WARN( "sfx.notify", "ConvertToMacro: Unknown macro url format" );
419 else if ( !aMacroName.isEmpty() )
421 aScript = "macro://";
422 if ( aLibrary != SfxGetpApp()->GetName() && aLibrary != "StarDesktop" && aLibrary != "application" )
423 aScript += ".";
424 aScript += "/" + aMacroName + "()";
426 else
427 // wrong properties
428 return;
430 if (aLibrary != "document")
432 if ( aLibrary.isEmpty() || (pDoc && ( aLibrary == pDoc->GetTitle( SFX_TITLE_APINAME ) || aLibrary == pDoc->GetTitle() )) )
433 aLibrary = "document";
434 else
435 aLibrary = "application";
438 o_normalizedDescriptor.put( PROP_SCRIPT, aScript );
439 o_normalizedDescriptor.put( PROP_LIBRARY, aLibrary );
440 o_normalizedDescriptor.put( PROP_MACRO_NAME, aMacroName );
443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */