LCOV - code coverage report
Current view: top level - shell/source/cmdmail - cmdmailsuppl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 12 113 10.6 %
Date: 2015-06-13 12:38:46 Functions: 4 9 44.4 %
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             : #include <config_folders.h>
      21             : 
      22             : #include <osl/diagnose.h>
      23             : #include <osl/thread.h>
      24             : 
      25             : #include <rtl/bootstrap.hxx>
      26             : 
      27             : #include <osl/file.hxx>
      28             : #include <rtl/strbuf.hxx>
      29             : #include "cmdmailsuppl.hxx"
      30             : #include "cmdmailmsg.hxx"
      31             : #include <com/sun/star/system/SimpleMailClientFlags.hpp>
      32             : #include <com/sun/star/container/XNameAccess.hpp>
      33             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      34             : #include <com/sun/star/beans/PropertyValue.hpp>
      35             : #include <com/sun/star/beans/XPropertySet.hpp>
      36             : #include <com/sun/star/uno/XComponentContext.hpp>
      37             : #include <cppuhelper/supportsservice.hxx>
      38             : 
      39             : #include <string.h>
      40             : #include <errno.h>
      41             : #include <unistd.h>
      42             : 
      43             : 
      44             : // namespace directives
      45             : 
      46             : 
      47             : using com::sun::star::beans::PropertyValue;
      48             : using com::sun::star::system::XSimpleMailClientSupplier;
      49             : using com::sun::star::system::XSimpleMailClient;
      50             : using com::sun::star::system::XSimpleMailMessage;
      51             : using com::sun::star::system::XSimpleMailMessage2;
      52             : using com::sun::star::container::XNameAccess;
      53             : using com::sun::star::container::NoSuchElementException;
      54             : using osl::MutexGuard;
      55             : using osl::FileBase;
      56             : 
      57             : using namespace cppu;
      58             : using namespace com::sun::star::system::SimpleMailClientFlags;
      59             : using namespace com::sun::star::uno;
      60             : using namespace com::sun::star::lang;
      61             : using namespace com::sun::star::configuration;
      62             : 
      63             : namespace // private
      64             : {
      65           1 :     Sequence< OUString > SAL_CALL Component_getSupportedServiceNames()
      66             :     {
      67           1 :         Sequence< OUString > aRet(1);
      68           1 :         aRet[0] = "com.sun.star.system.SimpleCommandMail";
      69           1 :         return aRet;
      70             :     }
      71             : 
      72             : } // end private namespace
      73             : 
      74             : 
      75             : 
      76           1 : CmdMailSuppl::CmdMailSuppl( const Reference< XComponentContext >& xContext ) :
      77           1 :     WeakImplHelper3< XSimpleMailClientSupplier, XSimpleMailClient, XServiceInfo >()
      78             : {
      79           1 :     m_xConfigurationProvider = theDefaultProvider::get(xContext);
      80           1 : }
      81             : 
      82             : 
      83             : // XSimpleMailClientSupplier
      84             : 
      85             : 
      86           0 : Reference< XSimpleMailClient > SAL_CALL CmdMailSuppl::querySimpleMailClient(  )
      87             :     throw (RuntimeException, std::exception)
      88             : {
      89           0 :     return static_cast < XSimpleMailClient * > (this);
      90             : }
      91             : 
      92             : 
      93             : // XSimpleMailClient
      94             : 
      95             : 
      96           0 : Reference< XSimpleMailMessage > SAL_CALL CmdMailSuppl::createSimpleMailMessage(  )
      97             :         throw (::com::sun::star::uno::RuntimeException, std::exception)
      98             : {
      99           0 :     return Reference< XSimpleMailMessage >( new CmdMailMsg(  ) );
     100             : }
     101             : 
     102             : 
     103             : // XSimpleMailClient
     104             : 
     105             : 
     106             : namespace {
     107             : 
     108           0 : void appendShellWord(OStringBuffer & buffer, OUString const & word, bool strict)
     109             : {
     110           0 :     OString sys;
     111           0 :     if (!word.convertToString(
     112           0 :             &sys, osl_getThreadTextEncoding(),
     113             :             (strict
     114             :              ? (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
     115             :                 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)
     116           0 :              : OUSTRING_TO_OSTRING_CVTFLAGS)))
     117             :     {
     118             :         throw css::uno::Exception(
     119           0 :             ("Could not convert \"" + word + "\" to encoding #"
     120           0 :              + OUString::number(osl_getThreadTextEncoding())),
     121           0 :             css::uno::Reference<css::uno::XInterface>());
     122             :     }
     123           0 :     buffer.append('\'');
     124           0 :     for (sal_Int32 i = 0; i != sys.getLength(); ++i) {
     125           0 :         char c = sys[i];
     126           0 :         switch (c) {
     127             :         case 0:
     128           0 :             if (strict) {
     129             :                 throw css::uno::Exception(
     130           0 :                     "Could not convert word containing NUL, \"" + word + "\"",
     131           0 :                     css::uno::Reference<css::uno::XInterface>());
     132             :             }
     133           0 :             break;
     134             :         case '\'':
     135           0 :             buffer.append("'\\''");
     136           0 :             break;
     137             :         default:
     138           0 :             buffer.append(c);
     139           0 :             break;
     140             :         }
     141             :     }
     142           0 :     buffer.append('\'');
     143           0 : }
     144             : 
     145             : }
     146             : 
     147           0 : void SAL_CALL CmdMailSuppl::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 /*aFlag*/ )
     148             :     throw (IllegalArgumentException, Exception, RuntimeException, std::exception)
     149             : {
     150           0 :     if ( ! xSimpleMailMessage.is() )
     151             :     {
     152             :         throw ::com::sun::star::lang::IllegalArgumentException( "No message specified" ,
     153           0 :             static_cast < XSimpleMailClient * > (this), 1 );
     154             :     }
     155             : 
     156           0 :     if( ! m_xConfigurationProvider.is() )
     157             :     {
     158             :         throw ::com::sun::star::uno::Exception( "Can not access configuration" ,
     159           0 :             static_cast < XSimpleMailClient * > (this) );
     160             :     }
     161             : 
     162             : 
     163           0 :     OUString aProgramURL("$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER "/senddoc");
     164           0 :     rtl::Bootstrap::expandMacros(aProgramURL);
     165             : 
     166           0 :     OUString aProgram;
     167           0 :     if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram))
     168             :     {
     169             :         throw ::com::sun::star::uno::Exception("Cound not convert executable path",
     170           0 :             static_cast < XSimpleMailClient * > (this));
     171             :     }
     172             : 
     173           0 :     OStringBuffer aBuffer;
     174           0 :     appendShellWord(aBuffer, aProgram, true);
     175             : 
     176             :     try
     177             :     {
     178             :         // Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
     179             :         // configuration node to retriece the users preferred email application. This may
     180             :         // transparently by redirected to e.g. the corresponding GConf setting in GNOME.
     181           0 :         OUString aConfigRoot = "org.openoffice.Office.Common/ExternalMailer";
     182             : 
     183           0 :         PropertyValue aProperty;
     184           0 :         aProperty.Name = "nodepath";
     185           0 :         aProperty.Value = makeAny( aConfigRoot );
     186             : 
     187           0 :         Sequence< Any > aArgumentList( 1 );
     188           0 :         aArgumentList[0] = makeAny( aProperty );
     189             : 
     190             :         Reference< XNameAccess > xNameAccess =
     191             :             Reference< XNameAccess > (
     192           0 :                 m_xConfigurationProvider->createInstanceWithArguments(
     193             :                     OUString("com.sun.star.configuration.ConfigurationAccess"),
     194           0 :                     aArgumentList ),
     195           0 :                 UNO_QUERY );
     196             : 
     197           0 :         if( xNameAccess.is() )
     198             :         {
     199           0 :             OUString aMailer;
     200             : 
     201             :             // Retrieve the value for "Program" node and append it feed senddoc with it
     202             :             // using the (undocumented) --mailclient switch
     203           0 :             xNameAccess->getByName("Program") >>= aMailer;
     204             : 
     205           0 :             if( !aMailer.isEmpty() )
     206             :             {
     207             :                 // make sure we have a system path
     208           0 :                 FileBase::getSystemPathFromFileURL( aMailer, aMailer );
     209             : 
     210           0 :                 aBuffer.append(" --mailclient ");
     211           0 :                 appendShellWord(aBuffer, aMailer, true);
     212           0 :             }
     213             : #ifdef MACOSX
     214             :             else
     215             :                 aBuffer.append(" --mailclient Mail");
     216             : #endif
     217           0 :         }
     218             : 
     219             :     }
     220             : 
     221           0 :     catch(const RuntimeException &e )
     222             :     {
     223           0 :         m_xConfigurationProvider.clear();
     224             :         OSL_TRACE( "RuntimeException caught accessing configuration provider." );
     225             :         OSL_TRACE( "%s", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
     226           0 :         throw;
     227             :     }
     228             : 
     229           0 :     Reference< XSimpleMailMessage2 > xMessage( xSimpleMailMessage, UNO_QUERY );
     230           0 :     if ( xMessage.is() )
     231             :     {
     232           0 :         rtl::OUString sBody = xMessage->getBody();
     233           0 :         if ( sBody.getLength() > 0 )
     234             :         {
     235           0 :             aBuffer.append(" --body ");
     236           0 :             appendShellWord(aBuffer, sBody, false);
     237           0 :         }
     238             :     }
     239             : 
     240             :     // Convert from, to, etc. in a best-effort rather than a strict way to the
     241             :     // system encoding, based on the assumption that the relevant address parts
     242             :     // of those strings are ASCII anyway and any problematic characters are only
     243             :     // in the human-readable, informational-only parts:
     244             : 
     245             :     // Append originator if set in the message
     246           0 :     if ( !xSimpleMailMessage->getOriginator().isEmpty() )
     247             :     {
     248           0 :         aBuffer.append(" --from ");
     249           0 :         appendShellWord(aBuffer, xSimpleMailMessage->getOriginator(), false);
     250             :     }
     251             : 
     252             :     // Append receipient if set in the message
     253           0 :     if ( !xSimpleMailMessage->getRecipient().isEmpty() )
     254             :     {
     255           0 :         aBuffer.append(" --to ");
     256           0 :         appendShellWord(aBuffer, xSimpleMailMessage->getRecipient(), false);
     257             :     }
     258             : 
     259             :     // Append carbon copy receipients set in the message
     260           0 :     Sequence< OUString > aStringList = xSimpleMailMessage->getCcRecipient();
     261           0 :     sal_Int32 n, nmax = aStringList.getLength();
     262           0 :     for ( n = 0; n < nmax; n++ )
     263             :     {
     264           0 :         aBuffer.append(" --cc ");
     265           0 :         appendShellWord(aBuffer, aStringList[n], false);
     266             :     }
     267             : 
     268             :     // Append blind carbon copy receipients set in the message
     269           0 :     aStringList = xSimpleMailMessage->getBccRecipient();
     270           0 :     nmax = aStringList.getLength();
     271           0 :     for ( n = 0; n < nmax; n++ )
     272             :     {
     273           0 :         aBuffer.append(" --bcc ");
     274           0 :         appendShellWord(aBuffer, aStringList[n], false);
     275             :     }
     276             : 
     277             :     // Append subject if set in the message
     278           0 :     if ( !xSimpleMailMessage->getSubject().isEmpty() )
     279             :     {
     280           0 :         aBuffer.append(" --subject ");
     281           0 :         appendShellWord(aBuffer, xSimpleMailMessage->getSubject(), false);
     282             :     }
     283             : 
     284             :     // Append attachments set in the message
     285           0 :     aStringList = xSimpleMailMessage->getAttachement();
     286           0 :     nmax = aStringList.getLength();
     287           0 :     for ( n = 0; n < nmax; n++ )
     288             :     {
     289           0 :         OUString aSystemPath;
     290           0 :         if ( FileBase::E_None == FileBase::getSystemPathFromFileURL(aStringList[n], aSystemPath) )
     291             :         {
     292           0 :             aBuffer.append(" --attach ");
     293           0 :             appendShellWord(aBuffer, aSystemPath, true);
     294             :         }
     295           0 :     }
     296             : 
     297           0 :     OString cmd = aBuffer.makeStringAndClear();
     298           0 :     FILE * f = popen(cmd.getStr(), "w");
     299           0 :     if (f == 0 || pclose(f) != 0)
     300             :     {
     301             :         throw ::com::sun::star::uno::Exception("No mail client configured",
     302           0 :             static_cast < XSimpleMailClient * > (this) );
     303           0 :     }
     304           0 : }
     305             : 
     306             : // XServiceInfo
     307           1 : OUString SAL_CALL CmdMailSuppl::getImplementationName(  )
     308             :     throw( RuntimeException, std::exception )
     309             : {
     310           1 :     return OUString("com.sun.star.comp.system.SimpleCommandMail");
     311             : }
     312             : 
     313             : //  XServiceInfo
     314           0 : sal_Bool SAL_CALL CmdMailSuppl::supportsService( const OUString& ServiceName )
     315             :     throw( RuntimeException, std::exception )
     316             : {
     317           0 :     return cppu::supportsService(this, ServiceName);
     318             : }
     319             : 
     320             : //  XServiceInfo
     321           1 : Sequence< OUString > SAL_CALL CmdMailSuppl::getSupportedServiceNames(    )
     322             :     throw( RuntimeException, std::exception )
     323             : {
     324           1 :     return Component_getSupportedServiceNames();
     325             : }
     326             : 
     327             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11