• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

mailtransport

messagequeuejob.cpp

00001 /*
00002     Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "messagequeuejob.h"
00021 
00022 #include "transport.h"
00023 #include "transportattribute.h"
00024 #include "transportmanager.h"
00025 
00026 #include <KDebug>
00027 #include <KLocalizedString>
00028 
00029 #include <akonadi/collection.h>
00030 #include <akonadi/item.h>
00031 #include <akonadi/itemcreatejob.h>
00032 #include <akonadi/kmime/addressattribute.h>
00033 #include <akonadi/kmime/specialmailcollections.h>
00034 #include <akonadi/kmime/specialmailcollectionsrequestjob.h>
00035 
00036 using namespace Akonadi;
00037 using namespace KMime;
00038 using namespace MailTransport;
00039 
00043 class MailTransport::MessageQueueJob::Private
00044 {
00045   public:
00046     Private( MessageQueueJob *qq )
00047       : q( qq )
00048     {
00049       started = false;
00050     }
00051 
00052     MessageQueueJob *const q;
00053 
00054     Message::Ptr message;
00055     TransportAttribute transportAttribute;
00056     DispatchModeAttribute dispatchModeAttribute;
00057     SentBehaviourAttribute sentBehaviourAttribute;
00058     AddressAttribute addressAttribute;
00059     bool started;
00060 
00065     bool validate();
00066 
00067     // slot
00068     void outboxRequestResult( KJob *job );
00069 
00070 };
00071 
00072 bool MessageQueueJob::Private::validate()
00073 {
00074   if( !message ) {
00075     q->setError( UserDefinedError );
00076     q->setErrorText( i18n( "Empty message." ) );
00077     q->emitResult();
00078     return false;
00079   }
00080 
00081   if( addressAttribute.to().count() + addressAttribute.cc().count() +
00082       addressAttribute.bcc().count() == 0 ) {
00083     q->setError( UserDefinedError );
00084     q->setErrorText( i18n( "Message has no recipients." ) );
00085     q->emitResult();
00086     return false;
00087   }
00088 
00089   const int transport = transportAttribute.transportId();
00090   if( TransportManager::self()->transportById( transport, false ) == 0 ) {
00091     q->setError( UserDefinedError );
00092     q->setErrorText( i18n( "Message has invalid transport." ) );
00093     q->emitResult();
00094     return false;
00095   }
00096 
00097   if( sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection &&
00098       !( sentBehaviourAttribute.moveToCollection().isValid() ) ) {
00099     q->setError( UserDefinedError );
00100     q->setErrorText( i18n( "Message has invalid sent-mail folder." ) );
00101     q->emitResult();
00102     return false;
00103   } else if( sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToDefaultSentCollection ) {
00104     // TODO require SpecialMailCollections::SentMail here?
00105   }
00106 
00107   return true; // all ok
00108 }
00109 
00110 void MessageQueueJob::Private::outboxRequestResult( KJob *job )
00111 {
00112   Q_ASSERT( !started );
00113   started = true;
00114 
00115   if( job->error() ) {
00116     kError() << "Failed to get the Outbox folder:" << job->error();
00117     return;
00118   }
00119 
00120   if( !validate() ) {
00121     // The error has been set; the result has been emitted.
00122     return;
00123   }
00124 
00125   SpecialMailCollectionsRequestJob *requestJob = qobject_cast<SpecialMailCollectionsRequestJob*>( job );
00126   if ( !requestJob ) {
00127     return;
00128   }
00129 
00130   // Create item.
00131   Item item;
00132   item.setMimeType( QLatin1String( "message/rfc822" ) );
00133   item.setPayload<Message::Ptr>( message );
00134 
00135   // Set attributes.
00136   item.addAttribute( addressAttribute.clone() );
00137   item.addAttribute( dispatchModeAttribute.clone() );
00138   item.addAttribute( sentBehaviourAttribute.clone() );
00139   item.addAttribute( transportAttribute.clone() );
00140 
00141   // Set flags.
00142   item.setFlag( "queued" );
00143 
00144   // Store the item in the outbox.
00145   const Collection collection = requestJob->collection();
00146   Q_ASSERT( collection.isValid() );
00147   ItemCreateJob *cjob = new ItemCreateJob( item, collection ); // job autostarts
00148   q->addSubjob( cjob );
00149 }
00150 
00151 MessageQueueJob::MessageQueueJob( QObject *parent )
00152   : KCompositeJob( parent ), d( new Private( this ) )
00153 {
00154 }
00155 
00156 MessageQueueJob::~MessageQueueJob()
00157 {
00158   delete d;
00159 }
00160 
00161 Message::Ptr MessageQueueJob::message() const
00162 {
00163   return d->message;
00164 }
00165 
00166 DispatchModeAttribute& MessageQueueJob::dispatchModeAttribute()
00167 {
00168   return d->dispatchModeAttribute;
00169 }
00170 
00171 AddressAttribute& MessageQueueJob::addressAttribute()
00172 {
00173   return d->addressAttribute;
00174 }
00175 
00176 TransportAttribute& MessageQueueJob::transportAttribute()
00177 {
00178   return d->transportAttribute;
00179 }
00180 
00181 SentBehaviourAttribute& MessageQueueJob::sentBehaviourAttribute()
00182 {
00183   return d->sentBehaviourAttribute;
00184 }
00185 
00186 void MessageQueueJob::setMessage( Message::Ptr message )
00187 {                                                                                                             
00188   d->message = message;
00189 }     
00190 
00191 void MessageQueueJob::start()
00192 {
00193   SpecialMailCollectionsRequestJob *rjob = new SpecialMailCollectionsRequestJob( this );
00194   rjob->requestDefaultCollection( SpecialMailCollections::Outbox );
00195   connect( rjob, SIGNAL(result(KJob*)), this, SLOT(outboxRequestResult(KJob*)) );
00196   rjob->start();
00197 }
00198 
00199 void MessageQueueJob::slotResult( KJob *job )
00200 {
00201   // error handling
00202   KCompositeJob::slotResult( job );
00203 
00204   if( !error() ) {
00205     emitResult();
00206   }
00207 }
00208 
00209 #include "messagequeuejob.moc"

mailtransport

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal