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 #include <com/sun/star/beans/NamedValue.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/task/XInteractionHandler.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
32 
33 #include <osl/conditn.hxx>
34 #include <osl/thread.hxx>
35 #include <rtl/instance.hxx>
36 #include <salhelper/refobj.hxx>
37 
38 #include "updateinfo.hxx"
39 #include "updatecheckconfiglistener.hxx"
40 #include "actionlistener.hxx"
41 #include "updatehdl.hxx"
42 #include "download.hxx"
43 
44 
45 class UpdateCheck;
46 class UpdateCheckConfig;
47 
48 class UpdateCheckInitData {
49 
50 public:
51     inline rtl::Reference< UpdateCheck > SAL_CALL operator() () const;
52 };
53 
54 class WorkerThread : public osl::Thread
55 {
56 public:
57     virtual void SAL_CALL cancel() = 0;
58 };
59 
60 class UpdateCheck :
61     public UpdateCheckConfigListener,
62     public IActionListener,
63     public DownloadInteractionHandler,
64     public salhelper::ReferenceObject,
65     public rtl::StaticWithInit< rtl::Reference< UpdateCheck >, UpdateCheckInitData >
66 {
67     UpdateCheck() : m_eState(NOT_INITIALIZED), m_eUpdateState(UPDATESTATES_COUNT), m_pThread(NULL) {};
68 
69 public:
70     inline SAL_CALL operator rtl::Reference< UpdateCheckConfigListener > ()
71         { return static_cast< UpdateCheckConfigListener * > (this); }
72 
73     void initialize(const com::sun::star::uno::Sequence<com::sun::star::beans::NamedValue>& rValues,
74                     const com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext>& xContext);
75 
76     /* Returns an instance of the specified service obtained from the specified
77      * component context
78      */
79 
80     static com::sun::star::uno::Reference< com::sun::star::uno::XInterface > createService(
81         const rtl::OUString& aServiceName,
82         const com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext>& xContext);
83 
84     // Update internal update info member
85     void setUpdateInfo(const UpdateInfo& aInfo);
86 
87     /* This method turns on the menubar icon, triggers the bubble window or
88      * updates the dialog text when appropriate
89      */
90     void setUIState(UpdateState eState, bool suppressBubble = false);
91 
92     // Returns the UI state that matches rInfo best
93     static UpdateState getUIState(const UpdateInfo& rInfo);
94 
95     // Check for updates failed
96     void setCheckFailedState();
97 
98     // Executes the update check dialog for manual checks and downloads interaction
99     void showDialog(bool forceCheck = false);
100 
101     // Returns true if the update dialog is currently showing
102     bool isDialogShowing() const;
103     bool shouldShowExtUpdDlg() const { return ( m_bShowExtUpdDlg && m_bHasExtensionUpdate ); }
104     void showExtensionDialog();
105     void setHasExtensionUpdates( bool bHasUpdates ) { m_bHasExtensionUpdate = bHasUpdates; }
106     bool hasOfficeUpdate() const { return (m_aUpdateInfo.BuildId.getLength() > 0); }
107 
108     // DownloadInteractionHandler
109     virtual bool downloadTargetExists(const rtl::OUString& rFileName);
110     virtual void downloadStalled(const rtl::OUString& rErrorMessage);
111     virtual void downloadProgressAt(sal_Int8 nProcent);
112     virtual void downloadStarted(const rtl::OUString& rLocalFileName, sal_Int64 nFileSize);
113     virtual void downloadFinished(const rtl::OUString& rLocalFileName);
114     // checks if the download target already exists and asks user what to do next
115     virtual bool checkDownloadDestination( const rtl::OUString& rFile );
116 
117     // Cancels the download action (and resumes checking if enabled)
118     void cancelDownload();
119 
120     // Returns the XInteractionHandler of the UpdateHandler instance if present (and visible)
121     com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > getInteractionHandler() const;
122 
123     // UpdateCheckConfigListener
124     virtual void autoCheckStatusChanged(bool enabled);
125     virtual void autoCheckIntervalChanged();
126 
127     // IActionListener
128     void cancel();
129     void download();
130     void install();
131     void pause();
132     void resume();
133     void closeAfterFailure();
134 
135     // rtl::IReference
136     virtual oslInterlockedCount SAL_CALL acquire() SAL_THROW(());
137     virtual oslInterlockedCount SAL_CALL release() SAL_THROW(());
138 
139 private:
140 
141     // Schedules or cancels next automatic check for updates
142     void enableAutoCheck(bool enable);
143 
144     // Starts/resumes or stops a download
145     void enableDownload(bool enable, bool paused=false);
146 
147     // Shuts down the currently running thread
148     void shutdownThread(bool join);
149 
150     // Returns the update handler instance
151     rtl::Reference<UpdateHandler> getUpdateHandler();
152 
153     // Open the given URL in a browser
154     void showReleaseNote(const rtl::OUString& rURL) const;
155 
156     // stores the release note url on disk to be used by setup app
157     static bool storeReleaseNote(sal_Int8 nNum, const rtl::OUString &rURL);
158 
159     /* This method turns on the menubar icon and triggers the bubble window
160      */
161     void handleMenuBarUI( rtl::Reference< UpdateHandler > rUpdateHandler,
162                           UpdateState& eState, bool suppressBubble );
163     enum State {
164         NOT_INITIALIZED,
165         DISABLED,
166         CHECK_SCHEDULED,
167         DOWNLOADING,
168         DOWNLOAD_PAUSED
169     };
170 
171     State m_eState;
172     UpdateState m_eUpdateState;
173 
174     mutable osl::Mutex m_aMutex;
175     WorkerThread *m_pThread;
176     osl::Condition m_aCondition;
177 
178     UpdateInfo m_aUpdateInfo;
179     rtl::OUString m_aImageName;
180     bool m_bHasExtensionUpdate;
181     bool m_bShowExtUpdDlg;
182 
183     rtl::Reference<UpdateHandler> m_aUpdateHandler;
184     com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet> m_xMenuBarUI;
185     com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> m_xContext;
186 
187     friend class UpdateCheckInitData;
188 };
189 
190 inline rtl::Reference< UpdateCheck > SAL_CALL
191 UpdateCheckInitData::operator() () const
192 {
193     return rtl::Reference< UpdateCheck > (new UpdateCheck());
194 }
195