update dev300-m58
[ooovba.git] / shell / source / cmdmail / cmdmailsuppl.cxx
blobdc316931389fbcedf91f90ccd258384c7a6a77bd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cmdmailsuppl.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
34 //------------------------------------------------------------------------
35 // includes
36 //------------------------------------------------------------------------
37 #include <osl/diagnose.h>
38 #include <osl/thread.h>
40 #include <rtl/bootstrap.hxx>
42 #include <osl/file.hxx>
43 #include <rtl/strbuf.hxx>
44 #include "cmdmailsuppl.hxx"
45 #include "cmdmailmsg.hxx"
46 #include <com/sun/star/system/SimpleMailClientFlags.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/beans/XPropertySet.hpp>
50 #include <com/sun/star/uno/XComponentContext.hpp>
52 #include <string.h>
53 #include <errno.h>
54 #include <unistd.h>
56 //------------------------------------------------------------------------
57 // namespace directives
58 //------------------------------------------------------------------------
60 using com::sun::star::beans::PropertyValue;
61 using com::sun::star::system::XSimpleMailClientSupplier;
62 using com::sun::star::system::XSimpleMailClient;
63 using com::sun::star::system::XSimpleMailMessage;
64 using com::sun::star::container::XNameAccess;
65 using com::sun::star::container::NoSuchElementException;
66 using rtl::OUString;
67 using rtl::OUStringToOString;
68 using rtl::OString;
69 using rtl::OStringBuffer;
70 using osl::MutexGuard;
71 using osl::FileBase;
73 using namespace cppu;
74 using namespace com::sun::star::system::SimpleMailClientFlags;
75 using namespace com::sun::star::uno;
76 using namespace com::sun::star::lang;
78 //------------------------------------------------------------------------
79 // defines
80 //------------------------------------------------------------------------
82 #define COMP_IMPL_NAME "com.sun.star.comp.system.SimpleCommandMail2"
84 //------------------------------------------------------------------------
85 // helper functions
86 //------------------------------------------------------------------------
88 namespace // private
90 Sequence< OUString > SAL_CALL Component_getSupportedServiceNames()
92 Sequence< OUString > aRet(1);
93 aRet[0] = OUString::createFromAscii("com.sun.star.system.SimpleCommandMail");
94 return aRet;
97 } // end private namespace
99 //-------------------------------------------------
101 //-------------------------------------------------
103 CmdMailSuppl::CmdMailSuppl( const Reference< XComponentContext >& xContext ) :
104 WeakImplHelper3< XSimpleMailClientSupplier, XSimpleMailClient, XServiceInfo >()
106 Reference< XMultiComponentFactory > xServiceManager = xContext->getServiceManager();
108 if ( xServiceManager.is() ) {
109 m_xConfigurationProvider = Reference< XMultiServiceFactory > (
110 xServiceManager->createInstanceWithContext(
111 OUString::createFromAscii( "com.sun.star.configuration.ConfigurationProvider" ), xContext ),
112 UNO_QUERY );
116 //-------------------------------------------------
117 // XSimpleMailClientSupplier
118 //-------------------------------------------------
120 Reference< XSimpleMailClient > SAL_CALL CmdMailSuppl::querySimpleMailClient( )
121 throw (RuntimeException)
123 return static_cast < XSimpleMailClient * > (this);
126 //------------------------------------------------
127 // XSimpleMailClient
128 //------------------------------------------------
130 Reference< XSimpleMailMessage > SAL_CALL CmdMailSuppl::createSimpleMailMessage( )
131 throw (::com::sun::star::uno::RuntimeException)
133 return Reference< XSimpleMailMessage >( new CmdMailMsg( ) );
136 //------------------------------------------------
137 // XSimpleMailClient
138 //------------------------------------------------
140 void SAL_CALL CmdMailSuppl::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 /*aFlag*/ )
141 throw (IllegalArgumentException, Exception, RuntimeException)
143 if ( ! xSimpleMailMessage.is() )
145 throw ::com::sun::star::lang::IllegalArgumentException(
146 OUString(RTL_CONSTASCII_USTRINGPARAM( "No message specified" )),
147 static_cast < XSimpleMailClient * > (this), 1 );
150 if( ! m_xConfigurationProvider.is() )
152 throw ::com::sun::star::uno::Exception(
153 OUString(RTL_CONSTASCII_USTRINGPARAM( "Can not access configuration" )),
154 static_cast < XSimpleMailClient * > (this) );
157 OStringBuffer aBuffer;
158 aBuffer.append("\"");
160 OUString aProgramURL(RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/program/senddoc"));
161 rtl::Bootstrap::expandMacros(aProgramURL);
163 OUString aProgram;
164 if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram))
166 throw ::com::sun::star::uno::Exception(
167 OUString(RTL_CONSTASCII_USTRINGPARAM("Cound not convert executable path")),
168 static_cast < XSimpleMailClient * > (this));
171 aBuffer.append(OUStringToOString(aProgram, osl_getThreadTextEncoding()));
172 aBuffer.append("\" ");
176 // Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
177 // configuration node to retriece the users preferred email application. This may
178 // transparently by redirected to e.g. the corresponding GConf setting in GNOME.
179 OUString aConfigRoot = OUString(
180 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Office.Common/ExternalMailer" ) );
182 PropertyValue aProperty;
183 aProperty.Name = OUString::createFromAscii( "nodepath" );
184 aProperty.Value = makeAny( aConfigRoot );
186 Sequence< Any > aArgumentList( 1 );
187 aArgumentList[0] = makeAny( aProperty );
189 Reference< XNameAccess > xNameAccess =
190 Reference< XNameAccess > (
191 m_xConfigurationProvider->createInstanceWithArguments(
192 OUString::createFromAscii( "com.sun.star.configuration.ConfigurationAccess" ),
193 aArgumentList ),
194 UNO_QUERY );
196 if( xNameAccess.is() )
198 OUString aMailer;
200 // Retrieve the value for "Program" node and append it feed senddoc with it
201 // using the (undocumented) --mailclient switch
202 xNameAccess->getByName( OUString::createFromAscii( "Program" ) ) >>= aMailer;
204 if( aMailer.getLength() )
206 // make sure we have a system path
207 FileBase::getSystemPathFromFileURL( aMailer, aMailer );
209 aBuffer.append("--mailclient ");
210 aBuffer.append(OUStringToOString( aMailer, osl_getThreadTextEncoding() ));
211 aBuffer.append(" ");
213 #ifdef MACOSX
214 else
215 aBuffer.append("--mailclient Mail ");
216 #endif
221 catch( RuntimeException e )
223 m_xConfigurationProvider.clear();
224 OSL_TRACE( "RuntimeException caught accessing configuration provider." );
225 OSL_TRACE( OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
226 throw e;
229 // Append originator if set in the message
230 if ( xSimpleMailMessage->getOriginator().getLength() > 0 )
232 aBuffer.append("--from \"");
233 aBuffer.append(OUStringToOString(xSimpleMailMessage->getOriginator(), osl_getThreadTextEncoding()));
234 aBuffer.append("\" ");
237 // Append receipient if set in the message
238 if ( xSimpleMailMessage->getRecipient().getLength() > 0 )
240 aBuffer.append("--to \"");
241 aBuffer.append(OUStringToOString(xSimpleMailMessage->getRecipient(), osl_getThreadTextEncoding()));
242 aBuffer.append("\" ");
245 // Append carbon copy receipients set in the message
246 Sequence< OUString > aStringList = xSimpleMailMessage->getCcRecipient();
247 sal_Int32 n, nmax = aStringList.getLength();
248 for ( n = 0; n < nmax; n++ )
250 aBuffer.append("--cc \"");
251 aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
252 aBuffer.append("\" ");
255 // Append blind carbon copy receipients set in the message
256 aStringList = xSimpleMailMessage->getBccRecipient();
257 nmax = aStringList.getLength();
258 for ( n = 0; n < nmax; n++ )
260 aBuffer.append("--bcc \"");
261 aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
262 aBuffer.append("\" ");
265 // Append subject if set in the message
266 if ( xSimpleMailMessage->getSubject().getLength() > 0 )
268 aBuffer.append("--subject \"");
269 aBuffer.append(OUStringToOString(xSimpleMailMessage->getSubject(), osl_getThreadTextEncoding()));
270 aBuffer.append("\" ");
273 // Append attachments set in the message
274 aStringList = xSimpleMailMessage->getAttachement();
275 nmax = aStringList.getLength();
276 for ( n = 0; n < nmax; n++ )
278 OUString aSystemPath;
279 if ( FileBase::E_None == FileBase::getSystemPathFromFileURL(aStringList[n], aSystemPath) )
281 aBuffer.append("--attach \"");
282 aBuffer.append(OUStringToOString(aSystemPath, osl_getThreadTextEncoding()));
283 aBuffer.append("\" ");
287 OString cmd = aBuffer.makeStringAndClear();
288 if ( 0 != pclose(popen(cmd.getStr(), "w")) )
290 throw ::com::sun::star::uno::Exception(
291 OUString(RTL_CONSTASCII_USTRINGPARAM( "No mail client configured" )),
292 static_cast < XSimpleMailClient * > (this) );
296 // -------------------------------------------------
297 // XServiceInfo
298 // -------------------------------------------------
300 OUString SAL_CALL CmdMailSuppl::getImplementationName( )
301 throw( RuntimeException )
303 return OUString::createFromAscii( COMP_IMPL_NAME );
306 // -------------------------------------------------
307 // XServiceInfo
308 // -------------------------------------------------
310 sal_Bool SAL_CALL CmdMailSuppl::supportsService( const OUString& ServiceName )
311 throw( RuntimeException )
313 Sequence < OUString > SupportedServicesNames = Component_getSupportedServiceNames();
315 for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
316 if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
317 return sal_True;
319 return sal_False;
322 // -------------------------------------------------
323 // XServiceInfo
324 // -------------------------------------------------
326 Sequence< OUString > SAL_CALL CmdMailSuppl::getSupportedServiceNames( )
327 throw( RuntimeException )
329 return Component_getSupportedServiceNames();