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