1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef INCLUDED_MAILDISPATCHER_HXX 29*cdf0e10cSrcweir #define INCLUDED_MAILDISPATCHER_HXX 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir //#ifndef _COM_SUN_STAR_MAIL_XMAILSERVER_HPP_ 32*cdf0e10cSrcweir //#include "com/sun/star/mail/XMailServer.hpp" 33*cdf0e10cSrcweir //#endif 34*cdf0e10cSrcweir #include "com/sun/star/mail/XSmtpService.hpp" 35*cdf0e10cSrcweir #include "com/sun/star/mail/XMailMessage.hpp" 36*cdf0e10cSrcweir #include <osl/thread.hxx> 37*cdf0e10cSrcweir #include <osl/conditn.hxx> 38*cdf0e10cSrcweir #include <salhelper/refobj.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <list> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir class IMailDispatcherListener; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir /** 45*cdf0e10cSrcweir A MailDispatcher should be used for sending a bunch a mail messages 46*cdf0e10cSrcweir asynchronously. Usually a client enqueues a number of mail messages 47*cdf0e10cSrcweir and then calls start to begin sending them. An instance of this class 48*cdf0e10cSrcweir must not be shared among different client threads. Instead each client 49*cdf0e10cSrcweir thread should create an own instance of this class. 50*cdf0e10cSrcweir */ 51*cdf0e10cSrcweir class MailDispatcher : public ::salhelper::ReferenceObject, private ::osl::Thread 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir public: 54*cdf0e10cSrcweir // bringing operator new/delete into scope 55*cdf0e10cSrcweir using osl::Thread::operator new; 56*cdf0e10cSrcweir using osl::Thread::operator delete; 57*cdf0e10cSrcweir using osl::Thread::join; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir public: 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir /** 62*cdf0e10cSrcweir @param xSmtpService 63*cdf0e10cSrcweir [in] a reference to a mail server. A user must be 64*cdf0e10cSrcweir connected to the mail server otherwise errors occur 65*cdf0e10cSrcweir during the delivery of mail messages. 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir @throws ::com::sun::star::uno::RuntimeException 68*cdf0e10cSrcweir on errors during construction of an instance of this class. 69*cdf0e10cSrcweir */ 70*cdf0e10cSrcweir MailDispatcher(::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> xMailService); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir /** 73*cdf0e10cSrcweir Shutdown the mail dispatcher. Every mail messages 74*cdf0e10cSrcweir not yet sent will be discarded. 75*cdf0e10cSrcweir */ 76*cdf0e10cSrcweir virtual ~MailDispatcher(); 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /** 79*cdf0e10cSrcweir Enqueue a mail message for delivery. A client must 80*cdf0e10cSrcweir start the mail dispatcher in order to send the 81*cdf0e10cSrcweir enqueued mail messages. 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir @param xMailMessage 84*cdf0e10cSrcweir [in] a mail message that should be send. 85*cdf0e10cSrcweir */ 86*cdf0e10cSrcweir void enqueueMailMessage(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> xMailMessage); 87*cdf0e10cSrcweir /** 88*cdf0e10cSrcweir Dequeues a mail message. 89*cdf0e10cSrcweir This enables the caller to remove attachments when sending mails is to be cancelled. 90*cdf0e10cSrcweir */ 91*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> dequeueMailMessage(); 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir /** 94*cdf0e10cSrcweir Start sending mail messages asynchronously. A client may register 95*cdf0e10cSrcweir a listener for mail dispatcher events. For every mail message sent 96*cdf0e10cSrcweir the notification will be sent. While handling such notification a 97*cdf0e10cSrcweir client may enqueue new mail messages. If there are no more mail 98*cdf0e10cSrcweir messages to send an respective notification is sent and the mail 99*cdf0e10cSrcweir dispatcher waits for more mail messages. 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir @precond not isStarted() 102*cdf0e10cSrcweir */ 103*cdf0e10cSrcweir void start(); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir /** 106*cdf0e10cSrcweir Stop sending mail messages. 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir @precond isStarted() 109*cdf0e10cSrcweir */ 110*cdf0e10cSrcweir void stop(); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir /** 113*cdf0e10cSrcweir Request shutdown of the mail dispatcher thread. 114*cdf0e10cSrcweir NOTE: You must call this method before you release 115*cdf0e10cSrcweir your last reference to this class otherwise the 116*cdf0e10cSrcweir mail dispatcher thread will never end. 117*cdf0e10cSrcweir */ 118*cdf0e10cSrcweir void shutdown(); 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir /** 121*cdf0e10cSrcweir Check whether the mail dispatcher is started or not. 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir @return 124*cdf0e10cSrcweir <TRUE/> if the sending thread is running. 125*cdf0e10cSrcweir */ 126*cdf0e10cSrcweir bool isStarted() const; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir /** returns if the thread is still running 129*cdf0e10cSrcweir */ 130*cdf0e10cSrcweir using osl::Thread::isRunning; 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir /** returns if shutdown has already been called 133*cdf0e10cSrcweir */ 134*cdf0e10cSrcweir bool isShutdownRequested() const 135*cdf0e10cSrcweir { return shutdown_requested_; } 136*cdf0e10cSrcweir /** 137*cdf0e10cSrcweir Register a listener for mail dispatcher events. 138*cdf0e10cSrcweir */ 139*cdf0e10cSrcweir void addListener(::rtl::Reference<IMailDispatcherListener> listener); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir /** 142*cdf0e10cSrcweir Unregister a listener for mail dispatcher events 143*cdf0e10cSrcweir */ 144*cdf0e10cSrcweir void removeListener(::rtl::Reference<IMailDispatcherListener> listener); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir protected: 147*cdf0e10cSrcweir virtual void SAL_CALL run(); 148*cdf0e10cSrcweir virtual void SAL_CALL onTerminated(); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir private: 151*cdf0e10cSrcweir std::list< ::rtl::Reference<IMailDispatcherListener> > cloneListener(); 152*cdf0e10cSrcweir void sendMailMessageNotifyListener(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> message); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir private: 155*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> mailserver_; 156*cdf0e10cSrcweir ::std::list< ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage > > messages_; 157*cdf0e10cSrcweir ::std::list< ::rtl::Reference<IMailDispatcherListener> > listeners_; 158*cdf0e10cSrcweir ::osl::Mutex message_container_mutex_; 159*cdf0e10cSrcweir ::osl::Mutex listener_container_mutex_; 160*cdf0e10cSrcweir ::osl::Mutex thread_status_mutex_; 161*cdf0e10cSrcweir ::osl::Condition mail_dispatcher_active_; 162*cdf0e10cSrcweir ::osl::Condition wakening_call_; 163*cdf0e10cSrcweir ::rtl::Reference<MailDispatcher> m_xSelfReference; 164*cdf0e10cSrcweir bool run_; 165*cdf0e10cSrcweir bool shutdown_requested_; 166*cdf0e10cSrcweir }; 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir #endif // INCLUDED_MAILDISPATCHER_HXX 169