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 <osl/diagnose.h>
21 #include <osl/thread.h>
23 #include <rtl/bootstrap.hxx>
25 #include <osl/file.hxx>
26 #include <rtl/strbuf.hxx>
27 #include "cmdmailsuppl.hxx"
28 #include "cmdmailmsg.hxx"
29 #include <com/sun/star/system/SimpleMailClientFlags.hpp>
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/configuration/theDefaultProvider.hpp>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/uno/XComponentContext.hpp>
40 //------------------------------------------------------------------------
41 // namespace directives
42 //------------------------------------------------------------------------
44 using com::sun::star::beans::PropertyValue
;
45 using com::sun::star::system::XSimpleMailClientSupplier
;
46 using com::sun::star::system::XSimpleMailClient
;
47 using com::sun::star::system::XSimpleMailMessage
;
48 using com::sun::star::container::XNameAccess
;
49 using com::sun::star::container::NoSuchElementException
;
50 using osl::MutexGuard
;
54 using namespace com::sun::star::system::SimpleMailClientFlags
;
55 using namespace com::sun::star::uno
;
56 using namespace com::sun::star::lang
;
57 using namespace com::sun::star::configuration
;
59 #define COMP_IMPL_NAME "com.sun.star.comp.system.SimpleCommandMail2"
61 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
67 Sequence
< OUString
> SAL_CALL
Component_getSupportedServiceNames()
69 Sequence
< OUString
> aRet(1);
70 aRet
[0] = "com.sun.star.system.SimpleCommandMail";
74 } // end private namespace
76 //-------------------------------------------------
78 CmdMailSuppl::CmdMailSuppl( const Reference
< XComponentContext
>& xContext
) :
79 WeakImplHelper3
< XSimpleMailClientSupplier
, XSimpleMailClient
, XServiceInfo
>()
81 m_xConfigurationProvider
= theDefaultProvider::get(xContext
);
84 //-------------------------------------------------
85 // XSimpleMailClientSupplier
86 //-------------------------------------------------
88 Reference
< XSimpleMailClient
> SAL_CALL
CmdMailSuppl::querySimpleMailClient( )
89 throw (RuntimeException
)
91 return static_cast < XSimpleMailClient
* > (this);
94 //------------------------------------------------
96 //------------------------------------------------
98 Reference
< XSimpleMailMessage
> SAL_CALL
CmdMailSuppl::createSimpleMailMessage( )
99 throw (::com::sun::star::uno::RuntimeException
)
101 return Reference
< XSimpleMailMessage
>( new CmdMailMsg( ) );
104 //------------------------------------------------
106 //------------------------------------------------
108 void SAL_CALL
CmdMailSuppl::sendSimpleMailMessage( const Reference
< XSimpleMailMessage
>& xSimpleMailMessage
, sal_Int32
/*aFlag*/ )
109 throw (IllegalArgumentException
, Exception
, RuntimeException
)
111 if ( ! xSimpleMailMessage
.is() )
113 throw ::com::sun::star::lang::IllegalArgumentException( "No message specified" ,
114 static_cast < XSimpleMailClient
* > (this), 1 );
117 if( ! m_xConfigurationProvider
.is() )
119 throw ::com::sun::star::uno::Exception( "Can not access configuration" ,
120 static_cast < XSimpleMailClient
* > (this) );
124 OUString
aProgramURL("$BRAND_BASE_DIR/program/senddoc");
125 rtl::Bootstrap::expandMacros(aProgramURL
);
128 if ( FileBase::E_None
!= FileBase::getSystemPathFromFileURL(aProgramURL
, aProgram
))
130 throw ::com::sun::star::uno::Exception("Cound not convert executable path",
131 static_cast < XSimpleMailClient
* > (this));
134 OStringBuffer
aBuffer("\"" + OUStringToOString(aProgram
, osl_getThreadTextEncoding()) + "\" ");
138 // Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
139 // configuration node to retriece the users preferred email application. This may
140 // transparently by redirected to e.g. the corresponding GConf setting in GNOME.
141 OUString aConfigRoot
= "org.openoffice.Office.Common/ExternalMailer";
143 PropertyValue aProperty
;
144 aProperty
.Name
= OUString("nodepath");
145 aProperty
.Value
= makeAny( aConfigRoot
);
147 Sequence
< Any
> aArgumentList( 1 );
148 aArgumentList
[0] = makeAny( aProperty
);
150 Reference
< XNameAccess
> xNameAccess
=
151 Reference
< XNameAccess
> (
152 m_xConfigurationProvider
->createInstanceWithArguments(
153 OUString("com.sun.star.configuration.ConfigurationAccess"),
157 if( xNameAccess
.is() )
161 // Retrieve the value for "Program" node and append it feed senddoc with it
162 // using the (undocumented) --mailclient switch
163 xNameAccess
->getByName( OUString("Program") ) >>= aMailer
;
165 if( !aMailer
.isEmpty() )
167 // make sure we have a system path
168 FileBase::getSystemPathFromFileURL( aMailer
, aMailer
);
170 aBuffer
.append("--mailclient " + OUStringToOString( aMailer
, osl_getThreadTextEncoding() ) +
175 aBuffer
.append("--mailclient Mail ");
181 catch(const RuntimeException
&e
)
183 m_xConfigurationProvider
.clear();
184 OSL_TRACE( "RuntimeException caught accessing configuration provider." );
185 OSL_TRACE( "%s", OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
189 // Append originator if set in the message
190 if ( !xSimpleMailMessage
->getOriginator().isEmpty() )
192 aBuffer
.append("--from \"" +
193 OUStringToOString(xSimpleMailMessage
->getOriginator(), osl_getThreadTextEncoding()) +
197 // Append receipient if set in the message
198 if ( !xSimpleMailMessage
->getRecipient().isEmpty() )
200 aBuffer
.append("--to \"" +
201 OUStringToOString(xSimpleMailMessage
->getRecipient(), osl_getThreadTextEncoding()) +
205 // Append carbon copy receipients set in the message
206 Sequence
< OUString
> aStringList
= xSimpleMailMessage
->getCcRecipient();
207 sal_Int32 n
, nmax
= aStringList
.getLength();
208 for ( n
= 0; n
< nmax
; n
++ )
210 aBuffer
.append("--cc \"" +
211 OUStringToOString(aStringList
[n
], osl_getThreadTextEncoding()) +
215 // Append blind carbon copy receipients set in the message
216 aStringList
= xSimpleMailMessage
->getBccRecipient();
217 nmax
= aStringList
.getLength();
218 for ( n
= 0; n
< nmax
; n
++ )
220 aBuffer
.append("--bcc \"" +
221 OUStringToOString(aStringList
[n
], osl_getThreadTextEncoding()) +
225 // Append subject if set in the message
226 if ( !xSimpleMailMessage
->getSubject().isEmpty() )
228 aBuffer
.append("--subject \"" +
229 OUStringToOString(xSimpleMailMessage
->getSubject(), osl_getThreadTextEncoding()) +
233 // Append attachments set in the message
234 aStringList
= xSimpleMailMessage
->getAttachement();
235 nmax
= aStringList
.getLength();
236 for ( n
= 0; n
< nmax
; n
++ )
238 OUString aSystemPath
;
239 if ( FileBase::E_None
== FileBase::getSystemPathFromFileURL(aStringList
[n
], aSystemPath
) )
241 aBuffer
.append("--attach \"" +
242 OUStringToOString(aSystemPath
, osl_getThreadTextEncoding()) +
247 OString cmd
= aBuffer
.makeStringAndClear();
248 if ( 0 != pclose(popen(cmd
.getStr(), "w")) )
250 throw ::com::sun::star::uno::Exception("No mail client configured",
251 static_cast < XSimpleMailClient
* > (this) );
255 // -------------------------------------------------
257 // -------------------------------------------------
259 OUString SAL_CALL
CmdMailSuppl::getImplementationName( )
260 throw( RuntimeException
)
262 return OUString(COMP_IMPL_NAME
);
265 // -------------------------------------------------
267 // -------------------------------------------------
269 sal_Bool SAL_CALL
CmdMailSuppl::supportsService( const OUString
& ServiceName
)
270 throw( RuntimeException
)
272 Sequence
< OUString
> SupportedServicesNames
= Component_getSupportedServiceNames();
274 for ( sal_Int32 n
= SupportedServicesNames
.getLength(); n
--; )
275 if ( SupportedServicesNames
[n
] == ServiceName
)
281 // -------------------------------------------------
283 // -------------------------------------------------
285 Sequence
< OUString
> SAL_CALL
CmdMailSuppl::getSupportedServiceNames( )
286 throw( RuntimeException
)
288 return Component_getSupportedServiceNames();
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */