00001
00029 #include "listjob.h"
00030 #include "kmfolderimap.h"
00031 #include "kmfoldercachedimap.h"
00032 #include "kmacctimap.h"
00033 #include "kmacctcachedimap.h"
00034 #include "folderstorage.h"
00035 #include "kmfolder.h"
00036 #include "progressmanager.h"
00037 using KPIM::ProgressManager;
00038
00039 #include <kdebug.h>
00040 #include <kurl.h>
00041 #include <kio/scheduler.h>
00042 #include <kio/job.h>
00043 #include <kio/global.h>
00044 #include <klocale.h>
00045
00046 using namespace KMail;
00047
00048 ListJob::ListJob( FolderStorage* storage, ImapAccountBase* account,
00049 ImapAccountBase::ListType type,
00050 bool secondStep, bool complete, bool hasInbox, const QString& path,
00051 KPIM::ProgressItem* item )
00052 : FolderJob( 0, tOther, (storage ? storage->folder() : 0) ),
00053 mStorage( storage ), mAccount( account ), mType( type ),
00054 mHasInbox( hasInbox ), mSecondStep( secondStep ), mComplete( complete ),
00055 mHonorLocalSubscription( false ), mPath( path ),
00056 mParentProgressItem( item )
00057 {
00058 }
00059
00060 ListJob::~ListJob()
00061 {
00062
00063 }
00064
00065 void ListJob::execute()
00066 {
00067 if ( mAccount->makeConnection() == ImapAccountBase::Error )
00068 {
00069 kdWarning(5006) << "ListJob - got no connection" << endl;
00070 deleteLater();
00071 return;
00072 } else if ( mAccount->makeConnection() == ImapAccountBase::Connecting )
00073 {
00074
00075 kdDebug(5006) << "ListJob - waiting for connection" << endl;
00076 connect( mAccount, SIGNAL( connectionResult(int, const QString&) ),
00077 this, SLOT( slotConnectionResult(int, const QString&) ) );
00078 return;
00079 }
00080
00081 if ( mPath.isEmpty() )
00082 {
00083 if ( mStorage && mStorage->folderType() == KMFolderTypeImap ) {
00084 mPath = static_cast<KMFolderImap*>(mStorage)->imapPath();
00085 } else if ( mStorage && mStorage->folderType() == KMFolderTypeCachedImap ) {
00086 mPath = static_cast<KMFolderCachedImap*>(mStorage)->imapPath();
00087 } else {
00088 kdError(5006) << "ListJob - no valid path and no folder given" << endl;
00089 deleteLater();
00090 return;
00091 }
00092 }
00093
00094 ImapAccountBase::jobData jd;
00095 jd.total = 1; jd.done = 0;
00096 jd.cancellable = true;
00097 jd.createInbox = ( mSecondStep && !mHasInbox ) ? true : false;
00098 jd.parent = mDestFolder;
00099 jd.onlySubscribed = ( mType != ImapAccountBase::List );
00100 jd.path = mPath;
00101 QString status = mDestFolder ? mDestFolder->prettyURL() : QString::null;
00102 if ( mParentProgressItem )
00103 {
00104 jd.progressItem = ProgressManager::createProgressItem(
00105 mParentProgressItem,
00106 "ListDir" + ProgressManager::getUniqueID(),
00107 status,
00108 i18n("retrieving folders"),
00109 false,
00110 mAccount->useSSL() || mAccount->useTLS() );
00111 mParentProgressItem->setStatus( status );
00112 }
00113
00114
00115
00116 jd.inboxOnly = !mSecondStep && mAccount->prefix() != "/"
00117 && mPath == mAccount->prefix() && !mHasInbox;
00118
00119 QString ltype = "LIST";
00120 if ( mType == ImapAccountBase::ListSubscribed )
00121 ltype = "LSUB";
00122 else if ( mType == ImapAccountBase::ListSubscribedNoCheck )
00123 ltype = "LSUBNOCHECK";
00124 KURL url = mAccount->getUrl();
00125 url.setPath( ( jd.inboxOnly ? QString("/") : mPath )
00126 + ";TYPE=" + ltype
00127 + ( mComplete ? ";SECTION=COMPLETE" : QString::null) );
00128
00129 KIO::SimpleJob *job = KIO::listDir( url, false );
00130 KIO::Scheduler::assignJobToSlave( mAccount->slave(), job );
00131 mAccount->insertJob( job, jd );
00132 connect( job, SIGNAL(result(KIO::Job *)),
00133 this, SLOT(slotListResult(KIO::Job *)) );
00134 connect( job, SIGNAL(entries(KIO::Job *, const KIO::UDSEntryList &)),
00135 this, SLOT(slotListEntries(KIO::Job *, const KIO::UDSEntryList &)) );
00136 }
00137
00138 void ListJob::slotConnectionResult( int errorCode, const QString& errorMsg )
00139 {
00140 Q_UNUSED( errorMsg );
00141 if ( !errorCode )
00142 execute();
00143 else {
00144 if ( mParentProgressItem )
00145 mParentProgressItem->setComplete();
00146 deleteLater();
00147 }
00148 }
00149
00150 void ListJob::slotListResult( KIO::Job* job )
00151 {
00152 ImapAccountBase::JobIterator it = mAccount->findJob( job );
00153 if ( it == mAccount->jobsEnd() )
00154 {
00155 deleteLater();
00156 return;
00157 }
00158 if ( job->error() )
00159 {
00160 mAccount->handleJobError( job,
00161 i18n( "Error while listing folder %1: " ).arg((*it).path),
00162 true );
00163 } else
00164 {
00165
00166 emit receivedFolders( mSubfolderNames, mSubfolderPaths,
00167 mSubfolderMimeTypes, mSubfolderAttributes, *it );
00168 mAccount->removeJob( it );
00169 }
00170 deleteLater();
00171 }
00172
00173 void ListJob::slotListEntries( KIO::Job* job, const KIO::UDSEntryList& uds )
00174 {
00175 ImapAccountBase::JobIterator it = mAccount->findJob( job );
00176 if ( it == mAccount->jobsEnd() )
00177 {
00178 deleteLater();
00179 return;
00180 }
00181 if( (*it).progressItem )
00182 (*it).progressItem->setProgress( 50 );
00183 QString name;
00184 KURL url;
00185 QString mimeType;
00186 QString attributes;
00187 for ( KIO::UDSEntryList::ConstIterator udsIt = uds.begin();
00188 udsIt != uds.end(); udsIt++ )
00189 {
00190 mimeType = QString::null;
00191 attributes = QString::null;
00192 for ( KIO::UDSEntry::ConstIterator eIt = (*udsIt).begin();
00193 eIt != (*udsIt).end(); eIt++ )
00194 {
00195
00196 if ( (*eIt).m_uds == KIO::UDS_NAME )
00197 name = (*eIt).m_str;
00198 else if ( (*eIt).m_uds == KIO::UDS_URL )
00199 url = KURL((*eIt).m_str, 106);
00200 else if ( (*eIt).m_uds == KIO::UDS_MIME_TYPE )
00201 mimeType = (*eIt).m_str;
00202 else if ( (*eIt).m_uds == KIO::UDS_EXTRA )
00203 attributes = (*eIt).m_str;
00204 }
00205 if ( (mimeType == "inode/directory" || mimeType == "message/digest"
00206 || mimeType == "message/directory")
00207 && name != ".." && (mAccount->hiddenFolders() || name.at(0) != '.')
00208 && (!(*it).inboxOnly || name.upper() == "INBOX") )
00209 {
00210 if ( ((*it).inboxOnly ||
00211 url.path() == "/INBOX/") && name.upper() == "INBOX" &&
00212 !mHasInbox )
00213 {
00214
00215 (*it).createInbox = true;
00216 }
00217
00218 if ( mHonorLocalSubscription && mAccount->onlyLocallySubscribedFolders()
00219 && !mAccount->locallySubscribedTo( url.path() ) ) {
00220 continue;
00221 }
00222
00223
00224
00225 if ( mSubfolderPaths.count() > 100 ||
00226 mSubfolderPaths.findIndex(url.path()) == -1 )
00227 {
00228 mSubfolderNames.append( name );
00229 mSubfolderPaths.append( url.path() );
00230 mSubfolderMimeTypes.append( mimeType );
00231 mSubfolderAttributes.append( attributes );
00232 }
00233 }
00234 }
00235 }
00236
00237
00238 void KMail::ListJob::setHonorLocalSubscription( bool value )
00239 {
00240 mHonorLocalSubscription = value;
00241 }
00242
00243 bool KMail::ListJob::honorLocalSubscription() const
00244 {
00245 return mHonorLocalSubscription;
00246 }
00247
00248 #include "listjob.moc"