summaryrefslogtreecommitdiff
path: root/shell/source/win32/simplemail/smplmailclient.cxx
blob: 16368a8dd345b4c986f782f5a69cf043f23a3314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*************************************************************************
 *
 *  $RCSfile: smplmailclient.cxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: tra $ $Date: 2001-05-25 08:21:23 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

//------------------------------------------------------------------------
// includes
//------------------------------------------------------------------------

#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif

#ifndef _SMPLMAILCLIENT_HXX_
#include "smplmailclient.hxx"
#endif

#ifndef _SMPLMAILMSG_HXX_
#include "smplmailmsg.hxx"
#endif

#ifndef _COM_SUN_STAR_SYSTEM_SIMPLEMAILCLIENTFLAGS_HPP_
#include <com/sun/star/system/SimpleMailClientFlags.hpp>
#endif

#ifndef _RTL_TENCINFO_H
#include <rtl/tencinfo.h>
#endif

#ifndef _OSL_THREAD_H_
#include <osl/thread.h>
#endif

#ifndef _OSL_FILE_HXX_
#include <osl/file.hxx>
#endif

//------------------------------------------------------------------------
// namespace directives
//------------------------------------------------------------------------

using com::sun::star::uno::Reference;
using com::sun::star::uno::Exception;
using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::Sequence;
using com::sun::star::lang::IllegalArgumentException;
using com::sun::star::system::XSimpleMailClient;
using com::sun::star::system::XSimpleMailMessage;
using rtl::OUString;
using rtl::OString;
using osl::Mutex;

using namespace com::sun::star::system::SimpleMailClientFlags;
using namespace cppu;

//------------------------------------------------
//
//------------------------------------------------

namespace // private
{
    const sal_uInt32 LEN_SMTP_PREFIX = 5; // "SMTP:"

    inline
    OString getSmtpPrefixedEmailAddress( const OUString& aAddress )
    {
        return  OString( "SMTP:" ) +
                OString(
                    aAddress.getStr( ),
                    aAddress.getLength( ),
                    osl_getThreadTextEncoding( ) );
    }

} // end private namespace

//------------------------------------------------
//
//------------------------------------------------

CSmplMailClient::CSmplMailClient( ) :
    m_pSimpleMapi( CSimpleMapi::create( ) )
{
}

//------------------------------------------------
//
//------------------------------------------------

Reference< XSimpleMailMessage > SAL_CALL CSmplMailClient::createSimpleMailMessage(  )
        throw (::com::sun::star::uno::RuntimeException)
{
    return Reference< XSimpleMailMessage >( new CSmplMailMsg( ) );
}

//------------------------------------------------
//
//------------------------------------------------

void SAL_CALL CSmplMailClient::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 aFlag )
    throw (IllegalArgumentException, Exception, RuntimeException)
{
    try
    {
        validateParameter( xSimpleMailMessage, aFlag );

        MapiMessage mapiMsg;
        FLAGS       flFlags;

        initMapiMessage( xSimpleMailMessage, mapiMsg );
        initMapiSendMailFlags( aFlag, flFlags );

        ULONG ulRet = m_pSimpleMapi->MAPISendMail(
            0,          // no session, create a new one
            0,          // no parent window
            &mapiMsg,   // a configured mapi message struct
            flFlags,    // some flags
            0 );        // reserved

        if ( SUCCESS_SUCCESS != ulRet )
            throw Exception(
                getMapiErrorMsg( ulRet ),
                static_cast< XSimpleMailClient* >( this ) );
    }
    catch( RuntimeException& )
    {
        OSL_ASSERT( sal_False );
    }
}

//------------------------------------------------
//
//------------------------------------------------

