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_SW_SOURCE_UIBASE_INC_MAILDISPATCHER_HXX
21 : #define INCLUDED_SW_SOURCE_UIBASE_INC_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 <rtl/ref.hxx>
28 : #include <salhelper/simplereferenceobject.hxx>
29 :
30 : #include <list>
31 :
32 : #include <swdllapi.h>
33 :
34 : class IMailDispatcherListener;
35 :
36 : /**
37 : A MailDispatcher should be used for sending a bunch a mail messages
38 : asynchronously. Usually a client enqueues a number of mail messages
39 : and then calls start to begin sending them. An instance of this class
40 : must not be shared among different client threads. Instead each client
41 : thread should create an own instance of this class.
42 : */
43 : class SW_DLLPUBLIC MailDispatcher
44 : : public salhelper::SimpleReferenceObject
45 : , private ::osl::Thread
46 : {
47 : public:
48 : // bringing operator new/delete into scope
49 : using osl::Thread::operator new;
50 : using osl::Thread::operator delete;
51 : using osl::Thread::join;
52 :
53 : public:
54 :
55 : /**
56 : @param xSmtpService
57 : [in] a reference to a mail server. A user must be
58 : connected to the mail server otherwise errors occur
59 : during the delivery of mail messages.
60 :
61 : @throws ::com::sun::star::uno::RuntimeException
62 : on errors during construction of an instance of this class.
63 : */
64 : MailDispatcher(::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> xMailService);
65 :
66 : /**
67 : Shutdown the mail dispatcher. Every mail messages
68 : not yet sent will be discarded.
69 : */
70 : virtual ~MailDispatcher();
71 :
72 : /**
73 : Enqueue a mail message for delivery. A client must
74 : start the mail dispatcher in order to send the
75 : enqueued mail messages.
76 :
77 : @param xMailMessage
78 : [in] a mail message that should be send.
79 : */
80 : void enqueueMailMessage(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> xMailMessage);
81 : /**
82 : Dequeues a mail message.
83 : This enables the caller to remove attachments when sending mails is to be cancelled.
84 : */
85 : ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> dequeueMailMessage();
86 :
87 : /**
88 : Start sending mail messages asynchronously. A client may register
89 : a listener for mail dispatcher events. For every mail message sent
90 : the notification will be sent. While handling such notification a
91 : client may enqueue new mail messages. If there are no more mail
92 : messages to send an respective notification is sent and the mail
93 : dispatcher waits for more mail messages.
94 :
95 : @precond not isStarted()
96 : */
97 : void start();
98 :
99 : /**
100 : Stop sending mail messages.
101 :
102 : @precond isStarted()
103 : */
104 : void stop();
105 :
106 : /**
107 : Request shutdown of the mail dispatcher thread.
108 : NOTE: You must call this method before you release
109 : your last reference to this class otherwise the
110 : mail dispatcher thread will never end.
111 : */
112 : void shutdown();
113 :
114 : /**
115 : Check whether the mail dispatcher is started or not.
116 :
117 : @return
118 : <TRUE/> if the sending thread is running.
119 : */
120 0 : bool isStarted() const { return run_;}
121 :
122 : /** returns if the thread is still running
123 : */
124 : using osl::Thread::isRunning;
125 :
126 : /** returns if shutdown has already been called
127 : */
128 0 : bool isShutdownRequested() const
129 0 : { return shutdown_requested_; }
130 : /**
131 : Register a listener for mail dispatcher events.
132 : */
133 : void addListener(::rtl::Reference<IMailDispatcherListener> listener);
134 :
135 : protected:
136 : virtual void SAL_CALL run() SAL_OVERRIDE;
137 : virtual void SAL_CALL onTerminated() SAL_OVERRIDE;
138 :
139 : private:
140 : std::list< ::rtl::Reference<IMailDispatcherListener> > cloneListener();
141 : void sendMailMessageNotifyListener(::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage> message);
142 :
143 : private:
144 : ::com::sun::star::uno::Reference< ::com::sun::star::mail::XSmtpService> mailserver_;
145 : ::std::list< ::com::sun::star::uno::Reference< ::com::sun::star::mail::XMailMessage > > messages_;
146 : ::std::list< ::rtl::Reference<IMailDispatcherListener> > listeners_;
147 : ::osl::Mutex message_container_mutex_;
148 : ::osl::Mutex listener_container_mutex_;
149 : ::osl::Mutex thread_status_mutex_;
150 : ::osl::Condition mail_dispatcher_active_;
151 : ::osl::Condition wakening_call_;
152 : ::rtl::Reference<MailDispatcher> m_xSelfReference;
153 : bool run_;
154 : bool shutdown_requested_;
155 : };
156 :
157 : #endif // INCLUDED_SW_SOURCE_UIBASE_INC_MAILDISPATCHER_HXX
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|