Akonadi Mime

messagequeuejob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
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
20using namespace Akonadi;
21using namespace KMime;
22
23/**
24 @internal
25*/
26class Akonadi::MessageQueueJobPrivate
27{
28public:
29 explicit MessageQueueJobPrivate(MessageQueueJob *qq)
30 : q(qq)
31 {
32 }
33
34 MessageQueueJob *const q;
35
36 Message::Ptr message;
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
54bool 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
82void 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"));
107 item.setPayload<Message::Ptr>(message);
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"
Attribute storing the From, To, Cc, Bcc addresses of a message.
QStringList to() const
Returns the addresses of the "To:" receivers.
QStringList cc() const
Returns the addresses of the "Cc:" receivers.
QStringList bcc() const
Returns the addresses of the "Bcc:" receivers.
bool isValid() 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.
DispatchModeAttribute & dispatchModeAttribute()
Returns a reference to the dispatch mode attribute for this message.
Akonadi::AddressAttribute & addressAttribute()
Returns a reference to the address attribute for this message.
void slotResult(KJob *) override
Called when the ItemCreateJob subjob finishes.
void setMessage(const KMime::Message::Ptr &message)
Sets the message to be sent.
TransportAttribute & transportAttribute()
Returns a reference to the transport attribute for this message.
~MessageQueueJob() override
Destroys the MessageQueueJob.
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.
MessageQueueJob(QObject *parent=nullptr)
Creates a new MessageQueueJob.
SentActionAttribute & sentActionAttribute()
Returns a reference to the sent action attribute for this message.
An Attribute that stores the action to execute after sending.
Attribute determining what will happen to a message after it is sent.
@ MoveToDefaultSentCollection
Move the item to the default sent-mail collection.
@ MoveToCollection
Move the item to a custom collection.
SentBehaviour sentBehaviour() const
Returns the sent-behaviour of the message.
Akonadi::Collection moveToCollection() const
Returns the collection to which the item should be moved after it is sent.
Attribute determining which transport to use for sending a message.
virtual bool addSubjob(KJob *job)
virtual void slotResult(KJob *job)
void setErrorText(const QString &errorText)
virtual QString errorString() const
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString i18n(const char *text, const TYPE &arg...)
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.
AKONADI_MIME_EXPORT const char Queued[]
The flag for a message being marked as queued.
qsizetype count() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setFlag(Flag flag, bool enabled)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:12:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.