kmail

quotajobs.cpp

00001 
00031 #include "quotajobs.h"
00032 #include <kio/scheduler.h>
00033 #include <kdebug.h>
00034 
00035 using namespace KMail;
00036 
00037 QuotaJobs::GetQuotarootJob* QuotaJobs::getQuotaroot(
00038     KIO::Slave* slave, const KURL& url )
00039 {
00040   TQByteArray packedArgs;
00041   TQDataStream stream( packedArgs, IO_WriteOnly );
00042   stream << (int)'Q' << (int)'R' << url;
00043 
00044   GetQuotarootJob* job = new GetQuotarootJob( url, packedArgs, false );
00045   KIO::Scheduler::assignJobToSlave( slave, job );
00046   return job;
00047 }
00048 
00049 QuotaJobs::GetQuotarootJob::GetQuotarootJob( const KURL& url,
00050                                              const TQByteArray &packedArgs,
00051                                              bool showProgressInfo )
00052   : KIO::SimpleJob( url, KIO::CMD_SPECIAL, packedArgs, showProgressInfo )
00053 {
00054   connect( this, TQT_SIGNAL(infoMessage(KIO::Job*,const TQString&)),
00055            TQT_SLOT(slotInfoMessage(KIO::Job*,const TQString&)) );
00056 }
00057 
00058 void QuotaJobs::GetQuotarootJob::slotInfoMessage( KIO::Job*, const TQString& str )
00059 {
00060   // Parse the result
00061   TQStringList results = TQStringList::split("\r", str);
00062   TQStringList roots;
00063   QuotaInfoList quotas;
00064   if ( results.size() > 0 ) {
00065     // the first line is the available roots
00066     roots = TQStringList::split(" ", results.front() );
00067     results.pop_front();
00068     // the rest are pairs of root -> list of triplets
00069     while ( results.size() > 0 ) {
00070       TQString root = results.front(); results.pop_front();
00071       // and the quotas
00072       if ( results.size() > 0 ) {
00073         TQStringList triplets = TQStringList::split(" ", results.front() );
00074         results.pop_front();
00075         while ( triplets.size() > 0 ) {
00076           // there's always three, the label, current and max values
00077           TQString name = triplets.front(); triplets.pop_front();
00078           TQString current = triplets.front(); triplets.pop_front();
00079           TQString max = triplets.front(); triplets.pop_front();
00080           QuotaInfo info( name, root, current, max );
00081           quotas.append( info );
00082         }
00083       }
00084     }
00085   }
00086   if ( !quotas.isEmpty() ) {
00087     emit quotaInfoReceived( quotas );
00088   }
00089   emit quotaRootResult( roots );
00090 }
00091 
00092 QuotaJobs::GetStorageQuotaJob* QuotaJobs::getStorageQuota(
00093     KIO::Slave* slave, const KURL& url )
00094 {
00095   GetStorageQuotaJob* job = new GetStorageQuotaJob( slave, url );
00096   return job;
00097 }
00098 
00099 
00100 QuotaJobs::GetStorageQuotaJob::GetStorageQuotaJob( KIO::Slave* slave, const KURL& url )
00101   : KIO::Job( false )
00102 {
00103     TQByteArray packedArgs;
00104     TQDataStream stream( packedArgs, IO_WriteOnly );
00105     stream << (int)'Q' << (int)'R' << url;
00106 
00107     QuotaJobs::GetQuotarootJob *job =
00108         new QuotaJobs::GetQuotarootJob( url, packedArgs, false );
00109     connect(job, TQT_SIGNAL(quotaInfoReceived(const QuotaInfoList&)),
00110             TQT_SLOT(slotQuotaInfoReceived(const QuotaInfoList&)));
00111     connect(job, TQT_SIGNAL(quotaRootResult(const TQStringList&)),
00112             TQT_SLOT(slotQuotarootResult(const TQStringList&)));
00113     KIO::Scheduler::assignJobToSlave( slave, job );
00114     addSubjob( job );
00115 }
00116 
00117 void QuotaJobs::GetStorageQuotaJob::slotQuotarootResult( const TQStringList& roots )
00118 {
00119     Q_UNUSED(roots); // we only support one for now
00120     if ( !mStorageQuotaInfo.isValid() && !error() ) {
00121       // No error, so the account supports quota, but no usable info
00122       // was transmitted => no quota set on the folder. Make the info
00123       // valid, bit leave it empty.
00124       mStorageQuotaInfo.setName( "STORAGE" );
00125     }
00126     if ( mStorageQuotaInfo.isValid() )
00127       emit storageQuotaResult( mStorageQuotaInfo );
00128 }
00129 
00130 void QuotaJobs::GetStorageQuotaJob::slotQuotaInfoReceived( const QuotaInfoList& infos )
00131 {
00132     QuotaInfoList::ConstIterator it( infos.begin() );
00133     while ( it != infos.end() ) {
00134       // FIXME we only use the first storage quota, for now
00135       if ( it->name() == "STORAGE" && !mStorageQuotaInfo.isValid() ) {
00136           mStorageQuotaInfo = *it;
00137       }
00138       ++it;
00139     }
00140 }
00141 
00142 QuotaInfo QuotaJobs::GetStorageQuotaJob::storageQuotaInfo() const
00143 {
00144   return mStorageQuotaInfo;
00145 }
00146 
00147 #include "quotajobs.moc"