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

Generated by: LCOV version 1.10