MailTransport

messagequeuejob.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "messagequeuejob.h"
8 
9 #include "transport.h"
10 #include "kmailtransportakonadi/transportattribute.h"
11 #include "transportmanager.h"
12 
13 #include "mailtransportakonadi_debug.h"
14 #include <KLocalizedString>
15 
16 #include <item.h>
17 #include <itemcreatejob.h>
18 #include <addressattribute.h>
19 #include <messageflags.h>
20 #include <specialmailcollections.h>
21 #include <specialmailcollectionsrequestjob.h>
22 
23 using namespace Akonadi;
24 using namespace KMime;
25 using namespace MailTransport;
26 
30 class Q_DECL_HIDDEN MailTransport::MessageQueueJob::Private
31 {
32 public:
33  Private(MessageQueueJob *qq)
34  : q(qq)
35  {
36  }
37 
38  MessageQueueJob *const q;
39 
41  TransportAttribute transportAttribute;
42  DispatchModeAttribute dispatchModeAttribute;
43  SentBehaviourAttribute sentBehaviourAttribute;
44  SentActionAttribute sentActionAttribute;
45  AddressAttribute addressAttribute;
46  bool started = false;
47 
52  bool validate();
53 
54  // slot
55  void outboxRequestResult(KJob *job);
56 };
57 
58 bool MessageQueueJob::Private::validate()
59 {
60  if (!message) {
61  q->setError(UserDefinedError);
62  q->setErrorText(i18n("Empty message."));
63  q->emitResult();
64  return false;
65  }
66 
67  if ((addressAttribute.to().count() + addressAttribute.cc().count()
68  +addressAttribute.bcc().count()) == 0) {
69  q->setError(UserDefinedError);
70  q->setErrorText(i18n("Message has no recipients."));
71  q->emitResult();
72  return false;
73  }
74 
75  const int transport = transportAttribute.transportId();
76  if (TransportManager::self()->transportById(transport, false) == nullptr) {
77  q->setError(UserDefinedError);
78  q->setErrorText(i18n("Message has invalid transport."));
79  q->emitResult();
80  return false;
81  }
82 
83  if (sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection
84  && !(sentBehaviourAttribute.moveToCollection().isValid())) {
85  q->setError(UserDefinedError);
86  q->setErrorText(i18n("Message has invalid sent-mail folder."));
87  q->emitResult();
88  return false;
89  } else if (sentBehaviourAttribute.sentBehaviour()
90  == SentBehaviourAttribute::MoveToDefaultSentCollection) {
91  // TODO require SpecialMailCollections::SentMail here?
92  }
93 
94  return true; // all ok
95 }
96 
97 void MessageQueueJob::Private::outboxRequestResult(KJob *job)
98 {
99  Q_ASSERT(!started);
100  started = true;
101 
102  if (job->error()) {
103  qCritical() << "Failed to get the Outbox folder:" << job->error() << job->errorString();
104  q->setError(job->error());
105  q->emitResult();
106  return;
107  }
108 
109  if (!validate()) {
110  // The error has been set; the result has been emitted.
111  return;
112  }
113 
114  auto *requestJob
115  = qobject_cast<SpecialMailCollectionsRequestJob *>(job);
116  if (!requestJob) {
117  return;
118  }
119 
120  // Create item.
121  Item item;
122  item.setMimeType(QStringLiteral("message/rfc822"));
123  item.setPayload<Message::Ptr>(message);
124 
125  // Set attributes.
126  item.addAttribute(addressAttribute.clone());
127  item.addAttribute(dispatchModeAttribute.clone());
128  item.addAttribute(sentBehaviourAttribute.clone());
129  item.addAttribute(sentActionAttribute.clone());
130  item.addAttribute(transportAttribute.clone());
131 
133  // Set flags.
134  item.setFlag(Akonadi::MessageFlags::Queued);
135 
136  // Store the item in the outbox.
137  const Collection collection = requestJob->collection();
138  Q_ASSERT(collection.isValid());
139  auto *cjob = new ItemCreateJob(item, collection); // job autostarts
140  q->addSubjob(cjob);
141 }
142 
143 MessageQueueJob::MessageQueueJob(QObject *parent)
144  : KCompositeJob(parent)
145  , d(new Private(this))
146 {
147 }
148 
150 {
151  delete d;
152 }
153 
155 {
156  return d->message;
157 }
158 
160 {
161  return d->dispatchModeAttribute;
162 }
163 
165 {
166  return d->addressAttribute;
167 }
168 
170 {
171  return d->transportAttribute;
172 }
173 
175 {
176  return d->sentBehaviourAttribute;
177 }
178 
180 {
181  return d->sentActionAttribute;
182 }
183 
185 {
186  d->message = message;
187 }
188 
190 {
191  auto *rjob = new SpecialMailCollectionsRequestJob(this);
192  rjob->requestDefaultCollection(SpecialMailCollections::Outbox);
193  connect(rjob, &SpecialMailCollectionsRequestJob::result, this, [this](KJob *job) {
194  d->outboxRequestResult(job);
195  });
196  rjob->start();
197 }
198 
200 {
201  // error handling
203 
204  if (!error()) {
205  emitResult();
206  }
207 }
208 
209 #include "moc_messagequeuejob.cpp"
KCOREADDONS_EXPORT void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
bool isValid() const
void setMessage(const KMime::Message::Ptr &message)
Sets the message to be sent.
void emitResult()
~MessageQueueJob() override
Destroys the MessageQueueJob.
DispatchModeAttribute & dispatchModeAttribute()
Returns a reference to the dispatch mode attribute for this message.
Attribute determining what will happen to a message after it is sent.
virtual QString errorString() const
Attribute determining how and when a message from the outbox should be dispatched.
Provides an interface for sending email.
KMime::Message::Ptr message() const
Returns the message to be sent.
TransportAttribute & transportAttribute()
Returns a reference to the transport attribute for this message.
virtual void slotResult(KJob *job)
void slotResult(KJob *) override
Called when the ItemCreateJob subjob finishes.
void start() override
Creates the item and places it in the outbox.
QString i18n(const char *text, const TYPE &arg...)
SentActionAttribute & sentActionAttribute()
Returns a reference to the sent action attribute for this message.
Akonadi::AddressAttribute & addressAttribute()
Returns a reference to the address attribute for this message.
AKONADI_MIME_EXPORT const char Queued[]
SentBehaviourAttribute & sentBehaviourAttribute()
Returns a reference to the sent behaviour attribute for this message.
AKONADI_MIME_EXPORT void copyMessageFlags(KMime::Message &from, Akonadi::Item &to)
An Attribute that stores the action to execute after sending.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Attribute determining which transport to use for sending a message.
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 16 2021 23:21:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.