00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "subscriptionjob_p.h"
00021
00022 #include "job_p.h"
00023
00024 using namespace Akonadi;
00025
00026 class Akonadi::SubscriptionJobPrivate : public JobPrivate
00027 {
00028 public:
00029 SubscriptionJobPrivate( SubscriptionJob *parent )
00030 : JobPrivate( parent )
00031 {
00032 }
00033
00034 void sendCommand( const QByteArray &cmd, const Collection::List &list )
00035 {
00036 mTag = newTag();
00037 QByteArray line = mTag + ' ' + cmd;
00038 foreach ( const Collection &col, list )
00039 line += ' ' + QByteArray::number( col.id() );
00040 line += '\n';
00041 writeData( line );
00042 newTag();
00043 }
00044
00045 void sendNextCommand()
00046 {
00047 Q_Q( SubscriptionJob );
00048
00049 QByteArray cmd;
00050 if ( !mSub.isEmpty() ) {
00051 sendCommand( "SUBSCRIBE", mSub );
00052 mSub.clear();
00053 } else if ( !mUnsub.isEmpty() ) {
00054 sendCommand( "UNSUBSCRIBE", mUnsub );
00055 mUnsub.clear();
00056 } else {
00057 q->emitResult();
00058 }
00059 }
00060
00061 Q_DECLARE_PUBLIC( SubscriptionJob )
00062
00063 QByteArray mTag;
00064 Collection::List mSub, mUnsub;
00065 };
00066
00067 SubscriptionJob::SubscriptionJob(QObject * parent)
00068 : Job( new SubscriptionJobPrivate( this ), parent )
00069 {
00070 }
00071
00072 SubscriptionJob::~SubscriptionJob()
00073 {
00074 }
00075
00076 void SubscriptionJob::subscribe(const Collection::List & list)
00077 {
00078 Q_D( SubscriptionJob );
00079
00080 d->mSub = list;
00081 }
00082
00083 void SubscriptionJob::unsubscribe(const Collection::List & list)
00084 {
00085 Q_D( SubscriptionJob );
00086
00087 d->mUnsub = list;
00088 }
00089
00090 void SubscriptionJob::doStart()
00091 {
00092 Q_D( SubscriptionJob );
00093
00094 d->sendNextCommand();
00095 }
00096
00097 void SubscriptionJob::doHandleResponse(const QByteArray &_tag, const QByteArray & data)
00098 {
00099 Q_D( SubscriptionJob );
00100
00101 if ( _tag == d->mTag ) {
00102 if ( data.startsWith( "OK" ) ) {
00103 d->sendNextCommand();
00104 } else {
00105 setError( Unknown );
00106 setErrorText( QString::fromUtf8( data ) );
00107 emitResult();
00108 }
00109 return;
00110 }
00111 }
00112
00113 #include "subscriptionjob_p.moc"