akonadi
subscriptionjob.cpp
00001 /* 00002 Copyright (c) 2007 Volker Krause <vkrause@kde.org> 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 "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(); // prevent automatic response handling 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"