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 FORMS_SOURCE_CLICKABLEIMAGE_HXX
29 #define FORMS_SOURCE_CLICKABLEIMAGE_HXX
30 
31 #include "FormComponent.hxx"
32 #include "EventThread.hxx"
33 #include "imgprod.hxx"
34 #include <tools/link.hxx>
35 #include <comphelper/propmultiplex.hxx>
36 #include <com/sun/star/form/XImageProducerSupplier.hpp>
37 #include <com/sun/star/form/FormButtonType.hpp>
38 #include <com/sun/star/form/XApproveActionListener.hpp>
39 #include <com/sun/star/form/XApproveActionBroadcaster.hpp>
40 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
41 #include <com/sun/star/form/submission/XSubmission.hpp>
42 #include <com/sun/star/frame/XModel.hpp>
43 #include <com/sun/star/frame/XDispatchProviderInterception.hpp>
44 #include <cppuhelper/implbase3.hxx>
45 
46 
47 class SfxMedium;
48 
49 //.........................................................................
50 namespace frm
51 {
52 //.........................................................................
53 
54     class OImageProducerThread_Impl;
55     class ControlFeatureInterception;
56     //==================================================================
57     // OClickableImageBaseModel
58     //==================================================================
59     typedef ::cppu::ImplHelper3 <   ::com::sun::star::form::XImageProducerSupplier
60                                 ,   ::com::sun::star::awt::XImageProducer
61                                 ,   ::com::sun::star::form::submission::XSubmissionSupplier
62                                 >   OClickableImageBaseModel_Base;
63 
64     class OClickableImageBaseModel  :public OClickableImageBaseModel_Base
65                                     ,public OControlModel
66                                     ,public OPropertyChangeListener
67     {
68     protected:
69 	    ::com::sun::star::form::FormButtonType	m_eButtonType;		// Art des Buttons (push,submit,reset)
70 	    ::rtl::OUString							m_sTargetURL;		// URL fuer den URL-Button
71 	    ::rtl::OUString							m_sTargetFrame;		// TargetFrame zum Oeffnen
72 
73         // ImageProducer stuff
74 	    ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageProducer>  	m_xProducer;
75 	    SfxMedium*								m_pMedium;			// Download-Medium
76 	    ImageProducer*							m_pProducer;
77 	    sal_Bool								m_bDispatchUrlInternal; // property: is not allowed to set : 1
78 	    sal_Bool								m_bDownloading : 1;	// laeuft ein Download?
79 	    sal_Bool 								m_bProdStarted : 1;
80 
81         // XSubmission stuff
82         ::com::sun::star::uno::Reference< ::com::sun::star::form::submission::XSubmission >
83                                                 m_xSubmissionDelegate;
84 
85 
86 	    DECL_STATIC_LINK( OClickableImageBaseModel, DataAvailableLink, void* );
87 	    DECL_STATIC_LINK( OClickableImageBaseModel, DownloadDoneLink, void* );
88 
89 	    inline ImageProducer* GetImageProducer() { return m_pProducer; }
90 
91 	    void StartProduction();
92 	    void SetURL(const ::rtl::OUString& rURL);
93 	    void DataAvailable();
94 	    void DownloadDone();
95 
96 	    ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> _getTypes();
97 	    inline sal_Bool isDispatchUrlInternal() const { return m_bDispatchUrlInternal; }
98 	    inline void		setDispatchUrlInternal(sal_Bool _bDispatch) { m_bDispatchUrlInternal = _bDispatch; }
99 
100     public:
101 	    DECLARE_DEFAULT_XTOR( OClickableImageBaseModel );
102 
103 	    // UNO Anbindung
104 	    DECLARE_UNO3_AGG_DEFAULTS(OClickableImageBaseModel, OControlModel);
105 	    virtual ::com::sun::star::uno::Any SAL_CALL queryAggregation(const ::com::sun::star::uno::Type& _rType) throw(::com::sun::star::uno::RuntimeException);
106 
107     protected:
108         // OComponentHelper
109 	    virtual void SAL_CALL disposing();
110 
111         // ::com::sun::star::form::XImageProducerSupplier
112 	    virtual ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageProducer> SAL_CALL getImageProducer() throw (::com::sun::star::uno::RuntimeException) { return m_xProducer; }
113 
114         // OPropertySetHelper
115 	    virtual void SAL_CALL getFastPropertyValue(::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const;
116 	    virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue) throw (::com::sun::star::uno::Exception);
117 
118 	    virtual sal_Bool SAL_CALL convertFastPropertyValue(::com::sun::star::uno::Any& rConvertedValue, ::com::sun::star::uno::Any& rOldValue, sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue )
119 		    throw(::com::sun::star::lang::IllegalArgumentException);
120 
121         using ::cppu::OPropertySetHelper::getFastPropertyValue;
122 
123         // OPropertyChangeListener
124 	    virtual void _propertyChanged(const ::com::sun::star::beans::PropertyChangeEvent&) throw(::com::sun::star::uno::RuntimeException);
125 
126         // XPropertyState
127 	    virtual ::com::sun::star::uno::Any getPropertyDefaultByHandle( sal_Int32 nHandle ) const;
128 
129         // XImageProducer
130         virtual void SAL_CALL addConsumer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer >& xConsumer ) throw (::com::sun::star::uno::RuntimeException);
131         virtual void SAL_CALL removeConsumer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer >& xConsumer ) throw (::com::sun::star::uno::RuntimeException);
132         virtual void SAL_CALL startProduction(  ) throw (::com::sun::star::uno::RuntimeException);
133 
134         // XSubmissionSupplier
135         virtual ::com::sun::star::uno::Reference< ::com::sun::star::form::submission::XSubmission > SAL_CALL getSubmission() throw (::com::sun::star::uno::RuntimeException);
136         virtual void SAL_CALL setSubmission( const ::com::sun::star::uno::Reference< ::com::sun::star::form::submission::XSubmission >& _submission ) throw (::com::sun::star::uno::RuntimeException);
137 
138         // XServiceInfo
139         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
140 
141         // XEventListener
142         using OControlModel::disposing;
143 
144     public:
145         struct GuardAccess { friend class ImageModelMethodGuard; private: GuardAccess() { } };
146         ::osl::Mutex&   getMutex( GuardAccess ) { return m_aMutex; }
147         ImageProducer*  getImageProducer( GuardAccess ) { return m_pProducer; }
148 
149     protected:
150         using OControlModel::getMutex;
151 
152 	    void implConstruct();
153 
154         // to be called from within the cloning-ctor of your derived class
155         void implInitializeImageURL( );
156     };
157 
158     //==================================================================
159     // ImageModelMethodGuard
160     //==================================================================
161     class ImageModelMethodGuard : public ::osl::MutexGuard
162     {
163     private:
164         typedef ::osl::MutexGuard   GuardBase;
165 
166     public:
167         ImageModelMethodGuard( OClickableImageBaseModel& _rModel )
168             :GuardBase( _rModel.getMutex( OClickableImageBaseModel::GuardAccess() ) )
169         {
170             if ( NULL == _rModel.getImageProducer( OClickableImageBaseModel::GuardAccess() ) )
171                 throw ::com::sun::star::lang::DisposedException(
172                     ::rtl::OUString(),
173                     static_cast< ::com::sun::star::form::XImageProducerSupplier* >( &_rModel )
174                 );
175         }
176     };
177 
178     //==================================================================
179     // OClickableImageBaseControl
180     //==================================================================
181     typedef ::cppu::ImplHelper3 <   ::com::sun::star::form::XApproveActionBroadcaster
182                                 ,   ::com::sun::star::form::submission::XSubmission
183                                 ,   ::com::sun::star::frame::XDispatchProviderInterception
184                                 >   OClickableImageBaseControl_BASE;
185 
186     class OClickableImageBaseControl    :public OClickableImageBaseControl_BASE
187                                         ,public OControl
188     {
189 	    friend class OImageProducerThread_Impl;
190 
191     private:
192 	    OImageProducerThread_Impl*          m_pThread;
193         ::cppu::OInterfaceContainerHelper   m_aSubmissionVetoListeners;
194         ::std::auto_ptr< ControlFeatureInterception >
195                                             m_pFeatureInterception;
196 
197     protected:
198         ::cppu::OInterfaceContainerHelper m_aApproveActionListeners;
199 	    ::cppu::OInterfaceContainerHelper m_aActionListeners;
200 	    ::rtl::OUString m_aActionCommand;
201 
202         // XSubmission
203         virtual void SAL_CALL submit(  ) throw (::com::sun::star::util::VetoException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
204         virtual void SAL_CALL submitWithInteraction( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& aHandler ) throw (::com::sun::star::util::VetoException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
205         virtual void SAL_CALL addSubmissionVetoListener( const ::com::sun::star::uno::Reference< ::com::sun::star::form::submission::XSubmissionVetoListener >& listener ) throw (::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
206         virtual void SAL_CALL removeSubmissionVetoListener( const ::com::sun::star::uno::Reference< ::com::sun::star::form::submission::XSubmissionVetoListener >& listener ) throw (::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
207 
208         // XServiceInfo
209         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
210 
211         // XEventListener
212         using OControl::disposing;
213 
214     public:
215 	    OClickableImageBaseControl(
216 		    const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory>& _rxFactory,
217 		    const ::rtl::OUString& _aService);
218 	    virtual ~OClickableImageBaseControl();
219 
220     protected:
221         // UNO Anbindung
222 	    DECLARE_UNO3_AGG_DEFAULTS(OClickableImageBaseControl, OControl);
223 	    virtual ::com::sun::star::uno::Any SAL_CALL queryAggregation(const ::com::sun::star::uno::Type& _rType) throw(::com::sun::star::uno::RuntimeException);
224 
225         // OComponentHelper
226 	    virtual void SAL_CALL disposing();
227 
228         // ::com::sun::star::form::XApproveActionBroadcaster
229 	    virtual void SAL_CALL addApproveActionListener(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XApproveActionListener>& _rxListener)
230 		    throw(::com::sun::star::uno::RuntimeException);
231 	    virtual void SAL_CALL removeApproveActionListener(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XApproveActionListener>& _rxListener)
232 		    throw(::com::sun::star::uno::RuntimeException);
233 
234         // XDispatchProviderInterception
235         virtual void SAL_CALL registerDispatchProviderInterceptor( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProviderInterceptor >& Interceptor ) throw (::com::sun::star::uno::RuntimeException);
236         virtual void SAL_CALL releaseDispatchProviderInterceptor( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProviderInterceptor >& Interceptor ) throw (::com::sun::star::uno::RuntimeException);
237 
238     protected:
239 	    virtual void actionPerformed_Impl( sal_Bool bNotifyListener, const ::com::sun::star::awt::MouseEvent& rEvt );
240 
241         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > _getTypes();
242 
243         /** approves the action by calling the approve listeners
244             @return <TRUE/> if and only if the action has <em>not</em> been cancelled by a listener
245         */
246         bool    approveAction( );
247 
248         /** retrieves (and if necessary creates) the image producer thread.
249 
250             Must be called with our mutex locked
251         */
252         OImageProducerThread_Impl* getImageProducerThread();
253 
254     private:
255         void implSubmit(
256             const ::com::sun::star::awt::MouseEvent& _rEvent,
257             const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& aHandler
258         ) SAL_THROW((com::sun::star::util::VetoException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException));
259     };
260 
261     //==================================================================
262     // OImageProducerThread_Impl
263     //==================================================================
264     class OImageProducerThread_Impl: public OComponentEventThread
265     {
266     protected:
267 
268 	    // Die folgende Methode wrrd gerufen um das Event unter Beruecksichtigung
269 	    // seines Typs zu duplizieren
270 	    virtual ::com::sun::star::lang::EventObject* cloneEvent( const ::com::sun::star::lang::EventObject* _pEvt ) const;
271 
272 	    // Ein Event bearbeiten. Der Mutex ist dabei nicht gelockt, pCompImpl
273 	    // bleibt aber in jedem Fall gueltig.
274 	    virtual void processEvent( ::cppu::OComponentHelper *pCompImpl,
275 							    const ::com::sun::star::lang::EventObject*,
276 							    const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl>&,
277 							    sal_Bool );
278 
279     public:
280 	    OImageProducerThread_Impl( OClickableImageBaseControl *pControl ) :
281 		    OComponentEventThread( pControl )
282 	    {}
283 
284 	    void addEvent() { ::com::sun::star::lang::EventObject aEvt; OComponentEventThread::addEvent( &aEvt ); }
285 
286     protected:
287         using OComponentEventThread::addEvent;
288     };
289 
290 //.........................................................................
291 }	// namespace frm
292 //.........................................................................
293 
294 #endif // FORMS_SOURCE_CLICKABLEIMAGE_HXX
295 
296