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 INCLUDED_UPDATE_HDL_HXX
29 #define INCLUDED_UPDATE_HDL_HXX
30 
31 #include "boost/utility.hpp"
32 #include <osl/mutex.hxx>
33 #include "com/sun/star/uno/Any.h"
34 #ifndef  _COM_SUN_STAR_UNO_REFERENCE_HPP_
35 #include "com/sun/star/uno/Reference.h"
36 #endif
37 #include "com/sun/star/uno/XComponentContext.hpp"
38 #include "com/sun/star/awt/Rectangle.hpp"
39 #include "com/sun/star/awt/XActionListener.hpp"
40 #include "com/sun/star/awt/XControlModel.hpp"
41 #include "com/sun/star/awt/XDialog.hpp"
42 #include "com/sun/star/awt/XTopWindowListener.hpp"
43 #include "com/sun/star/beans/NamedValue.hpp"
44 #include "com/sun/star/frame/XTerminateListener.hpp"
45 #include <com/sun/star/resource/XResourceBundle.hpp>
46 #include <com/sun/star/task/XInteractionHandler.hpp>
47 #include "cppuhelper/implbase4.hxx"
48 
49 #include "actionlistener.hxx"
50 
51 enum DialogControls
52 {
53     CANCEL_BUTTON = 0,
54     PAUSE_BUTTON,
55     RESUME_BUTTON,
56     INSTALL_BUTTON,
57     DOWNLOAD_BUTTON,
58     CLOSE_BUTTON,
59     HELP_BUTTON,
60     BUTTON_COUNT,
61     THROBBER_CTRL,
62     PROGRESS_CTRL
63 };
64 
65 enum UpdateState {
66     UPDATESTATE_CHECKING = 0,
67     UPDATESTATE_ERROR_CHECKING,
68     UPDATESTATE_NO_UPDATE_AVAIL,
69     UPDATESTATE_UPDATE_AVAIL,
70     UPDATESTATE_UPDATE_NO_DOWNLOAD,
71     UPDATESTATE_AUTO_START,
72     UPDATESTATE_DOWNLOADING,
73     UPDATESTATE_DOWNLOAD_PAUSED,
74     UPDATESTATE_ERROR_DOWNLOADING,
75     UPDATESTATE_DOWNLOAD_AVAIL,
76     UPDATESTATE_EXT_UPD_AVAIL,
77     UPDATESTATES_COUNT
78 };
79 
80 class UpdateHandler : ::boost::noncopyable,
81                       public cppu::WeakImplHelper4< com::sun::star::awt::XActionListener,
82                                                     com::sun::star::awt::XTopWindowListener,
83                                                     com::sun::star::task::XInteractionHandler,
84                                                     com::sun::star::frame::XTerminateListener >
85 {
86 private:
87     com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > mxContext;
88     com::sun::star::uno::Reference< com::sun::star::awt::XDialog > mxUpdDlg;
89     com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > mxInteractionHdl;
90     rtl::Reference< IActionListener > mxActionListener;
91 
92     UpdateState             meCurState;
93     UpdateState             meLastState;
94     sal_Int32               mnPercent;
95     short                   mnLastCtrlState;
96     bool                    mbDownloadBtnHasDots;
97     bool                    mbVisible;
98     bool                    mbStringsLoaded;
99     bool                    mbMinimized;
100     bool                    mbListenerAdded;
101     mutable bool            mbShowsMessageBox;
102 
103     osl::Mutex              maMutex;
104 
105     rtl::OUString           msNextVersion;
106     rtl::OUString           msDownloadPath;
107     rtl::OUString           msDownloadFile;
108     rtl::OUString           msDescriptionMsg;
109     rtl::OUString           msChecking;         // RID_UPDATE_STR_CHECKING
110     rtl::OUString           msCheckingError;    // RID_UPDATE_STR_CHECKING_ERR
111     rtl::OUString           msNoUpdFound;       // RID_UPDATE_STR_NO_UPD_FOUND
112     rtl::OUString           msUpdFound;         // RID_UPDATE_STR_UPD_FOUND
113     rtl::OUString           msDlgTitle;         // RID_UPDATE_STR_DLG_TITLE
114     rtl::OUString           msDownloadPause;    // RID_UPDATE_STR_DOWNLOAD_PAUSE
115     rtl::OUString           msDownloadError;    // RID_UPDATE_STR_DOWNLOAD_ERR
116     rtl::OUString           msDownloadWarning;  // RID_UPDATE_STR_DOWNLOAD_WARN
117     rtl::OUString           msDownloadDescr;    // RID_UPDATE_STR_DOWNLOAD_WARN
118     rtl::OUString           msDownloadNotAvail; // RID_UPDATE_STR_DOWNLOAD_UNAVAIL
119     rtl::OUString           msDownloading;      // RID_UPDATE_STR_DOWNLOADING
120     rtl::OUString           msReady2Install;    // RID_UPDATE_STR_READY_INSTALL
121     rtl::OUString           msCancelTitle;      // RID_UPDATE_STR_CANCEL_TITLE
122     rtl::OUString           msCancelMessage;    // RID_UPDATE_STR_CANCEL_DOWNLOAD
123     rtl::OUString           msInstallMessage;   // RID_UPDATE_STR_BEGIN_INSTALL
124     rtl::OUString           msInstallNow;       // RID_UPDATE_STR_INSTALL_NOW
125     rtl::OUString           msInstallLater;     // RID_UPDATE_STR_INSTALL_LATER
126     rtl::OUString           msInstallError;     // RID_UPDATE_STR_INSTALL_ERROR
127     rtl::OUString           msOverwriteWarning; // RID_UPDATE_STR_OVERWRITE_WARNING
128     rtl::OUString           msPercent;          // RID_UPDATE_STR_PERCENT
129     rtl::OUString           msReloadWarning;    // RID_UPDATE_STR_OVERWRITE_WARNING
130     rtl::OUString           msReloadReload;     // RID_UPDATE_STR_OVERWRITE_WARNING
131     rtl::OUString           msReloadContinue;   // RID_UPDATE_STR_OVERWRITE_WARNING
132     rtl::OUString           msStatusFL;         // RID_UPDATE_FT_STATUS
133     rtl::OUString           msDescription;      // RID_UPDATE_FT_DESCRIPTION
134     rtl::OUString           msClose;            // RID_UPDATE_BTN_CLOSE
135     rtl::OUString           msDownload;         // RID_UPDATE_BTN_DOWNLOAD
136     rtl::OUString           msInstall;          // RID_UPDATE_BTN_INSTALL
137     rtl::OUString           msPauseBtn;         // RID_UPDATE_BTN_PAUSE
138     rtl::OUString           msResumeBtn;        // RID_UPDATE_BTN_RESUME
139     rtl::OUString           msCancelBtn;        // RID_UPDATE_BTN_CANCEL
140     rtl::OUString           msButtonIDs[ BUTTON_COUNT ];
141     rtl::OUString           msBubbleTexts[ UPDATESTATES_COUNT ];
142     rtl::OUString           msBubbleTitles[ UPDATESTATES_COUNT ];
143 
144     void                    createDialog();
145     void                    updateState( UpdateState eNewState );
146     void                    startThrobber( bool bStart = true );
147     void                    setControlProperty( const rtl::OUString &rCtrlName,
148                                                 const rtl::OUString &rPropName,
149                                                 const com::sun::star::uno::Any &rPropValue );
150     void                    showControl( const rtl::OUString &rCtrlName, bool bShow = true );
151     void                    showControls( short nControls );
152     void                    focusControl( DialogControls eID );
153     void                    enableControls( short nCtrlState );
154     void                    setDownloadBtnLabel( bool bAppendDots );
155     void                    loadStrings();
156     rtl::OUString           loadString( const com::sun::star::uno::Reference< com::sun::star::resource::XResourceBundle > xBundle,
157                                         sal_Int32 nResourceId ) const;
158     rtl::OUString           substVariables( const rtl::OUString &rSource ) const;
159     static void             setProperty( com::sun::star::uno::Sequence< com::sun::star::beans::NamedValue > &rProps,
160                                          const int nIndex, const rtl::OUString &rPropName, const com::sun::star::uno::Any &rPropValue )
161                                          { rProps[ nIndex ].Name = rPropName; rProps[ nIndex ].Value = rPropValue; }
162     static void             insertControlModel( com::sun::star::uno::Reference< com::sun::star::awt::XControlModel > & rxDialogModel,
163                                                 rtl::OUString const & rServiceName,
164                                                 rtl::OUString const & rControlName,
165                                                 com::sun::star::awt::Rectangle const & rPosSize,
166                                                 com::sun::star::uno::Sequence< com::sun::star::beans::NamedValue > const & rProps );
167 
168     void                    setFullVersion( rtl::OUString& rString );
169     void                    searchAndReplaceAll( rtl::OUString &rText, const rtl::OUString &rWhat, const rtl::OUString &rWith ) const;
170 
171 public:
172                             UpdateHandler( const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & rxContext,
173                                            const rtl::Reference< IActionListener > & rxActionListener );
174     virtual                ~UpdateHandler();
175 
176     bool                    isVisible() const;
177     bool                    isMinimized() const { return mbMinimized; }
178     void                    setVisible( bool bVisible = true );
179     void                    setProgress( sal_Int32 nPercent );
180     void                    setNextVersion( const rtl::OUString &rNextVersion ) { msNextVersion = rNextVersion; }
181     void                    setDownloadPath( const rtl::OUString &rPath ) { msDownloadPath = rPath; }
182     void                    setDownloadFile( const rtl::OUString &rPath );
183     void                    setErrorMessage( const rtl::OUString &rErrorMsg );
184     void                    setDescription( const rtl::OUString &rDescription ){ msDescriptionMsg = rDescription; }
185 
186     void                    setState( UpdateState eState );
187     rtl::OUString           getBubbleText( UpdateState eState );
188     rtl::OUString           getBubbleTitle( UpdateState eState );
189     rtl::OUString           getDefaultInstErrMsg();
190     bool                    showWarning( const rtl::OUString &rWarning ) const;
191     bool                    showWarning( const rtl::OUString &rWarning, const rtl::OUString& rBtnText_1, const rtl::OUString& rBtnText_2 ) const;
192     bool                    showOverwriteWarning( const rtl::OUString &rFileName ) const;
193     bool                    showOverwriteWarning() const;
194 
195     // Allows runtime exceptions to be thrown by const methods
196     inline SAL_CALL operator com::sun::star::uno::Reference< com::sun::star::uno::XInterface > () const
197         { return const_cast< cppu::OWeakObject * > (static_cast< cppu::OWeakObject const * > (this)); };
198 
199     // XActionListener
200     virtual void SAL_CALL   disposing( const com::sun::star::lang::EventObject &rObj ) throw( com::sun::star::uno::RuntimeException );
201     virtual void SAL_CALL   actionPerformed( com::sun::star::awt::ActionEvent const & rEvent) throw( com::sun::star::uno::RuntimeException );
202 
203 	// XTopWindowListener
204     virtual void SAL_CALL   windowOpened( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
205     virtual void SAL_CALL   windowClosing( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
206     virtual void SAL_CALL   windowClosed( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
207     virtual void SAL_CALL   windowMinimized( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
208     virtual void SAL_CALL   windowNormalized( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
209     virtual void SAL_CALL   windowActivated( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
210     virtual void SAL_CALL   windowDeactivated( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
211 
212     // XInteractionHandler
213 	virtual void SAL_CALL   handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& Request )
214 		                        throw( com::sun::star::uno::RuntimeException );
215 
216     // XTerminateListener
217     virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException);
218     virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
219 };
220 
221 #endif /* INCLUDED_UPDATE_HDL_HXX */
222