Akonadi Mime

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

KDE's Doxygen guidelines are available online.