kmail

listjob.cpp

00001 /*
00002  * Copyright (c) 2004 Carsten Burghardt <burghardt@kde.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; version 2 of the License
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00016  *
00017  *  In addition, as a special exception, the copyright holders give
00018  *  permission to link the code of this program with any edition of
00019  *  the Qt library by Trolltech AS, Norway (or with modified versions
00020  *  of Qt that use the same license as Qt), and distribute linked
00021  *  combinations including the two.  You must obey the GNU General
00022  *  Public License in all respects for all of the code used other than
00023  *  Qt.  If you modify this file, you may extend this exception to
00024  *  your version of the file, but you are not obligated to do so.  If
00025  *  you do not wish to do so, delete this exception statement from
00026  *  your version.
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 <stdlib.h>
00048 
00049 using namespace KMail;
00050 
00051 ListJob::ListJob( ImapAccountBase* account, ImapAccountBase::ListType type,
00052     FolderStorage* storage, const QString& path, bool complete,
00053     KPIM::ProgressItem* item )
00054  : FolderJob( 0, tOther, (storage ? storage->folder() : 0) ),
00055    mStorage( storage ), mAccount( account ), mType( type ),
00056    mComplete( complete ), mPath( path ),
00057    mParentProgressItem( item )
00058 {
00059 }
00060 
00061 ListJob::~ListJob()
00062 {
00063 }
00064 
00065 void ListJob::execute()
00066 {
00067   if ( mAccount->makeConnection() == ImapAccountBase::Error )
00068   {
00069     kdWarning(5006) << "ListJob - got no connection" << endl;
00070     delete this;
00071     return;
00072   } else if ( mAccount->makeConnection() == ImapAccountBase::Connecting )
00073   {
00074     // We'll wait for the connectionResult signal from the account.
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   // this is needed until we have a common base class for d(imap)
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       delete this;
00090       return;
00091     }
00092   }
00093   if ( mNamespace.isEmpty() && mStorage )
00094   {
00095     mNamespace = mAccount->namespaceForFolder( mStorage );
00096   }
00097   // create jobData
00098   ImapAccountBase::jobData jd;
00099   jd.total = 1; jd.done = 0;
00100   jd.cancellable = true;
00101   jd.parent = mDestFolder;
00102   jd.onlySubscribed = ( mType == ImapAccountBase::ListSubscribed || 
00103                         mType == ImapAccountBase::ListSubscribedNoCheck ||
00104                         mType == ImapAccountBase::ListFolderOnlySubscribed );
00105   jd.path = mPath;
00106   jd.curNamespace = mNamespace;
00107   QString status = mDestFolder ? mDestFolder->prettyURL() : QString::null;
00108   if ( mParentProgressItem )
00109   {
00110     jd.progressItem = ProgressManager::createProgressItem(
00111         mParentProgressItem,
00112         "ListDir" + ProgressManager::getUniqueID(),
00113         status,
00114         i18n("retrieving folders"),
00115         false,
00116         mAccount->useSSL() || mAccount->useTLS() );
00117     mParentProgressItem->setStatus( status );
00118   }
00119 
00120   // make the URL
00121   QString ltype = "LIST";
00122   if ( mType == ImapAccountBase::ListSubscribed ||
00123        mType == ImapAccountBase::ListFolderOnlySubscribed )
00124     ltype = "LSUB";
00125   else if ( mType == ImapAccountBase::ListSubscribedNoCheck )
00126     ltype = "LSUBNOCHECK";
00127 
00128   QString section;
00129   if ( mComplete )
00130     section = ";SECTION=COMPLETE";
00131   else if ( mType == ImapAccountBase::ListFolderOnly || 
00132             mType == ImapAccountBase::ListFolderOnlySubscribed )
00133     section = ";SECTION=FOLDERONLY";
00134   
00135   KURL url = mAccount->getUrl();
00136   url.setPath( mPath 
00137       + ";TYPE=" + ltype
00138       + section );
00139   // go
00140   //kdDebug(5006) << "start listjob for " << url.path() << endl;
00141   KIO::SimpleJob *job = KIO::listDir( url, false );
00142   KIO::Scheduler::assignJobToSlave( mAccount->slave(), job );
00143   mAccount->insertJob( job, jd );
00144   connect( job, SIGNAL(result(KIO::Job *)),
00145       this, SLOT(slotListResult(KIO::Job *)) );
00146   connect( job, SIGNAL(entries(KIO::Job *, const KIO::UDSEntryList &)),
00147       this, SLOT(slotListEntries(KIO::Job *, const KIO::UDSEntryList &)) );
00148 }
00149 
00150 void ListJob::slotConnectionResult( int errorCode, const QString& errorMsg )
00151 {
00152   Q_UNUSED( errorMsg );
00153   if ( !errorCode )
00154     execute();
00155   else {
00156     if ( mParentProgressItem )
00157       mParentProgressItem->setComplete();
00158     delete this;
00159   }
00160 }
00161 
00162 void ListJob::slotListResult( KIO::Job* job )
00163 {
00164   ImapAccountBase::JobIterator it = mAccount->findJob( job );
00165   if ( it == mAccount->jobsEnd() )
00166   {
00167     delete this;
00168     return;
00169   }
00170   if ( job->error() )
00171   {
00172     mAccount->handleJobError( job,
00173         i18n( "Error while listing folder %1: " ).arg((*it).path),
00174         true );
00175   } else
00176   {
00177     // transport the information, include the jobData
00178     emit receivedFolders( mSubfolderNames, mSubfolderPaths,
00179         mSubfolderMimeTypes, mSubfolderAttributes, *it );
00180     mAccount->removeJob( it );
00181   }
00182   delete this;
00183 }
00184 
00185 void ListJob::slotListEntries( KIO::Job* job, const KIO::UDSEntryList& uds )
00186 {
00187   ImapAccountBase::JobIterator it = mAccount->findJob( job );
00188   if ( it == mAccount->jobsEnd() )
00189   {
00190     delete this;
00191     return;
00192   }
00193   if( (*it).progressItem )
00194     (*it).progressItem->setProgress( 50 );
00195   QString name;
00196   KURL url;
00197   QString mimeType;
00198   QString attributes;
00199   for ( KIO::UDSEntryList::ConstIterator udsIt = uds.begin();
00200         udsIt != uds.end(); udsIt++ )
00201   {
00202     mimeType = QString::null;
00203     attributes = QString::null;
00204     for ( KIO::UDSEntry::ConstIterator eIt = (*udsIt).begin();
00205           eIt != (*udsIt).end(); eIt++ )
00206     {
00207       // get the needed information
00208       if ( (*eIt).m_uds == KIO::UDS_NAME )
00209         name = (*eIt).m_str;
00210       else if ( (*eIt).m_uds == KIO::UDS_URL )
00211         url = KURL((*eIt).m_str, 106); // utf-8
00212       else if ( (*eIt).m_uds == KIO::UDS_MIME_TYPE )
00213         mimeType = (*eIt).m_str;
00214       else if ( (*eIt).m_uds == KIO::UDS_EXTRA )
00215         attributes = (*eIt).m_str;
00216     }
00217     if ( (mimeType == "inode/directory" || mimeType == "message/digest"
00218           || mimeType == "message/directory")
00219          && name != ".." && (mAccount->hiddenFolders() || name.at(0) != '.') )
00220     {
00221       // Some servers send _lots_ of duplicates
00222       // This check is too slow for huge lists
00223       if ( mSubfolderPaths.count() > 100 ||
00224            mSubfolderPaths.findIndex(url.path()) == -1 )
00225       {
00226         mSubfolderNames.append( name );
00227         mSubfolderPaths.append( url.path() );
00228         mSubfolderMimeTypes.append( mimeType );
00229         mSubfolderAttributes.append( attributes );
00230       }
00231     }
00232   }
00233 }
00234 
00235 #include "listjob.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys