21#include <com/sun/star/beans/XPropertySet.hpp>
22#include <com/sun/star/document/XEventBroadcaster.hpp>
23#include <com/sun/star/frame/XModel.hpp>
24#include <com/sun/star/script/ModuleType.hpp>
25#include <com/sun/star/script/vba/VBAEventId.hpp>
26#include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
27#include <com/sun/star/script/XLibraryContainer.hpp>
28#include <com/sun/star/util/VetoException.hpp>
29#include <com/sun/star/util/XChangesNotifier.hpp>
46 mxModel = getXSomethingFromArgs< frame::XModel >( rArgs, 0,
false );
49 catch( uno::Exception& )
58 SAL_WARN_IF( !
mbDisposed,
"vbahelper",
"VbaEventsHelperBase::~VbaEventsHelperBase - missing disposing notification" );
67 aEventQueue.emplace_back( nEventId, rArgs );
76 bool bExecuted =
false;
81 while( !aEventQueue.empty() )
87 throw uno::RuntimeException();
91 uno::Sequence< uno::Any > aEventArgs = aEventQueue.front().maArgs;
92 aEventQueue.pop_front();
93 SAL_INFO(
"vbahelper",
"VbaEventsHelperBase::processVbaEvent( \"" << rInfo.
maMacroName <<
"\" )");
102 if( !aMacroPath.isEmpty() )
110 throw lang::IllegalArgumentException();
132 throw util::VetoException();
140 SAL_INFO(
"vbahelper",
"VbaEventsHelperBase::notifyEvent( \"" << rEvent.EventName <<
"\" )");
152 catch( uno::Exception& )
158 uno::Reference< script::vba::XVBAModuleInfo > xSender( rEvent.Base, uno::UNO_QUERY );
163 for(
const util::ElementChange& rChange : rEvent.Changes )
165 OUString aModuleName;
166 if( (rChange.Accessor >>= aModuleName) && !aModuleName.isEmpty() )
try
169 if(
getModuleType( aModuleName ) == script::ModuleType::NORMAL )
176 catch( uno::Exception& )
184 uno::Reference< frame::XModel > xSender( rEvent.Source, uno::UNO_QUERY );
200 catch( uno::Exception& )
208 const char* pcMacroName, sal_Int32 nCancelIndex,
const uno::Any& rUserData )
213 rInfo.
maMacroName = OUString::createFromAscii( pcMacroName );
225 uno::Reference< document::XEventBroadcaster > xEventBroadcaster(
mxModel, uno::UNO_QUERY );
226 if( xEventBroadcaster.is() )
227 try { xEventBroadcaster->addEventListener(
this ); }
catch( uno::Exception& ) {}
235 uno::Reference< document::XEventBroadcaster > xEventBroadcaster(
mxModel, uno::UNO_QUERY );
236 if( xEventBroadcaster.is() )
237 try { xEventBroadcaster->removeEventListener(
this ); }
catch( uno::Exception& ) {}
247 EventHandlerInfoMap::const_iterator aIt =
maEventInfos.find( nEventId );
255 sal_Int32 nEventId )
const
257 EventHandlerInfoMap::const_iterator aIt =
maEventInfos.find( nEventId );
259 throw lang::IllegalArgumentException();
264 const uno::Sequence< uno::Any >& rArgs )
266 OUString aModuleName;
270 case script::ModuleType::NORMAL:
274 case script::ModuleType::DOCUMENT:
276 if( aModuleName.isEmpty() )
277 throw lang::IllegalArgumentException();
281 throw uno::RuntimeException(
"This module type is unsupported");
286 EventHandlerPathMap::iterator aIt =
maEventPaths.find( aModuleName );
299 throw uno::RuntimeException();
300 uno::Reference< beans::XPropertySet > xModelProps(
mxModel, uno::UNO_QUERY_THROW );
301 uno::Reference< container::XNameAccess > xBasicLibs( xModelProps->getPropertyValue(
302 "BasicLibraries" ), uno::UNO_QUERY_THROW );
306 uno::Reference< script::XLibraryContainer > xLibContainer(
307 xModelProps->getPropertyValue(
"BasicLibraries"), uno::UNO_QUERY_THROW);
313 uno::Reference< util::XChangesNotifier > xChangesNotifier(
mxModuleInfos, uno::UNO_QUERY_THROW );
314 xChangesNotifier->addChangesListener(
this );
316 catch( uno::Exception& )
320 throw uno::RuntimeException();
326 if (rModuleName.isEmpty())
335 catch (uno::Exception&)
347 if( rModuleName.isEmpty() )
348 return script::ModuleType::NORMAL;
353 return mxModuleInfos->getModuleInfo( rModuleName ).ModuleType;
355 catch( uno::Exception& )
358 throw uno::RuntimeException();
369 OUString sThisWorkbook;
373 =
maEventInfos[css::script::vba::VBAEventId::WORKBOOK_OPEN];
374 css::uno::Sequence<css::uno::Any> aNoArgs;
379 OUString sThisDocument;
383 =
maEventInfos[css::script::vba::VBAEventId::DOCUMENT_OPEN];
384 css::uno::Sequence<css::uno::Any> aNoArgs;
394 bool bOnlyPublic =
false;
395 OUString sSkipModule;
396 if (rInfo.
mnEventId == css::script::vba::VBAEventId::AUTO_NEW
397 || rInfo.
mnEventId == css::script::vba::VBAEventId::AUTO_OPEN
398 || rInfo.
mnEventId == css::script::vba::VBAEventId::AUTO_CLOSE)
403 sSkipModule = sThisWorkbook;
420 if (
sName.isEmpty() && rModuleName.isEmpty()
423 if (rInfo.
mnEventId == css::script::vba::VBAEventId::AUTO_NEW
424 || rInfo.
mnEventId == css::script::vba::VBAEventId::AUTO_OPEN
425 || rInfo.
mnEventId == css::script::vba::VBAEventId::AUTO_CLOSE)
428 bOnlyPublic, sSkipModule);
static OUString GetEventName(GlobalEventId nID)
virtual void SAL_CALL disposing(const css::lang::EventObject &rEvent) override
::std::deque< EventQueueEntry > EventQueue
void startListening()
Starts listening at the document model.
virtual void implPostProcessEvent(EventQueue &rEventQueue, const EventHandlerInfo &rInfo, bool bCancel)=0
Derived classes may do additional postprocessing.
void processVbaEventNoThrow(sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any > &rArgs)
Helper to execute event handlers without throwing any exceptions.
EventHandlerInfoMap maEventInfos
EventHandlerPathMap maEventPaths
virtual void SAL_CALL notifyEvent(const css::document::EventObject &rEvent) override
OUString getEventHandlerPath(const EventHandlerInfo &rInfo, const css::uno::Sequence< css::uno::Any > &rArgs)
Searches the event handler in the document and returns its full script path.
virtual bool implPrepareEvent(EventQueue &rEventQueue, const EventHandlerInfo &rInfo, const css::uno::Sequence< css::uno::Any > &rArgs)=0
Derived classes do additional preparations and return whether the event handler has to be called.
virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(const EventHandlerInfo &rInfo, const css::uno::Sequence< css::uno::Any > &rArgs)=0
Derived classes have to return the argument list for the specified VBA event handler.
virtual sal_Bool SAL_CALL processVbaEvent(sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any > &rArgs) override
ModulePathMap & updateModulePathMap(const OUString &rModuleName)
Updates the map containing paths to event handlers for a Basic module.
css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos
sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
virtual sal_Bool SAL_CALL hasVbaEventHandler(sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any > &rArgs) override
sal_Int32 getModuleType(const OUString &rModuleName)
Returns the type of the Basic module with the specified name.
void registerEventHandler(sal_Int32 nEventId, sal_Int32 nModuleType, const char *pcMacroName, sal_Int32 nCancelIndex=-1, const css::uno::Any &rUserData=css::uno::Any())
Registers a supported event handler.
::std::map< sal_Int32, OUString > ModulePathMap
virtual OUString implGetDocumentModuleName(const EventHandlerInfo &rInfo, const css::uno::Sequence< css::uno::Any > &rArgs) const =0
Derived classes have to return the name of the Basic document module.
css::uno::Reference< css::frame::XModel > mxModel
VbaEventsHelperBase(const css::uno::Sequence< css::uno::Any > &rArgs)
void stopListening()
Stops listening at the document model.
static void checkArgument(const css::uno::Sequence< css::uno::Any > &rArgs, sal_Int32 nIndex)
bool hasModule(const OUString &rModuleName)
virtual void SAL_CALL changesOccurred(const css::util::ChangesEvent &rEvent) override
const EventHandlerInfo & getEventHandlerInfo(sal_Int32 nEventId) const
Returns the event handler info struct for the specified event, or throws.
void ensureVBALibrary()
On first call, accesses the Basic library containing the VBA source code.
virtual ~VbaEventsHelperBase() override
#define SAL_WARN_IF(condition, area, stream)
#define SAL_INFO(area, stream)
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
bool extractBoolFromAny(const uno::Any &rAny)
bool executeMacro(SfxObjectShell *pShell, const OUString &sMacroName, uno::Sequence< uno::Any > &aArgs, uno::Any &aRet, const uno::Any &)
MSFILTER_DLLPUBLIC OUString getDefaultProjectName(SfxObjectShell const *pShell)
SfxObjectShell * getSfxObjShell(const uno::Reference< frame::XModel > &xModel)
OUString resolveVBAMacro(SfxObjectShell const *pShell, const OUString &rLibName, const OUString &rModuleName, const OUString &rMacroName, bool bOnlyPublic, const OUString &sSkipModule)