TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imapjob.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /* -*- mode: C++; c-file-style: "gnu" -*-
  2. *
  3. * This file is part of KMail, the KDE mail client.
  4. * Copyright (c) 2002-2003 Zack Rusin <zack@kde.org>
  5. * 2000-2002 Michael Haeckel <haeckel@kde.org>
  6. *
  7. * KMail is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation.
  10. *
  11. * KMail is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * In addition, as a special exception, the copyright holders give
  21. * permission to link the code of this program with any edition of
  22. * the TQt library by Trolltech AS, Norway (or with modified versions
  23. * of TQt that use the same license as TQt), and distribute linked
  24. * combinations including the two. You must obey the GNU General
  25. * Public License in all respects for all of the code used other than
  26. * TQt. If you modify this file, you may extend this exception to
  27. * your version of the file, but you are not obligated to do so. If
  28. * you do not wish to do so, delete this exception statement from
  29. * your version.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. #include <config.h>
  33. #endif
  34. #include "imapjob.h"
  35. #include "kmfolderimap.h"
  36. #include "kmfolder.h"
  37. #include "kmmsgpart.h"
  38. #include "progressmanager.h"
  39. using KPIM::ProgressManager;
  40. #include "util.h"
  41. #include <tqstylesheet.h>
  42. #include <tdeio/scheduler.h>
  43. #include <kdebug.h>
  44. #include <tdelocale.h>
  45. #include <mimelib/body.h>
  46. #include <mimelib/bodypart.h>
  47. #include <mimelib/string.h>
  48. namespace KMail {
  49. //-----------------------------------------------------------------------------
  50. ImapJob::ImapJob( KMMessage *msg, JobType jt, KMFolderImap* folder,
  51. TQString partSpecifier, const AttachmentStrategy *as )
  52. : FolderJob( msg, jt, folder? folder->folder() : 0, partSpecifier ),
  53. mAttachmentStrategy( as ), mParentProgressItem(0)
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. ImapJob::ImapJob( TQPtrList<KMMessage>& msgList, TQString sets, JobType jt,
  58. KMFolderImap* folder )
  59. : FolderJob( msgList, sets, jt, folder? folder->folder() : 0 ),
  60. mAttachmentStrategy ( 0 ), mParentProgressItem(0)
  61. {
  62. }
  63. void ImapJob::init( JobType jt, TQString sets, KMFolderImap* folder,
  64. TQPtrList<KMMessage>& msgList )
  65. {
  66. mJob = 0;
  67. assert(jt == tGetMessage || folder);
  68. KMMessage* msg = msgList.first();
  69. // guard against empty list
  70. if ( !msg ) {
  71. deleteLater();
  72. return;
  73. }
  74. mType = jt;
  75. mDestFolder = folder? folder->folder() : 0;
  76. // refcount++
  77. if (folder) {
  78. folder->open("imapjobdest");
  79. }
  80. KMFolder *msg_parent = msg->parent();
  81. if (msg_parent) {
  82. msg_parent->open("imapjobsrc");
  83. }
  84. mSrcFolder = msg_parent;
  85. // If there is a destination folder, this is a copy, move or put to an
  86. // imap folder, use its account for keeping track of the job. Otherwise,
  87. // this is a get job and the src folder is an imap one. Use its account
  88. // then.
  89. KMAcctImap *account = 0;
  90. if (folder) {
  91. account = folder->account();
  92. } else {
  93. if ( msg_parent && msg_parent->storage() )
  94. account = static_cast<KMFolderImap*>(msg_parent->storage())->account();
  95. }
  96. if ( !account ||
  97. account->makeConnection() == ImapAccountBase::Error ) {
  98. deleteLater();
  99. return;
  100. }
  101. account->mJobList.append( this );
  102. if ( jt == tPutMessage )
  103. {
  104. // transfers the complete message to the server
  105. TQPtrListIterator<KMMessage> it( msgList );
  106. KMMessage* curMsg;
  107. while ( ( curMsg = it.current() ) != 0 )
  108. {
  109. ++it;
  110. if ( mSrcFolder && !curMsg->isMessage() )
  111. {
  112. int idx = mSrcFolder->find( curMsg );
  113. curMsg = mSrcFolder->getMsg( idx );
  114. }
  115. KURL url = account->getUrl();
  116. TQString flags = KMFolderImap::statusToFlags( curMsg->status(), folder->permanentFlags() );
  117. url.setPath( folder->imapPath() + ";SECTION=" + flags );
  118. ImapAccountBase::jobData jd;
  119. jd.parent = 0; jd.offset = 0; jd.done = 0;
  120. jd.total = ( curMsg->msgSizeServer() > 0 ) ?
  121. curMsg->msgSizeServer() : curMsg->msgSize();
  122. jd.msgList.append( curMsg );
  123. TQCString cstr( curMsg->asString() );
  124. int a = cstr.find("\nX-UID: ");
  125. int b = cstr.find('\n', a);
  126. if (a != -1 && b != -1 && cstr.find("\n\n") > a) cstr.remove(a, b-a);
  127. jd.data.resize( cstr.length() + cstr.contains( "\n" ) - cstr.contains( "\r\n" ) );
  128. unsigned int i = 0;
  129. char prevChar = '\0';
  130. // according to RFC 2060 we need CRLF
  131. for ( char *ch = cstr.data(); *ch; ch++ )
  132. {
  133. if ( *ch == '\n' && (prevChar != '\r') ) {
  134. jd.data.at( i ) = '\r';
  135. i++;
  136. }
  137. jd.data.at( i ) = *ch;
  138. prevChar = *ch;
  139. i++;
  140. }
  141. jd.progressItem = ProgressManager::createProgressItem(
  142. mParentProgressItem,
  143. "ImapJobUploading"+ProgressManager::getUniqueID(),
  144. i18n("Uploading message data"),
  145. TQStyleSheet::escape( curMsg->subject() ),
  146. true,
  147. account->useSSL() || account->useTLS() );
  148. jd.progressItem->setTotalItems( jd.total );
  149. connect ( jd.progressItem, TQT_SIGNAL( progressItemCanceled( KPIM::ProgressItem*)),
  150. account, TQT_SLOT( slotAbortRequested( KPIM::ProgressItem* ) ) );
  151. TDEIO::SimpleJob *job = TDEIO::put( url, 0, false, false, false );
  152. TDEIO::Scheduler::assignJobToSlave( account->slave(), job );
  153. account->insertJob( job, jd );
  154. connect( job, TQT_SIGNAL(result(TDEIO::Job *)),
  155. TQT_SLOT(slotPutMessageResult(TDEIO::Job *)) );
  156. connect( job, TQT_SIGNAL(dataReq(TDEIO::Job *, TQByteArray &)),
  157. TQT_SLOT(slotPutMessageDataReq(TDEIO::Job *, TQByteArray &)) );
  158. connect( job, TQT_SIGNAL(infoMessage(TDEIO::Job *, const TQString &)),
  159. TQT_SLOT(slotPutMessageInfoData(TDEIO::Job *, const TQString &)) );
  160. connect( job, TQT_SIGNAL(processedSize(TDEIO::Job *, TDEIO::filesize_t)),
  161. TQT_SLOT(slotProcessedSize(TDEIO::Job *, TDEIO::filesize_t)));
  162. }
  163. }
  164. else if ( jt == tCopyMessage || jt == tMoveMessage )
  165. {
  166. KURL url = account->getUrl();
  167. KURL destUrl = account->getUrl();
  168. destUrl.setPath(folder->imapPath());
  169. KMFolderImap *imapDestFolder = static_cast<KMFolderImap*>(msg_parent->storage());
  170. url.setPath( imapDestFolder->imapPath() + ";UID=" + sets );
  171. ImapAccountBase::jobData jd;
  172. jd.parent = 0; jd.offset = 0;
  173. jd.total = 1; jd.done = 0;
  174. jd.msgList = msgList;
  175. TQByteArray packedArgs;
  176. TQDataStream stream( packedArgs, IO_WriteOnly );
  177. stream << (int) 'C' << url << destUrl;
  178. jd.progressItem = ProgressManager::createProgressItem(
  179. mParentProgressItem,
  180. "ImapJobCopyMove"+ProgressManager::getUniqueID(),
  181. i18n("Server operation"),
  182. i18n("Source folder: %1 - Destination folder: %2")
  183. .arg( TQStyleSheet::escape( msg_parent->prettyURL() ),
  184. TQStyleSheet::escape( mDestFolder->prettyURL() ) ),
  185. true,
  186. account->useSSL() || account->useTLS() );
  187. jd.progressItem->setTotalItems( jd.total );
  188. connect ( jd.progressItem, TQT_SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
  189. account, TQT_SLOT( slotAbortRequested(KPIM::ProgressItem* ) ) );
  190. TDEIO::SimpleJob *simpleJob = TDEIO::special( url, packedArgs, false );
  191. TDEIO::Scheduler::assignJobToSlave( account->slave(), simpleJob );
  192. mJob = simpleJob;
  193. account->insertJob( mJob, jd );
  194. connect( mJob, TQT_SIGNAL(result(TDEIO::Job *)),
  195. TQT_SLOT(slotCopyMessageResult(TDEIO::Job *)) );
  196. if ( jt == tMoveMessage )
  197. {
  198. connect( mJob, TQT_SIGNAL(infoMessage(TDEIO::Job *, const TQString &)),
  199. TQT_SLOT(slotCopyMessageInfoData(TDEIO::Job *, const TQString &)) );
  200. }
  201. }
  202. else {
  203. slotGetNextMessage();
  204. }
  205. }
  206. //-----------------------------------------------------------------------------
  207. ImapJob::~ImapJob()
  208. {
  209. if ( mDestFolder )
  210. {
  211. KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  212. if ( account ) {
  213. if ( mJob ) {
  214. ImapAccountBase::JobIterator it = account->findJob( mJob );
  215. if ( it != account->jobsEnd() ) {
  216. if( (*it).progressItem ) {
  217. (*it).progressItem->setComplete();
  218. (*it).progressItem = 0;
  219. }
  220. if ( !(*it).msgList.isEmpty() ) {
  221. for ( TQPtrListIterator<KMMessage> mit( (*it).msgList ); mit.current(); ++mit )
  222. mit.current()->setTransferInProgress( false );
  223. }
  224. }
  225. account->removeJob( mJob );
  226. }
  227. account->mJobList.remove( this );
  228. }
  229. mDestFolder->close("imapjobdest");
  230. }
  231. if ( mSrcFolder ) {
  232. if (!mDestFolder || mDestFolder != mSrcFolder) {
  233. if (! (mSrcFolder->folderType() == KMFolderTypeImap) ) return;
  234. KMAcctImap *account = static_cast<KMFolderImap*>(mSrcFolder->storage())->account();
  235. if ( account ) {
  236. if ( mJob ) {
  237. ImapAccountBase::JobIterator it = account->findJob( mJob );
  238. if ( it != account->jobsEnd() ) {
  239. if( (*it).progressItem ) {
  240. (*it).progressItem->setComplete();
  241. (*it).progressItem = 0;
  242. }
  243. if ( !(*it).msgList.isEmpty() ) {
  244. for ( TQPtrListIterator<KMMessage> mit( (*it).msgList ); mit.current(); ++mit )
  245. mit.current()->setTransferInProgress( false );
  246. }
  247. }
  248. account->removeJob( mJob ); // remove the associated tdeio job
  249. }
  250. account->mJobList.remove( this ); // remove the folderjob
  251. }
  252. }
  253. mSrcFolder->close("imapjobsrc");
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. void ImapJob::slotGetNextMessage()
  258. {
  259. KMMessage *msg = mMsgList.first();
  260. KMFolderImap *msgParent = msg ? static_cast<KMFolderImap*>(msg->storage()) : 0;
  261. if ( !msgParent || !msg || msg->UID() == 0 )
  262. {
  263. // broken message
  264. emit messageRetrieved( 0 );
  265. deleteLater();
  266. return;
  267. }
  268. KMAcctImap *account = msgParent->account();
  269. KURL url = account->getUrl();
  270. TQString path = msgParent->imapPath() + ";UID=" + TQString::number(msg->UID());
  271. ImapAccountBase::jobData jd;
  272. jd.parent = 0; jd.offset = 0;
  273. jd.total = 1; jd.done = 0;
  274. jd.msgList.append( msg );
  275. if ( !mPartSpecifier.isEmpty() )
  276. {
  277. if ( mPartSpecifier.find ("STRUCTURE", 0, false) != -1 ) {
  278. path += ";SECTION=STRUCTURE";
  279. } else if ( mPartSpecifier == "HEADER" ) {
  280. path += ";SECTION=HEADER";
  281. } else {
  282. path += ";SECTION=BODY.PEEK[" + mPartSpecifier + "]";
  283. DwBodyPart * part = msg->findDwBodyPart( msg->getFirstDwBodyPart(), mPartSpecifier );
  284. if (part)
  285. jd.total = part->BodySize();
  286. }
  287. } else {
  288. path += ";SECTION=BODY.PEEK[]";
  289. if (msg->msgSizeServer() > 0)
  290. jd.total = msg->msgSizeServer();
  291. }
  292. url.setPath( path );
  293. // kdDebug(5006) << "ImapJob::slotGetNextMessage - retrieve " << url.path() << endl;
  294. // protect the message, otherwise we'll get crashes afterwards
  295. msg->setTransferInProgress( true );
  296. jd.progressItem = ProgressManager::createProgressItem(
  297. mParentProgressItem,
  298. "ImapJobDownloading"+ProgressManager::getUniqueID(),
  299. i18n("Downloading message data"),
  300. i18n("Message with subject: ") +
  301. TQStyleSheet::escape( msg->subject() ),
  302. true,
  303. account->useSSL() || account->useTLS() );
  304. connect ( jd.progressItem, TQT_SIGNAL( progressItemCanceled( KPIM::ProgressItem*)),
  305. account, TQT_SLOT( slotAbortRequested( KPIM::ProgressItem* ) ) );
  306. jd.progressItem->setTotalItems( jd.total );
  307. TDEIO::SimpleJob *simpleJob = TDEIO::get( url, false, false );
  308. TDEIO::Scheduler::assignJobToSlave( account->slave(), simpleJob );
  309. mJob = simpleJob;
  310. account->insertJob( mJob, jd );
  311. if ( mPartSpecifier.find( "STRUCTURE", 0, false ) != -1 )
  312. {
  313. connect( mJob, TQT_SIGNAL(result(TDEIO::Job *)),
  314. this, TQT_SLOT(slotGetBodyStructureResult(TDEIO::Job *)) );
  315. } else {
  316. connect( mJob, TQT_SIGNAL(result(TDEIO::Job *)),
  317. this, TQT_SLOT(slotGetMessageResult(TDEIO::Job *)) );
  318. }
  319. connect( mJob, TQT_SIGNAL(data(TDEIO::Job *, const TQByteArray &)),
  320. msgParent, TQT_SLOT(slotSimpleData(TDEIO::Job *, const TQByteArray &)) );
  321. if ( jd.total > 1 )
  322. {
  323. connect(mJob, TQT_SIGNAL(processedSize(TDEIO::Job *, TDEIO::filesize_t)),
  324. this, TQT_SLOT(slotProcessedSize(TDEIO::Job *, TDEIO::filesize_t)));
  325. }
  326. }
  327. //-----------------------------------------------------------------------------
  328. void ImapJob::slotGetMessageResult( TDEIO::Job * job )
  329. {
  330. KMMessage *msg = mMsgList.first();
  331. if (!msg || !msg->parent() || !job) {
  332. emit messageRetrieved( 0 );
  333. deleteLater();
  334. return;
  335. }
  336. KMFolderImap* parent = static_cast<KMFolderImap*>(msg->storage());
  337. if (msg->transferInProgress())
  338. msg->setTransferInProgress( false );
  339. KMAcctImap *account = parent->account();
  340. if ( !account ) {
  341. emit messageRetrieved( 0 );
  342. deleteLater();
  343. return;
  344. }
  345. ImapAccountBase::JobIterator it = account->findJob( job );
  346. if ( it == account->jobsEnd() ) return;
  347. bool gotData = true;
  348. if (job->error())
  349. {
  350. TQString errorStr = i18n( "Error while retrieving messages from the server." );
  351. if ( (*it).progressItem )
  352. (*it).progressItem->setStatus( errorStr );
  353. account->handleJobError( job, errorStr );
  354. return;
  355. } else {
  356. if ((*it).data.size() > 0)
  357. {
  358. kdDebug(5006) << "ImapJob::slotGetMessageResult - retrieved part " << mPartSpecifier << endl;
  359. if ( mPartSpecifier.isEmpty() ||
  360. mPartSpecifier == "HEADER" )
  361. {
  362. uint size = msg->msgSizeServer();
  363. if ( size > 0 && mPartSpecifier.isEmpty() )
  364. (*it).done = size;
  365. ulong uid = msg->UID();
  366. // must set this first so that msg->fromByteArray sets the attachment status
  367. if ( mPartSpecifier.isEmpty() )
  368. msg->setComplete( true );
  369. else
  370. msg->setReadyToShow( false );
  371. // Convert CR/LF to LF.
  372. size_t dataSize = (*it).data.size();
  373. dataSize = Util::crlf2lf( (*it).data.data(), dataSize ); // always <=
  374. (*it).data.resize( dataSize );
  375. // During the construction of the message from the byteArray it does
  376. // not have a uid. Therefore we have to make sure that no connected
  377. // slots are called, since they would operate on uid == 0.
  378. msg->parent()->storage()->blockSignals( true );
  379. msg->fromByteArray( (*it).data );
  380. // now let others react
  381. msg->parent()->storage()->blockSignals( false );
  382. if ( size > 0 && msg->msgSizeServer() == 0 ) {
  383. msg->setMsgSizeServer(size);
  384. }
  385. // reconstruct the UID as it gets overwritten above
  386. msg->setUID(uid);
  387. } else {
  388. // Convert CR/LF to LF.
  389. size_t dataSize = (*it).data.size();
  390. dataSize = Util::crlf2lf( (*it).data.data(), dataSize ); // always <=
  391. (*it).data.resize( dataSize );
  392. // Update the body of the retrieved part (the message notifies all observers)
  393. msg->updateBodyPart( mPartSpecifier, (*it).data );
  394. msg->setReadyToShow( true );
  395. // Update the attachment state, we have to do this for every part as we actually
  396. // do not know if the message has no attachment or we simply did not load the header
  397. if (msg->attachmentState() != KMMsgHasAttachment)
  398. msg->updateAttachmentState();
  399. if (msg->invitationState() != KMMsgHasInvitation)
  400. msg->updateInvitationState();
  401. }
  402. } else {
  403. kdDebug(5006) << "ImapJob::slotGetMessageResult - got no data for " << mPartSpecifier << endl;
  404. gotData = false;
  405. msg->setReadyToShow( true );
  406. // nevertheless give visual feedback
  407. msg->notify();
  408. }
  409. }
  410. if (account->slave()) {
  411. account->removeJob(it);
  412. account->mJobList.remove(this);
  413. }
  414. /* This needs to be emitted last, so the slots that are hooked to it
  415. * don't unGetMsg the msg before we have finished. */
  416. if ( mPartSpecifier.isEmpty() ||
  417. mPartSpecifier == "HEADER" )
  418. {
  419. if ( gotData )
  420. emit messageRetrieved(msg);
  421. else
  422. {
  423. /* we got an answer but not data
  424. * this means that the msg is not on the server anymore so delete it */
  425. emit messageRetrieved( 0 );
  426. parent->ignoreJobsForMessage( msg );
  427. int idx = parent->find( msg );
  428. if (idx != -1) parent->removeMsg( idx, true );
  429. // the removeMsg will unGet the message, which will delete all
  430. // jobs, including this one
  431. return;
  432. }
  433. } else {
  434. emit messageUpdated(msg, mPartSpecifier);
  435. }
  436. deleteLater();
  437. }
  438. //-----------------------------------------------------------------------------
  439. void ImapJob::slotGetBodyStructureResult( TDEIO::Job * job )
  440. {
  441. KMMessage *msg = mMsgList.first();
  442. if (!msg || !msg->parent() || !job) {
  443. deleteLater();
  444. return;
  445. }
  446. KMFolderImap* parent = static_cast<KMFolderImap*>(msg->storage());
  447. if (msg->transferInProgress())
  448. msg->setTransferInProgress( false );
  449. KMAcctImap *account = parent->account();
  450. if ( !account ) {
  451. deleteLater();
  452. return;
  453. }
  454. ImapAccountBase::JobIterator it = account->findJob( job );
  455. if ( it == account->jobsEnd() ) return;
  456. if (job->error())
  457. {
  458. account->handleJobError( job, i18n( "Error while retrieving information on the structure of a message." ) );
  459. return;
  460. } else {
  461. if ((*it).data.size() > 0)
  462. {
  463. TQDataStream stream( (*it).data, IO_ReadOnly );
  464. account->handleBodyStructure(stream, msg, mAttachmentStrategy);
  465. }
  466. }
  467. if (account->slave()) {
  468. account->removeJob(it);
  469. account->mJobList.remove(this);
  470. }
  471. deleteLater();
  472. }
  473. //-----------------------------------------------------------------------------
  474. void ImapJob::slotPutMessageDataReq( TDEIO::Job *job, TQByteArray &data )
  475. {
  476. KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  477. if ( !account )
  478. {
  479. emit finished();
  480. deleteLater();
  481. return;
  482. }
  483. ImapAccountBase::JobIterator it = account->findJob( job );
  484. if ( it == account->jobsEnd() ) return;
  485. if ((*it).data.size() - (*it).offset > 0x8000)
  486. {
  487. data.duplicate((*it).data.data() + (*it).offset, 0x8000);
  488. (*it).offset += 0x8000;
  489. }
  490. else if ((*it).data.size() - (*it).offset > 0)
  491. {
  492. data.duplicate((*it).data.data() + (*it).offset, (*it).data.size() - (*it).offset);
  493. (*it).offset = (*it).data.size();
  494. } else data.resize(0);
  495. }
  496. //-----------------------------------------------------------------------------
  497. void ImapJob::slotPutMessageResult( TDEIO::Job *job )
  498. {
  499. KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  500. if ( !account )
  501. {
  502. emit finished();
  503. deleteLater();
  504. return;
  505. }
  506. ImapAccountBase::JobIterator it = account->findJob( job );
  507. if ( it == account->jobsEnd() ) return;
  508. bool deleteMe = false;
  509. if (job->error())
  510. {
  511. if ( (*it).progressItem )
  512. (*it).progressItem->setStatus( i18n("Uploading message data failed.") );
  513. account->handlePutError( job, *it, mDestFolder );
  514. return;
  515. } else {
  516. if ( (*it).progressItem )
  517. (*it).progressItem->setStatus( i18n("Uploading message data completed.") );
  518. if ( mParentProgressItem )
  519. {
  520. mParentProgressItem->incCompletedItems();
  521. mParentProgressItem->updateProgress();
  522. }
  523. KMMessage *msg = (*it).msgList.first();
  524. emit messageStored( msg );
  525. if ( msg == mMsgList.getLast() )
  526. {
  527. emit messageCopied( mMsgList );
  528. if (account->slave()) {
  529. account->mJobList.remove( this );
  530. }
  531. deleteMe = true;
  532. }
  533. }
  534. if (account->slave()) {
  535. account->removeJob( it ); // also clears progressitem
  536. }
  537. if ( deleteMe )
  538. deleteLater();
  539. }
  540. //-----------------------------------------------------------------------------
  541. void ImapJob::slotCopyMessageInfoData(TDEIO::Job * job, const TQString & data)
  542. {
  543. KMFolderImap * imapFolder = static_cast<KMFolderImap*>(mDestFolder->storage());
  544. KMAcctImap *account = imapFolder->account();
  545. if ( !account )
  546. {
  547. emit finished();
  548. deleteLater();
  549. return;
  550. }
  551. ImapAccountBase::JobIterator it = account->findJob( job );
  552. if ( it == account->jobsEnd() ) return;
  553. if (data.find("UID") != -1)
  554. {
  555. // split
  556. TQString oldUid = data.section(' ', 1, 1);
  557. TQString newUid = data.section(' ', 2, 2);
  558. // get lists of uids
  559. TQValueList<ulong> olduids = KMFolderImap::splitSets(oldUid);
  560. TQValueList<ulong> newuids = KMFolderImap::splitSets(newUid);
  561. int index = -1;
  562. KMMessage * msg;
  563. for ( msg = (*it).msgList.first(); msg; msg = (*it).msgList.next() )
  564. {
  565. ulong uid = msg->UID();
  566. index = olduids.findIndex(uid);
  567. if (index > -1)
  568. {
  569. // found, get the new uid
  570. imapFolder->saveMsgMetaData( msg, newuids[index] );
  571. }
  572. }
  573. }
  574. }
  575. //----------------------------------------------------------------------------
  576. void ImapJob::slotPutMessageInfoData(TDEIO::Job *job, const TQString &data)
  577. {
  578. KMFolderImap * imapFolder = static_cast<KMFolderImap*>(mDestFolder->storage());
  579. KMAcctImap *account = imapFolder->account();
  580. if ( !account )
  581. {
  582. emit finished();
  583. deleteLater();
  584. return;
  585. }
  586. ImapAccountBase::JobIterator it = account->findJob( job );
  587. if ( it == account->jobsEnd() ) return;
  588. if ( data.find("UID") != -1 )
  589. {
  590. ulong uid = ( data.right(data.length()-4) ).toInt();
  591. if ( !(*it).msgList.isEmpty() )
  592. {
  593. imapFolder->saveMsgMetaData( (*it).msgList.first(), uid );
  594. }
  595. }
  596. }
  597. //-----------------------------------------------------------------------------
  598. void ImapJob::slotCopyMessageResult( TDEIO::Job *job )
  599. {
  600. KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  601. if ( !account )
  602. {
  603. emit finished();
  604. deleteLater();
  605. return;
  606. }
  607. ImapAccountBase::JobIterator it = account->findJob( job );
  608. if ( it == account->jobsEnd() ) return;
  609. if (job->error())
  610. {
  611. mErrorCode = job->error();
  612. TQString errStr = i18n("Error while copying messages.");
  613. if ( (*it).progressItem )
  614. (*it).progressItem->setStatus( errStr );
  615. if ( account->handleJobError( job, errStr ) )
  616. deleteLater();
  617. return;
  618. } else {
  619. if ( !(*it).msgList.isEmpty() )
  620. {
  621. emit messageCopied((*it).msgList);
  622. } else if (mMsgList.first()) {
  623. emit messageCopied(mMsgList.first());
  624. }
  625. }
  626. if (account->slave()) {
  627. account->removeJob(it);
  628. account->mJobList.remove(this);
  629. }
  630. deleteLater();
  631. }
  632. //-----------------------------------------------------------------------------
  633. void ImapJob::execute()
  634. {
  635. init( mType, mSets, mDestFolder?
  636. dynamic_cast<KMFolderImap*>( mDestFolder->storage() ):0, mMsgList );
  637. }
  638. //-----------------------------------------------------------------------------
  639. void ImapJob::setParentFolder( const KMFolderImap* parent )
  640. {
  641. mParentFolder = const_cast<KMFolderImap*>( parent );
  642. }
  643. //-----------------------------------------------------------------------------
  644. void ImapJob::slotProcessedSize(TDEIO::Job * job, TDEIO::filesize_t processed)
  645. {
  646. KMMessage *msg = mMsgList.first();
  647. if (!msg || !job) {
  648. return;
  649. }
  650. KMFolderImap* parent = 0;
  651. if ( msg->parent() && msg->parent()->folderType() == KMFolderTypeImap )
  652. parent = static_cast<KMFolderImap*>(msg->parent()->storage());
  653. else if (mDestFolder) // put
  654. parent = static_cast<KMFolderImap*>(mDestFolder->storage());
  655. if (!parent) return;
  656. KMAcctImap *account = parent->account();
  657. if ( !account ) return;
  658. ImapAccountBase::JobIterator it = account->findJob( job );
  659. if ( it == account->jobsEnd() ) return;
  660. (*it).done = processed;
  661. if ( (*it).progressItem ) {
  662. (*it).progressItem->setCompletedItems( processed );
  663. (*it).progressItem->updateProgress();
  664. }
  665. emit progress( (*it).done, (*it).total );
  666. }
  667. }//namespace KMail
  668. #include "imapjob.moc"