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 "kmailtransportakonadi/transportattribute.h"
10 
11 #include "mailtransportakonadi_debug.h"
12 #include <KLocalizedString>
13 
14 #include <Akonadi/Item>
15 #include <Akonadi/ItemCreateJob>
16 #include <Akonadi/MessageFlags>
17 #include <Akonadi/SpecialMailCollections>
18 #include <Akonadi/SpecialMailCollectionsRequestJob>
19 
20 using namespace Akonadi;
21 using namespace KMime;
22 using namespace MailTransport;
23 
24 /**
25  @internal
26 */
27 class MailTransport::MessageQueueJobPrivate
28 {
29 public:
30  explicit MessageQueueJobPrivate(MessageQueueJob *qq)
31  : q(qq)
32  {
33  }
34 
35  MessageQueueJob *const q;
36 
38  TransportAttribute transportAttribute;
39  DispatchModeAttribute dispatchModeAttribute;
40  SentBehaviourAttribute sentBehaviourAttribute;
41  SentActionAttribute sentActionAttribute;
42  AddressAttribute addressAttribute;
43  bool started = false;
44 
45  /**
46  Returns true if this message has everything it needs and is ready to be
47  sent.
48  */
49  bool validate();
50 
51  // slot
52  void outboxRequestResult(KJob *job);
53 };
54 
55 bool MessageQueueJobPrivate::validate()
56 {
57  if (!message) {
58  q->setError(KJob::UserDefinedError);
59  q->setErrorText(i18n("Empty message."));
60  q->emitResult();
61  return false;
62  }
63 
64  if ((addressAttribute.to().count() + addressAttribute.cc().count() + addressAttribute.bcc().count()) == 0) {
65  q->setError(KJob::UserDefinedError);
66  q->setErrorText(i18n("Message has no recipients."));
67  q->emitResult();
68  return false;
69  }
70 
71  if (sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection && !(sentBehaviourAttribute.moveToCollection().isValid())) {
72  q->setError(KJob::UserDefinedError);
73  q->setErrorText(i18n("Message has invalid sent-mail folder."));
74  q->emitResult();
75  return false;
76  } else if (sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToDefaultSentCollection) {
77  // TODO require SpecialMailCollections::SentMail here?
78  }
79 
80  return true; // all ok
81 }
82 
83 void MessageQueueJobPrivate::outboxRequestResult(KJob *job)
84 {
85  Q_ASSERT(!started);
86  started = true;
87 
88  if (job->error()) {
89  qCritical() << "Failed to get the Outbox folder:" << job->error() << job->errorString();
90  q->setError(job->error());
91  q->emitResult();
92  return;
93  }
94 
95  if (!validate()) {
96  // The error has been set; the result has been emitted.
97  return;
98  }
99 
100  auto requestJob = qobject_cast<SpecialMailCollectionsRequestJob *>(job);
101  if (!requestJob) {
102  return;
103  }
104 
105  // Create item.
106  Item item;
107  item.setMimeType(QStringLiteral("message/rfc822"));
109 
110  // Set attributes.
111  item.addAttribute(addressAttribute.clone());
112  item.addAttribute(dispatchModeAttribute.clone());
113  item.addAttribute(sentBehaviourAttribute.clone());
114  item.addAttribute(sentActionAttribute.clone());
115  item.addAttribute(transportAttribute.clone());
116 
118  // Set flags.
120 
121  // Store the item in the outbox.
122  const Collection collection = requestJob->collection();
123  Q_ASSERT(collection.isValid());
124  auto cjob = new ItemCreateJob(item, collection); // job autostarts
125  q->addSubjob(cjob);
126 }
127 
128 MessageQueueJob::MessageQueueJob(QObject *parent)
129  : KCompositeJob(parent)
130  , d(new MessageQueueJobPrivate(this))
131 {
132 }
133 
135 
137 {
138  return d->message;
139 }
140 
142 {
143  return d->dispatchModeAttribute;
144 }
145 
147 {
148  return d->addressAttribute;
149 }
150 
152 {
153  return d->transportAttribute;
154 }
155 
157 {
158  return d->sentBehaviourAttribute;
159 }
160 
162 {
163  return d->sentActionAttribute;
164 }
165 
167 {
168  d->message = message;
169 }
170 
172 {
173  auto rjob = new SpecialMailCollectionsRequestJob(this);
174  rjob->requestDefaultCollection(SpecialMailCollections::Outbox);
175  connect(rjob, &SpecialMailCollectionsRequestJob::result, this, [this](KJob *job) {
176  d->outboxRequestResult(job);
177  });
178  rjob->start();
179 }
180 
182 {
183  // error handling
185 
186  if (!error()) {
187  emitResult();
188  }
189 }
190 
191 #include "moc_messagequeuejob.cpp"
const AKONADI_MIME_EXPORT char Queued[]
Provides an interface for sending email.
void slotResult(KJob *) override
Called when the ItemCreateJob subjob finishes.
void setMimeType(const QString &mimeType)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
AKONADI_MIME_EXPORT void copyMessageFlags(KMime::Message &from, Akonadi::Item &to)
Attribute determining how and when a message from the outbox should be dispatched.
SentActionAttribute & sentActionAttribute()
Returns a reference to the sent action attribute for this message.
QString i18n(const char *text, const TYPE &arg...)
DispatchModeAttribute & dispatchModeAttribute()
Returns a reference to the dispatch mode attribute for this message.
~MessageQueueJob() override
Destroys the MessageQueueJob.
TransportAttribute & transportAttribute()
Returns a reference to the transport attribute for this message.
SentBehaviourAttribute & sentBehaviourAttribute()
Returns a reference to the sent behaviour attribute for this message.
void start() override
Creates the item and places it in the outbox.
Attribute determining which transport to use for sending a message.
void addAttribute(Attribute *attribute)
An Attribute that stores the action to execute after sending.
virtual void slotResult(KJob *job)
KMime::Message::Ptr message() const
Returns the message to be sent.
bool isValid() const
void emitResult()
virtual QString errorString() const
int error() const
Attribute determining what will happen to a message after it is sent.
Akonadi::AddressAttribute & addressAttribute()
Returns a reference to the address attribute for this message.
void setFlag(const QByteArray &name)
void setMessage(const KMime::Message::Ptr &message)
Sets the message to be sent.
QString message
void setPayload(const T &p)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:19:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.