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