Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/source/uibase/dbui/maildispatcher.cxx
Line
Count
Source
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
#include <maildispatcher.hxx>
21
#include <imaildsplistener.hxx>
22
23
#include <algorithm>
24
25
#include <com/sun/star/mail/MailException.hpp>
26
#include <utility>
27
#include <osl/diagnose.h>
28
29
using namespace ::com::sun::star;
30
31
typedef std::vector< ::rtl::Reference<IMailDispatcherListener> > MailDispatcherListenerContainer_t;
32
33
namespace /* private */
34
{
35
    class MailDeliveryNotifier
36
    {
37
    public:
38
        MailDeliveryNotifier(uno::Reference<mail::XMailMessage> message) :
39
0
            message_(std::move(message))
40
0
        {}
41
42
        void operator() (::rtl::Reference<IMailDispatcherListener> const & listener) const
43
0
        { listener->mailDelivered(message_); }
44
45
    private:
46
        uno::Reference<mail::XMailMessage> message_;
47
    };
48
49
    class MailDeliveryErrorNotifier
50
    {
51
    public:
52
        MailDeliveryErrorNotifier(
53
            ::rtl::Reference<MailDispatcher> xMailDispatcher,
54
            uno::Reference<mail::XMailMessage> message,
55
            OUString error_message) :
56
0
            m_mail_dispatcher(std::move(xMailDispatcher)),
57
0
            m_message(std::move(message)),
58
0
            m_error_message(std::move(error_message))
59
0
        {}
60
61
        void operator() (::rtl::Reference<IMailDispatcherListener> const & listener) const
62
0
        { listener->mailDeliveryError(m_mail_dispatcher, m_message, m_error_message); }
63
64
    private:
65
        ::rtl::Reference<MailDispatcher> m_mail_dispatcher;
66
        uno::Reference<mail::XMailMessage> m_message;
67
        OUString m_error_message;
68
    };
69
70
} // namespace private
71
72
MailDispatcher::MailDispatcher(uno::Reference<mail::XSmtpService> mailserver) :
73
0
    m_xMailserver(std::move( mailserver )),
74
0
    m_bActive( false ),
75
0
    m_bShutdownRequested( false )