void CSmplMailClient::validateParameter(
    const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 aFlag )
{
    // check the flags, the allowed range is 0 - (2^n - 1)
    if ( aFlag < 0 || aFlag > 3 )
        throw IllegalArgumentException(
            OUString::createFromAscii( "Invalid flag value" ),
            static_cast< XSimpleMailClient* >( this ),
            2 );

    // check if a recipient is specified of the flags NO_USER_INTERFACE is specified
    if ( (aFlag & NO_USER_INTERFACE) && !xSimpleMailMessage->getRecipient( ).getLength( ) )
        throw IllegalArgumentException(
            OUString::createFromAscii( "No recipient specified" ),
            static_cast< XSimpleMailClient* >( this ),
            1 );
}

//------------------------------------------------
//
//------------------------------------------------

void CSmplMailClient::initMapiMessage(
    const Reference< XSimpleMailMessage >& xSimpleMailMessage, MapiMessage& aMapiMessage )
{
    ZeroMemory( &aMapiMessage, sizeof( aMapiMessage ) );

    if ( xSimpleMailMessage.is( ) )
    {
        // unfortunately the simple mapi functions have
        // only an ANSI prototype, so we have to convert
        // all strings to ascii assuming an us-ascii
        // encoding

        // we hand the buffer of this OStrings directly
        // to the MapiMessage members but have to
        // cast away the constness of the returned buffer
        // pointer, we assume the function MAPISendMail
        // doesn't alter the strings

        m_Subject = OString(
            xSimpleMailMessage->getSubject( ).getStr( ),
            xSimpleMailMessage->getSubject( ).getLength( ),
            osl_getThreadTextEncoding( ) );

        aMapiMessage.lpszSubject = const_cast< LPSTR >( m_Subject.getStr( ) );

        // set the originator information

        if ( xSimpleMailMessage->getOriginator( ).getLength( ) )
        {
            ZeroMemory( &m_MsgOriginator, sizeof( m_MsgOriginator ) );

            m_SmtpAddressOriginator = getSmtpPrefixedEmailAddress(
                xSimpleMailMessage->getOriginator( ) );

            m_MsgOriginator.ulRecipClass = MAPI_ORIG;
            m_MsgOriginator.lpszName     = "";
            m_MsgOriginator.lpszAddress  =
                const_cast< LPSTR >( m_SmtpAddressOriginator.getStr( ) );

            aMapiMessage.lpOriginator = &m_MsgOriginator;
        }

        // set the recipient information

        sal_uInt32 nRecips = calcNumRecipients( xSimpleMailMessage );
        if ( nRecips > 0 )
        {
            m_RecipientList.realloc( nRecips );
            m_RecipientList.clean( );
            m_RecipsSmtpAddressList.clear( );

            size_t nPos = 0;

            // init the main recipient
            OUString aRecipient = xSimpleMailMessage->getRecipient( );
            if ( aRecipient.getLength( ) )
            {
                m_RecipsSmtpAddressList.push_back(
                    getSmtpPrefixedEmailAddress( aRecipient ) );

                m_RecipientList[nPos].ulRecipClass = MAPI_TO;
                m_RecipientList[nPos].lpszName     =
                    const_cast< LPSTR >( m_RecipsSmtpAddressList.back( ).getStr( ) + LEN_SMTP_PREFIX );
                m_RecipientList[nPos].lpszAddress  =
                    const_cast< LPSTR >( m_RecipsSmtpAddressList.back( ).getStr( ) );

                nPos++;
            }

            // add all cc recipients to the list
            initRecipientList(
                xSimpleMailMessage->getCcRecipient( ),
                MAPI_CC,
                nPos );

            // add all bcc recipients to the list
            initRecipientList(
                xSimpleMailMessage->getBccRecipient( ),
                MAPI_BCC,
                nPos );

            aMapiMessage.lpRecips    = &m_RecipientList;
            aMapiMessage.nRecipCount = m_RecipientList.size( );
        }

        initAttachementList( xSimpleMailMessage );
        aMapiMessage.lpFiles    = &m_AttachementList;
        aMapiMessage.nFileCount = m_AttachementList.size( );
    }
}

//------------------------------------------------
//
//------------------------------------------------

sal_uInt32 CSmplMailClient::calcNumRecipients(
    const Reference< XSimpleMailMessage >& xSimpleMailMessage )
{
    sal_uInt32 nRecips = xSimpleMailMessage->getCcRecipient( ).getLength( );
    nRecips += xSimpleMailMessage->getBccRecipient( ).getLength( );

    if ( xSimpleMailMessage->getRecipient( ).getLength( ) )
        nRecips += 1;

    return nRecips;
}

//------------------------------------------------
//
//------------------------------------------------

void CSmplMailClient::initRecipientList(
    const Sequence< rtl::OUString >& aRecipList,
    ULONG ulRecipClass,
    size_t& nPos )
{
    OSL_PRECOND( nPos < m_RecipientList.size( ), "Wrong index" );

    for( sal_Int32 i = 0; i < aRecipList.getLength( ); i++ )
    {
        m_RecipsSmtpAddressList.push_back(
            getSmtpPrefixedEmailAddress( aRecipList[i] ) );

        m_RecipientList[nPos].ulRecipClass = ulRecipClass;
        m_RecipientList[nPos].lpszName =
            const_cast< LPSTR >( m_RecipsSmtpAddressList.back( ).getStr( ) + LEN_SMTP_PREFIX );
        m_RecipientList[nPos].lpszAddress  =
            const_cast< LPSTR >( m_RecipsSmtpAddressList.back( ).getStr( ) );

        nPos++;
    }
}

//------------------------------------------------
//
//------------------------------------------------

void CSmplMailClient::initAttachementList(
    const Reference< XSimpleMailMessage >& xSimpleMailMessage )
{
    Sequence< OUString > aAttachementList =
        xSimpleMailMessage->getAttachement( );

    sal_uInt32 nAttachements = aAttachementList.getLength( );

    // avoid old entries
    m_AttchmtsSysPathList.clear( );
    m_AttachementList.realloc( nAttachements );
    m_AttachementList.clean( );

    OUString aSysPath;
    for ( sal_uInt32 i = 0; i < nAttachements; i++ )
    {
        osl::FileBase::RC rc =
            osl::FileBase::getSystemPathFromFileURL(
                aAttachementList[i], aSysPath );

        if ( osl::FileBase::RC::E_None != rc )
            throw IllegalArgumentException(
                OUString::createFromAscii( " " ),
                static_cast< XSimpleMailClient* >( this ),
                1 );

        m_AttchmtsSysPathList.push_back(
            OString(
                aSysPath.getStr( ),
                aSysPath.getLength( ),
                osl_getThreadTextEncoding( ) ) );

        m_AttachementList[i].lpszPathName = const_cast< LPSTR >(
            m_AttchmtsSysPathList.back( ).getStr( ) );
        m_AttachementList[i].nPosition = -1;
    }
}

//------------------------------------------------
//
//------------------------------------------------

void CSmplMailClient::initMapiSendMailFlags( sal_Int32 aFlags, FLAGS& aMapiFlags )
{
    aMapiFlags = MAPI_NEW_SESSION | MAPI_UNICODE;

    if ( !( aFlags & NO_USER_INTERFACE ) )
        aMapiFlags |= MAPI_DIALOG;

    if ( !( aFlags & NO_LOGON_DIALOG ) )
        aMapiFlags |= MAPI_LOGON_UI;
}

//------------------------------------------------
//
//------------------------------------------------

rtl::OUString CSmplMailClient::getMapiErrorMsg( ULONG ulMapiError )
{
    LPVOID lpMsgBuff;
    DWORD dwRet = FormatMessageA(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE,
        GetModuleHandleA( "mapi32.dll" ),
        ulMapiError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        reinterpret_cast< LPSTR >( &lpMsgBuff ),
        0,
        NULL );

    OUString errMsg;

    if ( dwRet )
    {
        errMsg = OUString::createFromAscii(
            static_cast< LPSTR >( lpMsgBuff ) );
        LocalFree( lpMsgBuff );
    }

    return errMsg;
}