File: itiphandler_p.cpp

package info (click to toggle)
akonadi-calendar 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,808 kB
  • sloc: cpp: 14,079; xml: 36; makefile: 8; sh: 4
file content (256 lines) | stat: -rw-r--r-- 10,709 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
  Copyright (C) 2012 Sérgio Martins <iamsergio@gmail.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "itiphandler_p.h"
#include "fetchjobcalendar.h"
#include <kcalcore/incidence.h>
#include <KMessageBox>
#include <KLocalizedString>
#include "akonadicalendar_debug.h"
using namespace Akonadi;

ITIPHandler::Private::Private(ITIPHandlerComponentFactory *factory, ITIPHandler *qq)
    : m_calendarLoadError(false)
    , m_factory(factory ? factory : new ITIPHandlerComponentFactory(this))
    , m_scheduler(new MailScheduler(m_factory, qq))
    , m_method(KCalCore::iTIPNoMethod)
    , m_helper(new ITIPHandlerHelper(m_factory))
    , m_currentOperation(OperationNone)
    , m_uiDelegate(nullptr)
    , m_showDialogsOnError(true)
    , q(qq)
{
    m_helper->setParent(this);
    connect(m_scheduler, &Scheduler::transactionFinished,
            this, &ITIPHandler::Private::onSchedulerFinished);

    connect(m_helper, &ITIPHandlerHelper::finished,
            this, &ITIPHandler::Private::onHelperFinished);

    connect(m_helper, &ITIPHandlerHelper::sendIncidenceModifiedMessageFinished,
            this, &ITIPHandler::Private::onHelperModifyDialogClosed);
}

void ITIPHandler::Private::onSchedulerFinished(Akonadi::Scheduler::Result result,
        const QString &errorMessage)
{
    if (m_currentOperation == OperationNone) {
        qCritical() << "Operation can't be none!" << result << errorMessage;
        return;
    }

    if (m_currentOperation == OperationProcessiTIPMessage) {
        m_currentOperation = OperationNone;
        finishProcessiTIPMessage(result, errorMessage);
    } else if (m_currentOperation == OperationSendiTIPMessage) {
        m_currentOperation = OperationNone;
        finishSendiTIPMessage(result, errorMessage);
    } else if (m_currentOperation == OperationPublishInformation) {
        m_currentOperation = OperationNone;
        finishPublishInformation(result, errorMessage);
    } else {
        Q_ASSERT(false);
        qCritical() << "Unknown operation" << m_currentOperation;
    }
}

void ITIPHandler::Private::onHelperFinished(Akonadi::ITIPHandlerHelper::SendResult result,
        const QString &errorMessage)
{
    const bool success = result == ITIPHandlerHelper::ResultSuccess;

    if (m_currentOperation == OperationProcessiTIPMessage) {
        MailScheduler::Result result2 = success ? MailScheduler::ResultSuccess : MailScheduler::ResultGenericError;
        finishProcessiTIPMessage(result2, i18n("Error: %1", errorMessage));
    } else {
        emit q->iTipMessageSent(success ? ResultSuccess : ResultError,
                                success ? QString() : i18n("Error: %1", errorMessage));
    }
}

void ITIPHandler::Private::onCounterProposalDelegateFinished(bool success, const QString &errorMessage)
{
    Q_UNUSED(success);
    Q_UNUSED(errorMessage);
    // This will be used when we make editing counter proposals async.
}

void ITIPHandler::Private::onLoadFinished(bool success, const QString &errorMessage)
{
    if (m_currentOperation == OperationProcessiTIPMessage) {
        if (success) {

            // Harmless hack, processiTIPMessage() asserts that there's not current operation running
            // to prevent users from calling it twice.
            m_currentOperation = OperationNone;
            q->processiTIPMessage(m_queuedInvitation.receiver,
                                  m_queuedInvitation.iCal,
                                  m_queuedInvitation.action);
        } else {
            emit q->iTipMessageProcessed(ResultError, i18n("Error loading calendar: %1", errorMessage));
        }
    } else if (m_currentOperation == OperationSendiTIPMessage) {
        q->sendiTIPMessage(m_queuedInvitation.method,
                           m_queuedInvitation.incidence,
                           m_parentWidget);
    } else if (!success) {   //TODO
        m_calendarLoadError = true;
    }
}

void ITIPHandler::Private::finishProcessiTIPMessage(Akonadi::MailScheduler::Result result,
        const QString &errorMessage)
{
    // Handle when user cancels on the collection selection dialog
    if (result == MailScheduler::ResultUserCancelled) {
        emit q->iTipMessageProcessed(ResultCancelled, QString());
        return;
    }

    const bool success = result == MailScheduler::ResultSuccess;

    if (m_method == KCalCore::iTIPCounter) {
        // Here we're processing a counter-proposal that someone sent us and we're the organizer.
        // TODO: Shouldn't there be a test to see if we're the organizer?
        if (success) {
            // send update to all attendees
            Q_ASSERT(m_incidence);
            m_helper->setDialogParent(m_parentWidget);
            m_helper->sendIncidenceModifiedMessage(KCalCore::iTIPRequest,
                                                   KCalCore::Incidence::Ptr(m_incidence->clone()),
                                                   false);
            m_incidence.clear();
            return;
        } else {
            //fall through
        }
    }

    emit q->iTipMessageProcessed(success ? ResultSuccess : ResultError,
                                 success ? QString() : i18n("Error: %1", errorMessage));
}

