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