MailTransport

messagequeuejob.cpp
1 /*
2  Copyright (c) 2009 Constantin Berzan <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "messagequeuejob.h"
21 
22 #include "transport.h"
23 #include "kmailtransportakonadi/transportattribute.h"
24 #include "transportmanager.h"
25 
26 #include "mailtransportakonadi_debug.h"
27 #include <KLocalizedString>
28 
29 #include <collection.h>
30 #include <item.h>
31 #include <itemcreatejob.h>
32 #include <addressattribute.h>
33 #include <messageflags.h>
34 #include <specialmailcollections.h>
35 #include <specialmailcollectionsrequestjob.h>
36 
37 using namespace Akonadi;
38 using namespace KMime;
39 using namespace MailTransport;
40 
44 class Q_DECL_HIDDEN MailTransport::MessageQueueJob::Private
45 {
46 public:
47  Private(MessageQueueJob *qq)
48  : q(qq)
49  {
50  started = false;
51  }
52 
53  MessageQueueJob *const q;
54 
56  TransportAttribute transportAttribute;
57  DispatchModeAttribute dispatchModeAttribute;
58  SentBehaviourAttribute sentBehaviourAttribute;
59  SentActionAttribute sentActionAttribute;
60  AddressAttribute addressAttribute;
61  bool started;
62 
67  bool validate();
68 
69  // slot
70  void outboxRequestResult(KJob *job);
71 };
72 
73 bool MessageQueueJob::Private::validate()
74 {
75  if (!message) {
76  q->setError(UserDefinedError);
77  q->setErrorText(i18n("Empty message."));
78  q->emitResult();
79  return false;
80  }
81 
82  if ((addressAttribute.to().count() + addressAttribute.cc().count()
83  +addressAttribute.bcc().count()) == 0) {
84  q->setError(UserDefinedError);
85  q->setErrorText(i18n("Message has no recipients."));
86  q->emitResult();
87  return false;
88  }
89 
90  const int transport = transportAttribute.transportId();
91  if (TransportManager::self()->transportById(transport, false) == nullptr) {
92  q->setError(UserDefinedError);
93  q->setErrorText(i18n("Message has invalid transport."));
94  q->emitResult();
95  return false;
96  }
97 
98  if (sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection
99  && !(sentBehaviourAttribute.moveToCollection().isValid())) {
100  q->setError(UserDefinedError);
101  q->setErrorText(i18n("Message has invalid sent-mail folder."));
102  q->emitResult();
103  return false;
104  } else if (sentBehaviourAttribute.sentBehaviour()
105  == SentBehaviourAttribute::MoveToDefaultSentCollection) {
106  // TODO require SpecialMailCollections::SentMail here?
107  }
108 
109  return true; // all ok
110 }
111 
112 void MessageQueueJob::Private::outboxRequestResult(KJob *job)
113 {
114  Q_ASSERT(!started);
115  started = true;
116 
117  if (job->error()) {
118  qCritical() << "Failed to get the Outbox folder:" << job->error() << job->errorString();
119  q->setError(job->error());
120  q->emitResult();
121  return;
122  }
123 
124  if (!validate()) {
125  // The error has been set; the result has been emitted.
126  return;
127  }
128 
130  = qobject_cast<SpecialMailCollectionsRequestJob *>(job);
131  if (!requestJob) {
132  return;
133  }
134 
135  // Create item.
136  Item item;
137  item.setMimeType(QStringLiteral("message/rfc822"));
138  item.setPayload<Message::Ptr>(message);
139 
140  // Set attributes.
141  item.addAttribute(addressAttribute.clone());
142  item.addAttribute(dispatchModeAttribute.clone());
143  item.addAttribute(sentBehaviourAttribute.clone());
144  item.addAttribute(sentActionAttribute.clone());
145  item.addAttribute(transportAttribute.clone());
146 
148  // Set flags.
149  item.setFlag(Akonadi::MessageFlags::Queued);
150 
151  // Store the item in the outbox.
152  const Collection collection = requestJob->collection();
153  Q_ASSERT(collection.isValid());
154  ItemCreateJob *cjob = new ItemCreateJob(item, collection); // job autostarts
155  q->addSubjob(cjob);
156 }
157 
158 MessageQueueJob::MessageQueueJob(QObject *parent)
159  : KCompositeJob(parent)
160  , d(new Private(this))
161 {
162 }
163 
165 {
166  delete d;
167 }
168 
170 {
171  return d->message;
172 }
173 
175 {
176  return d->dispatchModeAttribute;
177 }
178 
180 {
181  return d->addressAttribute;
182 }
183 
185 {
186  return d->transportAttribute;
187 }
188 
190 {
191  return d->sentBehaviourAttribute;
192 }
193 
195 {
196  return d->sentActionAttribute;
197 }
198 
200 {
201  d->message = message;
202 }
203 
205 {
207  rjob->requestDefaultCollection(SpecialMailCollections::Outbox);
208  connect(rjob, &SpecialMailCollectionsRequestJob::result, this, [this](KJob *job) {
209  d->outboxRequestResult(job);
210  });
211  rjob->start();
212 }
213 
215 {
216  // error handling
218 
219  if (!error()) {
220  emitResult();
221  }
222 }
223 
224 #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 requestDefaultCollection(SpecialMailCollections::Type type)
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.
void start() override
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-2020 The KDE developers.
Generated on Mon Jun 1 2020 23:10:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.