1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_MAILDISPATCHER_HXX
21 #define INCLUDED_MAILDISPATCHER_HXX
23 #include "com/sun/star/mail/XSmtpService.hpp"
24 #include "com/sun/star/mail/XMailMessage.hpp"
25 #include <osl/thread.hxx>
26 #include <osl/conditn.hxx>
27 #include <salhelper/refobj.hxx>
31 class IMailDispatcherListener
;
34 A MailDispatcher should be used for sending a bunch a mail messages
35 asynchronously. Usually a client enqueues a number of mail messages
36 and then calls start to begin sending them. An instance of this class
37 must not be shared among different client threads. Instead each client
38 thread should create an own instance of this class.
40 class MailDispatcher
: public ::salhelper::ReferenceObject
, private ::osl::Thread
43 // bringing operator new/delete into scope
44 using osl::Thread::operator new;
45 using osl::Thread::operator delete;
46 using osl::Thread::join
;
52 [in] a reference to a mail server. A user must be
53 connected to the mail server otherwise errors occur
54 during the delivery of mail messages.
56 @throws ::com::sun::star::uno::RuntimeException
57 on errors during construction of an instance of this class.
59 MailDispatcher(::com::sun::star::uno::Reference
< ::com::sun::star::mail::XSmtpService
> xMailService
);
62 Shutdown the mail dispatcher. Every mail messages
63 not yet sent will be discarded.
65 virtual ~MailDispatcher();
68 Enqueue a mail message for delivery. A client must
69 start the mail dispatcher in order to send the
70 enqueued mail messages.
73 [in] a mail message that should be send.
75 void enqueueMailMessage(::com::sun::star::uno::Reference
< ::com::sun::star::mail::XMailMessage
> xMailMessage
);
77 Dequeues a mail message.
78 This enables the caller to remove attachments when sending mails is to be cancelled.
80 ::com::sun::star::uno::Reference
< ::com::sun::star::mail::XMailMessage
> dequeueMailMessage();
83 Start sending mail messages asynchronously. A client may register
84 a listener for mail dispatcher events. For every mail message sent
85 the notification will be sent. While handling such notification a
86 client may enqueue new mail messages. If there are no more mail
87 messages to send an respective notification is sent and the mail
88 dispatcher waits for more mail messages.
90 @precond not isStarted()
95 Stop sending mail messages.
102 Request shutdown of the mail dispatcher thread.
103 NOTE: You must call this method before you release
104 your last reference to this class otherwise the
105 mail dispatcher thread will never end.
110 Check whether the mail dispatcher is started or not.
113 <TRUE/> if the sending thread is running.
115 bool isStarted() const;
117 /** returns if the thread is still running
119 using osl::Thread::isRunning
;
121 /** returns if shutdown has already been called
123 bool isShutdownRequested() const
124 { return shutdown_requested_
; }
126 Register a listener for mail dispatcher events.
128 void addListener(::rtl::Reference
<IMailDispatcherListener
> listener
);
131 virtual void SAL_CALL
run();
132 virtual void SAL_CALL
onTerminated();
135 std::list
< ::rtl::Reference
<IMailDispatcherListener
> > cloneListener();
136 void sendMailMessageNotifyListener(::com::sun::star::uno::Reference
< ::com::sun::star::mail::XMailMessage
> message
);
139 ::com::sun::star::uno::Reference
< ::com::sun::star::mail::XSmtpService
> mailserver_
;
140 ::std::list
< ::com::sun::star::uno::Reference
< ::com::sun::star::mail::XMailMessage
> > messages_
;
141 ::std::list
< ::rtl::Reference
<IMailDispatcherListener
> > listeners_
;
142 ::osl::Mutex message_container_mutex_
;
143 ::osl::Mutex listener_container_mutex_
;
144 ::osl::Mutex thread_status_mutex_
;
145 ::osl::Condition mail_dispatcher_active_
;
146 ::osl::Condition wakening_call_
;
147 ::rtl::Reference
<MailDispatcher
> m_xSelfReference
;
149 bool shutdown_requested_
;
152 #endif // INCLUDED_MAILDISPATCHER_HXX
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */