Incidenceeditor

individualmailcomponentfactory.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5 */
6
7#include "individualmailcomponentfactory.h"
8#include "individualmaildialog.h"
9#include "opencomposerjob.h"
10
11#include <CalendarSupport/KCalPrefs>
12
13#include <KMessageBox>
14
15#include <QDBusConnection>
16#include <QDBusConnectionInterface>
17
18using namespace IncidenceEditorNG;
19
20// IndividualMessageQueueJob
21
22IndividualMessageQueueJob::IndividualMessageQueueJob(const KIdentityManagementCore::Identity &identity,
25 QObject *parent)
26 : Akonadi::MessageQueueJob(parent)
27 , mUpdate(update)
28 , mEdit(edit)
29 , mIdentity(identity)
30{
31}
32
33void IndividualMessageQueueJob::start()
34{
35 const auto to = addressAttribute().to();
36 QSet<QString> attendeesTo(to.begin(), to.end());
37 const auto cc = addressAttribute().cc();
38 QSet<QString> attendeesCc(cc.begin(), cc.end());
39
40 QStringList attendeesAutoTo;
41 QStringList attendeesAutoCc;
42 for (const KCalendarCore::Attendee &attendee : std::as_const(mUpdate)) {
43 if (attendeesTo.contains(attendee.email())) {
44 attendeesAutoTo.append(attendee.fullName());
45 }
46 if (attendeesCc.contains(attendee.email())) {
47 attendeesAutoCc.append(attendee.fullName());
48 }
49 }
50 if (!attendeesAutoTo.isEmpty() || !attendeesAutoCc.isEmpty() || !addressAttribute().bcc().isEmpty()) {
51 startQueueJob(attendeesAutoTo, addressAttribute().to(), attendeesAutoCc, addressAttribute().cc());
52 }
53
54 QStringList attendeesComposerTo;
55 QStringList attendeesComposerCc;
56 for (const KCalendarCore::Attendee &attendee : std::as_const(mEdit)) {
57 if (attendeesTo.contains(attendee.email())) {
58 attendeesComposerTo.append(attendee.fullName());
59 }
60 if (attendeesCc.contains(attendee.email())) {
61 attendeesComposerCc.append(attendee.fullName());
62 }
63 }
64 if (!attendeesComposerTo.isEmpty() || !attendeesComposerCc.isEmpty()) {
65 startComposerJob(attendeesComposerTo, attendeesComposerCc);
66 }
67
68 // No subjob has been started
69 if (!mQueueJob && !mComposerJob) {
70 emitResult();
71 }
72}
73
74void IndividualMessageQueueJob::startQueueJob(const QStringList &messageTo, const QStringList &to, const QStringList &messageCc, const QStringList &cc)
75{
77 msg->to()->fromUnicodeString(messageTo.join(QLatin1StringView(", ")));
78 msg->cc()->fromUnicodeString(messageCc.join(QLatin1StringView(", ")));
79 msg->assemble();
80
81 mQueueJob = new Akonadi::MessageQueueJob(this);
82 mQueueJob->setMessage(msg);
83 mQueueJob->transportAttribute().setTransportId(mIdentity.isNull() ? transportAttribute().transportId() : mIdentity.transport().toInt());
84 mQueueJob->addressAttribute().setFrom(addressAttribute().from());
85 mQueueJob->addressAttribute().setTo(to);
86 mQueueJob->addressAttribute().setCc(cc);
87 mQueueJob->addressAttribute().setBcc(addressAttribute().bcc());
88
89 if (mIdentity.disabledFcc()) {
91 } else {
92 const Akonadi::Collection sentCollection(mIdentity.fcc().toLongLong());
93 if (sentCollection.isValid()) {
95 mQueueJob->sentBehaviourAttribute().setMoveToCollection(sentCollection);
96 } else {
98 }
99 }
100
101 connect(mQueueJob, &Akonadi::MessageQueueJob::finished, this, &IndividualMessageQueueJob::handleJobFinished);
102 mQueueJob->start();
103}
104
105void IndividualMessageQueueJob::startComposerJob(const QStringList &to, const QStringList &cc)
106{
107 mComposerJob = new OpenComposerJob(this, to.join(QLatin1StringView(", ")), cc.join(QLatin1StringView(", ")), QString(), message(), mIdentity);
108 connect(mComposerJob, &OpenComposerJob::finished, this, &IndividualMessageQueueJob::handleJobFinished);
109 mComposerJob->start();
110}
111
112void IndividualMessageQueueJob::handleJobFinished(KJob *job)
113{
114 if (job->error()) {
115 if (job == mQueueJob && mComposerJob) {
116 mComposerJob->kill();
117 mComposerJob = nullptr;
118 } else if (job == mComposerJob && mQueueJob) {
119 mQueueJob->kill();
120 mQueueJob = nullptr;
121 }
122 setError(job->error());
124 emitResult();
125 return;
126 }
127 if (job == mQueueJob) {
128 if (!mComposerJob) {
129 emitResult();
130 }
131 mQueueJob = nullptr;
132 } else {
133 if (!mQueueJob) {
134 emitResult();
135 }
136 mComposerJob = nullptr;
137 }
138}
139
140// IndividualMailAskDelegator
141
142IndividualMailITIPHandlerDialogDelegate::IndividualMailITIPHandlerDialogDelegate(const KCalendarCore::Incidence::Ptr &incidence,
144 QWidget *parent)
145 : Akonadi::ITIPHandlerDialogDelegate(incidence, method, parent)
146 , mDialog(nullptr)
147{
148}
149
150void IndividualMailITIPHandlerDialogDelegate::openDialog(const QString &question,
151 const KCalendarCore::Attendee::List &attendees,
152 Action action,
153 const KGuiItem &buttonYes,
154 const KGuiItem &buttonNo)
155{
156 switch (action) {
158 Q_EMIT setUpdate(mIncidence, attendees);
159 Q_EMIT dialogClosed(KMessageBox::ButtonCode::PrimaryAction, mMethod, mIncidence);
160 break;
162 Q_EMIT dialogClosed(KMessageBox::ButtonCode::SecondaryAction, mMethod, mIncidence);
163 break;
164 default:
165 switch (CalendarSupport::KCalPrefs::instance()->sendPolicy()) {
166 case (CalendarSupport::KCalPrefs::InvitationPolicySend):
167 Q_EMIT setUpdate(mIncidence, attendees);
168 Q_EMIT dialogClosed(KMessageBox::ButtonCode::PrimaryAction, mMethod, mIncidence);
169 break;
170 case (CalendarSupport::KCalPrefs::InvitationPolicyDontSend):
171 Q_EMIT dialogClosed(KMessageBox::ButtonCode::SecondaryAction, mMethod, mIncidence);
172 break;
173 case (CalendarSupport::KCalPrefs::InvitationPolicyAsk):
174 default:
175 mDialog = new IndividualMailDialog(question, attendees, buttonYes, buttonNo, mParent);
176 connect(mDialog, &QDialog::finished, this, &IndividualMailITIPHandlerDialogDelegate::onDialogClosed);
177 mDialog->show();
178 break;
179 }
180 break;
181 }
182}
183
184void IndividualMailITIPHandlerDialogDelegate::openDialogIncidenceCreated(Recipient recipient,
185 const QString &question,
186 Action action,
187 const KGuiItem &buttonYes,
188 const KGuiItem &buttonNo)
189{
190 if (recipient == Attendees) {
191 openDialog(question, mIncidence->attendees(), action, buttonYes, buttonNo);
192 } else {
193 KCalendarCore::Attendee organizer(mIncidence->organizer().name(), mIncidence->organizer().email());
194 openDialog(question, KCalendarCore::Attendee::List() << organizer, action, buttonYes, buttonNo);
195 }
196}
197
198void IndividualMailITIPHandlerDialogDelegate::openDialogIncidenceModified(bool attendeeStatusChanged,
199 Recipient recipient,
200 const QString &question,
201 Action action,
202 const KGuiItem &buttonYes,
203 const KGuiItem &buttonNo)
204{
205 Q_UNUSED(attendeeStatusChanged)
206 if (recipient == Attendees) {
207 openDialog(question, mIncidence->attendees(), action, buttonYes, buttonNo);
208 } else {
209 KCalendarCore::Attendee organizer(mIncidence->organizer().name(), mIncidence->organizer().email());
210 openDialog(question, KCalendarCore::Attendee::List() << organizer, action, buttonYes, buttonNo);
211 }
212}
213
214void IndividualMailITIPHandlerDialogDelegate::openDialogIncidenceDeleted(Recipient recipient,
215 const QString &question,
216 Action action,
217 const KGuiItem &buttonYes,
218 const KGuiItem &buttonNo)
219{
220 if (recipient == Attendees) {
221 openDialog(question, mIncidence->attendees(), action, buttonYes, buttonNo);
222 } else {
223 KCalendarCore::Attendee organizer(mIncidence->organizer().name(), mIncidence->organizer().email());
224 openDialog(question, KCalendarCore::Attendee::List() << organizer, action, buttonYes, buttonNo);
225 }
226}
227
228void IndividualMailITIPHandlerDialogDelegate::onDialogClosed(int result)
229{
230 if (result == QDialogButtonBox::Yes) {
231 Q_EMIT setEdit(mIncidence, mDialog->editAttendees());
232 Q_EMIT setUpdate(mIncidence, mDialog->updateAttendees());
233 Q_EMIT dialogClosed(KMessageBox::ButtonCode::PrimaryAction, mMethod, mIncidence);
234 } else {
235 Q_EMIT dialogClosed(KMessageBox::ButtonCode::SecondaryAction, mMethod, mIncidence);
236 }
237}
238
239// IndividualMailJobFactory
240IndividualMailComponentFactory::IndividualMailComponentFactory(QObject *parent)
241 : Akonadi::ITIPHandlerComponentFactory(parent)
242{
243}
244
245Akonadi::MessageQueueJob *IndividualMailComponentFactory::createMessageQueueJob(const KCalendarCore::IncidenceBase::Ptr &incidence,
246 const KIdentityManagementCore::Identity &identity,
247 QObject *parent)
248{
249 return new IndividualMessageQueueJob(identity, mUpdate.take(incidence->uid()), mEdit.take(incidence->uid()), parent);
250}
251
252Akonadi::ITIPHandlerDialogDelegate *IndividualMailComponentFactory::createITIPHanderDialogDelegate(const KCalendarCore::Incidence::Ptr &incidence,
254 QWidget *parent)
255{
256 auto askDelegator = new IndividualMailITIPHandlerDialogDelegate(incidence, method, parent);
257 connect(askDelegator, &IndividualMailITIPHandlerDialogDelegate::setEdit, this, &IndividualMailComponentFactory::onSetEdit);
258 connect(askDelegator, &IndividualMailITIPHandlerDialogDelegate::setUpdate, this, &IndividualMailComponentFactory::onSetUpdate);
259
260 return askDelegator;
261}
262
263void IndividualMailComponentFactory::onSetEdit(const KCalendarCore::Incidence::Ptr &incidence, const KCalendarCore::Attendee::List &edit)
264{
265 mEdit[incidence->uid()] = edit;
266}
267
268void IndividualMailComponentFactory::onSetUpdate(const KCalendarCore::Incidence::Ptr &incidence, const KCalendarCore::Attendee::List &update)
269{
270 mUpdate[incidence->uid()] = update;
271}
272
273#include "moc_individualmailcomponentfactory.cpp"
QStringList to() const
void setCc(const QStringList &cc)
void setBcc(const QStringList &bcc)
void setFrom(const QString &from)
void setTo(const QStringList &to)
QStringList cc() const
KMime::Message::Ptr message() const
Akonadi::AddressAttribute & addressAttribute()
void setMessage(const KMime::Message::Ptr &message)
TransportAttribute & transportAttribute()
SentBehaviourAttribute & sentBehaviourAttribute()
void start() override
void setMoveToCollection(const Akonadi::Collection &moveToCollection)
void setSentBehaviour(SentBehaviour beh)
void setErrorText(const QString &errorText)
virtual QString errorString() const
void emitResult()
int error() const
void finished(KJob *job)
void setError(int errorCode)
bool kill(KJob::KillVerbosity verbosity=KJob::Quietly)
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
void update(Part *part, const QByteArray &data, qint64 dataSize)
void finished(int result)
void append(QList< T > &&value)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
int toInt(bool *ok, int base) const const
qlonglong toLongLong(bool *ok, int base) const const
QString join(QChar separator) const const
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.