summaryrefslogtreecommitdiffstats
path: root/kmail/imapjob.cpp
blob: 3ddb93f39292abb1bdf83a2c06c3172de81903f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*  -*- mode: C++; c-file-style: "gnu" -*-
 *
 *  This file is part of KMail, the KDE mail client.
 *  Copyright (c) 2002-2003 Zack Rusin <[email protected]>
 *                2000-2002 Michael Haeckel <[email protected]>
 *
 *  KMail is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License, version 2, as
 *  published by the Free Software Foundation.
 *
 *  KMail is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the TQt library by Trolltech AS, Norway (or with modified versions
 *  of TQt that use the same license as TQt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  TQt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "imapjob.h"
#include "kmfolderimap.h"
#include "kmfolder.h"
#include "kmmsgpart.h"
#include "progressmanager.h"
using KPIM::ProgressManager;
#include "util.h"

#include <tqstylesheet.h>
#include <kio/scheduler.h>
#include <kdebug.h>
#include <klocale.h>
#include <mimelib/body.h>
#include <mimelib/bodypart.h>
#include <mimelib/string.h>


namespace KMail {

//-----------------------------------------------------------------------------
ImapJob::ImapJob( KMMessage *msg, JobType jt, KMFolderImap* folder,
    TQString partSpecifier, const AttachmentStrategy *as )
  : FolderJob( msg, jt, folder? folder->folder() : 0, partSpecifier ),
    mAttachmentStrategy( as ), mParentProgressItem(0)
{
}

//-----------------------------------------------------------------------------
ImapJob::ImapJob( TQPtrList<KMMessage>& msgList, TQString sets, JobType jt,
                  KMFolderImap* folder )
  : FolderJob( msgList, sets, jt, folder? folder->folder() : 0 ),
    mAttachmentStrategy ( 0 ), mParentProgressItem(0)
{
}

void ImapJob::init( JobType jt, TQString sets, KMFolderImap* folder,
                    TQPtrList<KMMessage>& msgList )
{
  mJob = 0;

  assert(jt == tGetMessage || folder);
  KMMessage* msg = msgList.first();
   // guard against empty list
  if ( !msg ) {
    deleteLater();
    return;
  }
  mType = jt;
  mDestFolder = folder? folder->folder() : 0;
  // refcount++
  if (folder) {
    folder->open("imapjobdest");
  }
  KMFolder *msg_parent = msg->parent();
  if (msg_parent) {
    msg_parent->open("imapjobsrc");
  }
  mSrcFolder = msg_parent;
  // If there is a destination folder, this is a copy, move or put to an
  // imap folder, use its account for keeping track of the job. Otherwise,
  // this is a get job and the src folder is an imap one. Use its account
  // then.
  KMAcctImap *account = 0;
  if (folder) {
    account = folder->account();
  } else {
    if ( msg_parent && msg_parent->storage() )
      account = static_cast<KMFolderImap*>(msg_parent->storage())->account();
  }
  if ( !account ||
       account->makeConnection() == ImapAccountBase::Error ) {
    deleteLater();
    return;
  }
  account->mJobList.append( this );
  if ( jt == tPutMessage )
  {
    // transfers the complete message to the server
    TQPtrListIterator<KMMessage> it( msgList );
    KMMessage* curMsg;
    while ( ( curMsg = it.current() ) != 0 )
    {
      ++it;
      if ( mSrcFolder && !curMsg->isMessage() )
      {
        int idx = mSrcFolder->find( curMsg );
        curMsg = mSrcFolder->getMsg( idx );
      }
      KURL url = account->getUrl();
      TQString flags = KMFolderImap::statusToFlags( curMsg->status(), folder->permanentFlags() );
      url.setPath( folder->imapPath() + ";SECTION=" + flags );
      ImapAccountBase::jobData jd;
      jd.parent = 0; jd.offset = 0; jd.done = 0;
      jd.total = ( curMsg->msgSizeServer() > 0 ) ?
        curMsg->msgSizeServer() : curMsg->msgSize();
      jd.msgList.append( curMsg );
      TQCString cstr( curMsg->asString() );
      int a = cstr.find("\nX-UID: ");
      int b = cstr.find('\n', a);
      if (a != -1 && b != -1 && cstr.find("\n\n") > a) cstr.remove(a, b-a);
      jd.data.resize( cstr.length() + cstr.contains( "\n" ) - cstr.contains( "\r\n" ) );
      unsigned int i = 0;
      char prevChar = '\0';
      // according to RFC 2060 we need CRLF
      for ( char *ch = cstr.data(); *ch; ch++ )
      {
        if ( *ch == '\n' && (prevChar != '\r') ) {
          jd.data.tqat( i ) = '\r';
          i++;
        }
        jd.data.tqat( i ) = *ch;
        prevChar = *ch;
        i++;
      }
      jd.progressItem = ProgressManager::createProgressItem(
          mParentProgressItem,
          "ImapJobUploading"+ProgressManager::getUniqueID(),
          i18n("Uploading message data"),
          TQStyleSheet::escape( curMsg->subject() ),
          true,
          account->useSSL() || account->useTLS() );
      jd.progressItem->setTotalItems( jd.total );
      connect ( jd.progressItem, TQT_SIGNAL( progressItemCanceled( KPIM::ProgressItem*)),
          account, TQT_SLOT( slotAbortRequested( KPIM::ProgressItem* ) ) );
      KIO::SimpleJob *job = KIO::put( url, 0, false, false, false );
      KIO::Scheduler::assignJobToSlave( account->slave(), job );
      account->insertJob( job, jd );
      connect( job, TQT_SIGNAL(result(KIO::Job *)),
          TQT_SLOT(slotPutMessageResult(KIO::Job *)) );
      connect( job, TQT_SIGNAL(dataReq(KIO::Job *, TQByteArray &)),
          TQT_SLOT(slotPutMessageDataReq(KIO::Job *, TQByteArray &)) );
      connect( job, TQT_SIGNAL(infoMessage(KIO::Job *, const TQString &)),
          TQT_SLOT(slotPutMessageInfoData(KIO::Job *, const TQString &)) );
      connect( job, TQT_SIGNAL(processedSize(KIO::Job *, KIO::filesize_t)),
          TQT_SLOT(slotProcessedSize(KIO::Job *, KIO::filesize_t)));
    }
  }
  else if ( jt == tCopyMessage || jt == tMoveMessage )
  {
    KURL url = account->getUrl();
    KURL destUrl = account->getUrl();
    destUrl.setPath(folder->imapPath());
    KMFolderImap *imapDestFolder = static_cast<KMFolderImap*>(msg_parent->storage());
    url.setPath( imapDestFolder->imapPath() + ";UID=" + sets );
    ImapAccountBase::jobData jd;
    jd.parent = 0; jd.offset = 0;
    jd.total = 1; jd.done = 0;
    jd.msgList = msgList;

    TQByteArray packedArgs;
    TQDataStream stream( packedArgs, IO_WriteOnly );

    stream << (int) 'C' << url << destUrl;
    jd.progressItem = ProgressManager::createProgressItem(
                          mParentProgressItem,
                          "ImapJobCopyMove"+ProgressManager::getUniqueID(),
                          i18n("Server operation"),
                          i18n("Source folder: %1 - Destination folder: %2")
                            .tqarg( TQStyleSheet::escape( msg_parent->prettyURL() ),
                                  TQStyleSheet::escape( mDestFolder->prettyURL() ) ),
                          true,
                          account->useSSL() || account->useTLS() );
    jd.progressItem->setTotalItems( jd.total );
    connect ( jd.progressItem, TQT_SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
              account, TQT_SLOT( slotAbortRequested(KPIM::ProgressItem* ) ) );
    KIO::SimpleJob *simpleJob = KIO::special( url, packedArgs, false );
    KIO::Scheduler::assignJobToSlave( account->slave(), simpleJob );
    mJob = simpleJob;
    account->insertJob( mJob, jd );
    connect( mJob, TQT_SIGNAL(result(KIO::Job *)),
             TQT_SLOT(slotCopyMessageResult(KIO::Job *)) );
    if ( jt == tMoveMessage )
    {
      connect( mJob, TQT_SIGNAL(infoMessage(KIO::Job *, const TQString &)),
               TQT_SLOT(slotCopyMessageInfoData(KIO::Job *, const TQString &)) );
    }
  }
  else {
    slotGetNextMessage();
  }
}


//-----------------------------------------------------------------------------
ImapJob::~ImapJob()
{
  if ( mDestFolder )
  {
    KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
    if ( account ) {
      if ( mJob ) {
        ImapAccountBase::JobIterator it = account->findJob( mJob );
        if ( it != account->jobsEnd() ) {
          if( (*it).progressItem ) {
            (*it).progressItem->setComplete();
            (*it).progressItem = 0;
          }
          if ( !(*it).msgList.isEmpty() ) {
            for ( TQPtrListIterator<KMMessage> mit( (*it).msgList ); mit.current(); ++mit )
              mit.current()->setTransferInProgress( false );
          }
        }
        account->removeJob( mJob );
      }
      account->mJobList.remove( this );
    }
    mDestFolder->close("imapjobdest");
  }

  if ( mSrcFolder ) {
    if (!mDestFolder || mDestFolder != mSrcFolder) {
      if (! (mSrcFolder->folderType() == KMFolderTypeImap) ) return;
      KMAcctImap *account = static_cast<KMFolderImap*>(mSrcFolder->storage())->account();
      if ( account ) {
        if ( mJob ) {
          ImapAccountBase::JobIterator it = account->findJob( mJob );
          if ( it != account->jobsEnd() ) {
            if( (*it).progressItem ) {
              (*it).progressItem->setComplete();
              (*it).progressItem = 0;
            }
            if ( !(*it).msgList.isEmpty() ) {
              for ( TQPtrListIterator<KMMessage> mit( (*it).msgList ); mit.current(); ++mit )
                mit.current()->setTransferInProgress( false );
            }
          }
          account->removeJob( mJob ); // remove the associated kio job
        }
        account->mJobList.remove( this ); // remove the folderjob
      }
    }
    mSrcFolder->close("imapjobsrc");
  }
}


//-----------------------------------------------------------------------------
void ImapJob::slotGetNextMessage()
{
  KMMessage *msg = mMsgList.first();
  KMFolderImap *msgParent = msg ? static_cast<KMFolderImap*>(msg->storage()) : 0;
  if ( !msgParent || !msg || msg->UID() == 0 )
  {
    // broken message
    emit messageRetrieved( 0 );
    deleteLater();
    return;
  }
  KMAcctImap *account = msgParent->account();
  KURL url = account->getUrl();
  TQString path = msgParent->imapPath() + ";UID=" + TQString::number(msg->UID());
  ImapAccountBase::jobData jd;
  jd.parent = 0; jd.offset = 0;
  jd.total = 1; jd.done = 0;
  jd.msgList.append( msg );
  if ( !mPartSpecifier.isEmpty() )
  {
    if ( mPartSpecifier.find ("STRUCTURE", 0, false) != -1 ) {
      path += ";SECTION=STRUCTURE";
    } else if ( mPartSpecifier == "HEADER" ) {
      path += ";SECTION=HEADER";
    } else {
      path += ";SECTION=BODY.PEEK[" + mPartSpecifier + "]";
      DwBodyPart * part = msg->findDwBodyPart( msg->getFirstDwBodyPart(), mPartSpecifier );
      if (part)
        jd.total = part->BodySize();
    }
  } else {
      path += ";SECTION=BODY.PEEK[]";
      if (msg->msgSizeServer() > 0)
        jd.total = msg->msgSizeServer();
  }
  url.setPath( path );
//  kdDebug(5006) << "ImapJob::slotGetNextMessage - retrieve " << url.path() << endl;
  // protect the message, otherwise we'll get crashes afterwards
  msg->setTransferInProgress( true );
  jd.progressItem = ProgressManager::createProgressItem(
                          mParentProgressItem,
                          "ImapJobDownloading"+ProgressManager::getUniqueID(),
                          i18n("Downloading message data"),
                          i18n("Message with subject: ") +
                          TQStyleSheet::escape( msg->subject() ),
                          true,
                          account->useSSL() || account->useTLS() );
  connect ( jd.progressItem, TQT_SIGNAL( progressItemCanceled( KPIM::ProgressItem*)),
            account, TQT_SLOT( slotAbortRequested( KPIM::ProgressItem* ) ) );
  jd.progressItem->setTotalItems( jd.total );

  KIO::SimpleJob *simpleJob = KIO::get( url, false, false );
  KIO::Scheduler::assignJobToSlave( account->slave(), simpleJob );
  mJob = simpleJob;
  account->insertJob( mJob, jd );
  if ( mPartSpecifier.find( "STRUCTURE", 0, false ) != -1 )
  {
    connect( mJob, TQT_SIGNAL(result(KIO::Job *)),
             this, TQT_SLOT(slotGetBodyStructureResult(KIO::Job *)) );
  } else {
    connect( mJob, TQT_SIGNAL(result(KIO::Job *)),
             this, TQT_SLOT(slotGetMessageResult(KIO::Job *)) );
  }
  connect( mJob, TQT_SIGNAL(data(KIO::Job *, const TQByteArray &)),
           msgParent, TQT_SLOT(slotSimpleData(KIO::Job *, const TQByteArray &)) );
  if ( jd.total > 1 )
  {
    connect(mJob, TQT_SIGNAL(processedSize(KIO::Job *, KIO::filesize_t)),
        this, TQT_SLOT(slotProcessedSize(KIO::Job *, KIO::filesize_t)));
  }
}


//-----------------------------------------------------------------------------
void ImapJob::slotGetMessageResult( KIO::Job * job )
{
  KMMessage *msg = mMsgList.first();
  if (!msg || !msg->parent() || !job) {
    emit messageRetrieved( 0 );
    deleteLater();
    return;
  }
  KMFolderImap* parent = static_cast<KMFolderImap*>(msg->storage());
  if (msg->transferInProgress())
    msg->setTransferInProgress( false );
  KMAcctImap *account = parent->account();
  if ( !account ) {
    emit messageRetrieved( 0 );
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;

  bool gotData = true;
  if (job->error())
  {
    TQString errorStr = i18n( "Error while retrieving messages from the server." );
    if ( (*it).progressItem )
      (*it).progressItem->setqStatus( errorStr );
    account->handleJobError( job, errorStr );
    return;
  } else {
    if ((*it).data.size() > 0)
    {
      kdDebug(5006) << "ImapJob::slotGetMessageResult - retrieved part " << mPartSpecifier << endl;
      if ( mPartSpecifier.isEmpty() ||
           mPartSpecifier == "HEADER" )
      {
        uint size = msg->msgSizeServer();
        if ( size > 0 && mPartSpecifier.isEmpty() )
          (*it).done = size;
        ulong uid = msg->UID();
        // must set this first so that msg->fromByteArray sets the attachment status
        if ( mPartSpecifier.isEmpty() )
          msg->setComplete( true );
        else
          msg->setReadyToShow( false );

        // Convert CR/LF to LF.
        size_t dataSize = (*it).data.size();
        dataSize = Util::crlf2lf( (*it).data.data(), dataSize ); // always <=
        (*it).data.resize( dataSize );

        // During the construction of the message from the byteArray it does
        // not have a uid. Therefore we have to make sure that no connected
        // slots are called, since they would operate on uid == 0.
        msg->parent()->storage()->blockSignals( true );
        msg->fromByteArray( (*it).data );
        // now let others react
        msg->parent()->storage()->blockSignals( false );
        if ( size > 0 && msg->msgSizeServer() == 0 ) {
          msg->setMsgSizeServer(size);
        }
        // reconstruct the UID as it gets overwritten above
        msg->setUID(uid);

      } else {
        // Convert CR/LF to LF.
        size_t dataSize = (*it).data.size();
        dataSize = Util::crlf2lf( (*it).data.data(), dataSize ); // always <=
        (*it).data.resize( dataSize );

        // Update the body of the retrieved part (the message notifies all observers)
        msg->updateBodyPart( mPartSpecifier, (*it).data );
        msg->setReadyToShow( true );
        // Update the attachment state, we have to do this for every part as we actually
        // do not know if the message has no attachment or we simply did not load the header
        if (msg->attachmentState() != KMMsgHasAttachment)
          msg->updateAttachmentState();
        if (msg->invitationState() != KMMsgHasInvitation)
          msg->updateInvitationState();
      }
    } else {
      kdDebug(5006) << "ImapJob::slotGetMessageResult - got no data for " << mPartSpecifier << endl;
      gotData = false;
      msg->setReadyToShow( true );
      // nevertheless give visual feedback
      msg->notify();
    }
  }
  if (account->slave()) {
      account->removeJob(it);
      account->mJobList.remove(this);
  }
  /* This needs to be emitted last, so the slots that are hooked to it
   * don't unGetMsg the msg before we have finished. */
  if ( mPartSpecifier.isEmpty() ||
       mPartSpecifier == "HEADER" )
  {
    if ( gotData )
      emit messageRetrieved(msg);
    else
    {
      /* we got an answer but not data
       * this means that the msg is not on the server anymore so delete it */
      emit messageRetrieved( 0 );
      parent->ignoreJobsForMessage( msg );
      int idx = parent->find( msg );
      if (idx != -1) parent->removeMsg( idx, true );
      // the removeMsg will unGet the message, which will delete all
      // jobs, including this one
      return;
    }
  } else {
    emit messageUpdated(msg, mPartSpecifier);
  }
  deleteLater();
}

//-----------------------------------------------------------------------------
void ImapJob::slotGetBodyStructureResult( KIO::Job * job )
{
  KMMessage *msg = mMsgList.first();
  if (!msg || !msg->parent() || !job) {
    deleteLater();
    return;
  }
  KMFolderImap* parent = static_cast<KMFolderImap*>(msg->storage());
  if (msg->transferInProgress())
    msg->setTransferInProgress( false );
  KMAcctImap *account = parent->account();
  if ( !account ) {
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;


  if (job->error())
  {
    account->handleJobError( job, i18n( "Error while retrieving information on the structure of a message." ) );
    return;
  } else {
    if ((*it).data.size() > 0)
    {
      TQDataStream stream( (*it).data, IO_ReadOnly );
      account->handleBodyStructure(stream, msg, mAttachmentStrategy);
    }
  }
  if (account->slave()) {
      account->removeJob(it);
      account->mJobList.remove(this);
  }
  deleteLater();
}

//-----------------------------------------------------------------------------
void ImapJob::slotPutMessageDataReq( KIO::Job *job, TQByteArray &data )
{
  KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  if ( !account )
  {
    emit finished();
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;

  if ((*it).data.size() - (*it).offset > 0x8000)
  {
    data.duplicate((*it).data.data() + (*it).offset, 0x8000);
    (*it).offset += 0x8000;
  }
  else if ((*it).data.size() - (*it).offset > 0)
  {
    data.duplicate((*it).data.data() + (*it).offset, (*it).data.size() - (*it).offset);
    (*it).offset = (*it).data.size();
  } else data.resize(0);
}


//-----------------------------------------------------------------------------
void ImapJob::slotPutMessageResult( KIO::Job *job )
{
  KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  if ( !account )
  {
    emit finished();
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;
  bool deleteMe = false;
  if (job->error())
  {
    if ( (*it).progressItem )
      (*it).progressItem->setqStatus( i18n("Uploading message data failed.") );
    account->handlePutError( job, *it, mDestFolder );
    return;
  } else {
    if ( (*it).progressItem )
      (*it).progressItem->setqStatus( i18n("Uploading message data completed.") );
    if ( mParentProgressItem )
    {
      mParentProgressItem->incCompletedItems();
      mParentProgressItem->updateProgress();
    }
    KMMessage *msg = (*it).msgList.first();
    emit messageStored( msg );
    if ( msg == mMsgList.getLast() )
    {
      emit messageCopied( mMsgList );
      if (account->slave()) {
        account->mJobList.remove( this );
      }
      deleteMe = true;
    }
  }
  if (account->slave()) {
    account->removeJob( it ); // also clears progressitem
  }
  if ( deleteMe )
    deleteLater();
}

//-----------------------------------------------------------------------------
void ImapJob::slotCopyMessageInfoData(KIO::Job * job, const TQString & data)
{
  KMFolderImap * imapFolder = static_cast<KMFolderImap*>(mDestFolder->storage());
  KMAcctImap *account = imapFolder->account();
  if ( !account )
  {
    emit finished();
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;

  if (data.find("UID") != -1)
  {
    // split
    TQString oldUid = data.section(' ', 1, 1);
    TQString newUid = data.section(' ', 2, 2);

    // get lists of uids
    TQValueList<ulong> olduids = KMFolderImap::splitSets(oldUid);
    TQValueList<ulong> newuids = KMFolderImap::splitSets(newUid);

    int index = -1;
    KMMessage * msg;
    for ( msg = (*it).msgList.first(); msg; msg = (*it).msgList.next() )
    {
      ulong uid = msg->UID();
      index = olduids.findIndex(uid);
      if (index > -1)
      {
        // found, get the new uid
        imapFolder->saveMsgMetaData( msg, newuids[index] );
      }
    }
  }
}

//----------------------------------------------------------------------------
void ImapJob::slotPutMessageInfoData(KIO::Job *job, const TQString &data)
{
  KMFolderImap * imapFolder = static_cast<KMFolderImap*>(mDestFolder->storage());
  KMAcctImap *account = imapFolder->account();
  if ( !account )
  {
    emit finished();
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;

  if ( data.find("UID") != -1 )
  {
    ulong uid = ( data.right(data.length()-4) ).toInt();
    if ( !(*it).msgList.isEmpty() )
    {
      imapFolder->saveMsgMetaData( (*it).msgList.first(), uid );
    }
  }
}


//-----------------------------------------------------------------------------
void ImapJob::slotCopyMessageResult( KIO::Job *job )
{
  KMAcctImap *account = static_cast<KMFolderImap*>(mDestFolder->storage())->account();
  if ( !account )
  {
    emit finished();
    deleteLater();
    return;
  }
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;

  if (job->error())
  {
    mErrorCode = job->error();
    TQString errStr = i18n("Error while copying messages.");
    if ( (*it).progressItem )
      (*it).progressItem->setqStatus( errStr );
    if ( account->handleJobError( job, errStr  ) )
      deleteLater();
    return;
  } else {
    if ( !(*it).msgList.isEmpty() )
    {
      emit messageCopied((*it).msgList);
    } else if (mMsgList.first()) {
      emit messageCopied(mMsgList.first());
    }
  }
  if (account->slave()) {
    account->removeJob(it);
    account->mJobList.remove(this);
  }
  deleteLater();
}

//-----------------------------------------------------------------------------
void ImapJob::execute()
{
  init( mType, mSets, mDestFolder?
        dynamic_cast<KMFolderImap*>( mDestFolder->storage() ):0, mMsgList );
}

//-----------------------------------------------------------------------------
void ImapJob::setParentFolder( const KMFolderImap* parent )
{
  mParentFolder = const_cast<KMFolderImap*>( parent );
}

//-----------------------------------------------------------------------------
void ImapJob::slotProcessedSize(KIO::Job * job, KIO::filesize_t processed)
{
  KMMessage *msg = mMsgList.first();
  if (!msg || !job) {
    return;
  }
  KMFolderImap* parent = 0;
  if ( msg->parent() && msg->parent()->folderType() == KMFolderTypeImap )
    parent = static_cast<KMFolderImap*>(msg->parent()->storage());
  else if (mDestFolder) // put
    parent = static_cast<KMFolderImap*>(mDestFolder->storage());
  if (!parent) return;
  KMAcctImap *account = parent->account();
  if ( !account ) return;
  ImapAccountBase::JobIterator it = account->findJob( job );
  if ( it == account->jobsEnd() ) return;
  (*it).done = processed;
  if ( (*it).progressItem ) {
    (*it).progressItem->setCompletedItems( processed );
    (*it).progressItem->updateProgress();
  }
  emit progress( (*it).done, (*it).total );
}

}//namespace KMail

#include "imapjob.moc"