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 <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     // We'll wait for the connectionResult signal from the account.
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   // this is needed until we have a common base class for d(imap)
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   // create jobData
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   // make the URL
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   // go
00143   //kdDebug(5006) << "start listjob for " << url.path() << endl;
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     // transport the information, include the jobData
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       // get the needed information
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); // utf-8
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       // Some servers send _lots_ of duplicates
00225       // This check is too slow for huge lists
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"
KDE Home | KDE Accessibility Home | Description of Access Keys