xref: /aoo4110/main/sfx2/inc/sfx2/docmacromode.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SFX2_DOCMACROMODE_HXX
25 #define SFX2_DOCMACROMODE_HXX
26 
27 #include "sfx2/dllapi.h"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/task/XInteractionHandler.hpp>
31 #include <com/sun/star/embed/XStorage.hpp>
32 #include <com/sun/star/script/XLibraryContainer.hpp>
33 #include <com/sun/star/document/XEmbeddedScripts.hpp>
34 /** === end UNO includes === **/
35 
36 #include <boost/shared_ptr.hpp>
37 
38 //........................................................................
39 namespace sfx2
40 {
41 //........................................................................
42 
43 	//====================================================================
44 	//= IMacroDocumentAccess
45 	//====================================================================
46     /** provides access to several settings of a document, which are needed by ->DocumentMacroMode
47         to properly determine the current macro execution mode of this document
48     */
49     class SAL_NO_VTABLE IMacroDocumentAccess
50     {
51     public:
52         /** retrieves the current MacroExecutionMode.
53 
54             Usually, this is initialized from the media descriptor used to load the document,
55             respectively the one passed into the document's XModel::attachResource call.
56 
57             If no such mode was passed there, document implementations should return
58             MacroExecMode::NEVER_EXECUTE.
59 
60             @see ::com::sun::star::document::MediaDescriptor::MacroExecutionMode
61             @see ::com::sun::star::frame::XComponentLoader::loadComponentFromURL
62             @see ::com::sun::star::frame::XModel::attachResource
63 
64             @see setCurrentMacroExecMode
65 
66             @todo
67                 Effectively, this is the MacroExecutionMode of the MediaDescriptor of
68                 the document. Thus, this setting could be obtained from the XModel
69                 directly. We should introduce a getDocumentModel method here, which
70                 can be used for this and other purposes.
71         */
72         virtual sal_Int16
73                     getCurrentMacroExecMode() const = 0;
74 
75         /** sets the MacroExecutionMode of the document, as calculated by the DocumentMacroMode
76             class.
77 
78             Effectively, the existence of this method means that the responsibility
79             to store the current macro execution mode is not with the DocumentMacroMode
80             instance, but with the document instance itself.
81 
82             Usually, a document implementation will simply put the macro execution mode
83             into its media descriptor, as returned by XModel::getArgs.
84 
85             @see ::com::sun::star::document::MediaDescriptor::MacroExecutionMode
86             @see ::com::sun::star::frame::XComponentLoader::loadComponentFromURL
87             @see ::com::sun::star::frame::XModel::attachResource
88 
89             see getCurrentMacroExecMode
90         */
91         virtual sal_Bool
92                     setCurrentMacroExecMode( sal_uInt16 ) = 0;
93 
94         /** returns the origin of the document
95 
96             This usually is the document's location, or, if the document has been
97             newly created from a template, then the location of the template. Location
98             here means the complete path of the document, including the file name.
99 
100             @todo
101                 This probably can also be obtained from the XModel, by calling getURL
102                 or getLocation. If both are empty, then we need an UNO way to obtain
103                 the URL of the underlying template document - if any. If we have this,
104                 we could replace this method with a newly introduced method
105                 getDocumentModel and some internal code.
106         */
107         virtual ::rtl::OUString
108                     getDocumentLocation() const = 0;
109 
110         /** returns a zip-storage based on the last commited version of the document,
111             for readonly access
112 
113             The storage is intended to be used for signing. An implementation is
114             allowed to return <NULL/> here if and only if the document
115             does not support signing the script storages.
116 
117             @todo
118                 UNOize this, too. Once we have a getDocumentModel, we should be able to
119                 obtain the "last commit" storage via UNO API, provided it's an
120                 XStorageBasedDocument.
121         */
122         virtual ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
123                     getZipStorageToSign() = 0;
124 
125         /** checks whether the document's storage contains sub storages with macros or scripts
126 
127             A default implementation of this method will simply cann DocumentMacroMode::storageHasMacros
128             with the document's root storage. However, there might be document types where this
129             isn't sufficient (e.g. database documents which contain sub documents which can also
130             contain macro/script storages).
131         */
132         virtual sal_Bool
133                     documentStorageHasMacros() const = 0;
134 
135         /** provides access to the XEmbeddedScripts interface of the document
136 
137             Implementations are allowed to return <NULL/> here if and only if they
138             do not (yet) support embedding scripts.
139 
140             @todo
141                 can also be replaced with a call to the (to be introduced) getDocumentModel
142                 method, and a queryInterface.
143         */
144         virtual ::com::sun::star::uno::Reference< ::com::sun::star::document::XEmbeddedScripts >
145                     getEmbeddedDocumentScripts() const = 0;
146 
147         /** returns the state of the signatures for the scripts embedded in the document
148 
149             Note: On the medium run, the signature handling of a document should be outsourced
150             into a dedicated class, instead of being hard-wired into the SfxObjectShell. This
151             class could then be used outside the SfxObjectShell (e.g. in Base documents), too.
152             When this happens, this method here becomes should be replaced by a method at this
153             new class.
154 
155             @seealso <sfx2/signaturestate.hxx>
156         */
157         virtual sal_Int16
158                     getScriptingSignatureState() = 0;
159 
160         /** allows to detect whether there is a trusted scripting signature
161 
162             Note: On the medium run, the signature handling of a document should be outsourced
163             into a dedicated class, instead of being hard-wired into the SfxObjectShell. This
164             class could then be used outside the SfxObjectShell (e.g. in Base documents), too.
165             When this happens, this method here should be replaced by a method at this
166             new class.
167 
168             @seealso <sfx2/signaturestate.hxx>
169         */
170         virtual sal_Bool
171                     hasTrustedScriptingSignature( sal_Bool bAllowUIToAddAuthor ) = 0;
172 
173         /** shows a warning that the document's signature is broken
174 
175             Here, a similar note applies as to getScriptingSignatureState: This method doesn't
176             really belong here. It's just there because SfxObjectShell_Impl::bSignatureErrorIsShown
177             is not accessible where the method is called.
178             So, once the signature handling has been oursourced from SfxObjectShell/_Impl, so it
179             is re-usable in non-SFX contexts as well, this method here is also unneeded, probably.
180 
181             @param _rxInteraction
182                 the interaction handler to use for showing the warning. It is exactly the same
183                 as passed to DocumentMacroMode::adjustMacroMode, so it is <NULL/> if and
184                 only if the instance passed to that method was <NULL/>.
185         */
186         virtual void
187                     showBrokenSignatureWarning(
188                         const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& _rxInteraction
189                     ) const = 0;
190     };
191 
192 	//====================================================================
193 	//= DocumentMacroMode
194 	//====================================================================
195     struct DocumentMacroMode_Data;
196 
197     /** encapsulates handling the macro mode of a document
198 
199         @see com::sun::star::document::MacroExecMode
200     */
201 	class SFX2_DLLPUBLIC DocumentMacroMode
202 	{
203     public:
204         /** creates an instance
205 
206         @param _rDocumentAccess
207             access to the document which this instance works for. Must live as long as the
208             DocumentMacroMode instance lives, at least
209         */
210         DocumentMacroMode( IMacroDocumentAccess& _rDocumentAccess );
211         ~DocumentMacroMode();
212 
213         /** allows macro execution in the document
214 
215             Effectively, the macro mode is set to MacroExecMode::ALWAYS_EXECUTE_NO_WARN.
216 
217             @return
218                 <TRUE/>, always
219         */
220         sal_Bool    allowMacroExecution();
221 
222         /** disallows macro execution in the document
223 
224             Effectively, the macro mode is set to MacroExecMode::NEVER_EXECUTE.
225 
226             @return
227                 <TRUE/>, always
228         */
229         sal_Bool    disallowMacroExecution();
230 
231         /** checks whether the document allows executing contained macros.
232 
233             The method transforms the current macro execution mode into either
234             ALWAYS_EXECUTE_NO_WARN or NEVER_EXECUTE, depending on the current value,
235             possible configuration settings, and possible user interaction.
236 
237             @param _rxInteraction
238                 A handler for interactions which might become necessary.
239                 This includes
240                 <ul><li>Asking the user for confirmation for macro execution.</li>
241                     <li>Telling the user that macro execution is disabled.</li>
242                 </ul>
243 
244                 If the user needs to be asked for macro execution confirmation, and if
245                 this parameter is <NULL/>, the most defensive assumptions will be made,
246                 effectively disabling macro execution.
247 
248             @return
249                 <TRUE/> if and only if macro execution in this document is allowed.
250         */
251         sal_Bool    adjustMacroMode(
252                     const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& _rxInteraction
253                 );
254 
255         /** determines whether macro execution is disallowed
256 
257             There's a number of reasons why macro execution could be disallowed:
258             <ul><li>Somebody called ->disallowMacroExecution</li>
259                 <li>Macro execution is disabled globally, via the security options</li>
260                 <li>Macro execution mode was not defined initially, and the user denied
261                     executing macros for this particular document.</li>
262             </ul>
263 
264             Note that if this method returns <FALSE/>, then subsequent calls of
265             ->adjustMacroMode can still return <FALSE/>.
266             That is, if the current macro execution mode for the document is not yet known
267             (and inparticular <em>not</em> MacroExecMode::NEVER_EXECUTE), then ->isMacroExecutionDisallowed
268             will return <FALSE/>.
269             However, a subsequent call to ->adjustMacroMode can result in the user
270             denying macro execution, in which ->adjustMacroMode will return <FALSE/>,
271             and the next call to isMacroExecutionDisallowed will return <TRUE/>.
272         */
273         sal_Bool    isMacroExecutionDisallowed() const;
274 
275         /** determines whether the document actually has a macros library
276 
277             Effectively, this method checks the Basic library container (as returned by
278             IMacroDocumentAccess::getEmbeddedDocumentScripts().getBasicLibraries) for
279             content.
280         */
281         sal_Bool    hasMacroLibrary() const;
282 
283         /** determines whether the given document storage has sub storages containing scripts
284             or macros.
285 
286             Effectively, the method checks for the presence of a sub-storage name "Scripts" (where
287             BeanShell-/JavaScript-/Python-Scripts are stored, and a sub storage named "Basic" (where
288             Basic scripts are stored).
289         */
290         static sal_Bool
291                 storageHasMacros( const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& _rxStorage );
292 
293         /** checks the macro execution mode while loading the document.
294 
295             This must be called when the loading is effectively finished, but before any macro action
296             happened.
297 
298             The method will disallow macro execution for this document if it is disabled
299             globally (SvtSecurityOptions::IsMacroDisabled). Otherwise, it will check whether
300             the document contains a macro storage or macro libraries. If so, it will
301             properly calculate the MacroExecutionMode by calling adjustMacroMode.
302 
303             If the document doesn't contain macros, yet, then the macro execution for this
304             document will be allowed (again: unless disabled globally), since in this case
305             macros which later are newly created by the user should be allowed, of course.
306 
307             @return
308                 <TRUE/> if and only if macro execution is allowed in the document
309 
310             @see isMacroExecutionDisallowed
311             @see IMacroDocumentAccess::documentStorageHasMacros
312             @see hasMacroLibrary
313             @see IMacroDocumentAccess::checkForBrokenScriptingSignatures
314         */
315         sal_Bool
316                 checkMacrosOnLoading(
317                     const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& _rxInteraction
318                 );
319 
320     private:
321         ::boost::shared_ptr< DocumentMacroMode_Data >   m_pData;
322 	};
323 
324 //........................................................................
325 } // namespace sfx2
326 //........................................................................
327 
328 #endif // SFX2_DOCMACROMODE_HXX
329