Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "../../deployment/gui/dp_gui.hrc"
22 : #include "../../deployment/gui/dp_gui_shared.hxx"
23 : #include "unopkg_shared.h"
24 : #include "osl/thread.h"
25 : #include "tools/string.hxx"
26 : #include "tools/resmgr.hxx"
27 : #include "cppuhelper/implbase3.hxx"
28 : #include "cppuhelper/exc_hlp.hxx"
29 : #include "comphelper/anytostring.hxx"
30 : #include "unotools/configmgr.hxx"
31 : #include "com/sun/star/lang/WrappedTargetException.hpp"
32 : #include "com/sun/star/task/XInteractionAbort.hpp"
33 : #include "com/sun/star/task/XInteractionApprove.hpp"
34 : #include "com/sun/star/deployment/InstallException.hpp"
35 : #include "com/sun/star/container/ElementExistException.hpp"
36 : #include "com/sun/star/deployment/LicenseException.hpp"
37 : #include "com/sun/star/deployment/VersionException.hpp"
38 : #include "com/sun/star/deployment/PlatformException.hpp"
39 : #include "com/sun/star/i18n/Collator.hpp"
40 : #include "com/sun/star/i18n/CollatorOptions.hpp"
41 :
42 : #include <stdio.h>
43 : #include "deployment.hrc"
44 : #include "dp_version.hxx"
45 :
46 : using namespace ::com::sun::star;
47 : using namespace ::com::sun::star::ucb;
48 : using namespace ::com::sun::star::uno;
49 : using namespace ::unopkg;
50 : using ::rtl::OUString;
51 :
52 :
53 : namespace {
54 :
55 : //==============================================================================
56 : class CommandEnvironmentImpl
57 : : public ::cppu::WeakImplHelper3< XCommandEnvironment,
58 : task::XInteractionHandler,
59 : XProgressHandler >
60 : {
61 : sal_Int32 m_logLevel;
62 : bool m_option_force_overwrite;
63 : bool m_option_verbose;
64 : bool m_option_suppress_license;
65 : Reference< XComponentContext > m_xComponentContext;
66 : Reference< XProgressHandler > m_xLogFile;
67 :
68 : void update_( Any const & Status ) throw (RuntimeException);
69 : void printLicense(const OUString & sName,const OUString& sLicense,
70 : bool & accept, bool & decline);
71 :
72 : public:
73 : virtual ~CommandEnvironmentImpl();
74 : CommandEnvironmentImpl(
75 : Reference<XComponentContext> const & xComponentContext,
76 : OUString const & log_file,
77 : bool option_force_overwrite,
78 : bool option_verbose,
79 : bool option_suppress_license);
80 :
81 : // XCommandEnvironment
82 : virtual Reference< task::XInteractionHandler > SAL_CALL
83 : getInteractionHandler() throw (RuntimeException);
84 : virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
85 : throw (RuntimeException);
86 :
87 : // XInteractionHandler
88 : virtual void SAL_CALL handle(
89 : Reference< task::XInteractionRequest > const & xRequest )
90 : throw (RuntimeException);
91 :
92 : // XProgressHandler
93 : virtual void SAL_CALL push( Any const & Status ) throw (RuntimeException);
94 : virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException);
95 : virtual void SAL_CALL pop() throw (RuntimeException);
96 : };
97 :
98 :
99 : //______________________________________________________________________________
100 0 : CommandEnvironmentImpl::CommandEnvironmentImpl(
101 : Reference<XComponentContext> const & xComponentContext,
102 : OUString const & log_file,
103 : bool option_force_overwrite,
104 : bool option_verbose,
105 : bool option_suppressLicense)
106 : : m_logLevel(0),
107 : m_option_force_overwrite( option_force_overwrite ),
108 : m_option_verbose( option_verbose ),
109 : m_option_suppress_license( option_suppressLicense ),
110 0 : m_xComponentContext(xComponentContext)
111 : {
112 0 : if (!log_file.isEmpty()) {
113 0 : const Any logfile(log_file);
114 : m_xLogFile.set(
115 0 : xComponentContext->getServiceManager()
116 0 : ->createInstanceWithArgumentsAndContext(
117 : OUSTR("com.sun.star.comp.deployment.ProgressLog"),
118 0 : Sequence<Any>( &logfile, 1 ), xComponentContext ),
119 0 : UNO_QUERY_THROW );
120 : }
121 0 : }
122 :
123 : //______________________________________________________________________________
124 0 : CommandEnvironmentImpl::~CommandEnvironmentImpl()
125 : {
126 : try {
127 0 : Reference< lang::XComponent > xComp( m_xLogFile, UNO_QUERY );
128 0 : if (xComp.is())
129 0 : xComp->dispose();
130 : }
131 0 : catch (const RuntimeException & exc) {
132 : (void) exc;
133 : OSL_FAIL( ::rtl::OUStringToOString(
134 : exc.Message, osl_getThreadTextEncoding() ).getStr() );
135 : }
136 0 : }
137 :
138 : //May throw exceptions
139 0 : void CommandEnvironmentImpl::printLicense(
140 : const OUString & sName, const OUString& sLicense, bool & accept, bool &decline)
141 : {
142 0 : ResMgr * pResMgr = DeploymentResMgr::get();
143 0 : String s1tmp(ResId(RID_STR_UNOPKG_ACCEPT_LIC_1, *pResMgr));
144 0 : s1tmp.SearchAndReplaceAllAscii( "$NAME", sName );
145 0 : OUString s1(s1tmp);
146 0 : OUString s2 = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_2, *pResMgr));
147 0 : OUString s3 = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_3, *pResMgr));
148 0 : OUString s4 = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_4, *pResMgr));
149 0 : OUString sYES = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_YES, *pResMgr));
150 0 : OUString sY = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_Y, *pResMgr));
151 0 : OUString sNO = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_NO, *pResMgr));
152 0 : OUString sN = String(ResId(RID_STR_UNOPKG_ACCEPT_LIC_N, *pResMgr));
153 :
154 0 : OUString sNewLine(RTL_CONSTASCII_USTRINGPARAM("\n"));
155 :
156 0 : dp_misc::writeConsole(sNewLine + sNewLine + s1 + sNewLine + sNewLine);
157 0 : dp_misc::writeConsole(sLicense + sNewLine + sNewLine);
158 0 : dp_misc::writeConsole(s2 + sNewLine);
159 0 : dp_misc::writeConsole(s3);
160 :
161 : //the user may enter "yes" or "no", we compare in a case insensitive way
162 : Reference< css::i18n::XCollator > xCollator =
163 0 : css::i18n::Collator::create( m_xComponentContext );
164 0 : xCollator->loadDefaultCollator(
165 : toLocale(utl::ConfigManager::getLocale()),
166 0 : css::i18n::CollatorOptions::CollatorOptions_IGNORE_CASE);
167 :
168 0 : do
169 : {
170 0 : OUString sAnswer = dp_misc::readConsole();
171 0 : if (xCollator->compareString(sAnswer, sYES) == 0
172 0 : || xCollator->compareString(sAnswer, sY) == 0)
173 : {
174 0 : accept = true;
175 : break;
176 : }
177 0 : else if(xCollator->compareString(sAnswer, sNO) == 0
178 0 : || xCollator->compareString(sAnswer, sN) == 0)
179 : {
180 0 : decline = true;
181 : break;
182 : }
183 : else
184 : {
185 0 : dp_misc::writeConsole(sNewLine + sNewLine + s4 + sNewLine);
186 0 : }
187 : }
188 0 : while(true);
189 0 : }
190 :
191 : // XCommandEnvironment
192 : //______________________________________________________________________________
193 : Reference< task::XInteractionHandler >
194 0 : CommandEnvironmentImpl::getInteractionHandler() throw (RuntimeException)
195 : {
196 0 : return this;
197 : }
198 :
199 : //______________________________________________________________________________
200 0 : Reference< XProgressHandler > CommandEnvironmentImpl::getProgressHandler()
201 : throw (RuntimeException)
202 : {
203 0 : return this;
204 : }
205 :
206 : // XInteractionHandler
207 : //______________________________________________________________________________
208 0 : void CommandEnvironmentImpl::handle(
209 : Reference<task::XInteractionRequest> const & xRequest )
210 : throw (RuntimeException)
211 : {
212 0 : Any request( xRequest->getRequest() );
213 : OSL_ASSERT( request.getValueTypeClass() == TypeClass_EXCEPTION );
214 : dp_misc::TRACE(OUSTR("[unopkg_cmdenv.cxx] incoming request:\n")
215 0 : + ::comphelper::anyToString(request) + OUSTR("\n\n"));
216 :
217 : // selections:
218 0 : bool approve = false;
219 0 : bool abort = false;
220 :
221 0 : lang::WrappedTargetException wtExc;
222 0 : deployment::LicenseException licExc;
223 0 : deployment::InstallException instExc;
224 0 : deployment::PlatformException platExc;
225 0 : deployment::VersionException verExc;
226 :
227 :
228 0 : bool bLicenseException = false;
229 0 : if (request >>= wtExc) {
230 : // ignore intermediate errors of legacy packages, i.e.
231 : // former pkgchk behaviour:
232 : const Reference<deployment::XPackage> xPackage(
233 0 : wtExc.Context, UNO_QUERY );
234 : OSL_ASSERT( xPackage.is() );
235 0 : if (xPackage.is()) {
236 : const Reference<deployment::XPackageTypeInfo> xPackageType(
237 0 : xPackage->getPackageType() );
238 : OSL_ASSERT( xPackageType.is() );
239 0 : if (xPackageType.is()) {
240 0 : approve = (xPackage->isBundle() &&
241 0 : xPackageType->getMediaType().matchAsciiL(
242 : RTL_CONSTASCII_STRINGPARAM(
243 : "application/"
244 0 : "vnd.sun.star.legacy-package-bundle") ));
245 0 : }
246 : }
247 0 : abort = !approve;
248 0 : if (abort) {
249 : // notify cause as error:
250 0 : request = wtExc.TargetException;
251 : }
252 : else {
253 : // handable deployment error signalled, e.g.
254 : // bundle item registration failed, notify as warning:
255 0 : update_( wtExc.TargetException );
256 0 : }
257 : }
258 0 : else if (request >>= licExc)
259 : {
260 0 : if ( !m_option_suppress_license )
261 0 : printLicense(licExc.ExtensionName, licExc.Text, approve, abort);
262 : else
263 : {
264 0 : approve = true;
265 0 : abort = false;
266 : }
267 : }
268 0 : else if (request >>= instExc)
269 : {
270 : //Only if the unopgk was started with gui + extension then we user is asked.
271 : //In console mode there is no asking.
272 0 : approve = true;
273 : }
274 0 : else if (request >>= platExc)
275 : {
276 0 : String sMsg(ResId(RID_STR_UNSUPPORTED_PLATFORM, *dp_gui::DeploymentGuiResMgr::get()));
277 0 : sMsg.SearchAndReplaceAllAscii("%Name", platExc.package->getDisplayName());
278 0 : dp_misc::writeConsole(OUSTR("\n") + sMsg + OUSTR("\n\n"));
279 0 : approve = true;
280 : }
281 : else {
282 0 : deployment::VersionException nc_exc;
283 0 : if (request >>= nc_exc) {
284 : approve = m_option_force_overwrite ||
285 0 : (::dp_misc::compareVersions(nc_exc.NewVersion, nc_exc.Deployed->getVersion())
286 0 : == ::dp_misc::GREATER);
287 0 : abort = !approve;
288 : }
289 : else
290 0 : return; // unknown request => no selection at all
291 : }
292 :
293 : //In case of a user declining a license abort is true but this is intended,
294 : //therefore no logging
295 0 : if (abort && m_option_verbose && !bLicenseException)
296 : {
297 0 : OUString msg = ::comphelper::anyToString(request);
298 : dp_misc::writeConsoleError(
299 0 : OUSTR("\nERROR: ") + msg + OUSTR("\n"));
300 : }
301 :
302 : // select:
303 : Sequence< Reference<task::XInteractionContinuation> > conts(
304 0 : xRequest->getContinuations() );
305 : Reference<task::XInteractionContinuation> const * pConts =
306 0 : conts.getConstArray();
307 0 : sal_Int32 len = conts.getLength();
308 0 : for ( sal_Int32 pos = 0; pos < len; ++pos )
309 : {
310 0 : if (approve) {
311 : Reference<task::XInteractionApprove> xInteractionApprove(
312 0 : pConts[ pos ], UNO_QUERY );
313 0 : if (xInteractionApprove.is()) {
314 0 : xInteractionApprove->select();
315 : break;
316 0 : }
317 : }
318 0 : else if (abort) {
319 : Reference<task::XInteractionAbort> xInteractionAbort(
320 0 : pConts[ pos ], UNO_QUERY );
321 0 : if (xInteractionAbort.is()) {
322 0 : xInteractionAbort->select();
323 : break;
324 0 : }
325 : }
326 0 : }
327 : }
328 :
329 : // XProgressHandler
330 : //______________________________________________________________________________
331 0 : void CommandEnvironmentImpl::push( Any const & Status )
332 : throw (RuntimeException)
333 : {
334 0 : update_( Status );
335 : OSL_ASSERT( m_logLevel >= 0 );
336 0 : ++m_logLevel;
337 0 : if (m_xLogFile.is())
338 0 : m_xLogFile->push( Status );
339 0 : }
340 :
341 : //______________________________________________________________________________
342 0 : void CommandEnvironmentImpl::update_( Any const & Status )
343 : throw (RuntimeException)
344 : {
345 0 : if (! Status.hasValue())
346 : return;
347 0 : bool bUseErr = false;
348 0 : OUString msg;
349 0 : if (Status >>= msg) {
350 0 : if (! m_option_verbose)
351 : return;
352 : }
353 : else {
354 0 : ::rtl::OUStringBuffer buf;
355 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("WARNING: ") );
356 0 : deployment::DeploymentException dp_exc;
357 0 : if (Status >>= dp_exc) {
358 0 : buf.append( dp_exc.Message );
359 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", Cause: ") );
360 0 : buf.append( ::comphelper::anyToString(dp_exc.Cause) );
361 : }
362 : else {
363 0 : buf.append( ::comphelper::anyToString(Status) );
364 : }
365 0 : msg = buf.makeStringAndClear();
366 0 : bUseErr = true;
367 : }
368 : OSL_ASSERT( m_logLevel >= 0 );
369 0 : for ( sal_Int32 n = 0; n < m_logLevel; ++n )
370 : {
371 0 : if (bUseErr)
372 0 : dp_misc::writeConsoleError(" ");
373 : else
374 0 : dp_misc::writeConsole(" ");
375 : }
376 :
377 0 : if (bUseErr)
378 0 : dp_misc::writeConsoleError(msg + OUSTR("\n"));
379 : else
380 0 : dp_misc::writeConsole(msg + OUSTR("\n"));
381 : }
382 :
383 : //______________________________________________________________________________
384 0 : void CommandEnvironmentImpl::update( Any const & Status )
385 : throw (RuntimeException)
386 : {
387 0 : update_( Status );
388 0 : if (m_xLogFile.is())
389 0 : m_xLogFile->update( Status );
390 0 : }
391 :
392 : //______________________________________________________________________________
393 0 : void CommandEnvironmentImpl::pop() throw (RuntimeException)
394 : {
395 : OSL_ASSERT( m_logLevel > 0 );
396 0 : --m_logLevel;
397 0 : if (m_xLogFile.is())
398 0 : m_xLogFile->pop();
399 0 : }
400 :
401 :
402 : } // anon namespace
403 :
404 : namespace unopkg {
405 :
406 : //==============================================================================
407 0 : Reference< XCommandEnvironment > createCmdEnv(
408 : Reference< XComponentContext > const & xContext,
409 : OUString const & logFile,
410 : bool option_force_overwrite,
411 : bool option_verbose,
412 : bool option_suppress_license)
413 : {
414 : return new CommandEnvironmentImpl(
415 0 : xContext, logFile, option_force_overwrite, option_verbose, option_suppress_license);
416 : }
417 : } // unopkg
418 :
419 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|