Line data Source code
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 : #ifndef INCLUDED_VBAHELPER_VBAEVENTSHELPERBASE_HXX
21 : #define INCLUDED_VBAHELPER_VBAEVENTSHELPERBASE_HXX
22 :
23 : #include <deque>
24 : #include <map>
25 : #include <unordered_map>
26 : #include <com/sun/star/document/XEventListener.hpp>
27 : #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
28 : #include <com/sun/star/util/XChangesListener.hpp>
29 : #include <cppuhelper/implbase.hxx>
30 : #include <vbahelper/vbahelper.hxx>
31 :
32 : namespace com { namespace sun { namespace star {
33 : namespace script { namespace vba { class XVBAModuleInfo; } }
34 : namespace uno { class XComponentContext; }
35 : } } }
36 :
37 :
38 :
39 : typedef ::cppu::WeakImplHelper<
40 : css::script::vba::XVBAEventProcessor,
41 : css::document::XEventListener,
42 : css::util::XChangesListener,
43 : css::lang::XServiceInfo > VbaEventsHelperBase_BASE;
44 :
45 : class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
46 : {
47 : public:
48 : VbaEventsHelperBase(
49 : const css::uno::Sequence< css::uno::Any >& rArgs,
50 : const css::uno::Reference< css::uno::XComponentContext >& xContext );
51 : virtual ~VbaEventsHelperBase();
52 :
53 : // script::vba::XVBAEventProcessor
54 : virtual sal_Bool SAL_CALL hasVbaEventHandler( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
55 : virtual sal_Bool SAL_CALL processVbaEvent( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::util::VetoException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
56 :
57 : // document::XEventListener
58 : virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
59 :
60 : // util::XChangesListener
61 : virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 :
63 : // lang::XEventListener
64 : virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 :
66 : sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
67 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 :
69 : // little helpers ---------------------------------------------------------
70 :
71 : /** Helper to execute event handlers without throwing any exceptions. */
72 : void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
73 :
74 : /** Throws, if the passed sequence does not contain a value at the specified index. */
75 1239 : static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
76 1239 : { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
77 :
78 : /** Throws, if the passed sequence does not contain a value of a specific at the specified index. */
79 : template< typename Type >
80 0 : static inline void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
81 0 : { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
82 :
83 : protected:
84 :
85 :
86 1624 : struct EventHandlerInfo
87 : {
88 : sal_Int32 mnEventId;
89 : sal_Int32 mnModuleType;
90 : OUString maMacroName;
91 : sal_Int32 mnCancelIndex;
92 : css::uno::Any maUserData;
93 : };
94 :
95 : /** Registers a supported event handler.
96 :
97 : @param nEventId Event identifier from com.sun.star.script.vba.VBAEventId.
98 : @param nModuleType Type of the module containing the event handler.
99 : @param pcMacroName Name of the associated VBA event handler macro.
100 : @param nCancelIndex 0-based index of Cancel parameter, or -1.
101 : @param rUserData User data for free usage in derived implementations. */
102 : void registerEventHandler(
103 : sal_Int32 nEventId,
104 : sal_Int32 nModuleType,
105 : const sal_Char* pcMacroName,
106 : sal_Int32 nCancelIndex = -1,
107 : const css::uno::Any& rUserData = css::uno::Any() );
108 :
109 :
110 :
111 10887 : struct EventQueueEntry
112 : {
113 : sal_Int32 mnEventId;
114 : css::uno::Sequence< css::uno::Any > maArgs;
115 55 : inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
116 3574 : inline EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
117 : };
118 : typedef ::std::deque< EventQueueEntry > EventQueue;
119 :
120 : /** Derived classes do additional prpeparations and return whether the
121 : event handler has to be called. */
122 : virtual bool implPrepareEvent(
123 : EventQueue& rEventQueue,
124 : const EventHandlerInfo& rInfo,
125 : const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::uno::RuntimeException) = 0;
126 :
127 : /** Derived classes have to return the argument list for the specified VBA event handler. */
128 : virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(
129 : const EventHandlerInfo& rInfo,
130 : const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException) = 0;
131 :
132 : /** Derived classes may do additional postprocessing. Called even if the
133 : event handler does not exist, or if an error occurred during execution. */
134 : virtual void implPostProcessEvent(
135 : EventQueue& rEventQueue,
136 : const EventHandlerInfo& rInfo,
137 : bool bCancel ) throw (css::uno::RuntimeException) = 0;
138 :
139 : /** Derived classes have to return the name of the Basic document module. */
140 : virtual OUString implGetDocumentModuleName(
141 : const EventHandlerInfo& rInfo,
142 : const css::uno::Sequence< css::uno::Any >& rArgs ) const
143 : throw (css::lang::IllegalArgumentException,
144 : css::uno::RuntimeException) = 0;
145 :
146 : private:
147 : typedef ::std::map< sal_Int32, OUString > ModulePathMap;
148 :
149 : /** Starts listening at the document model. */
150 : void startListening();
151 : /** Stops listening at the document model. */
152 : void stopListening();
153 :
154 : /** Returns the event handler info struct for the specified event, or throws. */
155 : const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
156 :
157 : /** Searches the event handler in the document and returns its full script path. */
158 : OUString getEventHandlerPath(
159 : const EventHandlerInfo& rInfo,
160 : const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
161 :
162 : /** On first call, accesses the Basic library containing the VBA source code. */
163 : void ensureVBALibrary() throw (css::uno::RuntimeException);
164 :
165 : /** Returns the type of the Basic module with the specified name. */
166 : sal_Int32 getModuleType( const OUString& rModuleName ) throw (css::uno::RuntimeException);
167 :
168 : /** Updates the map containing paths to event handlers for a Basic module. */
169 : ModulePathMap& updateModulePathMap( const OUString& rModuleName ) throw (css::uno::RuntimeException);
170 :
171 : protected:
172 : css::uno::Reference< css::frame::XModel > mxModel;
173 : SfxObjectShell* mpShell;
174 :
175 : private:
176 : typedef std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
177 : typedef std::unordered_map< OUString, ModulePathMap, OUStringHash > EventHandlerPathMap;
178 :
179 : EventHandlerInfoMap maEventInfos;
180 : EventHandlerPathMap maEventPaths;
181 : css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
182 : OUString maLibraryName;
183 : bool mbDisposed;
184 : };
185 :
186 :
187 :
188 : #endif
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|