kmail

annotationjobs.cpp

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