• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

mailtransport

  • sources
  • kde-4.14
  • kdepimlibs
  • mailtransport
messagequeuejob.cpp
1 /*
2  Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
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 "transportattribute.h"
24 #include "transportmanager.h"
25 
26 #include <KDebug>
27 #include <KLocalizedString>
28 
29 #include <akonadi/collection.h>
30 #include <akonadi/item.h>
31 #include <akonadi/itemcreatejob.h>
32 #include <akonadi/kmime/addressattribute.h>
33 #include <akonadi/kmime/messageflags.h>
34 #include <akonadi/kmime/specialmailcollections.h>
35 #include <akonadi/kmime/specialmailcollectionsrequestjob.h>
36 
37 using namespace Akonadi;
38 using namespace KMime;
39 using namespace MailTransport;
40 
44 class MailTransport::MessageQueueJob::Private
45 {
46  public:
47  Private( MessageQueueJob *qq )
48  : q( qq )
49  {
50  started = false;
51  }
52 
53  MessageQueueJob *const q;
54 
55  Message::Ptr message;
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 
74 bool MessageQueueJob::Private::validate()
75 {
76  if ( !message ) {
77  q->setError( UserDefinedError );
78  q->setErrorText( i18n( "Empty message." ) );
79  q->emitResult();
80  return false;
81  }
82 
83  if ( addressAttribute.to().count() + addressAttribute.cc().count() +
84  addressAttribute.bcc().count() == 0 ) {
85  q->setError( UserDefinedError );
86  q->setErrorText( i18n( "Message has no recipients." ) );
87  q->emitResult();
88  return false;
89  }
90 
91  const int transport = transportAttribute.transportId();
92  if ( TransportManager::self()->transportById( transport, false ) == 0 ) {
93  q->setError( UserDefinedError );
94  q->setErrorText( i18n( "Message has invalid transport." ) );
95  q->emitResult();
96  return false;
97  }
98 
99  if ( sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection &&
100  !( sentBehaviourAttribute.moveToCollection().isValid() ) ) {
101  q->setError( UserDefinedError );
102  q->setErrorText( i18n( "Message has invalid sent-mail folder." ) );
103  q->emitResult();
104  return false;
105  } else if ( sentBehaviourAttribute.sentBehaviour() ==
106  SentBehaviourAttribute::MoveToDefaultSentCollection ) {
107  // TODO require SpecialMailCollections::SentMail here?
108  }
109 
110  return true; // all ok
111 }
112 
113 void MessageQueueJob::Private::outboxRequestResult( KJob *job )
114 {
115  Q_ASSERT( !started );
116  started = true;
117 
118  if ( job->error() ) {
119  kError() << "Failed to get the Outbox folder:" << job->error() << job->errorString();
120  q->setError( job->error() );
121  q->emitResult();
122  return;
123  }
124 
125  if ( !validate() ) {
126  // The error has been set; the result has been emitted.
127  return;
128  }
129 
130  SpecialMailCollectionsRequestJob *requestJob =
131  qobject_cast<SpecialMailCollectionsRequestJob*>( job );
132  if ( !requestJob ) {
133  return;
134  }
135 
136  // Create item.
137  Item item;
138  item.setMimeType( QLatin1String( "message/rfc822" ) );
139  item.setPayload<Message::Ptr>( message );
140 
141  // Set attributes.
142  item.addAttribute( addressAttribute.clone() );
143  item.addAttribute( dispatchModeAttribute.clone() );
144  item.addAttribute( sentBehaviourAttribute.clone() );
145  item.addAttribute( sentActionAttribute.clone() );
146  item.addAttribute( transportAttribute.clone() );
147 
148  Akonadi::MessageFlags::copyMessageFlags(*message, item);
149 
150  // Set flags.
151  item.setFlag( Akonadi::MessageFlags::Queued );
152 
153  // Store the item in the outbox.
154  const Collection collection = requestJob->collection();
155  Q_ASSERT( collection.isValid() );
156  ItemCreateJob *cjob = new ItemCreateJob( item, collection ); // job autostarts
157  q->addSubjob( cjob );
158 }
159 
160 MessageQueueJob::MessageQueueJob( QObject *parent )
161  : KCompositeJob( parent ), d( new Private( this ) )
162 {
163 }
164 
165 MessageQueueJob::~MessageQueueJob()
166 {
167  delete d;
168 }
169 
170 Message::Ptr MessageQueueJob::message() const
171 {
172  return d->message;
173 }
174 
175 DispatchModeAttribute &MessageQueueJob::dispatchModeAttribute()
176 {
177  return d->dispatchModeAttribute;
178 }
179 
180 AddressAttribute &MessageQueueJob::addressAttribute()
181 {
182  return d->addressAttribute;
183 }
184 
185 TransportAttribute &MessageQueueJob::transportAttribute()
186 {
187  return d->transportAttribute;
188 }
189 
190 SentBehaviourAttribute &MessageQueueJob::sentBehaviourAttribute()
191 {
192  return d->sentBehaviourAttribute;
193 }
194 
195 SentActionAttribute &MessageQueueJob::sentActionAttribute()
196 {
197  return d->sentActionAttribute;
198 }
199 
200 void MessageQueueJob::setMessage( Message::Ptr message )
201 {
202  d->message = message;
203 }
204 
205 void MessageQueueJob::start()
206 {
207  SpecialMailCollectionsRequestJob *rjob = new SpecialMailCollectionsRequestJob( this );
208  rjob->requestDefaultCollection( SpecialMailCollections::Outbox );
209  connect( rjob, SIGNAL(result(KJob*)), this, SLOT(outboxRequestResult(KJob*)) );
210  rjob->start();
211 }
212 
213 void MessageQueueJob::slotResult( KJob *job )
214 {
215  // error handling
216  KCompositeJob::slotResult( job );
217 
218  if ( !error() ) {
219  emitResult();
220  }
221 }
222 
223 #include "moc_messagequeuejob.cpp"
MailTransport::MessageQueueJob::dispatchModeAttribute
DispatchModeAttribute & dispatchModeAttribute()
Returns a reference to the dispatch mode attribue for this message.
Definition: messagequeuejob.cpp:175
MailTransport::SentBehaviourAttribute
Attribute determining what will happen to a message after it is sent.
Definition: sentbehaviourattribute.h:38
MailTransport::MessageQueueJob::~MessageQueueJob
virtual ~MessageQueueJob()
Destroys the MessageQueueJob.
Definition: messagequeuejob.cpp:165
MailTransport::DispatchModeAttribute
Attribute determining how and when a message from the outbox should be dispatched.
Definition: dispatchmodeattribute.h:39
MailTransport::MessageQueueJob
Provides an interface for sending email.
Definition: messagequeuejob.h:86
MailTransport::MessageQueueJob::message
KMime::Message::Ptr message() const
Returns the message to be sent.
Definition: messagequeuejob.cpp:170
MailTransport::MessageQueueJob::transportAttribute
TransportAttribute & transportAttribute()
Returns a reference to the transport attribue for this message.
Definition: messagequeuejob.cpp:185
QObject
MailTransport::MessageQueueJob::start
virtual void start()
Creates the item and places it in the outbox.
Definition: messagequeuejob.cpp:205
MailTransport::MessageQueueJob::sentActionAttribute
SentActionAttribute & sentActionAttribute()
Returns a reference to the sent action attribue for this message.
Definition: messagequeuejob.cpp:195
QLatin1String
MailTransport::MessageQueueJob::setMessage
void setMessage(KMime::Message::Ptr message)
Sets the message to be sent.
Definition: messagequeuejob.cpp:200
MailTransport::MessageQueueJob::addressAttribute
Akonadi::AddressAttribute & addressAttribute()
Returns a reference to the address attribue for this message.
Definition: messagequeuejob.cpp:180
MailTransport::MessageQueueJob::sentBehaviourAttribute
SentBehaviourAttribute & sentBehaviourAttribute()
Returns a reference to the sent behaviour attribue for this message.
Definition: messagequeuejob.cpp:190
MailTransport::SentActionAttribute
An Attribute that stores the action to execute after sending.
Definition: sentactionattribute.h:43
MailTransport::MessageQueueJob::slotResult
virtual void slotResult(KJob *)
Called when the ItemCreateJob subjob finishes.
Definition: messagequeuejob.cpp:213
MailTransport::TransportAttribute
Attribute determining which transport to use for sending a message.
Definition: transportattribute.h:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailtransport

Skip menu "mailtransport"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal