1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <config_folders.h>
22 #include <osl/thread.h>
24 #include <rtl/bootstrap.hxx>
26 #include <osl/file.hxx>
27 #include <rtl/strbuf.hxx>
28 #include <sal/log.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>
43 using com::sun::star::beans::PropertyValue
;
44 using com::sun::star::system::XSimpleMailClientSupplier
;
45 using com::sun::star::system::XSimpleMailClient
;
46 using com::sun::star::system::XSimpleMailMessage
;
47 using com::sun::star::system::XSimpleMailMessage2
;
48 using com::sun::star::container::XNameAccess
;
52 using namespace com::sun::star::system::SimpleMailClientFlags
;
53 using namespace com::sun::star::uno
;
54 using namespace com::sun::star::lang
;
55 using namespace com::sun::star::configuration
;
59 Sequence
< OUString
> Component_getSupportedServiceNames()
61 Sequence
< OUString
> aRet
{ "com.sun.star.system.SimpleCommandMail" };
67 CmdMailSuppl::CmdMailSuppl( const Reference
< XComponentContext
>& xContext
) :
68 WeakImplHelper
< XSimpleMailClientSupplier
, XSimpleMailClient
, XServiceInfo
>()
70 m_xConfigurationProvider
= theDefaultProvider::get(xContext
);
73 // XSimpleMailClientSupplier
75 Reference
< XSimpleMailClient
> SAL_CALL
CmdMailSuppl::querySimpleMailClient( )
77 return static_cast < XSimpleMailClient
* > (this);
82 Reference
< XSimpleMailMessage
> SAL_CALL
CmdMailSuppl::createSimpleMailMessage( )
84 return Reference
< XSimpleMailMessage
>( new CmdMailMsg( ) );
89 void appendShellWord(OStringBuffer
& buffer
, OUString
const & word
, bool strict
)
92 if (!word
.convertToString(
93 &sys
, osl_getThreadTextEncoding(),
95 ? (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
96 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
)
97 : OUSTRING_TO_OSTRING_CVTFLAGS
)))
99 throw css::uno::Exception(
100 ("Could not convert \"" + word
+ "\" to encoding #"
101 + OUString::number(osl_getThreadTextEncoding())),
102 css::uno::Reference
<css::uno::XInterface
>());
105 for (sal_Int32 i
= 0; i
!= sys
.getLength(); ++i
) {
110 throw css::uno::Exception(
111 "Could not convert word containing NUL, \"" + word
+ "\"",
112 css::uno::Reference
<css::uno::XInterface
>());
116 buffer
.append("'\\''");
128 void SAL_CALL
CmdMailSuppl::sendSimpleMailMessage( const Reference
< XSimpleMailMessage
>& xSimpleMailMessage
, sal_Int32
/*aFlag*/ )
130 if ( ! xSimpleMailMessage
.is() )
132 throw css::lang::IllegalArgumentException( "No message specified" ,
133 static_cast < XSimpleMailClient
* > (this), 1 );
136 if( ! m_xConfigurationProvider
.is() )
138 throw css::uno::Exception( "Can not access configuration" ,
139 static_cast < XSimpleMailClient
* > (this) );
143 OUString
aProgramURL("$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER
"/senddoc");
144 rtl::Bootstrap::expandMacros(aProgramURL
);
147 if ( FileBase::E_None
!= FileBase::getSystemPathFromFileURL(aProgramURL
, aProgram
))
149 throw css::uno::Exception("Could not convert executable path",
150 static_cast < XSimpleMailClient
* > (this));
153 OStringBuffer aBuffer
;
154 appendShellWord(aBuffer
, aProgram
, true);
158 // Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
159 // configuration node to retrieve the users preferred email application. This may
160 // transparently by redirected to e.g. the corresponding GConf setting in GNOME.
161 OUString aConfigRoot
= "org.openoffice.Office.Common/ExternalMailer";
163 PropertyValue aProperty
;
164 aProperty
.Name
= "nodepath";
165 aProperty
.Value
<<= aConfigRoot
;
167 Sequence
< Any
> aArgumentList( 1 );
168 aArgumentList
[0] <<= aProperty
;
170 Reference
< XNameAccess
> xNameAccess
=
171 Reference
< XNameAccess
> (
172 m_xConfigurationProvider
->createInstanceWithArguments(
173 "com.sun.star.configuration.ConfigurationAccess",
177 if( xNameAccess
.is() )
181 // Retrieve the value for "Program" node and append it feed senddoc with it
182 // using the (undocumented) --mailclient switch
183 xNameAccess
->getByName("Program") >>= aMailer
;
185 if( !aMailer
.isEmpty() )
187 // make sure we have a system path
188 FileBase::getSystemPathFromFileURL( aMailer
, aMailer
);
190 aBuffer
.append(" --mailclient ");
191 appendShellWord(aBuffer
, aMailer
, true);
195 aBuffer
.append(" --mailclient Mail");
201 catch(const RuntimeException
&e
)
203 m_xConfigurationProvider
.clear();
204 SAL_WARN("shell", "RuntimeException caught accessing configuration provider. " << e
);
208 Reference
< XSimpleMailMessage2
> xMessage( xSimpleMailMessage
, UNO_QUERY
);
211 OUString sBody
= xMessage
->getBody();
212 if ( sBody
.getLength() > 0 )
214 aBuffer
.append(" --body ");
215 appendShellWord(aBuffer
, sBody
, false);
219 // Convert from, to, etc. in a best-effort rather than a strict way to the
220 // system encoding, based on the assumption that the relevant address parts
221 // of those strings are ASCII anyway and any problematic characters are only
222 // in the human-readable, informational-only parts:
224 // Append originator if set in the message
225 if ( !xSimpleMailMessage
->getOriginator().isEmpty() )
227 aBuffer
.append(" --from ");
228 appendShellWord(aBuffer
, xSimpleMailMessage
->getOriginator(), false);
231 // Append recipient if set in the message
232 if ( !xSimpleMailMessage
->getRecipient().isEmpty() )
234 aBuffer
.append(" --to ");
235 appendShellWord(aBuffer
, xSimpleMailMessage
->getRecipient(), false);
238 // Append carbon copy recipients set in the message
239 Sequence
< OUString
> aStringList
= xSimpleMailMessage
->getCcRecipient();
240 sal_Int32 n
, nmax
= aStringList
.getLength();
241 for ( n
= 0; n
< nmax
; n
++ )
243 aBuffer
.append(" --cc ");
244 appendShellWord(aBuffer
, aStringList
[n
], false);
247 // Append blind carbon copy recipients set in the message
248 aStringList
= xSimpleMailMessage
->getBccRecipient();
249 nmax
= aStringList
.getLength();
250 for ( n
= 0; n
< nmax
; n
++ )
252 aBuffer
.append(" --bcc ");
253 appendShellWord(aBuffer
, aStringList
[n
], false);
256 // Append subject if set in the message
257 if ( !xSimpleMailMessage
->getSubject().isEmpty() )
259 aBuffer
.append(" --subject ");
260 appendShellWord(aBuffer
, xSimpleMailMessage
->getSubject(), false);
263 // Append attachments set in the message
264 aStringList
= xSimpleMailMessage
->getAttachement();
265 nmax
= aStringList
.getLength();
266 for ( n
= 0; n
< nmax
; n
++ )
268 OUString aSystemPath
;
269 if ( FileBase::E_None
== FileBase::getSystemPathFromFileURL(aStringList
[n
], aSystemPath
) )
271 aBuffer
.append(" --attach ");
272 appendShellWord(aBuffer
, aSystemPath
, true);
276 OString cmd
= aBuffer
.makeStringAndClear();
277 FILE * f
= popen(cmd
.getStr(), "w");
278 if (f
== nullptr || pclose(f
) != 0)
280 throw css::uno::Exception("No mail client configured",
281 static_cast < XSimpleMailClient
* > (this) );
287 OUString SAL_CALL
CmdMailSuppl::getImplementationName( )
289 return OUString("com.sun.star.comp.system.SimpleCommandMail");
292 sal_Bool SAL_CALL
CmdMailSuppl::supportsService( const OUString
& ServiceName
)
294 return cppu::supportsService(this, ServiceName
);
297 Sequence
< OUString
> SAL_CALL
CmdMailSuppl::getSupportedServiceNames( )
299 return Component_getSupportedServiceNames();
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */