Akonadi Calendar

itiphandler.cpp
1/*
2 SPDX-FileCopyrightText: 2002-2004 Klarälvdalens Datakonsult AB,
3 <info@klaralvdalens-datakonsult.se>
4
5 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
6 SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
7
8 SPDX-FileCopyrightText: 2012 SĂ©rgio Martins <iamsergio@gmail.com>
9
10 SPDX-License-Identifier: LGPL-2.0-or-later
11*/
12
13#include "itiphandler.h"
14#include "calendarsettings.h"
15#include "itiphandler_p.h"
16#include "itiphandlerhelper_p.h"
17#include "mailclient_p.h"
18#include "publishdialog.h"
19#include "utils_p.h"
20
21#include <KCalUtils/Stringify>
22#include <KCalendarCore/Attendee>
23#include <KCalendarCore/ICalFormat>
24
25#include <Akonadi/MessageQueueJob>
26#include <KIdentityManagementCore/IdentityManager>
27
28#include "akonadicalendar_debug.h"
29#include <KMessageBox>
30
31using namespace Akonadi;
32using namespace Qt::Literals::StringLiterals;
33// async emittion
34static void emitiTipMessageProcessed(ITIPHandler *handler, ITIPHandler::Result resultCode, const QString &errorString)
35{
37 "iTipMessageProcessed",
39 Q_ARG(Akonadi::ITIPHandler::Result, resultCode),
40 Q_ARG(QString, errorString));
41}
42
43GroupwareUiDelegate::GroupwareUiDelegate(QObject *parent)
44 : QObject(parent)
45{
46}
47
48GroupwareUiDelegate::~GroupwareUiDelegate() = default;
49
50ITIPHandlerComponentFactory::ITIPHandlerComponentFactory(QObject *parent)
51 : QObject(parent)
52{
53}
54
55ITIPHandlerComponentFactory::~ITIPHandlerComponentFactory() = default;
56
57Akonadi::MessageQueueJob *ITIPHandlerComponentFactory::createMessageQueueJob(const KCalendarCore::IncidenceBase::Ptr &incidence,
59 QObject *parent)
60{
61 Q_UNUSED(incidence)
62 Q_UNUSED(identity)
64}
65
67ITIPHandlerComponentFactory::createITIPHanderDialogDelegate(const KCalendarCore::Incidence::Ptr &incidence, KCalendarCore::iTIPMethod method, QWidget *parent)
68{
69 return new ITIPHandlerDialogDelegate(incidence, method, parent);
70}
71
73 : QObject(parent)
74 , d(new ITIPHandlerPrivate(/*factory=*/nullptr, this))
75{
76 qRegisterMetaType<Akonadi::ITIPHandler::Result>("Akonadi::ITIPHandler::Result");
77}
78
80 : QObject(parent)
81 , d(new ITIPHandlerPrivate(factory, this))
82{
83 qRegisterMetaType<Akonadi::ITIPHandler::Result>("Akonadi::ITIPHandler::Result");
84}
85
87
88void ITIPHandler::processiTIPMessage(const QString &receiver, const QString &iCal, const QString &action)
89{
90 qCDebug(AKONADICALENDAR_LOG) << "processiTIPMessage called with receiver=" << receiver << "; action=" << action;
91
92 if (d->m_currentOperation != OperationNone) {
93 d->m_currentOperation = OperationNone;
94 qCritical() << "There can't be an operation in progress!" << d->m_currentOperation;
95 return;
96 }
97
98 d->m_currentOperation = OperationProcessiTIPMessage;
99
100 if (!d->isLoaded()) {
101 d->m_queuedInvitation.receiver = receiver;
102 d->m_queuedInvitation.iCal = iCal;
103 d->m_queuedInvitation.action = action;
104 return;
105 }
106
107 if (d->m_calendarLoadError) {
108 d->m_currentOperation = OperationNone;
109 qCritical() << "Error loading calendar";
110 emitiTipMessageProcessed(this, ResultError, i18n("Error loading calendar."));
111 return;
112 }
113
115 KCalendarCore::ScheduleMessage::Ptr message = format.parseScheduleMessage(d->calendar(), iCal);
116
117 if (!message) {
118 const QString errorMessage = format.exception() ? i18n("Error message: %1", KCalUtils::Stringify::errorMessage(*format.exception()))
119 : i18n("Unknown error while parsing iCal invitation");
120
121 qCritical() << "Error parsing" << errorMessage;
122
123 if (d->m_showDialogsOnError) {
124 KMessageBox::detailedError(nullptr, // mParent, TODO
125 i18n("Error while processing an invitation or update."),
126 errorMessage);
127 }
128
129 d->m_currentOperation = OperationNone;
130 emitiTipMessageProcessed(this, ResultError, errorMessage);
131
132 return;
133 }
134
135 d->m_method = static_cast<KCalendarCore::iTIPMethod>(message->method());
136
138 d->m_incidence = message->event().dynamicCast<KCalendarCore::Incidence>();
139 if (!d->m_incidence) {
140 qCritical() << "Invalid incidence";
141 d->m_currentOperation = OperationNone;
142 emitiTipMessageProcessed(this, ResultError, i18n("Invalid incidence"));
143 return;
144 }
145
146 if (action.startsWith("accepted"_L1) || action.startsWith("tentative"_L1) || action.startsWith("delegated"_L1) || action.startsWith("counter"_L1)) {
147 // Find myself and set my status. This can't be done in the scheduler,
148 // since this does not know the choice I made in the KMail bpf
149 KCalendarCore::Attendee::List attendees = d->m_incidence->attendees();
150 for (auto &attendee : attendees) {
151 if (attendee.email() == receiver) {
152 if (action.startsWith("accepted"_L1)) {
153 attendee.setStatus(KCalendarCore::Attendee::Accepted);
154 } else if (action.startsWith("tentative"_L1)) {
155 attendee.setStatus(KCalendarCore::Attendee::Tentative);
156 } else if (CalendarSettings::self()->outlookCompatCounterProposals() && action.startsWith("counter"_L1)) {
157 attendee.setStatus(KCalendarCore::Attendee::Tentative);
158 } else if (action.startsWith("delegated"_L1)) {
159 attendee.setStatus(KCalendarCore::Attendee::Delegated);
160 }
161 break;
162 }
163 }
164 d->m_incidence->setAttendees(attendees);
165
166 if (CalendarSettings::self()->outlookCompatCounterProposals() || !action.startsWith("counter"_L1)) {
167 d->m_scheduler->acceptTransaction(d->m_incidence, d->calendar(), d->m_method, status, receiver);
168 return; // signal emitted in onSchedulerFinished().
169 }
170 // TODO: what happens here? we must emit a signal
171 } else if (action.startsWith("cancel"_L1)) {
172 // Delete the old incidence, if one is present
173 KCalendarCore::Incidence::Ptr existingIncidence = d->calendar()->incidenceFromSchedulingID(d->m_incidence->uid());
174 if (existingIncidence) {
175 d->m_scheduler->acceptTransaction(d->m_incidence, d->calendar(), KCalendarCore::iTIPCancel, status, receiver);
176 return; // signal emitted in onSchedulerFinished().
177 } else {
178 // We don't have the incidence, nothing to cancel
179 qCWarning(AKONADICALENDAR_LOG) << "Couldn't find the incidence to delete.\n"
180 << "You deleted it previously or didn't even accept the invitation it in the first place.\n"
181 << "; uid=" << d->m_incidence->uid() << "; identifier=" << d->m_incidence->instanceIdentifier()
182 << "; summary=" << d->m_incidence->summary();
183
184 qCDebug(AKONADICALENDAR_LOG) << "\n Here's what we do have with such a summary:";
185 const KCalendarCore::Incidence::List knownIncidences = calendar()->incidences();
186 for (const KCalendarCore::Incidence::Ptr &knownIncidence : knownIncidences) {
187 if (knownIncidence->summary() == d->m_incidence->summary()) {
188 qCDebug(AKONADICALENDAR_LOG) << "\nFound: uid=" << knownIncidence->uid() << "; identifier=" << knownIncidence->instanceIdentifier()
189 << "; schedulingId" << knownIncidence->schedulingID();
190 }
191 }
192
193 emitiTipMessageProcessed(this, ResultSuccess, QString());
194 }
195 } else if (action.startsWith("reply"_L1)) {
196 if (d->m_method != KCalendarCore::iTIPCounter) {
197 d->m_scheduler->acceptTransaction(d->m_incidence, d->calendar(), d->m_method, status, QString());
198 } else {
199 d->m_scheduler->acceptCounterProposal(d->m_incidence, d->calendar());
200 }
201 return; // signal emitted in onSchedulerFinished().
202 } else if (action.startsWith("request"_L1)) {
203 d->m_scheduler->acceptTransaction(d->m_incidence, d->calendar(), d->m_method, status, receiver);
204 return;
205 } else {
206 qCritical() << "Unknown incoming action" << action;
207
208 d->m_currentOperation = OperationNone;
209 emitiTipMessageProcessed(this, ResultError, i18n("Invalid action: %1", action));
210 }
211
212 if (action.startsWith("counter"_L1)) {
213 if (d->m_uiDelegate) {
214 Akonadi::Item item;
215 item.setMimeType(d->m_incidence->mimeType());
216 item.setPayload(KCalendarCore::Incidence::Ptr(d->m_incidence->clone()));
217
218 // TODO_KDE5: This blocks because m_uiDelegate is not a QObject and can't emit a finished()
219 // signal. Make async in kde5
220 d->m_uiDelegate->requestIncidenceEditor(item);
223 newIncidence = item.payload<KCalendarCore::Incidence::Ptr>();
224 }
225
226 if (*newIncidence == *d->m_incidence) {
227 emitiTipMessageProcessed(this, ResultCancelled, QString());
228 } else {
229 ITIPHandlerHelper::SendResult result = d->m_helper->sendCounterProposal(receiver, d->m_incidence, newIncidence);
230 if (result != ITIPHandlerHelper::ResultSuccess) {
231 // It gives success in all paths, this never happens
232 emitiTipMessageProcessed(this, ResultError, i18n("Error sending counter proposal"));
233 Q_ASSERT(false);
234 }
235 }
236 } else {
237 // This should never happen
238 qCWarning(AKONADICALENDAR_LOG) << "No UI delegate is set";
239 emitiTipMessageProcessed(this, ResultError, i18n("Could not start editor to edit counter proposal"));
240 }
241 }
242}
243
245{
246 if (!incidence) {
247 Q_ASSERT(false);
248 qCritical() << "Invalid incidence";
249 return;
250 }
251
252 d->m_queuedInvitation.method = method;
253 d->m_queuedInvitation.incidence = incidence;
254 d->m_parentWidget = parentWidget;
255
256 if (!d->isLoaded()) {
257 // This method will be called again once the calendar is loaded.
258 return;
259 }
260
261 Q_ASSERT(d->m_currentOperation == OperationNone);
262 if (d->m_currentOperation != OperationNone) {
263 qCritical() << "There can't be an operation in progress!" << d->m_currentOperation;
264 return;
265 }
266
267 if (incidence->attendeeCount() == 0 && method != KCalendarCore::iTIPPublish) {
268 if (d->m_showDialogsOnError) {
269 KMessageBox::information(parentWidget,
270 i18n("The item '%1' has no attendees. "
271 "Therefore no groupware message will be sent.",
272 incidence->summary()),
273 i18nc("@title:window", "Message Not Sent"),
274 QStringLiteral("ScheduleNoAttendees"));
275 }
276
277 return;
278 }
279
280 d->m_currentOperation = OperationSendiTIPMessage;
281
282 KCalendarCore::Incidence *incidenceCopy = incidence->clone();
283 incidenceCopy->registerObserver(nullptr);
284 incidenceCopy->clearAttendees();
285
286 d->m_scheduler->performTransaction(incidence, method);
287}
288
290{
291 Q_ASSERT(incidence);
292 if (!incidence) {
293 qCritical() << "Invalid incidence. Aborting.";
294 return;
295 }
296
297 Q_ASSERT(d->m_currentOperation == OperationNone);
298 if (d->m_currentOperation != OperationNone) {
299 qCritical() << "There can't be an operation in progress!" << d->m_currentOperation;
300 return;
301 }
302
303 d->m_queuedInvitation.incidence = incidence;
304 d->m_parentWidget = parentWidget;
305
306 d->m_currentOperation = OperationPublishInformation;
307
308 QPointer<Akonadi::PublishDialog> publishdlg = new Akonadi::PublishDialog();
309 if (incidence->attendeeCount() > 0) {
310 KCalendarCore::Attendee::List attendees = incidence->attendees();
313 for (it = attendees.constBegin(); it != end; ++it) {
314 publishdlg->addAttendee(*it);
315 }
316 }
317 if (publishdlg->exec() == QDialog::Accepted && publishdlg) {
318 d->m_scheduler->publish(incidence, publishdlg->addresses());
319 } else {
320 d->m_currentOperation = OperationNone;
322 }
323 delete publishdlg;
324}
325
326void ITIPHandler::sendAsICalendar(const KCalendarCore::Incidence::Ptr &originalIncidence, QWidget *parentWidget)
327{
328 Q_UNUSED(parentWidget)
329 Q_ASSERT(originalIncidence);
330 if (!originalIncidence) {
331 qCritical() << "Invalid incidence";
332 return;
333 }
334
335 // Clone so we can change organizer and recurid
336 KCalendarCore::Incidence::Ptr incidence = KCalendarCore::Incidence::Ptr(originalIncidence->clone());
337
338 QPointer<Akonadi::PublishDialog> publishdlg = new Akonadi::PublishDialog;
339 if (publishdlg->exec() == QDialog::Accepted && publishdlg) {
340 const QString recipients = publishdlg->addresses();
341 if (incidence->organizer().isEmpty()) {
342 incidence->setOrganizer(KCalendarCore::Person(Akonadi::CalendarUtils::fullName(), Akonadi::CalendarUtils::email()));
343 }
344
345 if (incidence->hasRecurrenceId()) {
346 // For an individual occurrence, recur id doesn't make sense, since we're not sending the whole recurrence series.
347 incidence->setRecurrenceId({});
348 }
349
351 const QString from = Akonadi::CalendarUtils::email();
352 const bool bccMe = Akonadi::CalendarSettings::self()->bcc();
353 const QString messageText = format.createScheduleMessage(incidence, KCalendarCore::iTIPRequest);
354 auto mailer = new MailClient(d->m_factory);
355 d->m_queuedInvitation.incidence = incidence;
356 connect(mailer, &MailClient::finished, d.get(), [this](Akonadi::MailClient::Result result, const QString &str) {
357 d->finishSendAsICalendar(result, str);
358 });
359
360 mailer->mailTo(incidence,
361 KIdentityManagementCore::IdentityManager::self()->identityForAddress(from),
362 from,
364 bccMe,
365 recipients,
366 messageText);
367 }
368 delete publishdlg;
369}
370
372{
373 d->m_uiDelegate = delegate;
374}
375
377{
378 if (d->m_calendar != calendar) {
379 d->m_calendar = calendar;
380 }
381}
382
384{
385 d->m_showDialogsOnError = enable;
386}
387
389{
390 return d->m_calendar;
391}
392
393#include "moc_itiphandler.cpp"
Ui delegate for editing counter proposals.
Definition itiphandler.h:50
Factory to create Akonadi::MessageQueueJob jobs or ITIPHandlerDialogDelegate objects.
Ui delegate for dialogs raised by the ITIPHandler and IncidenceChanger.
Definition itiphandler.h:66
Handles sending of iTip messages as well as processing incoming ones.
~ITIPHandler() override
Destroys this instance.
void setGroupwareUiDelegate(GroupwareUiDelegate *delegate)
Sets the UI delegate to edit counter proposals.
@ ResultCancelled
User cancelled the operation.
@ ResultSuccess
The invitation was successfully handled.
@ ResultError
An unexpected error occurred.
ITIPHandler(QObject *parent=nullptr)
Creates a new ITIPHandler instance.
Akonadi::CalendarBase::Ptr calendar() const
Returns the calendar used by this itip handler.
void setShowDialogsOnError(bool enable)
Sets if the ITIP handler should show dialogs on error.
void sendiTIPMessage(KCalendarCore::iTIPMethod method, const KCalendarCore::Incidence::Ptr &incidence, QWidget *parentWidget=nullptr)
Sends an iTip message.
void processiTIPMessage(const QString &receiver, const QString &iCal, const QString &type)
Processes a received iTip message.
void sendAsICalendar(const KCalendarCore::Incidence::Ptr &incidence, QWidget *parentWidget=nullptr)
Sends an e-mail with the incidence attached as iCalendar source.
void setCalendar(const Akonadi::CalendarBase::Ptr &calendar)
Sets the calendar that the itip handler should use.
void publishInformation(const KCalendarCore::Incidence::Ptr &incidence, QWidget *parentWidget=nullptr)
Publishes incidence incidence.
void informationPublished(Akonadi::ITIPHandler::Result, const QString &errorMessage)
Signal emitted after an incidence was published with publishInformation()
T payload() const
void setMimeType(const QString &mimeType)
void setPayload(const T &p)
bool hasPayload() const
Exception * exception() const
QString createScheduleMessage(const IncidenceBase::Ptr &incidence, iTIPMethod method)
ScheduleMessage::Ptr parseScheduleMessage(const Calendar::Ptr &calendar, const QString &string)
void registerObserver(IncidenceObserver *observer)
QSharedPointer< Incidence > Ptr
static IdentityManager * self()
Q_SCRIPTABLE CaptureState status()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
FreeBusyManager::Singleton.
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void detailedError(QWidget *parent, const QString &text, const QString &details, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
const_iterator constBegin() const const
const_iterator constEnd() const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QueuedConnection
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.