kmail

listjob.cpp
1/*
2 * Copyright (c) 2004 Carsten Burghardt <burghardt@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *
17 * In addition, as a special exception, the copyright holders give
18 * permission to link the code of this program with any edition of
19 * the TQt library by Trolltech AS, Norway (or with modified versions
20 * of TQt that use the same license as TQt), and distribute linked
21 * combinations including the two. You must obey the GNU General
22 * Public License in all respects for all of the code used other than
23 * TQt. If you modify this file, you may extend this exception to
24 * your version of the file, but you are not obligated to do so. If
25 * you do not wish to do so, delete this exception statement from
26 * your version.
27 */
28
29#include "listjob.h"
30#include "tdemessagebox.h"
31#include "kmfolderimap.h"
32#include "kmfoldercachedimap.h"
33#include "kmacctimap.h"
34#include "kmacctcachedimap.h"
35#include "folderstorage.h"
36#include "kmfolder.h"
37#include "progressmanager.h"
38using KPIM::ProgressManager;
39
40#include <kdebug.h>
41#include <kurl.h>
42#include <tdeio/scheduler.h>
43#include <tdeio/job.h>
44#include <tdeio/global.h>
45#include <tdelocale.h>
46
47#include <tqstylesheet.h>
48
49#include <stdlib.h>
50
51using namespace KMail;
52
53ListJob::ListJob( ImapAccountBase* account, ImapAccountBase::ListType type,
54 FolderStorage* storage, const TQString& path, bool complete,
55 KPIM::ProgressItem* item )
56 : FolderJob( 0, tOther, (storage ? storage->folder() : 0) ),
57 mStorage( storage ), mAccount( account ), mType( type ),
58 mComplete( complete ),
59 mHonorLocalSubscription( false ), mPath( path ),
60 mParentProgressItem( item )
61{
62}
63
64ListJob::~ListJob()
65{
66}
67
68void ListJob::execute()
69{
70 if ( mAccount->makeConnection() == ImapAccountBase::Error )
71 {
72 kdWarning(5006) << "ListJob - got no connection" << endl;
73 delete this;
74 return;
75 } else if ( mAccount->makeConnection() == ImapAccountBase::Connecting )
76 {
77 // We'll wait for the connectionResult signal from the account.
78 kdDebug(5006) << "ListJob - waiting for connection" << endl;
79 connect( mAccount, TQ_SIGNAL( connectionResult(int, const TQString&) ),
80 this, TQ_SLOT( slotConnectionResult(int, const TQString&) ) );
81 return;
82 }
83 // this is needed until we have a common base class for d(imap)
84 if ( mPath.isEmpty() )
85 {
86 if ( mStorage && mStorage->folderType() == KMFolderTypeImap ) {
87 mPath = static_cast<KMFolderImap*>(mStorage)->imapPath();
88 } else if ( mStorage && mStorage->folderType() == KMFolderTypeCachedImap ) {
89 mPath = static_cast<KMFolderCachedImap*>(mStorage)->imapPath();
90 } else {
91 kdError(5006) << "ListJob - no valid path and no folder given" << endl;
92 delete this;
93 return;
94 }
95 }
96 if ( mNamespace.isEmpty() && mStorage )
97 {
98 mNamespace = mAccount->namespaceForFolder( mStorage );
99 }
100 // create jobData
102 jd.total = 1; jd.done = 0;
103 jd.cancellable = true;
104 jd.parent = mDestFolder;
105 jd.onlySubscribed = ( mType == ImapAccountBase::ListSubscribed ||
106 mType == ImapAccountBase::ListSubscribedNoCheck ||
107 mType == ImapAccountBase::ListFolderOnlySubscribed );
108 jd.path = mPath;
109 jd.curNamespace = mNamespace;
110 if ( mParentProgressItem )
111 {
112 TQString escapedStatus = mDestFolder ? TQStyleSheet::escape( mDestFolder->prettyURL() )
113 : TQString();
114 jd.progressItem = ProgressManager::createProgressItem(
115 mParentProgressItem,
116 "ListDir" + ProgressManager::getUniqueID(),
117 escapedStatus,
118 i18n("retrieving folders"),
119 false,
120 mAccount->useSSL() || mAccount->useTLS() );
121 mParentProgressItem->setStatus( escapedStatus );
122 }
123
124 // make the URL
125 TQString ltype = "LIST";
126 if ( mType == ImapAccountBase::ListSubscribed ||
127 mType == ImapAccountBase::ListFolderOnlySubscribed )
128 ltype = "LSUB";
129 else if ( mType == ImapAccountBase::ListSubscribedNoCheck )
130 ltype = "LSUBNOCHECK";
131
132 TQString section;
133 if ( mComplete )
134 section = ";SECTION=COMPLETE";
135 else if ( mType == ImapAccountBase::ListFolderOnly ||
136 mType == ImapAccountBase::ListFolderOnlySubscribed )
137 section = ";SECTION=FOLDERONLY";
138
139 KURL url = mAccount->getUrl();
140 url.setPath( mPath
141 + ";TYPE=" + ltype
142 + section );
143 // go
144 //kdDebug(5006) << "start listjob for " << url.path() << endl;
145 TDEIO::SimpleJob *job = TDEIO::listDir( url, false );
146 TDEIO::Scheduler::assignJobToSlave( mAccount->slave(), job );
147 mAccount->insertJob( job, jd );
148 connect( job, TQ_SIGNAL(result(TDEIO::Job *)),
149 this, TQ_SLOT(slotListResult(TDEIO::Job *)) );
150 connect( job, TQ_SIGNAL(entries(TDEIO::Job *, const TDEIO::UDSEntryList &)),
151 this, TQ_SLOT(slotListEntries(TDEIO::Job *, const TDEIO::UDSEntryList &)) );
152}
153
154void ListJob::slotConnectionResult( int errorCode, const TQString& errorMsg )
155{
156 Q_UNUSED( errorMsg );
157 if ( !errorCode )
158 execute();
159 else {
160 if ( mParentProgressItem )
161 mParentProgressItem->setComplete();
162 delete this;
163 }
164}
165
166void ListJob::slotListResult( TDEIO::Job* job )
167{
168 ImapAccountBase::JobIterator it = mAccount->findJob( job );
169 if ( it == mAccount->jobsEnd() )
170 {
171 delete this;
172 return;
173 }
174 if ( job->error() )
175 {
176 mAccount->handleJobError( job,
177 i18n( "Error while listing folder %1: " ).arg((*it).path),
178 true );
179 } else
180 {
181 // transport the information, include the jobData
182 emit receivedFolders( mSubfolderNames, mSubfolderPaths,
183 mSubfolderMimeTypes, mSubfolderAttributes, *it );
184 mAccount->removeJob( it );
185 }
186 delete this;
187}
188
189void ListJob::slotListEntries( TDEIO::Job* job, const TDEIO::UDSEntryList& uds )
190{
191 ImapAccountBase::JobIterator it = mAccount->findJob( job );
192 if ( it == mAccount->jobsEnd() )
193 {
194 delete this;
195 return;
196 }
197 if( (*it).progressItem )
198 (*it).progressItem->setProgress( 50 );
199 TQString name;
200 KURL url;
201 TQString mimeType;
202 TQString attributes;
203 for ( TDEIO::UDSEntryList::ConstIterator udsIt = uds.begin();
204 udsIt != uds.end(); udsIt++ )
205 {
206 mimeType = TQString();
207 attributes = TQString();
208 for ( TDEIO::UDSEntry::ConstIterator eIt = (*udsIt).begin();
209 eIt != (*udsIt).end(); eIt++ )
210 {
211 // get the needed information
212 if ( (*eIt).m_uds == TDEIO::UDS_NAME )
213 name = (*eIt).m_str;
214 else if ( (*eIt).m_uds == TDEIO::UDS_URL )
215 url = KURL((*eIt).m_str, 106); // utf-8
216 else if ( (*eIt).m_uds == TDEIO::UDS_MIME_TYPE )
217 mimeType = (*eIt).m_str;
218 else if ( (*eIt).m_uds == TDEIO::UDS_EXTRA )
219 attributes = (*eIt).m_str;
220 }
221 if ( (mimeType == "inode/directory" || mimeType == "message/digest"
222 || mimeType == "message/directory")
223 && name != ".." && (mAccount->hiddenFolders() || name.at(0) != '.') )
224 {
225 if ( mHonorLocalSubscription && mAccount->onlyLocallySubscribedFolders()
226 && !mAccount->locallySubscribedTo( url.path() ) ) {
227 continue;
228 }
229
230 // Some servers send _lots_ of duplicates
231 // This check is too slow for huge lists
232 if ( mSubfolderPaths.count() > 100 ||
233 mSubfolderPaths.findIndex(url.path()) == -1 )
234 {
235 mSubfolderNames.append( name );
236 mSubfolderPaths.append( url.path() );
237 mSubfolderMimeTypes.append( mimeType );
238 mSubfolderAttributes.append( attributes );
239 }
240 }
241 }
242}
243
244
246{
247 mHonorLocalSubscription = value;
248}
249
251{
252 return mHonorLocalSubscription;
253}
254
255#include "listjob.moc"
The FolderStorage class is the bass class for the storage related aspects of a collection of mail (a ...
Definition: folderstorage.h:80
virtual KMFolderType folderType() const
Returns the type of this folder.
Definition: folderstorage.h:96
void receivedFolders(const TQStringList &, const TQStringList &, const TQStringList &, const TQStringList &, const ImapAccountBase::jobData &)
Emitted when new folders have been received.
void slotConnectionResult(int errorCode, const TQString &errorMsg)
Called from the account when a connection was established.
Definition: listjob.cpp:154
void slotListResult(TDEIO::Job *job)
Is called when the listing is done Passes the folders and the jobData to the responding folder.
Definition: listjob.cpp:166
void setHonorLocalSubscription(bool value)
Set whether the listing should include only folders that the account is subscribed to locally.
Definition: listjob.cpp:245
bool honorLocalSubscription() const
Return whether the listing includes only folders that the account is subscribed to locally.
Definition: listjob.cpp:250
void slotListEntries(TDEIO::Job *job, const TDEIO::UDSEntryList &uds)
Collects the folder information.
Definition: listjob.cpp:189
folderdiaquotatab.h
Definition: aboutdata.cpp:40