76
0
{
77
0
    m_aWakeupCondition.reset();
78
0
    m_aRunCondition.reset();
79
80
0
    if (!create())
81
0
        throw uno::RuntimeException();
82
83
    // wait until the mail dispatcher thread is really alive
84
    // and has acquired a reference to this instance of the
85
    // class
86
0
    m_aRunCondition.wait();
87
0
}
88
89
MailDispatcher::~MailDispatcher()
90
0
{
91
0
}
92
93
void MailDispatcher::enqueueMailMessage(uno::Reference<mail::XMailMessage> const & message)
94
0
{
95
0
    ::osl::MutexGuard thread_status_guard( m_aThreadStatusMutex );
96
0
    ::osl::MutexGuard message_container_guard( m_aMessageContainerMutex );
97
98
0
    OSL_PRECOND( !m_bShutdownRequested, "MailDispatcher thread is shutting down already" );
99
100
0
    m_aXMessageList.push_back( message );
101
0
    if ( m_bActive )
102
0
        m_aWakeupCondition.set();
103
0
}
104
105
uno::Reference<mail::XMailMessage> MailDispatcher::dequeueMailMessage()
106
0
{
107
0
    ::osl::MutexGuard guard( m_aMessageContainerMutex );
108
0
    uno::Reference<mail::XMailMessage> message;
109
0
    if ( !m_aXMessageList.empty() )
110
0
    {
111
0
        message = m_aXMessageList.front();
112
0
        m_aXMessageList.pop_front();
113
0
    }
114
0
    return message;
115
0
}
116
117
void MailDispatcher::start()
118
0
{
119
0
    OSL_PRECOND(!isStarted(), "MailDispatcher is already started!");
120
121
0
    ::osl::ClearableMutexGuard thread_status_guard( m_aThreadStatusMutex );
122
123
0
    OSL_PRECOND(!m_bShutdownRequested, "MailDispatcher thread is shutting down already");
124
125
0
    if ( !m_bShutdownRequested )
126
0
    {
127
0
        m_bActive = true;
128
0
        m_aWakeupCondition.set();
129
0
        thread_status_guard.clear();
130
0
    }
131
0
}
132
133
void MailDispatcher::stop()
134
0
{
135
0
    OSL_PRECOND(isStarted(), "MailDispatcher not started!");
136
137
0
    ::osl::ClearableMutexGuard thread_status_guard( m_aThreadStatusMutex );
138
139
0
    OSL_PRECOND(!m_bShutdownRequested, "MailDispatcher thread is shutting down already");
140
141
0
    if (!m_bShutdownRequested)
142
0
    {
143
0
        m_bActive = false;
144
0
        m_aWakeupCondition.reset();
145
0
        thread_status_guard.clear();
146
0
    }
147
0
}
148
149
void MailDispatcher::shutdown()
150
0
{
151
0
    ::osl::MutexGuard thread_status_guard( m_aThreadStatusMutex );
152
153
0
    OSL_PRECOND(!m_bShutdownRequested, "MailDispatcher thread is shutting down already");
154
155
0
    m_bShutdownRequested = true;
156
0
    m_aWakeupCondition.set();
157
0
}
158
159
160
void MailDispatcher::addListener(::rtl::Reference<IMailDispatcherListener> const & listener)
161
0
{
162
0
    OSL_PRECOND(!m_bShutdownRequested, "MailDispatcher thread is shutting down already");
163
164
0
    ::osl::MutexGuard guard( m_aListenerContainerMutex );
165
0
    m_aListenerVector.push_back( listener );
166
0
}
167
168
std::vector< ::rtl::Reference<IMailDispatcherListener> > MailDispatcher::cloneListener()
169
0
{
170
0
    ::osl::MutexGuard guard( m_aListenerContainerMutex );
171
0
    return m_aListenerVector;
172
0
}
173
174
void MailDispatcher::sendMailMessageNotifyListener(uno::Reference<mail::XMailMessage> const & message)
175
0
{
176
0
    try
177
0
    {
178
0
        m_xMailserver->sendMailMessage( message );
179
0
        MailDispatcherListenerContainer_t aClonedListenerVector(cloneListener());
180
0
        std::for_each( aClonedListenerVector.begin(), aClonedListenerVector.end(),
181
0
                       MailDeliveryNotifier(message) );
182
0
    }
183
0
    catch (const mail::MailException& ex)
184
0
    {
185
0
        MailDispatcherListenerContainer_t aClonedListenerVector(cloneListener());
186
0
        std::for_each( aClonedListenerVector.begin(), aClonedListenerVector.end(),
187
0
                       MailDeliveryErrorNotifier(this, message, ex.Message) );
188
0
    }
189
0
    catch (const uno::RuntimeException& ex)
190
0
    {
191
0
        MailDispatcherListenerContainer_t aClonedListenerVector(cloneListener());
192
0
        std::for_each( aClonedListenerVector.begin(), aClonedListenerVector.end(),
193
0
                       MailDeliveryErrorNotifier(this, message, ex.Message) );
194
0
    }
195
0
}
196
197
void MailDispatcher::run()
198
0
{
199
0
    osl_setThreadName("MailDispatcher");
200
201
    // acquire a self reference in order to avoid race
202
    // conditions. The last client of this class must
203
    // call shutdown before releasing his last reference
204
    // to this class in order to shutdown this thread
205
    // which will release his (the very last reference
206
    // to the class and so force their destruction
207
0
    m_xSelfReference = this;
208
209
    // signal that the mail dispatcher thread is now alive
210
0
    m_aRunCondition.set();
211
212
0
    for(;;)
213
0
    {
214
0
        m_aWakeupCondition.wait();
215
216
0
        ::osl::ClearableMutexGuard thread_status_guard( m_aThreadStatusMutex );
217
0
        if ( m_bShutdownRequested )
218
0
            break;
219
220
0
        ::osl::ClearableMutexGuard message_container_guard( m_aMessageContainerMutex );
221
222
0
        if ( !m_aXMessageList.empty() )
223
0
        {
224
0
            thread_status_guard.clear();
225
0
            uno::Reference<mail::XMailMessage> message = m_aXMessageList.front();
226
0
            m_aXMessageList.pop_front();
227
0
            message_container_guard.clear();
228
0
            sendMailMessageNotifyListener( message );
229
0
        }
230
0
        else // idle - put ourself to sleep
231
0
        {
232
0
            m_aWakeupCondition.reset();
233
0
            message_container_guard.clear();
234
0
            thread_status_guard.clear();
235
0
            MailDispatcherListenerContainer_t aListenerListcloned( cloneListener() );
236
0
            for( const auto & l : aListenerListcloned)
237
0
                l->idle();
238
0
        }
239
0
    }
240
0
}
241
242
void MailDispatcher::onTerminated()
243
0
{
244
    //keep the reference until the end of onTerminated() because of the call order in the
245
    //_threadFunc() from osl/thread.hxx
246
0
    m_xSelfReference = nullptr;
247
0
}
248
249
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */