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/messageflags.h> 00034 #include <akonadi/kmime/specialmailcollections.h> 00035 #include <akonadi/kmime/specialmailcollectionsrequestjob.h> 00036 00037 using namespace Akonadi; 00038 using namespace KMime; 00039 using namespace MailTransport; 00040 00044 class MailTransport::MessageQueueJob::Private 00045 { 00046 public: 00047 Private( MessageQueueJob *qq ) 00048 : q( qq ) 00049 { 00050 started = false; 00051 } 00052 00053 MessageQueueJob *const q; 00054 00055 Message::Ptr message; 00056 TransportAttribute transportAttribute; 00057 DispatchModeAttribute dispatchModeAttribute; 00058 SentBehaviourAttribute sentBehaviourAttribute; 00059 SentActionAttribute sentActionAttribute; 00060 AddressAttribute addressAttribute; 00061 bool started; 00062 00067 bool validate(); 00068 00069 // slot 00070 void outboxRequestResult( KJob *job ); 00071 00072 }; 00073 00074 bool MessageQueueJob::Private::validate() 00075 { 00076 if( !message ) { 00077 q->setError( UserDefinedError ); 00078 q->setErrorText( i18n( "Empty message." ) ); 00079 q->emitResult(); 00080 return false; 00081 } 00082 00083 if( addressAttribute.to().count() + addressAttribute.cc().count() + 00084 addressAttribute.bcc().count() == 0 ) { 00085 q->setError( UserDefinedError ); 00086 q->setErrorText( i18n( "Message has no recipients." ) ); 00087 q->emitResult(); 00088 return false; 00089 } 00090 00091 const int transport = transportAttribute.transportId(); 00092 if( TransportManager::self()->transportById( transport, false ) == 0 ) { 00093 q->setError( UserDefinedError ); 00094 q->setErrorText( i18n( "Message has invalid transport." ) ); 00095 q->emitResult(); 00096 return false; 00097 } 00098 00099 if( sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection && 00100 !( sentBehaviourAttribute.moveToCollection().isValid() ) ) { 00101 q->setError( UserDefinedError ); 00102 q->setErrorText( i18n( "Message has invalid sent-mail folder." ) ); 00103 q->emitResult(); 00104 return false; 00105 } else if( sentBehaviourAttribute.sentBehaviour() == 00106 SentBehaviourAttribute::MoveToDefaultSentCollection ) { 00107 // TODO require SpecialMailCollections::SentMail here? 00108 } 00109 00110 return true; // all ok 00111 } 00112 00113 void MessageQueueJob::Private::outboxRequestResult( KJob *job ) 00114 { 00115 Q_ASSERT( !started ); 00116 started = true; 00117 00118 if( job->error() ) { 00119 kError() << "Failed to get the Outbox folder:" << job->error() << job->errorString(); 00120 q->setError( job->error() ); 00121 q->emitResult(); 00122 return; 00123 } 00124 00125 if( !validate() ) { 00126 // The error has been set; the result has been emitted. 00127 return; 00128 } 00129 00130 SpecialMailCollectionsRequestJob *requestJob = 00131 qobject_cast<SpecialMailCollectionsRequestJob*>( job ); 00132 if ( !requestJob ) { 00133 return; 00134 } 00135 00136 // Create item. 00137 Item item; 00138 item.setMimeType( QLatin1String( "message/rfc822" ) ); 00139 item.setPayload<Message::Ptr>( message ); 00140 00141 // Set attributes. 00142 item.addAttribute( addressAttribute.clone() ); 00143 item.addAttribute( dispatchModeAttribute.clone() ); 00144 item.addAttribute( sentBehaviourAttribute.clone() ); 00145 item.addAttribute( sentActionAttribute.clone() ); 00146 item.addAttribute( transportAttribute.clone() ); 00147 00148 // Set flags. 00149 item.setFlag( Akonadi::MessageFlags::Queued ); 00150 00151 // Store the item in the outbox. 00152 const Collection collection = requestJob->collection(); 00153 Q_ASSERT( collection.isValid() ); 00154 ItemCreateJob *cjob = new ItemCreateJob( item, collection ); // job autostarts 00155 q->addSubjob( cjob ); 00156 } 00157 00158 MessageQueueJob::MessageQueueJob( QObject *parent ) 00159 : KCompositeJob( parent ), d( new Private( this ) ) 00160 { 00161 } 00162 00163 MessageQueueJob::~MessageQueueJob() 00164 { 00165 delete d; 00166 } 00167 00168 Message::Ptr MessageQueueJob::message() const 00169 { 00170 return d->message; 00171 } 00172 00173 DispatchModeAttribute &MessageQueueJob::dispatchModeAttribute() 00174 { 00175 return d->dispatchModeAttribute; 00176 } 00177 00178 AddressAttribute &MessageQueueJob::addressAttribute() 00179 { 00180 return d->addressAttribute; 00181 } 00182 00183 TransportAttribute &MessageQueueJob::transportAttribute() 00184 { 00185 return d->transportAttribute; 00186 } 00187 00188 SentBehaviourAttribute &MessageQueueJob::sentBehaviourAttribute() 00189 { 00190 return d->sentBehaviourAttribute; 00191 } 00192 00193 SentActionAttribute &MessageQueueJob::sentActionAttribute() 00194 { 00195 return d->sentActionAttribute; 00196 } 00197 00198 void MessageQueueJob::setMessage( Message::Ptr message ) 00199 { 00200 d->message = message; 00201 } 00202 00203 void MessageQueueJob::start() 00204 { 00205 SpecialMailCollectionsRequestJob *rjob = new SpecialMailCollectionsRequestJob( this ); 00206 rjob->requestDefaultCollection( SpecialMailCollections::Outbox ); 00207 connect( rjob, SIGNAL(result(KJob*)), this, SLOT(outboxRequestResult(KJob*)) ); 00208 rjob->start(); 00209 } 00210 00211 void MessageQueueJob::slotResult( KJob *job ) 00212 { 00213 // error handling 00214 KCompositeJob::slotResult( job ); 00215 00216 if( !error() ) { 00217 emitResult(); 00218 } 00219 } 00220 00221 #include "messagequeuejob.moc"