Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_MAILDISPATCHER_HXX
21 : #define INCLUDED_MAILDISPATCHER_HXX
22 :
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>
28 :
29 : #include <list>
30 :
31 : class IMailDispatcherListener;
32 :
33 : /**
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.
39 : */
40 : class MailDispatcher : public ::salhelper::ReferenceObject, private ::osl::Thread
41 : {
42 : public:
43 : // bringing operator new/delete into scope
44 : using osl::Thread::operator new;
45 : using osl::Thread::operator delete;
46 : using osl::Thread::join;
47 :
48 : public:
49 :
50 : /**
51 : @param xSmtpService
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.
55 :
56 : @throws ::com::sun::star::uno::RuntimeException
57 : on errors during construction of an instance of this class.
58 : */
59 : MailDispatcher(::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> xMailService);
60 :
61 : /**
62 : Shutdown the mail dispatcher. Every mail messages
63 : not yet sent will be discarded.
64 : */
65 : virtual ~MailDispatcher();
66 :
67 : /**
68 : Enqueue a mail message for delivery. A client must
69 : start the mail dispatcher in order to send the
70 : enqueued mail messages.
71 :
72 : @param xMailMessage
73 : [in] a mail message that should be send.
74 : */
75 : void enqueueMailMessage(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> xMailMessage);
76 : /**
77 : Dequeues a mail message.
78 : This enables the caller to remove attachments when sending mails is to be cancelled.
79 : */
80 : ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> dequeueMailMessage();
81 :
82 : /**
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.
89 :
90 : @precond not isStarted()
91 : */
92 : void start();
93 :
94 : /**
95 : Stop sending mail messages.
96 :
97 : @precond isStarted()
98 : */
99 : void stop();
100 :
101 : /**
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.
106 : */
107 : void shutdown();
108 :
109 : /**
110 : Check whether the mail dispatcher is started or not.
111 :
112 : @return
113 : <TRUE/> if the sending thread is running.
114 : */
115 : bool isStarted() const;
116 :
117 : /** returns if the thread is still running
118 : */
119 : using osl::Thread::isRunning;
120 :
121 : /** returns if shutdown has already been called
122 : */
123 0 : bool isShutdownRequested() const
124 0 : { return shutdown_requested_; }
125 : /**
126 : Register a listener for mail dispatcher events.
127 : */
128 : void addListener(::rtl::Reference<IMailDispatcherListener> listener);
129 :
130 : protected:
131 : virtual void SAL_CALL run();
132 : virtual void SAL_CALL onTerminated();
133 :
134 : private:
135 : std::list< ::rtl::Reference<IMailDispatcherListener> > cloneListener();
136 : void sendMailMessageNotifyListener(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> message);
137 :
138 : private:
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;
148 : bool run_;
149 : bool shutdown_requested_;
150 : };
151 :
152 : #endif // INCLUDED_MAILDISPATCHER_HXX
153 :
154 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|