kmail

annotationjobs.cpp

00001 
00031 #include "annotationjobs.h"
00032 #include <tdeio/scheduler.h>
00033 #include <kdebug.h>
00034 
00035 using namespace KMail;
00036 
00037 TDEIO::SimpleJob* AnnotationJobs::setAnnotation(
00038     TDEIO::Slave* slave, const KURL& url, const TQString& entry,
00039     const TQMap<TQString,TQString>& attributes )
00040 {
00041   TQByteArray packedArgs;
00042   TQDataStream stream( packedArgs, IO_WriteOnly );
00043   stream << (int)'M' << (int)'S' << url << entry << attributes;
00044 
00045   TDEIO::SimpleJob* job = TDEIO::special( url, packedArgs, false );
00046   TDEIO::Scheduler::assignJobToSlave( slave, job );
00047   return job;
00048 }
00049 
00050 AnnotationJobs::GetAnnotationJob* AnnotationJobs::getAnnotation(
00051     TDEIO::Slave* slave, const KURL& url, const TQString& entry,
00052     const TQStringList& attributes )
00053 {
00054   TQByteArray packedArgs;
00055   TQDataStream stream( packedArgs, IO_WriteOnly );
00056   stream << (int)'M' << (int)'G' << url << entry << attributes;
00057 
00058   GetAnnotationJob* job = new GetAnnotationJob( url, entry, packedArgs, false );
00059   TDEIO::Scheduler::assignJobToSlave( slave, job );
00060   return job;
00061 }
00062 
00063 AnnotationJobs::GetAnnotationJob::GetAnnotationJob( const KURL& url, const TQString& entry,
00064                                                     const TQByteArray &packedArgs,
00065                                                     bool showProgressInfo )
00066   : TDEIO::SimpleJob( url, TDEIO::CMD_SPECIAL, packedArgs, showProgressInfo ),
00067     mEntry( entry )
00068 {
00069   connect( this, TQT_SIGNAL(infoMessage(TDEIO::Job*,const TQString&)),
00070            TQT_SLOT(slotInfoMessage(TDEIO::Job*,const TQString&)) );
00071 }
00072 
00073 void AnnotationJobs::GetAnnotationJob::slotInfoMessage( TDEIO::Job*, const TQString& str )
00074 {
00075   // Parse the result
00076   TQStringList lst = TQStringList::split( "\r", str );
00077   while ( lst.count() >= 2 ) // we take items 2 by 2
00078   {
00079     TQString name = lst.front(); lst.pop_front();
00080     TQString value = lst.front(); lst.pop_front();
00081     mAnnotations.append( AnnotationAttribute( mEntry, name, value ) );
00082   }
00083 }
00084 
00085 AnnotationJobs::MultiGetAnnotationJob::MultiGetAnnotationJob(
00086   TDEIO::Slave* slave, const KURL& url, const TQStringList& entries, bool showProgressInfo )
00087   : TDEIO::Job( showProgressInfo ),
00088     mSlave( slave ),
00089     mUrl( url ), mEntryList( entries ), mEntryListIterator( mEntryList.begin() )
00090 {
00091   TQTimer::singleShot(0, this, TQT_SLOT(slotStart()));
00092 }
00093 
00094 
00095 void AnnotationJobs::MultiGetAnnotationJob::slotStart()
00096 {
00097   if ( mEntryListIterator != mEntryList.end() ) {
00098     TQStringList attributes;
00099     attributes << "value";
00100     TDEIO::Job* job = getAnnotation( mSlave, mUrl, *mEntryListIterator, attributes );
00101     addSubjob( job );
00102   } else { // done!
00103     emitResult();
00104   }
00105 }
00106 
00107 void AnnotationJobs::MultiGetAnnotationJob::slotResult( TDEIO::Job *job )
00108 {
00109   if ( job->error() ) {
00110     TDEIO::Job::slotResult( job ); // will set the error and emit result(this)
00111     return;
00112   }
00113   subjobs.remove( job );
00114   const TQString& entry = *mEntryListIterator;
00115   TQString 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( TDEIO::Slave* slave, const KURL& url, const TQStringList& entries )
00134 {
00135   return new MultiGetAnnotationJob( slave, url, entries, false /*showProgressInfo*/ );
00136 }
00137 
00139 
00140 AnnotationJobs::MultiSetAnnotationJob::MultiSetAnnotationJob(
00141   TDEIO::Slave* slave, const KURL& url, const AnnotationList& annotations, bool showProgressInfo )
00142   : TDEIO::Job( showProgressInfo ),
00143     mSlave( slave ),
00144     mUrl( url ), mAnnotationList( annotations ), mAnnotationListIterator( mAnnotationList.begin() )
00145 {
00146   TQTimer::singleShot(0, this, TQT_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     TQMap<TQString, TQString> attributes;
00157     attributes.insert( attr.name, attr.value );
00158     kdDebug() << k_funcinfo << " setting annotation " << attr.entry << " " << attr.name << " " << attr.value << endl;
00159     TDEIO::Job* job = setAnnotation( mSlave, mUrl, attr.entry, attributes );
00160     addSubjob( job );
00161   } else { // done!
00162     emitResult();
00163   }
00164 }
00165 
00166 void AnnotationJobs::MultiSetAnnotationJob::slotResult( TDEIO::Job *job )
00167 {
00168   if ( job->error() ) {
00169     TDEIO::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   TDEIO::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( TDEIO::Slave* slave,
00189                                                                     const KURL& baseUrl,
00190                                                                     const TQStringList& paths,
00191                                                                     const TQString& annotation )
00192   : TDEIO::Job( false ),
00193     mSlave( slave ),
00194     mUrl( baseUrl ),
00195     mPathList( paths ),
00196     mPathListIterator( mPathList.begin() ),
00197     mAnnotation( annotation )
00198 {
00199   TQTimer::singleShot(0, this, TQT_SLOT(slotStart()));
00200 }
00201 
00202 
00203 void AnnotationJobs::MultiUrlGetAnnotationJob::slotStart()
00204 {
00205   if ( mPathListIterator != mPathList.end() ) {
00206     TQStringList attributes;
00207     attributes << "value";
00208     KURL url(mUrl);
00209     url.setPath( *mPathListIterator );
00210     TDEIO::Job* job = getAnnotation( mSlave, url, mAnnotation, attributes );
00211     addSubjob( job );
00212   } else { // done!
00213     emitResult();
00214   }
00215 }
00216 
00217 void AnnotationJobs::MultiUrlGetAnnotationJob::slotResult( TDEIO::Job *job )
00218 {
00219   if ( job->error() ) {
00220     TDEIO::Job::slotResult( job ); // will set the error and emit result(this)
00221     return;
00222   }
00223   subjobs.remove( job );
00224   const TQString& 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 TQMap<TQString, TQString> AnnotationJobs::MultiUrlGetAnnotationJob::annotations() const
00240 {
00241   return mAnnotations;
00242 }
00243 
00244 AnnotationJobs::MultiUrlGetAnnotationJob* AnnotationJobs::multiUrlGetAnnotation( TDEIO::Slave* slave,
00245                                                                                  const KURL& baseUrl,
00246                                                                                  const TQStringList& paths,
00247                                                                                  const TQString& annotation )
00248 {
00249   return new MultiUrlGetAnnotationJob( slave, baseUrl, paths, annotation );
00250 }
00251 
00252 
00253 #include "annotationjobs.moc"