Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <osl/diagnose.h>
30 : : #include <osl/thread.h>
31 : :
32 : : #include <rtl/bootstrap.hxx>
33 : :
34 : : #include <osl/file.hxx>
35 : : #include <rtl/strbuf.hxx>
36 : : #include "cmdmailsuppl.hxx"
37 : : #include "cmdmailmsg.hxx"
38 : : #include <com/sun/star/system/SimpleMailClientFlags.hpp>
39 : : #include <com/sun/star/container/XNameAccess.hpp>
40 : : #include <com/sun/star/beans/PropertyValue.hpp>
41 : : #include <com/sun/star/beans/XPropertySet.hpp>
42 : : #include <com/sun/star/uno/XComponentContext.hpp>
43 : :
44 : : #include <string.h>
45 : : #include <errno.h>
46 : : #include <unistd.h>
47 : :
48 : : //------------------------------------------------------------------------
49 : : // namespace directives
50 : : //------------------------------------------------------------------------
51 : :
52 : : using com::sun::star::beans::PropertyValue;
53 : : using com::sun::star::system::XSimpleMailClientSupplier;
54 : : using com::sun::star::system::XSimpleMailClient;
55 : : using com::sun::star::system::XSimpleMailMessage;
56 : : using com::sun::star::container::XNameAccess;
57 : : using com::sun::star::container::NoSuchElementException;
58 : : using rtl::OUString;
59 : : using rtl::OUStringToOString;
60 : : using rtl::OString;
61 : : using rtl::OStringBuffer;
62 : : using osl::MutexGuard;
63 : : using osl::FileBase;
64 : :
65 : : using namespace cppu;
66 : : using namespace com::sun::star::system::SimpleMailClientFlags;
67 : : using namespace com::sun::star::uno;
68 : : using namespace com::sun::star::lang;
69 : :
70 : : #define COMP_IMPL_NAME "com.sun.star.comp.system.SimpleCommandMail2"
71 : :
72 : : //------------------------------------------------------------------------
73 : : // helper functions
74 : : //------------------------------------------------------------------------
75 : :
76 : : namespace // private
77 : : {
78 : 0 : Sequence< OUString > SAL_CALL Component_getSupportedServiceNames()
79 : : {
80 : 0 : Sequence< OUString > aRet(1);
81 : 0 : aRet[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.system.SimpleCommandMail"));
82 : 0 : return aRet;
83 : : }
84 : :
85 : : } // end private namespace
86 : :
87 : : //-------------------------------------------------
88 : :
89 : 0 : CmdMailSuppl::CmdMailSuppl( const Reference< XComponentContext >& xContext ) :
90 : 0 : WeakImplHelper3< XSimpleMailClientSupplier, XSimpleMailClient, XServiceInfo >()
91 : : {
92 : 0 : Reference< XMultiComponentFactory > xServiceManager = xContext->getServiceManager();
93 : :
94 : 0 : if ( xServiceManager.is() ) {
95 : : m_xConfigurationProvider = Reference< XMultiServiceFactory > (
96 : 0 : xServiceManager->createInstanceWithContext(
97 : 0 : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationProvider")), xContext ),
98 : 0 : UNO_QUERY );
99 : 0 : }
100 : 0 : }
101 : :
102 : : //-------------------------------------------------
103 : : // XSimpleMailClientSupplier
104 : : //-------------------------------------------------
105 : :
106 : 0 : Reference< XSimpleMailClient > SAL_CALL CmdMailSuppl::querySimpleMailClient( )
107 : : throw (RuntimeException)
108 : : {
109 : 0 : return static_cast < XSimpleMailClient * > (this);
110 : : }
111 : :
112 : : //------------------------------------------------
113 : : // XSimpleMailClient
114 : : //------------------------------------------------
115 : :
116 : 0 : Reference< XSimpleMailMessage > SAL_CALL CmdMailSuppl::createSimpleMailMessage( )
117 : : throw (::com::sun::star::uno::RuntimeException)
118 : : {
119 : 0 : return Reference< XSimpleMailMessage >( new CmdMailMsg( ) );
120 : : }
121 : :
122 : : //------------------------------------------------
123 : : // XSimpleMailClient
124 : : //------------------------------------------------
125 : :
126 : 0 : void SAL_CALL CmdMailSuppl::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 /*aFlag*/ )
127 : : throw (IllegalArgumentException, Exception, RuntimeException)
128 : : {
129 : 0 : if ( ! xSimpleMailMessage.is() )
130 : : {
131 : : throw ::com::sun::star::lang::IllegalArgumentException(
132 : : OUString(RTL_CONSTASCII_USTRINGPARAM( "No message specified" )),
133 : 0 : static_cast < XSimpleMailClient * > (this), 1 );
134 : : }
135 : :
136 : 0 : if( ! m_xConfigurationProvider.is() )
137 : : {
138 : : throw ::com::sun::star::uno::Exception(
139 : : OUString(RTL_CONSTASCII_USTRINGPARAM( "Can not access configuration" )),
140 : 0 : static_cast < XSimpleMailClient * > (this) );
141 : : }
142 : :
143 : 0 : OStringBuffer aBuffer;
144 : 0 : aBuffer.append("\"");
145 : :
146 : 0 : OUString aProgramURL(RTL_CONSTASCII_USTRINGPARAM("$BRAND_BASE_DIR/program/senddoc"));
147 : 0 : rtl::Bootstrap::expandMacros(aProgramURL);
148 : :
149 : 0 : OUString aProgram;
150 : 0 : if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram))
151 : : {
152 : : throw ::com::sun::star::uno::Exception(
153 : : OUString(RTL_CONSTASCII_USTRINGPARAM("Cound not convert executable path")),
154 : 0 : static_cast < XSimpleMailClient * > (this));
155 : : }
156 : :
157 : 0 : aBuffer.append(OUStringToOString(aProgram, osl_getThreadTextEncoding()));
158 : 0 : aBuffer.append("\" ");
159 : :
160 : : try
161 : : {
162 : : // Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
163 : : // configuration node to retriece the users preferred email application. This may
164 : : // transparently by redirected to e.g. the corresponding GConf setting in GNOME.
165 : : OUString aConfigRoot = OUString(
166 : 0 : RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Office.Common/ExternalMailer" ) );
167 : :
168 : 0 : PropertyValue aProperty;
169 : 0 : aProperty.Name = OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath"));
170 : 0 : aProperty.Value = makeAny( aConfigRoot );
171 : :
172 : 0 : Sequence< Any > aArgumentList( 1 );
173 : 0 : aArgumentList[0] = makeAny( aProperty );
174 : :
175 : : Reference< XNameAccess > xNameAccess =
176 : : Reference< XNameAccess > (
177 : 0 : m_xConfigurationProvider->createInstanceWithArguments(
178 : : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
179 : 0 : aArgumentList ),
180 : 0 : UNO_QUERY );
181 : :
182 : 0 : if( xNameAccess.is() )
183 : : {
184 : 0 : OUString aMailer;
185 : :
186 : : // Retrieve the value for "Program" node and append it feed senddoc with it
187 : : // using the (undocumented) --mailclient switch
188 : 0 : xNameAccess->getByName( OUString(RTL_CONSTASCII_USTRINGPARAM("Program")) ) >>= aMailer;
189 : :
190 : 0 : if( !aMailer.isEmpty() )
191 : : {
192 : : // make sure we have a system path
193 : 0 : FileBase::getSystemPathFromFileURL( aMailer, aMailer );
194 : :
195 : 0 : aBuffer.append("--mailclient ");
196 : 0 : aBuffer.append(OUStringToOString( aMailer, osl_getThreadTextEncoding() ));
197 : 0 : aBuffer.append(" ");
198 : 0 : }
199 : : #ifdef MACOSX
200 : : else
201 : : aBuffer.append("--mailclient Mail ");
202 : : #endif
203 : 0 : }
204 : :
205 : : }
206 : :
207 : 0 : catch(const RuntimeException &e )
208 : : {
209 : 0 : m_xConfigurationProvider.clear();
210 : : OSL_TRACE( "RuntimeException caught accessing configuration provider." );
211 : : OSL_TRACE( "%s", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
212 : 0 : throw;
213 : : }
214 : :
215 : : // Append originator if set in the message
216 : 0 : if ( !xSimpleMailMessage->getOriginator().isEmpty() )
217 : : {
218 : 0 : aBuffer.append("--from \"");
219 : 0 : aBuffer.append(OUStringToOString(xSimpleMailMessage->getOriginator(), osl_getThreadTextEncoding()));
220 : 0 : aBuffer.append("\" ");
221 : : }
222 : :
223 : : // Append receipient if set in the message
224 : 0 : if ( !xSimpleMailMessage->getRecipient().isEmpty() )
225 : : {
226 : 0 : aBuffer.append("--to \"");
227 : 0 : aBuffer.append(OUStringToOString(xSimpleMailMessage->getRecipient(), osl_getThreadTextEncoding()));
228 : 0 : aBuffer.append("\" ");
229 : : }
230 : :
231 : : // Append carbon copy receipients set in the message
232 : 0 : Sequence< OUString > aStringList = xSimpleMailMessage->getCcRecipient();
233 : 0 : sal_Int32 n, nmax = aStringList.getLength();
234 : 0 : for ( n = 0; n < nmax; n++ )
235 : : {
236 : 0 : aBuffer.append("--cc \"");
237 : 0 : aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
238 : 0 : aBuffer.append("\" ");
239 : : }
240 : :
241 : : // Append blind carbon copy receipients set in the message
242 : 0 : aStringList = xSimpleMailMessage->getBccRecipient();
243 : 0 : nmax = aStringList.getLength();
244 : 0 : for ( n = 0; n < nmax; n++ )
245 : : {
246 : 0 : aBuffer.append("--bcc \"");
247 : 0 : aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
248 : 0 : aBuffer.append("\" ");
249 : : }
250 : :
251 : : // Append subject if set in the message
252 : 0 : if ( !xSimpleMailMessage->getSubject().isEmpty() )
253 : : {
254 : 0 : aBuffer.append("--subject \"");
255 : 0 : aBuffer.append(OUStringToOString(xSimpleMailMessage->getSubject(), osl_getThreadTextEncoding()));
256 : 0 : aBuffer.append("\" ");
257 : : }
258 : :
259 : : // Append attachments set in the message
260 : 0 : aStringList = xSimpleMailMessage->getAttachement();
261 : 0 : nmax = aStringList.getLength();
262 : 0 : for ( n = 0; n < nmax; n++ )
263 : : {
264 : 0 : OUString aSystemPath;
265 : 0 : if ( FileBase::E_None == FileBase::getSystemPathFromFileURL(aStringList[n], aSystemPath) )
266 : : {
267 : 0 : aBuffer.append("--attach \"");
268 : 0 : aBuffer.append(OUStringToOString(aSystemPath, osl_getThreadTextEncoding()));
269 : 0 : aBuffer.append("\" ");
270 : : }
271 : 0 : }
272 : :
273 : 0 : OString cmd = aBuffer.makeStringAndClear();
274 : 0 : if ( 0 != pclose(popen(cmd.getStr(), "w")) )
275 : : {
276 : : throw ::com::sun::star::uno::Exception(
277 : : OUString(RTL_CONSTASCII_USTRINGPARAM( "No mail client configured" )),
278 : 0 : static_cast < XSimpleMailClient * > (this) );
279 : 0 : }
280 : 0 : }
281 : :
282 : : // -------------------------------------------------
283 : : // XServiceInfo
284 : : // -------------------------------------------------
285 : :
286 : 0 : OUString SAL_CALL CmdMailSuppl::getImplementationName( )
287 : : throw( RuntimeException )
288 : : {
289 : 0 : return OUString(RTL_CONSTASCII_USTRINGPARAM( COMP_IMPL_NAME ));
290 : : }
291 : :
292 : : // -------------------------------------------------
293 : : // XServiceInfo
294 : : // -------------------------------------------------
295 : :
296 : 0 : sal_Bool SAL_CALL CmdMailSuppl::supportsService( const OUString& ServiceName )
297 : : throw( RuntimeException )
298 : : {
299 : 0 : Sequence < OUString > SupportedServicesNames = Component_getSupportedServiceNames();
300 : :
301 : 0 : for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
302 : 0 : if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
303 : 0 : return sal_True;
304 : :
305 : 0 : return sal_False;
306 : : }
307 : :
308 : : // -------------------------------------------------
309 : : // XServiceInfo
310 : : // -------------------------------------------------
311 : :
312 : 0 : Sequence< OUString > SAL_CALL CmdMailSuppl::getSupportedServiceNames( )
313 : : throw( RuntimeException )
314 : : {
315 : 0 : return Component_getSupportedServiceNames();
316 : : }
317 : :
318 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|