Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <osl/diagnose.h>
21 : #include <osl/thread.h>
22 :
23 : #include <rtl/bootstrap.hxx>
24 :
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>
35 :
36 : #include <string.h>
37 : #include <errno.h>
38 : #include <unistd.h>
39 :
40 : //------------------------------------------------------------------------
41 : // namespace directives
42 : //------------------------------------------------------------------------
43 :
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 rtl::OUString;
51 : using rtl::OUStringToOString;
52 : using rtl::OString;
53 : using rtl::OStringBuffer;
54 : using osl::MutexGuard;
55 : using osl::FileBase;
56 :
57 : using namespace cppu;
58 : using namespace com::sun::star::system::SimpleMailClientFlags;
59 : using namespace com::sun::star::uno;
60 : using namespace com::sun::star::lang;
61 : using namespace com::sun::star::configuration;
62 :
63 : #define COMP_IMPL_NAME "com.sun.star.comp.system.SimpleCommandMail2"
64 :
65 : //------------------------------------------------------------------------
66 : // helper functions
67 : //------------------------------------------------------------------------
68 :
69 : namespace // private
70 : {
71 0 : Sequence< OUString > SAL_CALL Component_getSupportedServiceNames()
72 : {
73 0 : Sequence< OUString > aRet(1);
74 0 : aRet[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.system.SimpleCommandMail"));
75 0 : return aRet;
76 : }
77 :
78 : } // end private namespace
79 :
80 : //-------------------------------------------------
81 :
82 0 : CmdMailSuppl::CmdMailSuppl( const Reference< XComponentContext >& xContext ) :
83 0 : WeakImplHelper3< XSimpleMailClientSupplier, XSimpleMailClient, XServiceInfo >()
84 : {
85 0 : m_xConfigurationProvider = theDefaultProvider::get(xContext);
86 0 : }
87 :
88 : //-------------------------------------------------
89 : // XSimpleMailClientSupplier
90 : //-------------------------------------------------
91 :
92 0 : Reference< XSimpleMailClient > SAL_CALL CmdMailSuppl::querySimpleMailClient( )
93 : throw (RuntimeException)
94 : {
95 0 : return static_cast < XSimpleMailClient * > (this);
96 : }
97 :
98 : //------------------------------------------------
99 : // XSimpleMailClient
100 : //------------------------------------------------
101 :
102 0 : Reference< XSimpleMailMessage > SAL_CALL CmdMailSuppl::createSimpleMailMessage( )
103 : throw (::com::sun::star::uno::RuntimeException)
104 : {
105 0 : return Reference< XSimpleMailMessage >( new CmdMailMsg( ) );
106 : }
107 :
108 : //------------------------------------------------
109 : // XSimpleMailClient
110 : //------------------------------------------------
111 :
112 0 : void SAL_CALL CmdMailSuppl::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 /*aFlag*/ )
113 : throw (IllegalArgumentException, Exception, RuntimeException)
114 : {
115 0 : if ( ! xSimpleMailMessage.is() )
116 : {
117 : throw ::com::sun::star::lang::IllegalArgumentException(
118 : OUString(RTL_CONSTASCII_USTRINGPARAM( "No message specified" )),
119 0 : static_cast < XSimpleMailClient * > (this), 1 );
120 : }
121 :
122 0 : if( ! m_xConfigurationProvider.is() )
123 : {
124 : throw ::com::sun::star::uno::Exception(
125 : OUString(RTL_CONSTASCII_USTRINGPARAM( "Can not access configuration" )),
126 0 : static_cast < XSimpleMailClient * > (this) );
127 : }
128 :
129 0 : OStringBuffer aBuffer;
130 0 : aBuffer.append("\"");
131 :
132 0 : OUString aProgramURL(RTL_CONSTASCII_USTRINGPARAM("$BRAND_BASE_DIR/program/senddoc"));
133 0 : rtl::Bootstrap::expandMacros(aProgramURL);
134 :
135 0 : OUString aProgram;
136 0 : if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram))
137 : {
138 : throw ::com::sun::star::uno::Exception(
139 : OUString(RTL_CONSTASCII_USTRINGPARAM("Cound not convert executable path")),
140 0 : static_cast < XSimpleMailClient * > (this));
141 : }
142 :
143 0 : aBuffer.append(OUStringToOString(aProgram, osl_getThreadTextEncoding()));
144 0 : aBuffer.append("\" ");
145 :
146 : try
147 : {
148 : // Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
149 : // configuration node to retriece the users preferred email application. This may
150 : // transparently by redirected to e.g. the corresponding GConf setting in GNOME.
151 : OUString aConfigRoot = OUString(
152 0 : RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Office.Common/ExternalMailer" ) );
153 :
154 0 : PropertyValue aProperty;
155 0 : aProperty.Name = OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath"));
156 0 : aProperty.Value = makeAny( aConfigRoot );
157 :
158 0 : Sequence< Any > aArgumentList( 1 );
159 0 : aArgumentList[0] = makeAny( aProperty );
160 :
161 : Reference< XNameAccess > xNameAccess =
162 : Reference< XNameAccess > (
163 0 : m_xConfigurationProvider->createInstanceWithArguments(
164 : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
165 0 : aArgumentList ),
166 0 : UNO_QUERY );
167 :
168 0 : if( xNameAccess.is() )
169 : {
170 0 : OUString aMailer;
171 :
172 : // Retrieve the value for "Program" node and append it feed senddoc with it
173 : // using the (undocumented) --mailclient switch
174 0 : xNameAccess->getByName( OUString(RTL_CONSTASCII_USTRINGPARAM("Program")) ) >>= aMailer;
175 :
176 0 : if( !aMailer.isEmpty() )
177 : {
178 : // make sure we have a system path
179 0 : FileBase::getSystemPathFromFileURL( aMailer, aMailer );
180 :
181 0 : aBuffer.append("--mailclient ");
182 0 : aBuffer.append(OUStringToOString( aMailer, osl_getThreadTextEncoding() ));
183 0 : aBuffer.append(" ");
184 0 : }
185 : #ifdef MACOSX
186 : else
187 : aBuffer.append("--mailclient Mail ");
188 : #endif
189 0 : }
190 :
191 : }
192 :
193 0 : catch(const RuntimeException &e )
194 : {
195 0 : m_xConfigurationProvider.clear();
196 : OSL_TRACE( "RuntimeException caught accessing configuration provider." );
197 : OSL_TRACE( "%s", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
198 0 : throw;
199 : }
200 :
201 : // Append originator if set in the message
202 0 : if ( !xSimpleMailMessage->getOriginator().isEmpty() )
203 : {
204 0 : aBuffer.append("--from \"");
205 0 : aBuffer.append(OUStringToOString(xSimpleMailMessage->getOriginator(), osl_getThreadTextEncoding()));
206 0 : aBuffer.append("\" ");
207 : }
208 :
209 : // Append receipient if set in the message
210 0 : if ( !xSimpleMailMessage->getRecipient().isEmpty() )
211 : {
212 0 : aBuffer.append("--to \"");
213 0 : aBuffer.append(OUStringToOString(xSimpleMailMessage->getRecipient(), osl_getThreadTextEncoding()));
214 0 : aBuffer.append("\" ");
215 : }
216 :
217 : // Append carbon copy receipients set in the message
218 0 : Sequence< OUString > aStringList = xSimpleMailMessage->getCcRecipient();
219 0 : sal_Int32 n, nmax = aStringList.getLength();
220 0 : for ( n = 0; n < nmax; n++ )
221 : {
222 0 : aBuffer.append("--cc \"");
223 0 : aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
224 0 : aBuffer.append("\" ");
225 : }
226 :
227 : // Append blind carbon copy receipients set in the message
228 0 : aStringList = xSimpleMailMessage->getBccRecipient();
229 0 : nmax = aStringList.getLength();
230 0 : for ( n = 0; n < nmax; n++ )
231 : {
232 0 : aBuffer.append("--bcc \"");
233 0 : aBuffer.append(OUStringToOString(aStringList[n], osl_getThreadTextEncoding()));
234 0 : aBuffer.append("\" ");
235 : }
236 :
237 : // Append subject if set in the message
238 0 : if ( !xSimpleMailMessage->getSubject().isEmpty() )
239 : {
240 0 : aBuffer.append("--subject \"");
241 0 : aBuffer.append(OUStringToOString(xSimpleMailMessage->getSubject(), osl_getThreadTextEncoding()));
242 0 : aBuffer.append("\" ");
243 : }
244 :
245 : // Append attachments set in the message
246 0 : aStringList = xSimpleMailMessage->getAttachement();
247 0 : nmax = aStringList.getLength();
248 0 : for ( n = 0; n < nmax; n++ )
249 : {
250 0 : OUString aSystemPath;
251 0 : if ( FileBase::E_None == FileBase::getSystemPathFromFileURL(aStringList[n], aSystemPath) )
252 : {
253 0 : aBuffer.append("--attach \"");
254 0 : aBuffer.append(OUStringToOString(aSystemPath, osl_getThreadTextEncoding()));
255 0 : aBuffer.append("\" ");
256 : }
257 0 : }
258 :
259 0 : OString cmd = aBuffer.makeStringAndClear();
260 0 : if ( 0 != pclose(popen(cmd.getStr(), "w")) )
261 : {
262 : throw ::com::sun::star::uno::Exception(
263 : OUString(RTL_CONSTASCII_USTRINGPARAM( "No mail client configured" )),
264 0 : static_cast < XSimpleMailClient * > (this) );
265 0 : }
266 0 : }
267 :
268 : // -------------------------------------------------
269 : // XServiceInfo
270 : // -------------------------------------------------
271 :
272 0 : OUString SAL_CALL CmdMailSuppl::getImplementationName( )
273 : throw( RuntimeException )
274 : {
275 0 : return OUString(RTL_CONSTASCII_USTRINGPARAM( COMP_IMPL_NAME ));
276 : }
277 :
278 : // -------------------------------------------------
279 : // XServiceInfo
280 : // -------------------------------------------------
281 :
282 0 : sal_Bool SAL_CALL CmdMailSuppl::supportsService( const OUString& ServiceName )
283 : throw( RuntimeException )
284 : {
285 0 : Sequence < OUString > SupportedServicesNames = Component_getSupportedServiceNames();
286 :
287 0 : for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
288 0 : if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
289 0 : return sal_True;
290 :
291 0 : return sal_False;
292 : }
293 :
294 : // -------------------------------------------------
295 : // XServiceInfo
296 : // -------------------------------------------------
297 :
298 0 : Sequence< OUString > SAL_CALL CmdMailSuppl::getSupportedServiceNames( )
299 : throw( RuntimeException )
300 : {
301 0 : return Component_getSupportedServiceNames();
302 : }
303 :
304 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|