1*1d2dbeb0SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*1d2dbeb0SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*1d2dbeb0SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*1d2dbeb0SAndrew Rist * distributed with this work for additional information 6*1d2dbeb0SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*1d2dbeb0SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*1d2dbeb0SAndrew Rist * "License"); you may not use this file except in compliance 9*1d2dbeb0SAndrew Rist * with the License. You may obtain a copy of the License at 10*1d2dbeb0SAndrew Rist * 11*1d2dbeb0SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*1d2dbeb0SAndrew Rist * 13*1d2dbeb0SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*1d2dbeb0SAndrew Rist * software distributed under the License is distributed on an 15*1d2dbeb0SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*1d2dbeb0SAndrew Rist * KIND, either express or implied. See the License for the 17*1d2dbeb0SAndrew Rist * specific language governing permissions and limitations 18*1d2dbeb0SAndrew Rist * under the License. 19*1d2dbeb0SAndrew Rist * 20*1d2dbeb0SAndrew Rist *************************************************************/ 21*1d2dbeb0SAndrew Rist 22*1d2dbeb0SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef INCLUDED_MAILDISPATCHER_HXX 25cdf0e10cSrcweir #define INCLUDED_MAILDISPATCHER_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir //#ifndef _COM_SUN_STAR_MAIL_XMAILSERVER_HPP_ 28cdf0e10cSrcweir //#include "com/sun/star/mail/XMailServer.hpp" 29cdf0e10cSrcweir //#endif 30cdf0e10cSrcweir #include "com/sun/star/mail/XSmtpService.hpp" 31cdf0e10cSrcweir #include "com/sun/star/mail/XMailMessage.hpp" 32cdf0e10cSrcweir #include <osl/thread.hxx> 33cdf0e10cSrcweir #include <osl/conditn.hxx> 34cdf0e10cSrcweir #include <salhelper/refobj.hxx> 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include <list> 37cdf0e10cSrcweir 38cdf0e10cSrcweir class IMailDispatcherListener; 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** 41cdf0e10cSrcweir A MailDispatcher should be used for sending a bunch a mail messages 42cdf0e10cSrcweir asynchronously. Usually a client enqueues a number of mail messages 43cdf0e10cSrcweir and then calls start to begin sending them. An instance of this class 44cdf0e10cSrcweir must not be shared among different client threads. Instead each client 45cdf0e10cSrcweir thread should create an own instance of this class. 46cdf0e10cSrcweir */ 47cdf0e10cSrcweir class MailDispatcher : public ::salhelper::ReferenceObject, private ::osl::Thread 48cdf0e10cSrcweir { 49cdf0e10cSrcweir public: 50cdf0e10cSrcweir // bringing operator new/delete into scope 51cdf0e10cSrcweir using osl::Thread::operator new; 52cdf0e10cSrcweir using osl::Thread::operator delete; 53cdf0e10cSrcweir using osl::Thread::join; 54cdf0e10cSrcweir 55cdf0e10cSrcweir public: 56cdf0e10cSrcweir 57cdf0e10cSrcweir /** 58cdf0e10cSrcweir @param xSmtpService 59cdf0e10cSrcweir [in] a reference to a mail server. A user must be 60cdf0e10cSrcweir connected to the mail server otherwise errors occur 61cdf0e10cSrcweir during the delivery of mail messages. 62cdf0e10cSrcweir 63cdf0e10cSrcweir @throws ::com::sun::star::uno::RuntimeException 64cdf0e10cSrcweir on errors during construction of an instance of this class. 65cdf0e10cSrcweir */ 66cdf0e10cSrcweir MailDispatcher(::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> xMailService); 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** 69cdf0e10cSrcweir Shutdown the mail dispatcher. Every mail messages 70cdf0e10cSrcweir not yet sent will be discarded. 71cdf0e10cSrcweir */ 72cdf0e10cSrcweir virtual ~MailDispatcher(); 73cdf0e10cSrcweir 74cdf0e10cSrcweir /** 75cdf0e10cSrcweir Enqueue a mail message for delivery. A client must 76cdf0e10cSrcweir start the mail dispatcher in order to send the 77cdf0e10cSrcweir enqueued mail messages. 78cdf0e10cSrcweir 79cdf0e10cSrcweir @param xMailMessage 80cdf0e10cSrcweir [in] a mail message that should be send. 81cdf0e10cSrcweir */ 82cdf0e10cSrcweir void enqueueMailMessage(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> xMailMessage); 83cdf0e10cSrcweir /** 84cdf0e10cSrcweir Dequeues a mail message. 85cdf0e10cSrcweir This enables the caller to remove attachments when sending mails is to be cancelled. 86cdf0e10cSrcweir */ 87cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> dequeueMailMessage(); 88cdf0e10cSrcweir 89cdf0e10cSrcweir /** 90cdf0e10cSrcweir Start sending mail messages asynchronously. A client may register 91cdf0e10cSrcweir a listener for mail dispatcher events. For every mail message sent 92cdf0e10cSrcweir the notification will be sent. While handling such notification a 93cdf0e10cSrcweir client may enqueue new mail messages. If there are no more mail 94cdf0e10cSrcweir messages to send an respective notification is sent and the mail 95cdf0e10cSrcweir dispatcher waits for more mail messages. 96cdf0e10cSrcweir 97cdf0e10cSrcweir @precond not isStarted() 98cdf0e10cSrcweir */ 99cdf0e10cSrcweir void start(); 100cdf0e10cSrcweir 101cdf0e10cSrcweir /** 102cdf0e10cSrcweir Stop sending mail messages. 103cdf0e10cSrcweir 104cdf0e10cSrcweir @precond isStarted() 105cdf0e10cSrcweir */ 106cdf0e10cSrcweir void stop(); 107cdf0e10cSrcweir 108cdf0e10cSrcweir /** 109cdf0e10cSrcweir Request shutdown of the mail dispatcher thread. 110cdf0e10cSrcweir NOTE: You must call this method before you release 111cdf0e10cSrcweir your last reference to this class otherwise the 112cdf0e10cSrcweir mail dispatcher thread will never end. 113cdf0e10cSrcweir */ 114cdf0e10cSrcweir void shutdown(); 115cdf0e10cSrcweir 116cdf0e10cSrcweir /** 117cdf0e10cSrcweir Check whether the mail dispatcher is started or not. 118cdf0e10cSrcweir 119cdf0e10cSrcweir @return 120cdf0e10cSrcweir <TRUE/> if the sending thread is running. 121cdf0e10cSrcweir */ 122cdf0e10cSrcweir bool isStarted() const; 123cdf0e10cSrcweir 124cdf0e10cSrcweir /** returns if the thread is still running 125cdf0e10cSrcweir */ 126cdf0e10cSrcweir using osl::Thread::isRunning; 127cdf0e10cSrcweir 128cdf0e10cSrcweir /** returns if shutdown has already been called 129cdf0e10cSrcweir */ isShutdownRequested() const130cdf0e10cSrcweir bool isShutdownRequested() const 131cdf0e10cSrcweir { return shutdown_requested_; } 132cdf0e10cSrcweir /** 133cdf0e10cSrcweir Register a listener for mail dispatcher events. 134cdf0e10cSrcweir */ 135cdf0e10cSrcweir void addListener(::rtl::Reference<IMailDispatcherListener> listener); 136cdf0e10cSrcweir 137cdf0e10cSrcweir /** 138cdf0e10cSrcweir Unregister a listener for mail dispatcher events 139cdf0e10cSrcweir */ 140cdf0e10cSrcweir void removeListener(::rtl::Reference<IMailDispatcherListener> listener); 141cdf0e10cSrcweir 142cdf0e10cSrcweir protected: 143cdf0e10cSrcweir virtual void SAL_CALL run(); 144cdf0e10cSrcweir virtual void SAL_CALL onTerminated(); 145cdf0e10cSrcweir 146cdf0e10cSrcweir private: 147cdf0e10cSrcweir std::list< ::rtl::Reference<IMailDispatcherListener> > cloneListener(); 148cdf0e10cSrcweir void sendMailMessageNotifyListener(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> message); 149cdf0e10cSrcweir 150cdf0e10cSrcweir private: 151cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> mailserver_; 152cdf0e10cSrcweir ::std::list< ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage > > messages_; 153cdf0e10cSrcweir ::std::list< ::rtl::Reference<IMailDispatcherListener> > listeners_; 154cdf0e10cSrcweir ::osl::Mutex message_container_mutex_; 155cdf0e10cSrcweir ::osl::Mutex listener_container_mutex_; 156cdf0e10cSrcweir ::osl::Mutex thread_status_mutex_; 157cdf0e10cSrcweir ::osl::Condition mail_dispatcher_active_; 158cdf0e10cSrcweir ::osl::Condition wakening_call_; 159cdf0e10cSrcweir ::rtl::Reference<MailDispatcher> m_xSelfReference; 160cdf0e10cSrcweir bool run_; 161cdf0e10cSrcweir bool shutdown_requested_; 162cdf0e10cSrcweir }; 163cdf0e10cSrcweir 164cdf0e10cSrcweir #endif // INCLUDED_MAILDISPATCHER_HXX 165