Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/sw/source/uibase/inc/maildispatcher.hxx
Line
Count
Source (jump to first uncovered line)
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
#include <vector>
32
33
#include <swdllapi.h>
34
35
class IMailDispatcherListener;
36
37
/**
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.
43
*/
44
class SW_DLLPUBLIC MailDispatcher final : 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
    /**
54
        @param xSmtpService
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.
58
59
        @throws css::uno::RuntimeException
60
        on errors during construction of an instance of this class.
61
    */
62
    MailDispatcher(css::uno::Reference<css::mail::XSmtpService> xMailService);
63
64
    /**
65
        Shutdown the mail dispatcher. Every mail messages
66
        not yet sent will be discarded.
67
    */
68
    virtual ~MailDispatcher() override;
69
70
    /**
71
        Enqueue a mail message for delivery. A client must
72
        start the mail dispatcher in order to send the
73
        enqueued mail messages.
74
75
        @param xMailMessage
76
        [in] a mail message that should be send.
77
    */
78
    void enqueueMailMessage(css::uno::Reference<css::mail::XMailMessage> const& xMailMessage);
79
    /**
80
        Dequeues a mail message.
81
        This enables the caller to remove attachments when sending mails is to be cancelled.
82
    */
83
    css::uno::Reference<css::mail::XMailMessage> dequeueMailMessage();
84
85
    /**
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.
92
93
        @precond not isStarted()
94
    */
95
    void start();
96
97
    /**
98
        Stop sending mail messages.
99
100
        @precond isStarted()
101
    */
102
    void stop();
103
104
    /**
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.
109
    */
110
    void shutdown();
111
112
    /**
113
        Check whether the mail dispatcher is started or not.
114
115
        @return
116
        <TRUE/> if the sending thread is running.
117
    */
118
0
    bool isStarted() const { return m_bActive; }
119
120
    /** returns if the thread is still running
121
    */
122
    using osl::Thread::isRunning;
123
124
    /**
125
     * returns if shutdown has already been called
126
     */
127
0
    bool isShutdownRequested() const { return m_bShutdownRequested; }
128
129
    /**
130
     * Register a listener for mail dispatcher events
131
     */
132
    void addListener(::rtl::Reference<IMailDispatcherListener> const& listener);
133
134
private:
135
    virtual void SAL_CALL run() override;
136
    virtual void SAL_CALL onTerminated() override;
137
138
    std::vector<::rtl::Reference<IMailDispatcherListener>> cloneListener();
139
    void sendMailMessageNotifyListener(css::uno::Reference<css::mail::XMailMessage> const& message);
140
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;
150
    bool m_bActive;
151
    bool m_bShutdownRequested;
152
};
153
154
#endif // INCLUDED_SW_SOURCE_UIBASE_INC_MAILDISPATCHER_HXX
155
156
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */