LCOV - code coverage report
Current view: top level - desktop/source/pkgchk/unopkg - unopkg_cmdenv.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 159 0.0 %
Date: 2014-04-14 Functions: 0 12 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10