00001
00033 #include "annotationjobs.h"
00034 #include <kio/scheduler.h>
00035 #include <kdebug.h>
00036
00037 using namespace KMail;
00038
00039 KIO::SimpleJob* AnnotationJobs::setAnnotation(
00040 KIO::Slave* slave, const KUrl& url, const QString& entry,
00041 const QMap<QString,QString>& attributes )
00042 {
00043 QByteArray packedArgs;
00044 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
00045 stream << (int)'M' << (int)'S' << url << entry << attributes;
00046
00047 KIO::SimpleJob* job = KIO::special( url, packedArgs, KIO::HideProgressInfo );
00048 KIO::Scheduler::assignJobToSlave( slave, job );
00049 return job;
00050 }
00051
00052 AnnotationJobs::GetAnnotationJob* AnnotationJobs::getAnnotation(
00053 KIO::Slave* slave, const KUrl& url, const QString& entry,
00054 const QStringList& attributes )
00055 {
00056 QByteArray packedArgs;
00057 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
00058 stream << (int)'M' << (int)'G' << url << entry << attributes;
00059
00060 GetAnnotationJob* job = new GetAnnotationJob( url, entry, packedArgs);
00061 KIO::Scheduler::assignJobToSlave( slave, job );
00062 return job;
00063 }
00064
00065 AnnotationJobs::GetAnnotationJob::GetAnnotationJob( const KUrl& url, const QString& entry,
00066 const QByteArray &packedArgs)
00067 : KIO::SpecialJob( url, packedArgs),
00068 mEntry( entry )
00069 {
00070 connect( this, SIGNAL(infoMessage(KJob*,const QString&,const QString&)),
00071 SLOT(slotInfoMessage(KJob*,const QString&,const QString&)) );
00072 }
00073
00074 void AnnotationJobs::GetAnnotationJob::slotInfoMessage( KJob*, const QString& str,const QString& )
00075 {
00076
00077 QStringList lst = str.split( "\r", QString::SkipEmptyParts );
00078 while ( lst.count() >= 2 )
00079 {
00080 QString name = lst.front(); lst.pop_front();
00081 QString value = lst.front(); lst.pop_front();
00082 mAnnotations.append( AnnotationAttribute( mEntry, name, value ) );
00083 }
00084 }
00085
00086 AnnotationJobs::MultiGetAnnotationJob::MultiGetAnnotationJob(
00087 KIO::Slave* slave, const KUrl& url, const QStringList& entries )
00088 : KIO::Job(),
00089 mSlave( slave ),
00090 mUrl( url ), mEntryList( entries ), mEntryListIterator( mEntryList.begin() )
00091 {
00092 QTimer::singleShot(0, this, SLOT(slotStart()));
00093 }
00094
00095
00096 void AnnotationJobs::MultiGetAnnotationJob::slotStart()
00097 {
00098 if ( mEntryListIterator != mEntryList.end() ) {
00099 QStringList attributes;
00100 attributes << "value";
00101 KIO::Job* job = getAnnotation( mSlave, mUrl, *mEntryListIterator, attributes );
00102 addSubjob( job );
00103 } else {
00104 emitResult();
00105 }
00106 }
00107
00108 void AnnotationJobs::MultiGetAnnotationJob::slotResult( KJob *job )
00109 {
00110 if ( job->error() ) {
00111 KIO::Job::slotResult( job );
00112 return;
00113 }
00114 removeSubjob( job );
00115 const QString& entry = *mEntryListIterator;
00116 QString value;
00117 bool found = false;
00118 GetAnnotationJob* getJob = static_cast<GetAnnotationJob *>( job );
00119 const AnnotationList& lst = getJob->annotations();
00120 for ( int i = 0 ; i < lst.size() ; ++ i ) {
00121 kDebug(5006) <<"found annotation" << lst[i].name <<" =" << lst[i].value;
00122 if ( lst[i].name.startsWith( "value." ) ) {
00123 found = true;
00124 value = lst[i].value;
00125 break;
00126 }
00127 }
00128 emit annotationResult( entry, value, found );
00129
00130 ++mEntryListIterator;
00131 slotStart();
00132 }
00133
00134 AnnotationJobs::MultiGetAnnotationJob* AnnotationJobs::multiGetAnnotation( KIO::Slave* slave, const KUrl& url, const QStringList& entries )
00135 {
00136 return new MultiGetAnnotationJob( slave, url, entries );
00137 }
00138
00140
00141 AnnotationJobs::MultiSetAnnotationJob::MultiSetAnnotationJob(
00142 KIO::Slave* slave, const KUrl& url, const AnnotationList& annotations )
00143 : KIO::Job(),
00144 mSlave( slave ),
00145 mUrl( url ), mAnnotationList( annotations ), mAnnotationListIterator( mAnnotationList.begin() )
00146 {
00147 QTimer::singleShot(0, this, SLOT(slotStart()));
00148 }
00149
00150
00151 void AnnotationJobs::MultiSetAnnotationJob::slotStart()
00152 {
00153 if ( mAnnotationListIterator != mAnnotationList.end() ) {
00154 const AnnotationAttribute& attr = *mAnnotationListIterator;
00155
00156
00157 QMap<QString, QString> attributes;
00158 attributes.insert( attr.name, attr.value );
00159 kDebug(5006) <<" setting annotation" << attr.entry << attr.name << attr.value;
00160 KIO::Job* job = setAnnotation( mSlave, mUrl, attr.entry, attributes );
00161 addSubjob( job );
00162 } else {
00163 emitResult();
00164 }
00165 }
00166
00167 void AnnotationJobs::MultiSetAnnotationJob::slotResult( KJob *job )
00168 {
00169 if ( job->error() ) {
00170 KIO::Job::slotResult( job );
00171 return;
00172 }
00173 removeSubjob( job );
00174 const AnnotationAttribute& attr = *mAnnotationListIterator;
00175 emit annotationChanged( attr.entry, attr.name, attr.value );
00176
00177
00178 ++mAnnotationListIterator;
00179 slotStart();
00180 }
00181
00182 AnnotationJobs::MultiSetAnnotationJob* AnnotationJobs::multiSetAnnotation(
00183 KIO::Slave* slave, const KUrl& url, const AnnotationList& annotations )
00184 {
00185 return new MultiSetAnnotationJob( slave, url, annotations );
00186 }
00187
00188
00189 AnnotationJobs::MultiUrlGetAnnotationJob::MultiUrlGetAnnotationJob( KIO::Slave* slave,
00190 const KUrl& baseUrl,
00191 const QStringList& paths,
00192 const QString& annotation )
00193 : KIO::Job(),
00194 mSlave( slave ),
00195 mUrl( baseUrl ),
00196 mPathList( paths ),
00197 mPathListIterator( mPathList.begin() ),
00198 mAnnotation( annotation )
00199 {
00200 QTimer::singleShot(0, this, SLOT(slotStart()));
00201 }
00202
00203
00204 void AnnotationJobs::MultiUrlGetAnnotationJob::slotStart()
00205 {
00206 if ( mPathListIterator != mPathList.end() ) {
00207 QStringList attributes;
00208 attributes << "value";
00209 KUrl url(mUrl);
00210 url.setPath( *mPathListIterator );
00211 KIO::Job* job = getAnnotation( mSlave, url, mAnnotation, attributes );
00212 addSubjob( job );
00213 } else {
00214 emitResult();
00215 }
00216 }
00217
00218 void AnnotationJobs::MultiUrlGetAnnotationJob::slotResult( KJob *job )
00219 {
00220 if ( job->error() ) {
00221 KIO::Job::slotResult( job );
00222 return;
00223 }
00224 removeSubjob( job );
00225 const QString& path = *mPathListIterator;
00226 GetAnnotationJob* getJob = static_cast<GetAnnotationJob *>( job );
00227 const AnnotationList& lst = getJob->annotations();
00228 for ( int i = 0 ; i < lst.size() ; ++ i ) {
00229 kDebug(5006) <<"MultiURL: found annotation" << lst[i].name <<" =" << lst[i].value <<" for path:" << path;
00230 if ( lst[i].name.startsWith( "value." ) ) {
00231 mAnnotations.insert( path, lst[i].value );
00232 break;
00233 }
00234 }
00235
00236 ++mPathListIterator;
00237 slotStart();
00238 }
00239
00240 QMap<QString, QString> AnnotationJobs::MultiUrlGetAnnotationJob::annotations() const
00241 {
00242 return mAnnotations;
00243 }
00244
00245 AnnotationJobs::MultiUrlGetAnnotationJob* AnnotationJobs::multiUrlGetAnnotation( KIO::Slave* slave,
00246 const KUrl& baseUrl,
00247 const QStringList& paths,
00248 const QString& annotation )
00249 {
00250 return new MultiUrlGetAnnotationJob( slave, baseUrl, paths, annotation );
00251 }
00252
00253
00254 #include "annotationjobs.moc"