LibreOffice Module sfx2 (master) 1
macroloader.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <config_features.h>
21
22#include <macroloader.hxx>
23
24#include <com/sun/star/frame/DispatchResultState.hpp>
25#include <basic/basmgr.hxx>
26#include <basic/sbuno.hxx>
27#include <basic/sberrors.hxx>
29#include <cppuhelper/weak.hxx>
32#include <sfx2/app.hxx>
33#include <sfx2/frame.hxx>
34#include <sfx2/objsh.hxx>
35#include <tools/urlobj.hxx>
36#include <vcl/svapp.hxx>
37
38#include <memory>
39
40using namespace ::com::sun::star;
41using namespace ::com::sun::star::frame;
42using namespace ::com::sun::star::lang;
43using namespace ::com::sun::star::uno;
44using namespace ::com::sun::star::util;
45
46SfxMacroLoader::SfxMacroLoader(const css::uno::Sequence< css::uno::Any >& aArguments)
47{
48 Reference < XFrame > xFrame;
49 if ( aArguments.hasElements() )
50 {
51 aArguments[0] >>= xFrame;
53 }
54}
55
57{
58 return "com.sun.star.comp.sfx2.SfxMacroLoader";
59}
60
61sal_Bool SAL_CALL SfxMacroLoader::supportsService(OUString const & ServiceName)
62{
64}
65
66css::uno::Sequence<OUString> SAL_CALL SfxMacroLoader::getSupportedServiceNames()
67{
68 return { "com.sun.star.frame.ProtocolHandler" };
69}
70
71SfxObjectShell* SfxMacroLoader::GetObjectShell(const Reference <XFrame>& xFrame)
72{
73 SfxObjectShell* pDocShell = nullptr;
74
75 if ( xFrame.is() )
76 {
77 SfxFrame* pFrame=nullptr;
78 for ( pFrame = SfxFrame::GetFirst(); pFrame; pFrame = SfxFrame::GetNext( *pFrame ) )
79 {
80 if ( pFrame->GetFrameInterface() == xFrame )
81 break;
82 }
83
84 if ( pFrame )
85 pDocShell = pFrame->GetCurrentDocument();
86 }
87
88 return pDocShell;
89}
90
92{
93 Reference < XFrame > xFrame( m_xFrame.get(), UNO_QUERY );
95}
96
97uno::Reference<frame::XDispatch> SAL_CALL SfxMacroLoader::queryDispatch(
98 const util::URL& aURL ,
99 const OUString& /*sTargetFrameName*/,
100 sal_Int32 /*nSearchFlags*/ )
101{
102 uno::Reference<frame::XDispatch> xDispatcher;
103 if(aURL.Complete.startsWith("macro:"))
104 xDispatcher = this;
105 return xDispatcher;
106}
107
108
109uno::Sequence< uno::Reference<frame::XDispatch> > SAL_CALL
110 SfxMacroLoader::queryDispatches( const uno::Sequence < frame::DispatchDescriptor >& seqDescriptor )
111{
112 sal_Int32 nCount = seqDescriptor.getLength();
113 uno::Sequence< uno::Reference<frame::XDispatch> > lDispatcher(nCount);
114 std::transform(seqDescriptor.begin(), seqDescriptor.end(), lDispatcher.getArray(),
115 [this](const frame::DispatchDescriptor& rDescr) -> uno::Reference<frame::XDispatch> {
116 return queryDispatch(rDescr.FeatureURL, rDescr.FrameName, rDescr.SearchFlags); });
117 return lDispatcher;
118}
119
120
122 const util::URL& aURL, const uno::Sequence<beans::PropertyValue>& /*lArgs*/,
123 const uno::Reference<frame::XDispatchResultListener>& xListener )
124{
125 SolarMutexGuard aGuard;
126
127 uno::Any aAny;
128 ErrCode nErr = loadMacro( aURL.Complete, aAny, GetObjectShell_Impl() );
129 if( !xListener.is() )
130 return;
131
132 // always call dispatchFinished(), because we didn't load a document but
133 // executed a macro instead!
134 frame::DispatchResultEvent aEvent;
135
136 aEvent.Source = getXWeak();
137 if( nErr == ERRCODE_NONE )
138 aEvent.State = frame::DispatchResultState::SUCCESS;
139 else
140 aEvent.State = frame::DispatchResultState::FAILURE;
141
142 xListener->dispatchFinished( aEvent ) ;
143}
144
146 const util::URL& aURL, const uno::Sequence<beans::PropertyValue>& )
147{
148 uno::Any aRet;
149 ErrCode nErr = loadMacro( aURL.Complete, aRet, GetObjectShell_Impl() );
150
151 // aRet gets set to a different value only if nErr == ERRCODE_NONE
152 // Return it in such case to preserve the original behaviour
153
154 // In all other cases (nErr != ERRCODE_NONE), the calling code gets
155 // the actual error code back
156 if ( nErr != ERRCODE_NONE )
157 {
158 beans::PropertyValue aErrorCode;
159
160 aErrorCode.Name = "ErrorCode";
161 aErrorCode.Value <<= sal_uInt32(nErr);
162
163 aRet <<= aErrorCode;
164 }
165
166 return aRet;
167}
168
170 const util::URL& aURL, const uno::Sequence<beans::PropertyValue>& /*lArgs*/ )
171{
172 SolarMutexGuard aGuard;
173
174 uno::Any aAny;
175 loadMacro( aURL.Complete, aAny, GetObjectShell_Impl() );
176}
177
179 const uno::Reference< frame::XStatusListener >& ,
180 const util::URL& )
181{
182 /* TODO
183 How we can handle different listener for further coming or currently running dispatch() jobs
184 without any inconsistency!
185 */
186}
187
188
190 const uno::Reference< frame::XStatusListener >&,
191 const util::URL& )
192{
193}
194
195ErrCode SfxMacroLoader::loadMacro( const OUString& rURL, css::uno::Any& rRetval, SfxObjectShell* pSh )
196{
197#if !HAVE_FEATURE_SCRIPTING
198 (void) rURL;
199 (void) rRetval;
200 (void) pSh;
202#else
203 SfxObjectShell* pCurrent = pSh;
204 if ( !pCurrent )
205 // all not full qualified names use the BASIC of the given or current document
206 pCurrent = SfxObjectShell::Current();
207
208 // 'macro:///lib.mod.proc(args)' => macro of App-BASIC
209 // 'macro://[docname|.]/lib.mod.proc(args)' => macro of current or qualified document
210 // 'macro://obj.method(args)' => direct API call, execute it via App-BASIC
211 const OUString& aMacro( rURL );
212 sal_Int32 nThirdSlashPos = aMacro.indexOf( '/', 8 );
213 sal_Int32 nArgsPos = aMacro.indexOf( '(' );
215 BasicManager *pBasMgr = nullptr;
216 ErrCode nErr = ERRCODE_NONE;
217
218 // should a macro function be executed ( no direct API call)?
219 if ( -1 != nThirdSlashPos && ( -1 == nArgsPos || nThirdSlashPos < nArgsPos ) )
220 {
221 // find BasicManager
222 SfxObjectShell* pDoc = nullptr;
223 OUString aBasMgrName( INetURLObject::decode(aMacro.subView( 8, nThirdSlashPos-8 ), INetURLObject::DecodeMechanism::WithCharset) );
224 if ( aBasMgrName.isEmpty() )
225 pBasMgr = pAppMgr;
226 else if ( aBasMgrName == "." )
227 {
228 // current/actual document
229 pDoc = pCurrent;
230 if (pDoc)
231 pBasMgr = pDoc->GetBasicManager();
232 }
233 else
234 {
235 // full qualified name, find document by name
237 pObjSh && !pBasMgr;
238 pObjSh = SfxObjectShell::GetNext(*pObjSh) )
239 if ( aBasMgrName == pObjSh->GetTitle(SFX_TITLE_APINAME) )
240 {
241 pDoc = pObjSh;
242 pBasMgr = pDoc->GetBasicManager();
243 }
244 }
245
246 if ( pBasMgr )
247 {
248 const bool bIsAppBasic = ( pBasMgr == pAppMgr );
249 const bool bIsDocBasic = ( pBasMgr != pAppMgr );
250
251 if ( pDoc )
252 {
253 // security check for macros from document basic if an SFX doc is given
254 if ( !pDoc->AdjustMacroMode() )
255 // check forbids execution
257 }
258
259 // find BASIC method
260 OUString aQualifiedMethod( INetURLObject::decode(aMacro.subView( nThirdSlashPos+1 ), INetURLObject::DecodeMechanism::WithCharset) );
261 OUString aArgs;
262 if ( -1 != nArgsPos )
263 {
264 // remove arguments from macro name
265 aArgs = aQualifiedMethod.copy( nArgsPos - nThirdSlashPos - 1 );
266 aQualifiedMethod = aQualifiedMethod.copy( 0, nArgsPos - nThirdSlashPos - 1 );
267 }
268
269 if ( pBasMgr->HasMacro( aQualifiedMethod ) )
270 {
271 Any aOldThisComponent;
272 const bool bSetDocMacroMode = ( pDoc != nullptr ) && bIsDocBasic;
273 const bool bSetGlobalThisComponent = ( pDoc != nullptr ) && bIsAppBasic;
274 if ( bSetDocMacroMode )
275 {
276 // mark document: it executes an own macro, so it's in a modal mode
277 pDoc->SetMacroMode_Impl();
278 }
279
280 if ( bSetGlobalThisComponent )
281 {
282 // document is executed via AppBASIC, adjust ThisComponent variable
283 pAppMgr->SetGlobalUNOConstant( "ThisComponent", Any( pDoc->GetModel() ), &aOldThisComponent );
284 }
285
286 // just to let the shell be alive
287 SfxObjectShellRef xKeepDocAlive = pDoc;
288
289 {
290 // attempt to protect the document against the script tampering with its Undo Context
291 std::optional< ::framework::DocumentUndoGuard > pUndoGuard;
292 if ( bIsDocBasic )
293 pUndoGuard.emplace( pDoc->GetModel() );
294
295 // execute the method
296 SbxVariableRef retValRef = new SbxVariable;
297 nErr = pBasMgr->ExecuteMacro( aQualifiedMethod, aArgs, retValRef.get() );
298 if ( nErr == ERRCODE_NONE )
299 rRetval = sbxToUnoValue( retValRef.get() );
300 }
301
302 if ( bSetGlobalThisComponent )
303 {
304 pAppMgr->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
305 }
306
307 if ( bSetDocMacroMode )
308 {
309 // remove flag for modal mode
310 pDoc->SetMacroMode_Impl( false );
311 }
312 }
313 else
315 }
316 else
318 }
319 else
320 {
321 // direct API call on a specified object
322 OUString aCall =
323 "[" +
324 INetURLObject::decode(aMacro.subView(6),
326 "]";
327 pAppMgr->GetLib(0)->Execute(aCall);
328 nErr = SbxBase::GetError();
329 }
330
332 return nErr;
333#endif
334}
335
336extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
338 css::uno::XComponentContext *,
339 css::uno::Sequence<css::uno::Any> const &arguments)
340{
341 return cppu::acquire(new SfxMacroLoader(arguments));
342}
343
344/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
StarBASIC * GetLib(sal_uInt16 nLib) const
bool HasMacro(OUString const &i_fullyQualifiedName) const
ErrCode ExecuteMacro(OUString const &i_fullyQualifiedName, SbxArray *i_arguments, SbxValue *i_retValue)
void SetGlobalUNOConstant(const OUString &rName, const css::uno::Any &_rValue, css::uno::Any *pOldValue=nullptr)
static OUString decode(std::u16string_view rText, DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
static ErrCode const & GetError()
static void ResetError()
SbxVariable * Execute(const OUString &)
static BasicManager * GetBasicManager()
Definition: appbas.cxx:62
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
Definition: frame.cxx:515
SAL_WARN_UNUSED_RESULT SfxObjectShell * GetCurrentDocument() const
Definition: frame.cxx:250
static SAL_WARN_UNUSED_RESULT SfxFrame * GetNext(SfxFrame &)
Definition: frame.cxx:711
static SAL_WARN_UNUSED_RESULT SfxFrame * GetFirst()
Definition: frame.cxx:706
css::uno::WeakReference< css::frame::XFrame > m_xFrame
Definition: macroloader.hxx:48
virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xControl, const css::util::URL &aURL) override
static SfxObjectShell * GetObjectShell(const css::uno::Reference< css::frame::XFrame > &xFrame)
Definition: macroloader.cxx:71
virtual OUString SAL_CALL getImplementationName() override
Definition: macroloader.cxx:56
virtual sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
Definition: macroloader.cxx:61
virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor > &seqDescriptor) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: macroloader.cxx:66
SfxObjectShell * GetObjectShell_Impl()
Definition: macroloader.cxx:91
virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL &aURL, const OUString &sTargetFrameName, sal_Int32 eSearchFlags) override
Definition: macroloader.cxx:97
virtual void SAL_CALL dispatch(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &lArgs) override
virtual void SAL_CALL dispatchWithNotification(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &lArgs, const css::uno::Reference< css::frame::XDispatchResultListener > &Listener) override
SfxMacroLoader(const css::uno::Sequence< css::uno::Any > &aArguments)
Definition: macroloader.cxx:46
virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xControl, const css::util::URL &aURL) override
static ErrCode loadMacro(const OUString &aURL, css::uno::Any &rRetval, SfxObjectShell *pDoc)
virtual css::uno::Any SAL_CALL dispatchWithReturnValue(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &lArgs) override
BasicManager * GetBasicManager() const
Definition: objxtor.cxx:644
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetNext(const SfxObjectShell &rPrev, const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
Definition: objxtor.cxx:452
SAL_DLLPRIVATE void SetMacroMode_Impl(bool bModal=true)
Definition: objmisc.cxx:433
css::uno::Reference< css::frame::XModel3 > GetModel() const
Definition: objxtor.cxx:838
bool AdjustMacroMode()
adjusts the internal macro mode, according to the current security settings
Definition: objmisc.cxx:1605
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetFirst(const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
Definition: objxtor.cxx:427
static SAL_WARN_UNUSED_RESULT SfxObjectShell * Current()
Definition: objxtor.cxx:481
T * get() const
int nCount
URL aURL
#define ERRCODE_IO_ACCESSDENIED
#define ERRCODE_IO_NOTEXISTS
#define ERRCODE_NONE
Sequence< PropertyValue > aArguments
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_sfx2_SfxMacroLoader_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &arguments)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
#define SFX_TITLE_APINAME
Definition: objsh.hxx:115
#define ERRCODE_BASIC_PROC_UNDEFINED
Any sbxToUnoValue(const SbxValue *pVar)
Reference< XFrame > xFrame
unsigned char sal_Bool