2010-10-12 15:53:47 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-10-02 14:13:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2001-06-26 07:49:57 +00:00
|
|
|
|
|
|
|
#include <osl/diagnose.h>
|
2004-06-17 14:54:22 +00:00
|
|
|
#include <osl/thread.h>
|
2008-04-15 13:51:25 +00:00
|
|
|
|
|
|
|
#include <rtl/bootstrap.hxx>
|
|
|
|
|
2001-06-27 05:30:38 +00:00
|
|
|
#include <osl/file.hxx>
|
2004-06-17 14:54:22 +00:00
|
|
|
#include <rtl/strbuf.hxx>
|
2001-06-26 07:49:57 +00:00
|
|
|
#include "cmdmailsuppl.hxx"
|
|
|
|
#include "cmdmailmsg.hxx"
|
|
|
|
#include <com/sun/star/system/SimpleMailClientFlags.hpp>
|
|
|
|
#include <com/sun/star/container/XNameAccess.hpp>
|
2012-10-30 14:02:56 +02:00
|
|
|
#include <com/sun/star/configuration/theDefaultProvider.hpp>
|
2001-06-26 07:49:57 +00:00
|
|
|
#include <com/sun/star/beans/PropertyValue.hpp>
|
2004-06-17 10:34:39 +00:00
|
|
|
#include <com/sun/star/beans/XPropertySet.hpp>
|
|
|
|
#include <com/sun/star/uno/XComponentContext.hpp>
|
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-06-26 07:49:57 +00:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// namespace directives
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
using com::sun::star::beans::PropertyValue;
|
|
|
|
using com::sun::star::system::XSimpleMailClientSupplier;
|
|
|
|
using com::sun::star::system::XSimpleMailClient;
|
|
|
|
using com::sun::star::system::XSimpleMailMessage;
|
|
|
|
using com::sun::star::container::XNameAccess;
|
|
|
|
using com::sun::star::container::NoSuchElementException;
|
|
|
|
using rtl::OUString;
|
|
|
|
using rtl::OUStringToOString;
|
2004-06-17 14:54:22 +00:00
|
|
|
using rtl::OString;
|
|
|
|
using rtl::OStringBuffer;
|
2001-06-26 07:49:57 +00:00
|
|
|
using osl::MutexGuard;
|
2001-06-27 05:30:38 +00:00
|
|
|
using osl::FileBase;
|
2001-06-26 07:49:57 +00:00
|
|
|
|
|
|
|
using namespace cppu;
|
|
|
|
using namespace com::sun::star::system::SimpleMailClientFlags;
|
|
|
|
using namespace com::sun::star::uno;
|
|
|
|
using namespace com::sun::star::lang;
|
2012-10-30 14:02:56 +02:00
|
|
|
using namespace com::sun::star::configuration;
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
#define COMP_IMPL_NAME "com.sun.star.comp.system.SimpleCommandMail2"
|
2001-06-26 07:49:57 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// helper functions
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace // private
|
|
|
|
{
|
|
|
|
Sequence< OUString > SAL_CALL Component_getSupportedServiceNames()
|
|
|
|
{
|
|
|
|
Sequence< OUString > aRet(1);
|
2010-11-19 19:34:59 +01:00
|
|
|
aRet[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.system.SimpleCommandMail"));
|
2001-06-26 07:49:57 +00:00
|
|
|
return aRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end private namespace
|
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
CmdMailSuppl::CmdMailSuppl( const Reference< XComponentContext >& xContext ) :
|
|
|
|
WeakImplHelper3< XSimpleMailClientSupplier, XSimpleMailClient, XServiceInfo >()
|
2001-06-26 07:49:57 +00:00
|
|
|
{
|
2012-10-30 14:02:56 +02:00
|
|
|
m_xConfigurationProvider = theDefaultProvider::get(xContext);
|
2001-06-26 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
// XSimpleMailClientSupplier
|
|
|
|
//-------------------------------------------------
|
|
|
|
|
|
|
|
Reference< XSimpleMailClient > SAL_CALL CmdMailSuppl::querySimpleMailClient( )
|
|
|
|
throw (RuntimeException)
|
|
|
|
{
|
|
|
|
return static_cast < XSimpleMailClient * > (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// XSimpleMailClient
|
|
|
|
//------------------------------------------------
|
|
|
|
|
|
|
|
Reference< XSimpleMailMessage > SAL_CALL CmdMailSuppl::createSimpleMailMessage( )
|
|
|
|
throw (::com::sun::star::uno::RuntimeException)
|
|
|
|
{
|
2004-10-22 07:14:46 +00:00
|
|
|
return Reference< XSimpleMailMessage >( new CmdMailMsg( ) );
|
2001-06-26 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// XSimpleMailClient
|
|
|
|
//------------------------------------------------
|
|
|
|
|
2006-06-19 13:18:58 +00:00
|
|
|
void SAL_CALL CmdMailSuppl::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 /*aFlag*/ )
|
2001-06-26 07:49:57 +00:00
|
|
|
throw (IllegalArgumentException, Exception, RuntimeException)
|
|
|
|
{
|
2004-06-17 14:54:22 +00:00
|
|
|
if ( ! xSimpleMailMessage.is() )
|
|
|
|
{
|
|
|
|
throw ::com::sun::star::lang::IllegalArgumentException(
|
|
|
|
OUString(RTL_CONSTASCII_USTRINGPARAM( "No message specified" )),
|
|
|
|
static_cast < XSimpleMailClient * > (this), 1 );
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
if( ! m_xConfigurationProvider.is() )
|
2001-06-26 07:49:57 +00:00
|
|
|
{
|
2004-06-17 14:54:22 +00:00
|
|
|
throw ::com::sun::star::uno::Exception(
|
|
|
|
OUString(RTL_CONSTASCII_USTRINGPARAM( "Can not access configuration" )),
|
|
|
|
static_cast < XSimpleMailClient * > (this) );
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
OStringBuffer aBuffer;
|
2006-01-20 12:29:23 +00:00
|
|
|
aBuffer.append("\"");
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2011-10-06 21:29:44 +02:00
|
|
|
OUString aProgramURL(RTL_CONSTASCII_USTRINGPARAM("$BRAND_BASE_DIR/program/senddoc"));
|
2008-04-15 13:51:25 +00:00
|
|
|
rtl::Bootstrap::expandMacros(aProgramURL);
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
OUString aProgram;
|
|
|
|
if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram))
|
|
|
|
{
|
|
|
|
throw ::com::sun::star::uno::Exception(
|
|
|
|
OUString(RTL_CONSTASCII_USTRINGPARAM("Cound not convert executable path")),
|
|
|
|
static_cast < XSimpleMailClient * > (this));
|
|
|
|
}
|
|
|
|
|
2008-04-15 13:51:25 +00:00
|
|
|
aBuffer.append(OUStringToOString(aProgram, osl_getThreadTextEncoding()));
|
|
|
|
aBuffer.append("\" ");
|
2004-06-17 14:54:22 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
|
|
|
|
// configuration node to retriece the users preferred email application. This may
|
|
|
|
// transparently by redirected to e.g. the corresponding GConf setting in GNOME.
|
|
|
|
OUString aConfigRoot = OUString(
|
|
|
|
RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Office.Common/ExternalMailer" ) );
|
|
|
|
|
|
|
|
PropertyValue aProperty;
|
2010-11-19 19:34:59 +01:00
|
|
|
aProperty.Name = OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath"));
|
2004-06-17 14:54:22 +00:00
|
|
|
aProperty.Value = makeAny( aConfigRoot );
|
|
|
|
|
|
|
|
Sequence< Any > aArgumentList( 1 );
|
|
|
|
aArgumentList[0] = makeAny( aProperty );
|
|
|
|
|
|
|
|
Reference< XNameAccess > xNameAccess =
|
|
|
|
Reference< XNameAccess > (
|
|
|
|
m_xConfigurationProvider->createInstanceWithArguments(
|
2010-11-19 19:34:59 +01:00
|
|
|
OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
|
2004-06-17 14:54:22 +00:00
|
|
|
aArgumentList ),
|
|
|
|
UNO_QUERY );
|
|
|
|
|
|
|
|
if( xNameAccess.is() )
|
2001-06-26 07:49:57 +00:00
|
|
|
{
|
2004-06-17 14:54:22 +00:00
|
|
|
OUString aMailer;
|
|
|
|
|
|
|
|
// Retrieve the value for "Program" node and append it feed senddoc with it
|
|
|
|
// using the (undocumented) --mailclient switch
|
2010-11-19 19:34:59 +01:00
|
|
|
xNameAccess->getByName( OUString(RTL_CONSTASCII_USTRINGPARAM("Program")) ) >>= aMailer;
|
2004-06-17 14:54:22 +00:00
|
|
|
|
2012-01-11 11:37:33 -02:00
|
|
|
if( !aMailer.isEmpty() )
|
2004-06-17 14:54:22 +00:00
|
|
|
{
|
|
|
|
// make sure we have a system path
|
|
|
|
FileBase::getSystemPathFromFileURL( aMailer, aMailer );
|
|
|
|
|
|
|
|
aBuffer.append("--mailclient ");
|
|
|
|
aBuffer.append(OUStringToOString( aMailer, osl_getThreadTextEncoding() ));
|
|
|
|
aBuffer.append(" ");
|
|
|
|
}
|
2006-01-20 12:29:23 +00:00
|
|
|
#ifdef MACOSX
|
|
|
|
else
|
|
|
|
aBuffer.append("--mailclient Mail ");
|
|
|
|
#endif
|
2001-06-26 07:49:57 +00:00
|
|
|
}
|
2004-06-17 14:54:22 +00:00
|
|
|
|
2001-06-26 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
2011-09-17 01:03:18 +02:00
|
|
|
catch(const RuntimeException &e )
|
2001-06-26 07:49:57 +00:00
|
|
|
{
|
2004-06-17 14:54:22 +00:00
|
|
|
m_xConfigurationProvider.clear();
|
|
|
|
OSL_TRACE( "RuntimeException caught accessing configuration provider." );
|
2011-03-12 12:45:06 +01:00
|
|
|
OSL_TRACE( "%s", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
|
2011-09-17 01:03:18 +02:00
|
|
|
throw;
|
2004-06-17 14:54:22 +00:00
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
// Append originator if set in the message
|
2012-01-11 11:37:33 -02:00
|
|
|
if ( !xSimpleMailMessage->getOriginator().isEmpty() )
|
2004-06-17 14:54:22 +00:00
|
|
|
{
|
|
|
|
aBuffer.append("--from \"");
|
|
|
|
aBuffer.append(OUStringToOString(xSimpleMailMessage->getOriginator(), osl_getThreadTextEncoding()));
|
|
|
|
aBuffer.append("\" ");
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
// Append receipient if set in the message
|
2012-01-11 11:37:33 -02:00
|
|
|
if ( !xSimpleMailMessage->getRecipient().isEmpty() )
|
2004-06-17 14:54:22 +00:00
|
|
|
{
|
|
|
|
aBuffer.append("--to \"");
|
|
|
|
aBuffer.append(OUStringToOString(xSimpleMailMessage->getRecipient(), osl_getThreadTextEncoding()));
|
|
|
|
aBuffer.append("\" ");
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
// Append carbon copy receipients set in the message
|
|
|
|
Sequence< OUString > aStringList = xSimpleMailMessage->getCcRecipient();
|
|
|
|
sal_Int32 n, nmax = aStringList.getLength();
|
|
|
|
for ( n = 0; n < nmax; n++ )
|
|
|
|
{
|
|
|
|
aBuffer.append("--cc \"");
|
|
|
|
aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
|
|
|
|
aBuffer.append("\" ");
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
// Append blind carbon copy receipients set in the message
|
|
|
|
aStringList = xSimpleMailMessage->getBccRecipient();
|
|
|
|
nmax = aStringList.getLength();
|
|
|
|
for ( n = 0; n < nmax; n++ )
|
|
|
|
{
|
|
|
|
aBuffer.append("--bcc \"");
|
|
|
|
aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
|
|
|
|
aBuffer.append("\" ");
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
// Append subject if set in the message
|
2012-01-11 11:37:33 -02:00
|
|
|
if ( !xSimpleMailMessage->getSubject().isEmpty() )
|
2004-06-17 14:54:22 +00:00
|
|
|
{
|
|
|
|
aBuffer.append("--subject \"");
|
|
|
|
aBuffer.append(OUStringToOString(xSimpleMailMessage->getSubject(), osl_getThreadTextEncoding()));
|
|
|
|
aBuffer.append("\" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append attachments set in the message
|
|
|
|
aStringList = xSimpleMailMessage->getAttachement();
|
|
|
|
nmax = aStringList.getLength();
|
|
|
|
for ( n = 0; n < nmax; n++ )
|
|
|
|
{
|
2004-10-22 07:14:46 +00:00
|
|
|
OUString aSystemPath;
|
|
|
|
if ( FileBase::E_None == FileBase::getSystemPathFromFileURL(aStringList[n], aSystemPath) )
|
|
|
|
{
|
|
|
|
aBuffer.append("--attach \"");
|
2006-01-20 12:29:23 +00:00
|
|
|
aBuffer.append(OUStringToOString(aSystemPath, osl_getThreadTextEncoding()));
|
2004-10-22 07:14:46 +00:00
|
|
|
aBuffer.append("\" ");
|
|
|
|
}
|
2004-06-17 14:54:22 +00:00
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
|
2004-06-17 14:54:22 +00:00
|
|
|
OString cmd = aBuffer.makeStringAndClear();
|
|
|
|
if ( 0 != pclose(popen(cmd.getStr(), "w")) )
|
|
|
|
{
|
|
|
|
throw ::com::sun::star::uno::Exception(
|
|
|
|
OUString(RTL_CONSTASCII_USTRINGPARAM( "No mail client configured" )),
|
|
|
|
static_cast < XSimpleMailClient * > (this) );
|
|
|
|
}
|
2001-06-26 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------
|
|
|
|
// XServiceInfo
|
|
|
|
// -------------------------------------------------
|
|
|
|
|
|
|
|
OUString SAL_CALL CmdMailSuppl::getImplementationName( )
|
|
|
|
throw( RuntimeException )
|
|
|
|
{
|
2010-11-19 19:34:59 +01:00
|
|
|
return OUString(RTL_CONSTASCII_USTRINGPARAM( COMP_IMPL_NAME ));
|
2001-06-26 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------
|
|
|
|
// XServiceInfo
|
|
|
|
// -------------------------------------------------
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL CmdMailSuppl::supportsService( const OUString& ServiceName )
|
|
|
|
throw( RuntimeException )
|
|
|
|
{
|
|
|
|
Sequence < OUString > SupportedServicesNames = Component_getSupportedServiceNames();
|
|
|
|
|
|
|
|
for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
|
|
|
|
if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
|
|
|
|
return sal_True;
|
|
|
|
|
|
|
|
return sal_False;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------
|
|
|
|
// XServiceInfo
|
|
|
|
// -------------------------------------------------
|
|
|
|
|
|
|
|
Sequence< OUString > SAL_CALL CmdMailSuppl::getSupportedServiceNames( )
|
|
|
|
throw( RuntimeException )
|
|
|
|
{
|
|
|
|
return Component_getSupportedServiceNames();
|
|
|
|
}
|
|
|
|
|
2010-10-12 15:53:47 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|