void ITIPHandler::Private::onHelperModifyDialogClosed(ITIPHandlerHelper::SendResult sendResult, KCalCore::iTIPMethod /*method*/, const KCalCore::Incidence::Ptr &)
{
    if (sendResult == ITIPHandlerHelper::ResultNoSendingNeeded ||
            sendResult == ITIPHandlerHelper::ResultCanceled) {
        emit q->iTipMessageSent(ResultSuccess, QString());
    }
}

void ITIPHandler::Private::finishSendiTIPMessage(Akonadi::MailScheduler::Result result,
        const QString &errorMessage)
{
    if (result == Scheduler::ResultSuccess) {
        if (m_parentWidget) {
            KMessageBox::information(m_parentWidget,
                                     i18n("The groupware message for item '%1' "
                                          "was successfully sent.\nMethod: %2",
                                          m_queuedInvitation.incidence->summary(),
                                          KCalCore::ScheduleMessage::methodName(m_queuedInvitation.method)),
                                     i18n("Sending Free/Busy"),
                                     QStringLiteral("FreeBusyPublishSuccess"));
        }
        emit q->iTipMessageSent(ITIPHandler::ResultSuccess, QString());
    } else {
        const QString error = i18nc("Groupware message sending failed. "
                                    "%2 is request/reply/add/cancel/counter/etc.",
                                    "Unable to send the item '%1'.\nMethod: %2",
                                    m_queuedInvitation.incidence->summary(),
                                    KCalCore::ScheduleMessage::methodName(m_queuedInvitation.method));
        if (m_parentWidget) {
            KMessageBox::error(m_parentWidget, error);
        }
        qCritical() << "Groupware message sending failed." << error << errorMessage;
        emit q->iTipMessageSent(ITIPHandler::ResultError, error + errorMessage);
    }
}

void ITIPHandler::Private::finishPublishInformation(Akonadi::MailScheduler::Result result,
        const QString &errorMessage)
{
    if (result == Scheduler::ResultSuccess) {
        if (m_parentWidget) {
            KMessageBox::information(m_parentWidget,
                                     i18n("The item information was successfully sent."),
                                     i18n("Publishing"),
                                     QStringLiteral("IncidencePublishSuccess"));
        }
        emit q->informationPublished(ITIPHandler::ResultSuccess, QString());
    } else {
        const QString error = i18n("Unable to publish the item '%1'",
                                   m_queuedInvitation.incidence->summary());
        if (m_parentWidget) {
            KMessageBox::error(m_parentWidget, error);
        }
        qCritical() << "Publish failed." << error << errorMessage;
        emit q->informationPublished(ITIPHandler::ResultError, error + errorMessage);
    }
}

void ITIPHandler::Private::finishSendAsICalendar(Akonadi::MailClient::Result result,
        const QString &errorMessage)
{
    if (result == MailClient::ResultSuccess) {
        if (m_parentWidget) {
            KMessageBox::information(m_parentWidget,
                                     i18n("The item information was successfully sent."),
                                     i18n("Forwarding"),
                                     QStringLiteral("IncidenceForwardSuccess"));
        }
        emit q->sentAsICalendar(ITIPHandler::ResultSuccess, QString());
    } else {
        if (m_parentWidget) {
            KMessageBox::error(m_parentWidget,
                               i18n("Unable to forward the item '%1'",
                                    m_queuedInvitation.incidence->summary()),
                               i18n("Forwarding Error"));
        }
        qCritical() << "Sent as iCalendar failed." << errorMessage;
        emit q->sentAsICalendar(ITIPHandler::ResultError, errorMessage);
    }

    sender()->deleteLater(); // Delete the mailer
}

CalendarBase::Ptr ITIPHandler::Private::calendar()
{
    if (!m_calendar) {
        FetchJobCalendar::Ptr fetchJobCalendar = FetchJobCalendar::Ptr(new FetchJobCalendar());
        connect(fetchJobCalendar.data(), &FetchJobCalendar::loadFinished,
                this, &Private::onLoadFinished);

        m_calendar = fetchJobCalendar;
    }

    return m_calendar;
}

bool ITIPHandler::Private::isLoaded()
{
    FetchJobCalendar::Ptr fetchJobCalendar = calendar().dynamicCast<Akonadi::FetchJobCalendar>();
    if (fetchJobCalendar) {
        return fetchJobCalendar->isLoaded();
    }

    // If it's an ETMCalendar, set through setCalendar(), then it's already loaded, it's a requirement of setCalendar().
    // ETM doesn't have any way to check if it's already populated, so we have to require loaded calendars.
    return true;
}