nss: upgrade to release 3.73
[LibreOffice.git] / desktop / source / pkgchk / unopkg / unopkg_cmdenv.cxx
blob98a957c014e8d7677d4556aac2612310ae7cef9e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <strings.hrc>
22 #include <dp_misc.h>
23 #include <dp_shared.hxx>
24 #include "unopkg_shared.h"
25 #include <i18nlangtag/languagetag.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <comphelper/anytostring.hxx>
29 #include <tools/diagnose_ex.h>
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/DeploymentException.hpp>
35 #include <com/sun/star/deployment/InstallException.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>
42 #include <dp_version.hxx>
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::ucb;
46 using namespace ::com::sun::star::uno;
47 using namespace ::unopkg;
50 namespace {
53 class CommandEnvironmentImpl
54 : public ::cppu::WeakImplHelper< XCommandEnvironment,
55 task::XInteractionHandler,
56 XProgressHandler >
58 sal_Int32 m_logLevel;
59 bool m_option_force_overwrite;
60 bool m_option_verbose;
61 bool m_option_suppress_license;
62 Reference< XComponentContext > m_xComponentContext;
63 Reference< XProgressHandler > m_xLogFile;
65 /// @throws RuntimeException
66 void update_( Any const & Status );
67 void printLicense(std::u16string_view sName,const OUString& sLicense,
68 bool & accept, bool & decline);
70 public:
71 virtual ~CommandEnvironmentImpl() override;
72 CommandEnvironmentImpl(
73 Reference<XComponentContext> const & xComponentContext,
74 bool option_force_overwrite,
75 bool option_verbose,
76 bool option_suppress_license);
78 // XCommandEnvironment
79 virtual Reference< task::XInteractionHandler > SAL_CALL
80 getInteractionHandler() override;
81 virtual Reference< XProgressHandler > SAL_CALL getProgressHandler() override;
83 // XInteractionHandler
84 virtual void SAL_CALL handle(
85 Reference< task::XInteractionRequest > const & xRequest ) override;
87 // XProgressHandler
88 virtual void SAL_CALL push( Any const & Status ) override;
89 virtual void SAL_CALL update( Any const & Status ) override;
90 virtual void SAL_CALL pop() override;
94 CommandEnvironmentImpl::CommandEnvironmentImpl(
95 Reference<XComponentContext> const & xComponentContext,
96 bool option_force_overwrite,
97 bool option_verbose,
98 bool option_suppressLicense)
99 : m_logLevel(0),
100 m_option_force_overwrite( option_force_overwrite ),
101 m_option_verbose( option_verbose ),
102 m_option_suppress_license( option_suppressLicense ),
103 m_xComponentContext(xComponentContext)
105 m_xLogFile.set(
106 xComponentContext->getServiceManager()
107 ->createInstanceWithArgumentsAndContext(
108 "com.sun.star.comp.deployment.ProgressLog",
109 Sequence<Any>(), xComponentContext ),
110 UNO_QUERY_THROW );
114 CommandEnvironmentImpl::~CommandEnvironmentImpl()
116 try {
117 Reference< lang::XComponent > xComp( m_xLogFile, UNO_QUERY );
118 if (xComp.is())
119 xComp->dispose();
121 catch (const RuntimeException &) {
122 TOOLS_WARN_EXCEPTION( "desktop", "" );
126 //May throw exceptions
127 void CommandEnvironmentImpl::printLicense(
128 std::u16string_view sName, const OUString& sLicense, bool & accept, bool &decline)
130 OUString s1tmp(DpResId(RID_STR_UNOPKG_ACCEPT_LIC_1));
131 OUString s1(s1tmp.replaceAll("$NAME", sName));
132 OUString s2 = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_2);
133 OUString s3 = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_3);
134 OUString s4 = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_4);
135 OUString sYES = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_YES);
136 OUString sY = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_Y);
137 OUString sNO = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_NO);
138 OUString sN = DpResId(RID_STR_UNOPKG_ACCEPT_LIC_N);
140 OUString sNewLine("\n");
142 dp_misc::writeConsole(sNewLine + sNewLine + s1 + sNewLine + sNewLine);
143 dp_misc::writeConsole(sLicense + sNewLine + sNewLine);
144 dp_misc::writeConsole(s2 + sNewLine);
145 dp_misc::writeConsole(s3);
147 //the user may enter "yes" or "no", we compare in a case insensitive way
148 Reference< css::i18n::XCollator > xCollator =
149 css::i18n::Collator::create( m_xComponentContext );
150 xCollator->loadDefaultCollator(
151 LanguageTag(utl::ConfigManager::getUILocale()).getLocale(),
152 css::i18n::CollatorOptions::CollatorOptions_IGNORE_CASE);
156 OUString sAnswer = dp_misc::readConsole();
157 if (xCollator->compareString(sAnswer, sYES) == 0
158 || xCollator->compareString(sAnswer, sY) == 0)
160 accept = true;
161 break;
163 else if(xCollator->compareString(sAnswer, sNO) == 0
164 || xCollator->compareString(sAnswer, sN) == 0)
166 decline = true;
167 break;
169 else
171 dp_misc::writeConsole(sNewLine + sNewLine + s4 + sNewLine);
174 while(true);
177 // XCommandEnvironment
179 Reference< task::XInteractionHandler >
180 CommandEnvironmentImpl::getInteractionHandler()
182 return this;
186 Reference< XProgressHandler > CommandEnvironmentImpl::getProgressHandler()
188 return this;
191 // XInteractionHandler
193 void CommandEnvironmentImpl::handle(
194 Reference<task::XInteractionRequest> const & xRequest )
196 Any request( xRequest->getRequest() );
197 OSL_ASSERT( request.getValueTypeClass() == TypeClass_EXCEPTION );
198 dp_misc::TRACE("[unopkg_cmdenv.cxx] incoming request:\n"
199 + ::comphelper::anyToString(request) + "\n\n");
201 // selections:
202 bool approve = false;
203 bool abort = false;
205 lang::WrappedTargetException wtExc;
206 deployment::LicenseException licExc;
207 deployment::InstallException instExc;
208 deployment::PlatformException platExc;
210 if (request >>= wtExc) {
211 // ignore intermediate errors of legacy packages, i.e.
212 // former pkgchk behaviour:
213 const Reference<deployment::XPackage> xPackage(
214 wtExc.Context, UNO_QUERY );
215 OSL_ASSERT( xPackage.is() );
216 if (xPackage.is()) {
217 const Reference<deployment::XPackageTypeInfo> xPackageType(
218 xPackage->getPackageType() );
219 OSL_ASSERT( xPackageType.is() );
220 if (xPackageType.is()) {
221 approve = (xPackage->isBundle() &&
222 xPackageType->getMediaType().match(
223 "application/vnd.sun.star.legacy-package-bundle") );
226 abort = !approve;
227 if (abort) {
228 // notify cause as error:
229 request = wtExc.TargetException;
231 else {
232 // handable deployment error signalled, e.g.
233 // bundle item registration failed, notify as warning:
234 update_( wtExc.TargetException );
237 else if (request >>= licExc)
239 if ( !m_option_suppress_license )
240 printLicense(licExc.ExtensionName, licExc.Text, approve, abort);
241 else
243 approve = true;
244 abort = false;
247 else if (request >>= instExc)
249 //Only if the unopgk was started with gui + extension then the user is asked.
250 //In console mode there is no asking.
251 approve = true;
253 else if (request >>= platExc)
255 OUString sMsg(DpResId(RID_STR_UNSUPPORTED_PLATFORM));
256 sMsg = sMsg.replaceAll("%Name", platExc.package->getDisplayName());
257 dp_misc::writeConsole("\n" + sMsg + "\n\n");
258 approve = true;
260 else {
261 deployment::VersionException nc_exc;
262 if (request >>= nc_exc) {
263 approve = m_option_force_overwrite ||
264 (::dp_misc::compareVersions(nc_exc.NewVersion, nc_exc.Deployed->getVersion())
265 == ::dp_misc::GREATER);
266 abort = !approve;
268 else
269 return; // unknown request => no selection at all
272 if (abort && m_option_verbose)
274 OUString msg = ::comphelper::anyToString(request);
275 dp_misc::writeConsoleError("\nERROR: " + msg + "\n");
278 // select:
279 const css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> xIC = xRequest->getContinuations();
280 for ( auto const& rCont : xIC )
282 if (approve) {
283 Reference<task::XInteractionApprove> xInteractionApprove(
284 rCont, UNO_QUERY );
285 if (xInteractionApprove.is()) {
286 xInteractionApprove->select();
287 break;
290 else if (abort) {
291 Reference<task::XInteractionAbort> xInteractionAbort(
292 rCont, UNO_QUERY );
293 if (xInteractionAbort.is()) {
294 xInteractionAbort->select();
295 break;
301 // XProgressHandler
303 void CommandEnvironmentImpl::push( Any const & Status )
305 update_( Status );
306 OSL_ASSERT( m_logLevel >= 0 );
307 ++m_logLevel;
308 if (m_xLogFile.is())
309 m_xLogFile->push( Status );
313 void CommandEnvironmentImpl::update_( Any const & Status )
315 if (! Status.hasValue())
316 return;
317 bool bUseErr = false;
318 OUString msg;
319 if (Status >>= msg) {
320 if (! m_option_verbose)
321 return;
323 else {
324 OUStringBuffer buf;
325 buf.append( "WARNING: " );
326 deployment::DeploymentException dp_exc;
327 if (Status >>= dp_exc) {
328 buf.append( dp_exc.Message );
329 buf.append( ", Cause: " );
330 buf.append( ::comphelper::anyToString(dp_exc.Cause) );
332 else {
333 buf.append( ::comphelper::anyToString(Status) );
335 msg = buf.makeStringAndClear();
336 bUseErr = true;
338 OSL_ASSERT( m_logLevel >= 0 );
339 for ( sal_Int32 n = 0; n < m_logLevel; ++n )
341 if (bUseErr)
342 dp_misc::writeConsoleError(" ");
343 else
344 dp_misc::writeConsole(" ");
347 if (bUseErr)
348 dp_misc::writeConsoleError(msg + "\n");
349 else
350 dp_misc::writeConsole(msg + "\n");
354 void CommandEnvironmentImpl::update( Any const & Status )
356 update_( Status );
357 if (m_xLogFile.is())
358 m_xLogFile->update( Status );
362 void CommandEnvironmentImpl::pop()
364 OSL_ASSERT( m_logLevel > 0 );
365 --m_logLevel;
366 if (m_xLogFile.is())
367 m_xLogFile->pop();
371 } // anon namespace
373 namespace unopkg {
376 Reference< XCommandEnvironment > createCmdEnv(
377 Reference< XComponentContext > const & xContext,
378 bool option_force_overwrite,
379 bool option_verbose,
380 bool option_suppress_license)
382 return new CommandEnvironmentImpl(
383 xContext, option_force_overwrite, option_verbose, option_suppress_license);
385 } // unopkg
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */