cid#1607171 Data race condition
[LibreOffice.git] / include / oox / ole / vbaproject.hxx
bloba5a239ab03bdf6ec7f84fb00cd24c4890ff283b9
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 #ifndef INCLUDED_OOX_OLE_VBAPROJECT_HXX
21 #define INCLUDED_OOX_OLE_VBAPROJECT_HXX
23 #include <functional>
24 #include <map>
25 #include <string_view>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <oox/dllapi.h>
29 #include <oox/helper/refmap.hxx>
30 #include <oox/helper/refvector.hxx>
31 #include <rtl/ustring.hxx>
32 #include <sal/types.h>
34 namespace com::sun::star {
35 namespace container { class XNameContainer; }
36 namespace frame { class XModel; }
37 namespace script { class XLibraryContainer; }
38 namespace script::vba { class XVBAMacroResolver; }
39 namespace uno { class XComponentContext; }
40 namespace uno { class XInterface; }
41 namespace io { class XInputStream; }
44 namespace oox {
45 class GraphicHelper;
46 class StorageBase;
49 namespace oox::ole {
51 class VbaModule;
53 class VbaFilterConfig
55 public:
56 explicit VbaFilterConfig(
57 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
58 std::u16string_view rConfigCompName );
59 ~VbaFilterConfig();
61 /** Returns true, if the VBA source code and forms should be imported. */
62 bool isImportVba() const;
63 /** Returns true, if the VBA source code should be imported executable. */
64 bool isImportVbaExecutable() const;
65 /** Returns true, if the VBA source code and forms should be exported. */
66 bool isExportVba() const;
68 private:
69 css::uno::Reference< css::uno::XInterface >
70 mxConfigAccess;
74 /** Base class for objects that attach a macro to a specific action.
76 Purpose is to collect objects that need to attach a VBA macro to an action.
77 The VBA project will be loaded at a very late point of the document import
78 process, because it depends on an initialized core document model (e.g.
79 spreadsheet codenames). Some objects that want to attach a VBA macro to an
80 action (e.g. mouse click action for drawing shapes) are loaded long before
81 the VBA project. The drawback is that in most cases macros are specified
82 without module name, or the VBA project name is part of the macro name.
83 In the former case, all code modules have to be scanned for the macro to be
84 able to create a valid script URL.
86 The import code will register these requests to attach a VBA macro with an
87 instance of a class derived from this base class. The derived class will
88 store all information needed to finally attach the macro to the action,
89 once the VBA project has been imported.
91 class OOX_DLLPUBLIC VbaMacroAttacherBase
93 public:
94 explicit VbaMacroAttacherBase( OUString aMacroName );
95 virtual ~VbaMacroAttacherBase();
97 /** Resolves the internal macro name to the related macro URL, and attaches
98 the macro to the object. */
99 void resolveAndAttachMacro(
100 const css::uno::Reference< css::script::vba::XVBAMacroResolver >& rxResolver );
102 private:
103 /** Called after the VBA project has been imported. Derived classes will
104 attach the passed script to the object represented by this instance. */
105 virtual void attachMacro( const OUString& rScriptUrl ) = 0;
107 private:
108 OUString maMacroName;
111 typedef std::shared_ptr< VbaMacroAttacherBase > VbaMacroAttacherRef;
114 class OOX_DLLPUBLIC VbaProject : public VbaFilterConfig
116 public:
117 explicit VbaProject(
118 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
119 const css::uno::Reference< css::frame::XModel >& rxDocModel,
120 std::u16string_view rConfigCompName );
121 virtual ~VbaProject();
123 /** Imports the entire VBA project from the passed storage.
125 @param rVbaPrjStrg The root storage of the entire VBA project.
127 void importVbaProject(
128 StorageBase& rVbaPrjStrg,
129 const GraphicHelper& rGraphicHelper );
131 bool importVbaProject(
132 StorageBase& rVbaPrjStrg );
134 /// Imports VBA data for a VBA project, e.g. word/vbaData.xml.
135 void importVbaData(const css::uno::Reference<css::io::XInputStream>& xInputStream);
137 /** Reads vba module related information from the project streams */
138 void readVbaModules( StorageBase& rVbaPrjStrg );
139 /** Imports (and creates) vba modules and user forms from the vba project records previously read.
140 Note: ( expects that readVbaModules was already called ) */
141 void importModulesAndForms( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper );
142 /** Registers a macro attacher object. For details, see description of the
143 VbaMacroAttacherBase class. */
144 void registerMacroAttacher( const VbaMacroAttacherRef& rxAttacher );
146 /** Attaches VBA macros to objects registered via registerMacroAttacher(). */
147 void attachMacros();
149 void setOleOverridesSink( css::uno::Reference< css::container::XNameContainer > const & rxOleOverridesSink ){ mxOleOverridesSink = rxOleOverridesSink; }
151 protected:
152 /** Registers a dummy module that will be created when the VBA project is
153 imported. */
154 void addDummyModule( const OUString& rName, sal_Int32 nType );
156 /** Called when the import process of the VBA project has been started. */
157 virtual void prepareImport();
159 private:
160 VbaProject( const VbaProject& ) = delete;
161 VbaProject& operator=( const VbaProject& ) = delete;
163 /** Returns the Basic or dialog library container. */
164 css::uno::Reference< css::script::XLibraryContainer >
165 getLibraryContainer( sal_Int32 nPropId );
166 /** Opens a Basic or dialog library, creates missing if not found. */
167 css::uno::Reference< css::container::XNameContainer >
168 openLibrary( sal_Int32 nPropId );
169 /** Creates and returns the Basic library of the document used for import. */
170 css::uno::Reference< css::container::XNameContainer > const &
171 createBasicLibrary();
172 /** Creates and returns the dialog library of the document used for import. */
173 css::uno::Reference< css::container::XNameContainer > const &
174 createDialogLibrary();
176 /** Imports the VBA code modules and forms. */
177 void importVba(
178 StorageBase& rVbaPrjStrg,
179 const GraphicHelper& rGraphicHelper );
181 /** Copies the entire VBA project storage to the passed document model. */
182 void copyStorage( StorageBase& rVbaPrjStrg );
184 private:
185 typedef RefVector< VbaMacroAttacherBase > MacroAttacherVector;
186 typedef ::std::map< OUString, sal_Int32 > DummyModuleMap;
188 css::uno::Reference< css::uno::XComponentContext >
189 mxContext; ///< Component context with service manager.
190 css::uno::Reference< css::frame::XModel >
191 mxDocModel; ///< Document model used to import/export the VBA project.
192 css::uno::Reference< css::container::XNameContainer >
193 mxBasicLib; ///< The Basic library of the document used for import.
194 css::uno::Reference< css::container::XNameContainer >
195 mxDialogLib; ///< The dialog library of the document used for import.
196 MacroAttacherVector maMacroAttachers; ///< Objects that want to attach a VBA macro to an action.
197 DummyModuleMap maDummyModules; ///< Additional empty modules created on import.
198 OUString maPrjName; ///< Name of the VBA project.
199 css::uno::Reference< css::container::XNameContainer >
200 mxOleOverridesSink;
201 typedef RefMap< OUString, VbaModule > VbaModuleMap;
202 VbaModuleMap maModules;
203 VbaModuleMap maModulesByStrm;
207 } // namespace oox::ole
209 #endif
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */