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