20 #include "messagequeuejob.h"
22 #include "transport.h"
23 #include "transportattribute.h"
24 #include "transportmanager.h"
27 #include <KLocalizedString>
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>
37 using namespace Akonadi;
38 using namespace KMime;
39 using namespace MailTransport;
44 class MailTransport::MessageQueueJob::Private
60 AddressAttribute addressAttribute;
70 void outboxRequestResult( KJob *job );
74 bool MessageQueueJob::Private::validate()
77 q->setError( UserDefinedError );
78 q->setErrorText( i18n(
"Empty message." ) );
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." ) );
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." ) );
99 if ( sentBehaviourAttribute.sentBehaviour() == SentBehaviourAttribute::MoveToCollection &&
100 !( sentBehaviourAttribute.moveToCollection().isValid() ) ) {
101 q->setError( UserDefinedError );
102 q->setErrorText( i18n(
"Message has invalid sent-mail folder." ) );
105 }
else if ( sentBehaviourAttribute.sentBehaviour() ==
106 SentBehaviourAttribute::MoveToDefaultSentCollection ) {
113 void MessageQueueJob::Private::outboxRequestResult( KJob *job )
115 Q_ASSERT( !started );
118 if ( job->error() ) {
119 kError() <<
"Failed to get the Outbox folder:" << job->error() << job->errorString();
120 q->setError( job->error() );
130 SpecialMailCollectionsRequestJob *requestJob =
131 qobject_cast<SpecialMailCollectionsRequestJob*>( job );
139 item.setPayload<Message::Ptr>( message );
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() );
148 Akonadi::MessageFlags::copyMessageFlags(*message, item);
151 item.setFlag( Akonadi::MessageFlags::Queued );
154 const Collection collection = requestJob->collection();
155 Q_ASSERT( collection.isValid() );
156 ItemCreateJob *cjob =
new ItemCreateJob( item, collection );
157 q->addSubjob( cjob );
160 MessageQueueJob::MessageQueueJob(
QObject *parent )
161 : KCompositeJob( parent ), d( new Private( this ) )
177 return d->dispatchModeAttribute;
182 return d->addressAttribute;
187 return d->transportAttribute;
192 return d->sentBehaviourAttribute;
197 return d->sentActionAttribute;
207 SpecialMailCollectionsRequestJob *rjob =
new SpecialMailCollectionsRequestJob(
this );
208 rjob->requestDefaultCollection( SpecialMailCollections::Outbox );
209 connect( rjob, SIGNAL(result(KJob*)),
this, SLOT(outboxRequestResult(KJob*)) );
216 KCompositeJob::slotResult( job );
223 #include "moc_messagequeuejob.cpp"
DispatchModeAttribute & dispatchModeAttribute()
Returns a reference to the dispatch mode attribue for this message.
Attribute determining what will happen to a message after it is sent.
virtual ~MessageQueueJob()
Destroys the MessageQueueJob.
Attribute determining how and when a message from the outbox should be dispatched.
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 attribue for this message.
virtual void start()
Creates the item and places it in the outbox.
SentActionAttribute & sentActionAttribute()
Returns a reference to the sent action attribue for this message.
void setMessage(KMime::Message::Ptr message)
Sets the message to be sent.
Akonadi::AddressAttribute & addressAttribute()
Returns a reference to the address attribue for this message.
SentBehaviourAttribute & sentBehaviourAttribute()
Returns a reference to the sent behaviour attribue for this message.
An Attribute that stores the action to execute after sending.
virtual void slotResult(KJob *)
Called when the ItemCreateJob subjob finishes.
Attribute determining which transport to use for sending a message.