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