Update ooo320-m1
[ooovba.git] / desktop / source / deployment / gui / dp_gui_updatability.cxx
blobd78ade6a8f121e002701676e930f6f42ad4cc7e8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_gui_updatability.cxx,v $
10 * $Revision: 1.6.86.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "sal/config.h"
36 #include <cstddef>
38 #include "com/sun/star/deployment/DeploymentException.hpp"
39 #include "com/sun/star/deployment/UpdateInformationProvider.hpp"
40 #include "com/sun/star/deployment/XPackage.hpp"
41 #include "com/sun/star/deployment/XPackageManager.hpp"
42 #include "com/sun/star/deployment/XUpdateInformationProvider.hpp"
43 #include "com/sun/star/task/XAbortChannel.hpp"
44 #include "com/sun/star/ucb/CommandAbortedException.hpp"
45 #include "com/sun/star/ucb/CommandFailedException.hpp"
46 #include "com/sun/star/ucb/XCommandEnvironment.hpp"
47 #include "com/sun/star/uno/Reference.hxx"
48 #include "com/sun/star/uno/RuntimeException.hpp"
49 #include "com/sun/star/uno/Sequence.hxx"
50 #include "com/sun/star/uno/XInterface.hpp"
51 #include "osl/conditn.hxx"
52 #include "osl/diagnose.h"
53 #include "osl/mutex.hxx"
54 #include "rtl/ref.hxx"
55 #include "rtl/ustring.h"
56 #include "rtl/ustring.hxx"
57 #include "sal/types.h"
58 #include "vcl/svapp.hxx"
59 #include "vcl/window.hxx"
60 #include "vos/mutex.hxx"
62 #include "dp_misc.h"
63 #include "dp_gui_thread.hxx"
64 #include "dp_gui_updatability.hxx"
66 namespace com { namespace sun { namespace star { namespace uno {
67 class XComponentContext;
68 } } } }
70 using dp_gui::Updatability;
72 namespace {
74 namespace css = com::sun::star;
78 class Updatability::Thread: public dp_gui::Thread {
79 public:
80 Thread(
81 css::uno::Sequence< css::uno::Reference<
82 css::deployment::XPackageManager > > const & packageManagers,
83 Window & enabled);
85 void start();
87 void stop();
89 private:
90 Thread(Thread &); // not defined
91 void operator =(Thread &); // not defined
93 virtual ~Thread();
95 virtual void execute();
97 enum Input { NONE, START, STOP };
99 bool m_predeterminedUpdateUrl;
100 css::uno::Sequence< css::uno::Reference<
101 css::deployment::XPackageManager > > m_packageManagers;
103 osl::Condition m_wakeup;
104 osl::Mutex m_mutex;
105 Window * m_enabled;
106 Input m_input;
107 css::uno::Reference< css::task::XAbortChannel > m_abort;
110 Updatability::Thread::Thread(
111 css::uno::Sequence< css::uno::Reference<
112 css::deployment::XPackageManager > > const & packageManagers,
113 Window & enabled):
114 m_predeterminedUpdateUrl(dp_misc::getExtensionDefaultUpdateURL().getLength() > 0),
115 m_packageManagers(packageManagers),
116 m_enabled(&enabled),
117 m_input(NONE)
120 void Updatability::Thread::start() {
121 css::uno::Reference< css::task::XAbortChannel > abort;
123 osl::MutexGuard g(m_mutex);
124 m_input = START;
125 abort = m_abort;
126 m_abort.clear();
128 m_wakeup.set();
129 if (abort.is()) {
130 abort->sendAbort();
134 void Updatability::Thread::stop() {
135 css::uno::Reference< css::task::XAbortChannel > abort;
137 vos::OGuard g1(Application::GetSolarMutex());
138 osl::MutexGuard g2(m_mutex);
139 m_input = STOP;
140 m_enabled = NULL;
141 abort = m_abort;
142 m_abort.clear();
144 m_wakeup.set();
145 if (abort.is()) {
146 abort->sendAbort();
150 Updatability::Thread::~Thread() {}
152 void Updatability::Thread::execute() {
153 for (;;) {
155 if (m_wakeup.wait() != osl::Condition::result_ok) {
156 dp_misc::TRACE("dp_gui::Updatability::Thread::run: ignored \n");
157 dp_misc::TRACE("osl::Condition::wait failure\n");
159 m_wakeup.reset();
160 Input input;
162 osl::MutexGuard g(m_mutex);
163 input = m_input;
164 m_input = NONE;
166 if (input == NONE) {
167 continue;
169 start:
170 if (input == STOP) {
171 break;
173 bool enabled = false;
174 for (sal_Int32 i = 0; !enabled && i < m_packageManagers.getLength();
175 ++i)
177 css::uno::Reference< css::task::XAbortChannel > abort(
178 m_packageManagers[i]->createAbortChannel());
180 osl::MutexGuard g(m_mutex);
181 input = m_input;
182 m_input = NONE;
183 if (input == NONE) {
184 //In case input would be STOP then we would later break out of the loop
185 //before further calls to the XPackageManger are done. That is, the abort
186 //channel would not be used anyway.
187 m_abort = abort;
189 if (input != NONE) {
190 goto start;
193 css::uno::Sequence<
194 css::uno::Reference< css::deployment::XPackage > > ps;
195 try {
196 ps = m_packageManagers[i]->getDeployedPackages(
197 abort,
198 css::uno::Reference< css::ucb::XCommandEnvironment >());
199 } catch (css::deployment::DeploymentException &) {
200 // If there are any problematic package managers, enable the
201 // update button and let the update process report any problems
202 // to the user:
203 enabled = true;
204 continue;
205 } catch (css::ucb::CommandFailedException &) {
206 throw css::uno::RuntimeException(
207 rtl::OUString(
208 RTL_CONSTASCII_USTRINGPARAM(
209 "CommandFailedException: cannot happen")),
210 css::uno::Reference< css::uno::XInterface >());
211 } catch (css::ucb::CommandAbortedException &) {
212 osl::MutexGuard g(m_mutex);
213 input = m_input;
214 m_input = NONE;
215 OSL_ASSERT(input != NONE);
216 goto start;
217 } catch (css::lang::IllegalArgumentException &) {
218 throw css::uno::RuntimeException(
219 rtl::OUString(
220 RTL_CONSTASCII_USTRINGPARAM(
221 "IllegalArgumentException: cannot happen")),
222 css::uno::Reference< css::uno::XInterface >());
224 if (m_predeterminedUpdateUrl && ps.getLength() != 0) {
225 enabled = true;
226 } else {
227 for (sal_Int32 j = 0; j < ps.getLength(); ++j) {
228 if (ps[j]->getUpdateInformationURLs().getLength() != 0) {
229 enabled = true;
230 break;
233 osl::MutexGuard g(m_mutex);
234 input = m_input;
235 m_input = NONE;
237 if (input != NONE) {
238 goto start;
243 vos::OGuard g1(Application::GetSolarMutex());
244 Window * e;
246 osl::MutexGuard g2(m_mutex);
247 e = m_enabled;
249 if (e != NULL) {
250 e->Enable(enabled);
255 Updatability::Updatability(
256 css::uno::Sequence<
257 css::uno::Reference< css::deployment::XPackageManager > > const &
258 packageManagers,
259 Window & enabled):
260 m_thread(new Thread(packageManagers, enabled))
262 m_thread->launch();
265 Updatability::~Updatability() {
269 void Updatability::start() {
270 m_thread->start();
273 void Updatability::stop() {
274 m_thread->stop();
275 // Bad hack; m_thread calls Application::GetSolarMutex, which only works
276 // as long as DeInitVCL has not been called:
277 ULONG n = Application::ReleaseSolarMutex();
278 m_thread->join();
279 Application::AcquireSolarMutex(n);