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

mailtransport

  • 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  // update status flags
149  if ( KMime::isSigned( message.get() ) )
150  item.setFlag( Akonadi::MessageFlags::Signed );
151  if ( KMime::isEncrypted( message.get() ) )
152  item.setFlag( Akonadi::MessageFlags::Encrypted );
153  if ( KMime::isInvitation( message.get() ) )
154  item.setFlag( Akonadi::MessageFlags::HasInvitation );
155  if ( KMime::hasAttachment( message.get() ) )
156  item.setFlag( Akonadi::MessageFlags::HasAttachment );
157 
158 
159  // Set flags.
160  item.setFlag( Akonadi::MessageFlags::Queued );
161 
162  // Store the item in the outbox.
163  const Collection collection = requestJob->collection();
164  Q_ASSERT( collection.isValid() );
165  ItemCreateJob *cjob = new ItemCreateJob( item, collection ); // job autostarts
166  q->addSubjob( cjob );
167 }
168 
169 MessageQueueJob::MessageQueueJob( QObject *parent )
170  : KCompositeJob( parent ), d( new Private( this ) )
171 {
172 }
173 
174 MessageQueueJob::~MessageQueueJob()
175 {
176  delete d;
177 }
178 
179 Message::Ptr MessageQueueJob::message() const
180 {
181  return d->message;
182 }
183 
184 DispatchModeAttribute &MessageQueueJob::dispatchModeAttribute()
185 {
186  return d->dispatchModeAttribute;
187 }
188 
189 AddressAttribute &MessageQueueJob::addressAttribute()
190 {
191  return d->addressAttribute;
192 }
193 
194 TransportAttribute &MessageQueueJob::transportAttribute()
195 {
196  return d->transportAttribute;
197 }
198 
199 SentBehaviourAttribute &MessageQueueJob::sentBehaviourAttribute()
200 {
201  return d->sentBehaviourAttribute;
202 }
203 
204 SentActionAttribute &MessageQueueJob::sentActionAttribute()
205 {
206  return d->sentActionAttribute;
207 }
208 
209 void MessageQueueJob::setMessage( Message::Ptr message )
210 {
211  d->message = message;
212 }
213 
214 void MessageQueueJob::start()
215 {
216  SpecialMailCollectionsRequestJob *rjob = new SpecialMailCollectionsRequestJob( this );
217  rjob->requestDefaultCollection( SpecialMailCollections::Outbox );
218  connect( rjob, SIGNAL(result(KJob*)), this, SLOT(outboxRequestResult(KJob*)) );
219  rjob->start();
220 }
221 
222 void MessageQueueJob::slotResult( KJob *job )
223 {
224  // error handling
225  KCompositeJob::slotResult( job );
226 
227  if( !error() ) {
228  emitResult();
229  }
230 }
231 
232 #include "messagequeuejob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:05:23 by doxygen 1.8.1.1 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-4.9.1 API Reference

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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