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_SW_SOURCE_UIBASE_INC_MAILDISPATCHER_HXX
21 #define INCLUDED_SW_SOURCE_UIBASE_INC_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 <rtl/ref.hxx>
28 #include <salhelper/simplereferenceobject.hxx>
35 class IMailDispatcherListener
;
38 A MailDispatcher should be used for sending a bunch a mail messages
39 asynchronously. Usually a client enqueues a number of mail messages
40 and then calls start to begin sending them. An instance of this class
41 must not be shared among different client threads. Instead each client
42 thread should create an own instance of this class.
44 class SW_DLLPUBLIC MailDispatcher final
: public salhelper::SimpleReferenceObject
,
48 // bringing operator new/delete into scope
49 using osl::Thread::operator new;
50 using osl::Thread::operator delete;
51 using osl::Thread::join
;
55 [in] a reference to a mail server. A user must be
56 connected to the mail server otherwise errors occur
57 during the delivery of mail messages.
59 @throws css::uno::RuntimeException
60 on errors during construction of an instance of this class.
62 MailDispatcher(css::uno::Reference
<css::mail::XSmtpService
> xMailService
);
65 Shutdown the mail dispatcher. Every mail messages
66 not yet sent will be discarded.
68 virtual ~MailDispatcher() override
;
71 Enqueue a mail message for delivery. A client must
72 start the mail dispatcher in order to send the
73 enqueued mail messages.
76 [in] a mail message that should be send.
78 void enqueueMailMessage(css::uno::Reference
<css::mail::XMailMessage
> const& xMailMessage
);
80 Dequeues a mail message.
81 This enables the caller to remove attachments when sending mails is to be cancelled.
83 css::uno::Reference
<css::mail::XMailMessage
> dequeueMailMessage();
86 Start sending mail messages asynchronously. A client may register
87 a listener for mail dispatcher events. For every mail message sent
88 the notification will be sent. While handling such notification a
89 client may enqueue new mail messages. If there are no more mail
90 messages to send a respective notification is sent and the mail
91 dispatcher waits for more mail messages.
93 @precond not isStarted()
98 Stop sending mail messages.
105 Request shutdown of the mail dispatcher thread.
106 NOTE: You must call this method before you release
107 your last reference to this class otherwise the
108 mail dispatcher thread will never end.
113 Check whether the mail dispatcher is started or not.
116 <TRUE/> if the sending thread is running.
118 bool isStarted() const { return m_bActive
; }
120 /** returns if the thread is still running
122 using osl::Thread::isRunning
;
125 * returns if shutdown has already been called
127 bool isShutdownRequested() const { return m_bShutdownRequested
; }
130 * Register a listener for mail dispatcher events
132 void addListener(::rtl::Reference
<IMailDispatcherListener
> const& listener
);
135 virtual void SAL_CALL
run() override
;
136 virtual void SAL_CALL
onTerminated() override
;
138 std::vector
<::rtl::Reference
<IMailDispatcherListener
>> cloneListener();
139 void sendMailMessageNotifyListener(css::uno::Reference
<css::mail::XMailMessage
> const& message
);
141 css::uno::Reference
<css::mail::XSmtpService
> m_xMailserver
;
142 std::list
<css::uno::Reference
<css::mail::XMailMessage
>> m_aXMessageList
;
143 std::vector
<::rtl::Reference
<IMailDispatcherListener
>> m_aListenerVector
;
144 ::osl::Mutex m_aMessageContainerMutex
;
145 ::osl::Mutex m_aListenerContainerMutex
;
146 ::osl::Mutex m_aThreadStatusMutex
;
147 ::osl::Condition m_aRunCondition
;
148 ::osl::Condition m_aWakeupCondition
;
149 ::rtl::Reference
<MailDispatcher
> m_xSelfReference
;
151 bool m_bShutdownRequested
;
154 #endif // INCLUDED_SW_SOURCE_UIBASE_INC_MAILDISPATCHER_HXX
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */