1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _DBA_COREDATAACCESS_DOCUMENTDEFINITION_HXX_
29 #define _DBA_COREDATAACCESS_DOCUMENTDEFINITION_HXX_
30 
31 #ifndef _CPPUHELPER_PROPSHLP_HXX
32 #include <cppuhelper/propshlp.hxx>
33 #endif
34 #ifndef _CPPUHELPER_IMPLBASE4_HXX_
35 #include <cppuhelper/implbase4.hxx>
36 #endif
37 #ifndef DBA_CONTENTHELPER_HXX
38 #include "ContentHelper.hxx"
39 #endif
40 #ifndef COMPHELPER_PROPERTYSTATECONTAINER_HXX
41 #include <comphelper/propertystatecontainer.hxx>
42 #endif
43 #ifndef _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
44 #include <comphelper/proparrhlp.hxx>
45 #endif
46 #ifndef _DBASHARED_APITOOLS_HXX_
47 #include "apitools.hxx"
48 #endif
49 #ifndef _COMPHELPER_UNO3_HXX_
50 #include <comphelper/uno3.hxx>
51 #endif
52 #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
53 #include <com/sun/star/sdbc/XConnection.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_FRAME_XCOMPONENTLOADER_HPP_
56 #include <com/sun/star/frame/XComponentLoader.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_FRAME_XCONTROLLER_HPP_
59 #include <com/sun/star/frame/XController.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_EMBED_XSTATECHANGELISTENER_HPP_
62 #include <com/sun/star/embed/XStateChangeListener.hpp>
63 #endif
64 #include <com/sun/star/sdb/XSubDocument.hpp>
65 #include <com/sun/star/util/XCloseListener.hpp>
66 #include <com/sun/star/container/XHierarchicalName.hpp>
67 
68 namespace comphelper
69 {
70     class NamedValueCollection;
71 }
72 
73 //........................................................................
74 namespace dbaccess
75 {
76 //........................................................................
77 
78 	class OInterceptor;
79 	class OEmbeddedClientHelper;
80 //==========================================================================
81 //= ODocumentDefinition - a database "document" which is simply a link to a real
82 //=					  document
83 //==========================================================================
84 
85 typedef ::cppu::ImplHelper4 <   ::com::sun::star::embed::XComponentSupplier
86                             ,   ::com::sun::star::sdb::XSubDocument
87                             ,   ::com::sun::star::util::XCloseListener
88                             ,   ::com::sun::star::container::XHierarchicalName
89                             >   ODocumentDefinition_Base;
90 
91 class ODocumentDefinition
92 		:public OContentHelper
93 		,public ::comphelper::OPropertyStateContainer
94 		,public ::comphelper::OPropertyArrayUsageHelper< ODocumentDefinition >
95         ,public ODocumentDefinition_Base
96 {
97 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XEmbeddedObject>			m_xEmbeddedObject;
98 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XStateChangeListener >	m_xListener;
99     ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >             m_xLastKnownConnection;
100 
101 	OInterceptor*																		m_pInterceptor;
102 	sal_Bool																			m_bForm; // <TRUE/> if it is a form
103 	sal_Bool																			m_bOpenInDesign;
104 	sal_Bool																			m_bInExecute;
105     sal_Bool																			m_bRemoveListener;
106 	OEmbeddedClientHelper*																m_pClientHelper;
107 
108 protected:
109 	virtual ~ODocumentDefinition();
110 
111 public:
112 
113 	ODocumentDefinition(
114             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContainer,
115 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&,
116 			const TContentPtr& _pImpl,
117 			sal_Bool _bForm
118 		);
119 
120     void    initialLoad(
121 			    const ::com::sun::star::uno::Sequence< sal_Int8 >& i_rClassID,
122                 const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& i_rCreationArgs,
123 			    const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& i_rConnection
124             );
125 
126 // com::sun::star::lang::XTypeProvider
127 	DECLARE_TYPEPROVIDER( );
128 
129 // ::com::sun::star::uno::XInterface
130 	DECLARE_XINTERFACE( )
131 
132 // ::com::sun::star::lang::XServiceInfo
133 	DECLARE_SERVICE_INFO_STATIC();
134 
135 // ::com::sun::star::beans::XPropertySet
136 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException);
137 
138     // OPropertySetHelper
139 	virtual void SAL_CALL getFastPropertyValue(
140                                 ::com::sun::star::uno::Any& o_rValue,
141                                 sal_Int32 i_nHandle
142                             ) const;
143 
144 	// XComponentSupplier
145 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable > SAL_CALL getComponent(  ) throw (::com::sun::star::uno::RuntimeException);
146 
147     // XSubDocument
148     virtual ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > SAL_CALL open(  ) throw (::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
149     virtual ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > SAL_CALL openDesign(  ) throw (::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
150     virtual void SAL_CALL store(  ) throw (::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
151     virtual ::sal_Bool SAL_CALL close(  ) throw (::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
152 
153 	// XHierarchicalName
154     virtual ::rtl::OUString SAL_CALL getHierarchicalName(  ) throw (::com::sun::star::uno::RuntimeException);
155     virtual ::rtl::OUString SAL_CALL composeHierarchicalName( const ::rtl::OUString& aRelativeName ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
156 
157 // OPropertySetHelper
158 	virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper();
159 
160 	// XCommandProcessor
161 	virtual ::com::sun::star::uno::Any SAL_CALL execute( const ::com::sun::star::ucb::Command& aCommand, sal_Int32 CommandId, const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >& Environment ) throw (::com::sun::star::uno::Exception, ::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::RuntimeException) ;
162 
163 	// XRename
164 	virtual void SAL_CALL rename( const ::rtl::OUString& newName ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException);
165 
166     // XCloseListener
167     virtual void SAL_CALL queryClosing( const ::com::sun::star::lang::EventObject& Source, ::sal_Bool GetsOwnership ) throw (::com::sun::star::util::CloseVetoException, ::com::sun::star::uno::RuntimeException);
168     virtual void SAL_CALL notifyClosing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
169 
170     // XEventListener
171     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
172 
173     /** returns the forms/reports container storage, depending on m_bForm. Our own storage
174         inside this container storage is the one with the name as indicated by m_pImpl->m_aProps.sPersistentName.
175     */
176 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
177         getContainerStorage() const;
178 
179 	sal_Bool save(sal_Bool _bApprove);
180     sal_Bool saveAs();
181 	void closeObject();
182 	sal_Bool isModified();
183     inline sal_Bool isNewReport() const { return !m_bForm && !m_pImpl->m_aProps.bAsTemplate; }
184 
185 	static void fillReportData(
186                     const ::comphelper::ComponentContext& _rContext,
187                     const ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable >& _rxComponent,
188                     const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxActiveConnection
189                 );
190 
191     const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&
192         getConnection() const { return m_xLastKnownConnection; }
193 
194     /** prepares closing the document component
195 
196         The method suspends the controller associated with the document, and saves the document
197         if necessary.
198 
199         @return
200             <TRUE/> if and only if the document component can be closed
201     */
202     bool prepareClose();
203 
204     static ::com::sun::star::uno::Sequence< sal_Int8 > getDefaultDocumentTypeClassId();
205 
206     static ::rtl::OUString GetDocumentServiceFromMediaType(
207         const ::rtl::OUString& _rMediaType,
208         const ::comphelper::ComponentContext& _rContext,
209         ::com::sun::star::uno::Sequence< sal_Int8 >& _rClassId
210     );
211     static ::rtl::OUString GetDocumentServiceFromMediaType(
212         const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& _rxContainerStorage,
213         const ::rtl::OUString& _rEntityName,
214         const ::comphelper::ComponentContext& _rContext,
215         ::com::sun::star::uno::Sequence< sal_Int8 >& _rClassId
216     );
217 
218     struct NotifierAccess { friend class NameChangeNotifier; private: NotifierAccess() { } };
219     const ::rtl::OUString& getCurrentName() const { return m_pImpl->m_aProps.aTitle; }
220     void firePropertyChange(
221                   sal_Int32 i_nHandle,
222             const ::com::sun::star::uno::Any& i_rNewValue,
223             const ::com::sun::star::uno::Any& i_rOldValue,
224                   sal_Bool i_bVetoable,
225             const NotifierAccess
226         );
227 
228 private:
229     /** does necessary initializations after our embedded object has been switched to ACTIVE
230     */
231     void    impl_onActivateEmbeddedObject_nothrow( const bool i_bReactivated );
232 
233     /** initializes a newly created view/controller of a form which is displaying our embedded object
234 
235         Has only to be called if the respective embedded object has been loaded for design (and
236         not for data entry)
237 
238         @param  _rxController
239             the controller which belongs to the XModel of our (active) embedded object
240     */
241     static void impl_initFormEditView( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& _rxController );
242 
243     /** removes the given frame from the desktop's frame collection
244         @raises ::com::sun::star::uno::RuntimeException
245     */
246     static void impl_removeFrameFromDesktop_throw(
247                     const ::comphelper::ComponentContext& _rContxt,
248                     const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& _rxFrame
249                 );
250 
251     /** opens the UI for this sub document
252     */
253     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
254             impl_openUI_nolck_throw( bool _bForEditing );
255 
256     /** stores our document, if it's already loaded
257     */
258     void    impl_store_throw();
259 
260     /** closes our document, if it's open
261     */
262     bool    impl_close_throw();
263 
264     /** returns our component, creates it if necessary
265     */
266     ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable >
267             impl_getComponent_throw( const bool i_ForceCreate = true );
268 
269     /** shows or hides our component
270 
271         The embedded object must exist, and be in state LOADED, at least.
272     */
273     void    impl_showOrHideComponent_throw( const bool i_bShow );
274 
275 	// OPropertyArrayUsageHelper
276 	virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
277 
278 	virtual void getPropertyDefaultByHandle( sal_Int32 _nHandle, ::com::sun::star::uno::Any& _rDefault ) const;
279 
280     // helper
281 	virtual void SAL_CALL disposing();
282 
283     // OContentHelper overridables
284     virtual ::rtl::OUString determineContentType() const;
285 
286 	/** fills the load arguments
287 	*/
288     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >
289         fillLoadArgs(
290             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,
291             const bool _bSuppressMacros,
292 			const bool _bReadOnly,
293             const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& i_rOpenCommandArguments,
294 			::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _out_rEmbeddedObjectDescriptor
295         );
296 
297     /** splits the given arguments to an "open*" command into arguments for loading the document, and arguments to be
298         put into the EmbeddedObjectDescriptor
299 
300         Any values already present in <code>o_rDocumentLoadArgs</code> and <code>o_rEmbeddedObjectDescriptor</code>
301         will be overwritten by values from <code>i_rOpenCommandArguments</code>, if applicable, otherwise they will
302         be preserved.
303 
304         @param i_rOpenCommandArguments
305             the arguments passed to the "open*" command at the content
306         @param o_rDocumentLoadArgs
307             the arguments to be passed when actually loading the embedded document.
308         @param o_rEmbeddedObjectDescriptor
309             the EmbeddedObjectDescriptor to be passed when initializing the embedded object
310     */
311     void separateOpenCommandArguments(
312             const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >&    i_rOpenCommandArguments,
313             ::comphelper::NamedValueCollection&                                                 o_rDocumentLoadArgs,
314             ::comphelper::NamedValueCollection&                                                 o_rEmbeddedObjectDescriptor
315         );
316 
317 	/** loads the EmbeddedObject if not already loaded
318 		@param	_aClassID
319 			If set, it will be used to create the embedded object.
320 	*/
321 	void loadEmbeddedObject(
322                 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,
323                 const ::com::sun::star::uno::Sequence< sal_Int8 >& _aClassID,
324                 const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rAdditionalArgs,
325                 const bool _bSuppressMacros,
326 				const bool _bReadOnly
327             );
328 
329     /** loads the embedded object, if not already loaded. No new object can be created with this method.
330     */
331     void    loadEmbeddedObject( bool _bSuppressMacros = false )
332     {
333         loadEmbeddedObject(
334             NULL,
335             ::com::sun::star::uno::Sequence< sal_Int8 >(),
336             ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >(),
337             _bSuppressMacros,
338             false
339         );
340     }
341 
342     /** loads the embedded object for preview. Macros will be suppressed, and the document will
343         be read-only.
344     */
345     void    loadEmbeddedObjectForPreview()
346     {
347         loadEmbeddedObject(
348             NULL,
349             ::com::sun::star::uno::Sequence< sal_Int8 >(),
350             ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >(),
351             true,
352             true
353         );
354     }
355 
356 	/** searches for read-only flag in the args of the model and sets it to the given value,
357 		if the value was not found, it will be appended.
358 		@param	_bReadOnly
359 			If <TRUE/> the document will be switched to readonly mode
360 	*/
361 	void updateDocumentTitle();
362 
363 	void registerProperties();
364 
365     /** determines whether the document we represent supports embedded scripts and macros
366     */
367     sal_Bool objectSupportsEmbeddedScripts() const;
368 
369     //-------------------------------------------------------------------------
370     //- commands
371     //-------------------------------------------------------------------------
372 
373     void onCommandGetDocumentProperties( ::com::sun::star::uno::Any& _rProps );
374 	void onCommandInsert( const ::rtl::OUString& _sURL, const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >& Environment ) throw( ::com::sun::star::uno::Exception );
375 	void onCommandPreview( ::com::sun::star::uno::Any& _rImage );
376     ::com::sun::star::uno::Any
377         onCommandOpenSomething(
378             const ::com::sun::star::uno::Any& _rArgument,
379             const bool _bActivate,
380             const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >& _rxEnvironment
381         );
382 private:
383     using ::cppu::OPropertySetHelper::getFastPropertyValue;
384 };
385 
386 class NameChangeNotifier
387 {
388 public:
389     NameChangeNotifier(
390         ODocumentDefinition& i_rDocumentDefinition,
391         const ::rtl::OUString& i_rNewName,
392         ::osl::ResettableMutexGuard& i_rClearForNotify
393     );
394     ~NameChangeNotifier();
395 
396 private:
397             ODocumentDefinition&            m_rDocumentDefinition;
398     const   ::com::sun::star::uno::Any      m_aOldValue;
399     const   ::com::sun::star::uno::Any      m_aNewValue;
400             ::osl::ResettableMutexGuard&    m_rClearForNotify;
401 
402     void    impl_fireEvent_throw( const sal_Bool i_bVetoable );
403 };
404 
405 //........................................................................
406 }	// namespace dbaccess
407 //........................................................................
408 
409 #endif // _DBA_COREDATAACCESS_DOCUMENTDEFINITION_HXX_
410 
411