pop3resource.cpp 32.9 KB
Newer Older
Thomas McGuire's avatar
Thomas McGuire committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Copyright 2009 Thomas McGuire <mcguire@kde.org>

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Library General Public License as published
   by the Free Software Foundation; either version 2 of the License or
   ( at your option ) version 3 or, at the discretion of KDE e.V.
   ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include "pop3resource.h"
Igor Trindade Oliveira's avatar
Igor Trindade Oliveira committed
20
#include "accountdialog.h"
Thomas McGuire's avatar
Thomas McGuire committed
21
#include "settings.h"
22
#include "jobs.h"
Thomas McGuire's avatar
Thomas McGuire committed
23

24
#include <Akonadi/CollectionFetchJob>
Thomas McGuire's avatar
Thomas McGuire committed
25
#include <Akonadi/ItemCreateJob>
26
#include <akonadi/kmime/messageflags.h>
Volker Krause's avatar
Volker Krause committed
27
28
#include <akonadi/kmime/specialmailcollectionsrequestjob.h>
#include <akonadi/kmime/specialmailcollections.h>
29
#include <kmime/kmime_util.h>
30
#include <Mailtransport/PrecommandJob>
31
#include <Mailtransport/Transport>
Thomas McGuire's avatar
Thomas McGuire committed
32

33
#include <KPasswordDialog>
Thomas McGuire's avatar
Thomas McGuire committed
34
#include <KMessageBox>
35
#include <kwallet.h>
Thomas McGuire's avatar
Thomas McGuire committed
36

37
38
#include <QTimer>

39
using namespace Akonadi;
40
using namespace MailTransport;
41
using namespace KWallet;
Thomas McGuire's avatar
Thomas McGuire committed
42

43
44
45
46
POP3Resource::POP3Resource( const QString &id )
    : ResourceBase( id ),
      mState( Idle ),
      mPopSession( 0 ),
47
      mAskAgain( false ),
48
      mIntervalTimer( new QTimer( this ) ),
49
      mTestLocalInbox( false ),
50
      mWallet( 0 )
51
{
Thomas McGuire's avatar
Thomas McGuire committed
52
  setNeedsNetwork( true );
53
  Settings::self()->setResourceId( identifier() );
54
  resetState();
Thomas McGuire's avatar
Thomas McGuire committed
55

56
57
  connect( this, SIGNAL(abortRequested()),
           this, SLOT(slotAbortRequested()) );
58
59
  connect( mIntervalTimer, SIGNAL(timeout()),
           this, SLOT(intervalCheckTriggered()) );
Volker Krause's avatar
Volker Krause committed
60
  connect( this, SIGNAL(reloadConfiguration()), SLOT(configurationChanged()) );
61
62
63
64
65
66
67
}

POP3Resource::~POP3Resource()
{
  Settings::self()->writeConfig();
}

Volker Krause's avatar
Volker Krause committed
68
69
70
71
72
73
void POP3Resource::configurationChanged()
{
  Settings::self()->writeConfig();
  updateIntervalTimer();
}

74
75
void POP3Resource::updateIntervalTimer()
{
76
77
  if ( Settings::self()->intervalCheckEnabled() && mState == Idle ) {
    mIntervalTimer->start( Settings::self()->intervalCheckInterval() * 1000 * 60 );
78
79
80
81
82
83
84
85
86
  }
  else {
    mIntervalTimer->stop();
  }
}

void POP3Resource::intervalCheckTriggered()
{
  Q_ASSERT( mState == Idle );
87
  if ( isOnline() ) {
88
89
90
91
92
93
    kDebug() << "Starting interval mail check.";
    startMailCheck();
    mIntervalCheckInProgress = true;
  } else {
    mIntervalTimer->start();
  }
94
95
}

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
void POP3Resource::aboutToQuit()
{
  if ( mState != Idle )
    cancelSync( i18n( "Mail check aborted." ) );
}

void POP3Resource::slotAbortRequested()
{
  if ( mState != Idle )
    cancelSync( i18n( "Mail check was canceled manually." ), false /* no error */ );
}

void POP3Resource::configure( WId windowId )
{
  QPointer<AccountDialog> accountDialog( new AccountDialog( this, windowId ) );
  if ( accountDialog->exec() == QDialog::Accepted ) {
112
    updateIntervalTimer();
113
114
115
116
117
118
    emit configurationDialogAccepted();
  }
  else {
    emit configurationDialogRejected();
  }

Laurent Montel's avatar
Minor    
Laurent Montel committed
119
  delete accountDialog;
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
}

void POP3Resource::retrieveItems( const Akonadi::Collection &collection )
{
  Q_UNUSED( collection );
  kWarning() << "This should never be called, we don't have a collection!";
}

bool POP3Resource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
{
  Q_UNUSED( item );
  Q_UNUSED( parts );
  kWarning() << "This should never be called, we don't have any item!";
  return false;
}

136
137
138
139
140
141
142
143
QString POP3Resource::buildLabelForPasswordDialog( const QString &detailedError ) const
{
  QString queryText = i18n( "Please enter the username and password for account '%1'.",
                            agentName() );
  queryText += "<br>" + detailedError;
  return queryText;
}

144
145
146
147
148
149
void POP3Resource::walletOpenedForLoading( bool success )
{
  bool passwordLoaded = success;
  if ( success ) {
    if ( mWallet && mWallet->isOpen() && mWallet->hasFolder( "pop3" ) ) {
      mWallet->setFolder( "pop3" );
150
151
152
153
      if ( mWallet->hasEntry( identifier() ) )
        mWallet->readPassword( identifier(), mPassword );
      else
        passwordLoaded = false;
154
155
156
157
158
159
160
161
162
    }
    else {
      passwordLoaded = false;
    }
  }
  delete mWallet;
  mWallet = 0;

  if ( !passwordLoaded ) {
163
164
    QString queryText = buildLabelForPasswordDialog(
        i18n( "You are asked here because the password could not be loaded from the wallet." ) );
165
    showPasswordDialog( queryText );
166
167
  }
  else {
168
    advanceState( Connect );
169
170
171
172
173
  }
}

void POP3Resource::walletOpenedForSaving( bool success )
{
174
  if ( success ) {
Laurent Montel's avatar
Laurent Montel committed
175
    if ( mWallet && mWallet->isOpen() ) {
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      if ( !mWallet->hasFolder( "pop3" ) ) {
        mWallet->createFolder( "pop3" );
      }
      mWallet->setFolder( "pop3" );
      mWallet->writePassword( identifier(), mPassword );
    }
  }
  else
    kWarning() << "Unable to write the password to the wallet.";

  delete mWallet;
  mWallet = 0;
  finish();
}


192
void POP3Resource::showPasswordDialog( const QString &queryText )
193
{
194
195
196
197
  QPointer<KPasswordDialog> dlg =
    new KPasswordDialog(
      0,
      KPasswordDialog::ShowUsernameLine );
198
  dlg->setModal( true );
199
200
201
202
203
204
205
206
207
208
  dlg->setUsername( Settings::self()->login() );
  dlg->setPassword( mPassword );
  dlg->setPrompt( queryText );
  dlg->setCaption( name() );
  dlg->addCommentLine( i18n( "Account:" ), name() );

  bool gotIt = false;
  if ( dlg->exec() ) {
    mPassword = dlg->password();
    Settings::self()->setLogin( dlg->username() );
209
    Settings::self()->writeConfig();
210
    if ( !dlg->password().isEmpty()  ) {
211
212
      mSavePassword = true;
    }
213

214
    mAskAgain = false;
215
    advanceState( Connect );
216
217
218
219
220
    gotIt = true;
  }
  delete dlg;
  if ( !gotIt ) {
    cancelSync( i18n( "No username and password supplied." ) );
221
222
223
  }
}

224
225
226
227
228
229
void POP3Resource::advanceState( State nextState )
{
  mState = nextState;
  doStateStep();
}

230
231
232
233
234
235
236
237
238
239
240
241
242
void POP3Resource::doStateStep()
{
  switch ( mState )
  {
  case Idle:
    {
      Q_ASSERT( false );
      kWarning() << "State machine should not be called in idle state!";
      break;
    }
  case FetchTargetCollection:
    {
      kDebug() << "================ Starting state FetchTargetCollection ==========";
Thomas McGuire's avatar
Thomas McGuire committed
243
      emit status( Running, i18n( "Preparing transmission from \"%1\".", name() ) );
244
      Collection targetCollection( Settings::self()->targetCollection() );
245
246
      if ( !targetCollection.isValid() ) {
        // No target collection set in the config? Try requesting a default inbox
Volker Krause's avatar
Volker Krause committed
247
248
        SpecialMailCollectionsRequestJob *requestJob = new SpecialMailCollectionsRequestJob( this );
        requestJob->requestDefaultCollection( SpecialMailCollections::Inbox );
249
250
251
252
253
254
255
256
257
258
259
260
261
        requestJob->start();
        connect ( requestJob, SIGNAL(result(KJob*)),
                  this, SLOT(localFolderRequestJobFinished(KJob*)) );
      }
      else {
        CollectionFetchJob *fetchJob = new CollectionFetchJob( targetCollection,
                                                          CollectionFetchJob::Base );
        fetchJob->start();
        connect( fetchJob, SIGNAL(result(KJob*)),
                 this, SLOT(targetCollectionFetchJobFinished(KJob*)) );
      }
      break;
    }
262
263
264
  case Precommand:
    {
      kDebug() << "================ Starting state Precommand =====================";
265
266
      if ( !Settings::self()->precommand().isEmpty() ) {
        PrecommandJob *precommandJob = new PrecommandJob( Settings::self()->precommand(), this );
267
268
269
        connect( precommandJob, SIGNAL(result(KJob*)),
                 this, SLOT(precommandResult(KJob*)) );
        precommandJob->start();
Thomas McGuire's avatar
Thomas McGuire committed
270
        emit status( Running, i18n( "Executing precommand." ) );
271
272
      }
      else {
273
        advanceState( RequestPassword );
274
275
276
277
278
279
280
      }
      break;
    }
  case RequestPassword:
    {
      kDebug() << "================ Starting state RequestPassword ================";

281
      // Don't show any wallet or password prompts when we are unit-testing
282
283
      if ( !Settings::self()->unitTestPassword().isEmpty() ) {
        mPassword = Settings::self()->unitTestPassword();
284
        advanceState( Connect );
285
286
287
        break;
      }

288
      const bool passwordNeeded = Settings::self()->authenticationMethod() != MailTransport::Transport::EnumAuthenticationType::GSSAPI;
289
290
      const bool loadPasswordFromWallet = !mAskAgain && passwordNeeded && !Settings::self()->login().isEmpty() &&
                                          mPassword.isEmpty();
291
      if ( loadPasswordFromWallet ) {
292
        mWallet = Wallet::openWallet( Wallet::NetworkWallet(), winIdForDialogs(),
293
                                      Wallet::Asynchronous );
294
295
      }
      if ( loadPasswordFromWallet && mWallet ) {
296
297
298
        connect( mWallet, SIGNAL(walletOpened(bool)),
                 this, SLOT(walletOpenedForLoading(bool)) );
      }
299
      else if ( passwordNeeded && ( mPassword.isEmpty() || mAskAgain ) ) {
300
        QString detail;
301
        if ( mAskAgain )
302
          detail = i18n( "You are asked here because the previous login was not successful." );
303
        else if ( Settings::self()->login().isEmpty() )
304
          detail = i18n( "You are asked here because the username you supplied is empty." );
305
306
        else if ( !mWallet )
          detail = i18n( "You are asked here because the wallet password storage is disabled." );
307

308
        showPasswordDialog( buildLabelForPasswordDialog( detail ) );
309
310
      }
      else {
311
        // No password needed or using previous password, go on with Connect
312
        advanceState( Connect );
313
      }
314

315
316
      break;
    }
317
318
319
320
  case Connect:
    {
      kDebug() << "================ Starting state Connect ========================";
      Q_ASSERT( !mPopSession );
321
      mPopSession = new POPSession( mPassword );
Laurent Montel's avatar
Laurent Montel committed
322
323
      connect( mPopSession, SIGNAL(slaveError(int,QString)),
               this, SLOT(slotSessionError(int,QString)) );
324
      advanceState( Login );
325
326
327
328
329
330
331
332
333
334
335
336
337
338
      break;
    }
  case Login:
    {
      kDebug() << "================ Starting state Login ==========================";

      LoginJob *loginJob = new LoginJob( mPopSession );
      connect( loginJob, SIGNAL(result(KJob*)),
               this, SLOT(loginJobResult(KJob*)) );
      loginJob->start();
      break;
    }
  case List:
    {
339
      kDebug() << "================ Starting state List ===========================";
Thomas McGuire's avatar
Thomas McGuire committed
340
      emit status( Running, i18n( "Fetching mail listing." ) );
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
      ListJob *listJob = new ListJob( mPopSession );
      connect( listJob, SIGNAL(result(KJob*)),
               this, SLOT(listJobResult(KJob*)) );
      listJob->start();
    }
    break;
  case UIDList:
    {
      kDebug() << "================ Starting state UIDList ========================";
      UIDListJob *uidListJob = new UIDListJob( mPopSession );
      connect( uidListJob, SIGNAL(result(KJob*)),
               this, SLOT(uidListJobResult(KJob*)) );
      uidListJob->start();
    }
    break;
  case Download:
    {
358
      kDebug() << "================ Starting state Download =======================";
359
360
361
362
363
364
365
      FetchJob *fetchJob = new FetchJob( mPopSession );

      // Determine which mails we want to download. Those are all mails which are
      // currently on ther server, minus the ones we have already downloaded (we
      // remember which UIDs we have downloaded in the settings)
      QList<int> idsToDownload = mIdsToSizeMap.keys();
      const QList<QString> UIDsOnServer = mIdsToUidsMap.values();
366
      const QList<QString> alreadyDownloadedUIDs = Settings::self()->seenUidList();
367
368
      foreach( const QString &uidOnServer, UIDsOnServer ) {
        if ( alreadyDownloadedUIDs.contains( uidOnServer ) ) {
369
          const int idOfUIDOnServer = mUidsToIdsMap.value( uidOnServer, -1 );
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
          Q_ASSERT( idOfUIDOnServer != -1 );
          idsToDownload.removeAll( idOfUIDOnServer );
        }
      }
      mIdsToDownload = idsToDownload;
      kDebug() << "We are going to download" << mIdsToDownload.size() << "messages";

      // For proper progress, the job needs to know the sizes of the messages, so
      // put them into a list here
      QList<int> sizesOfMessagesToDownload;
      foreach( int id, idsToDownload ) {
        sizesOfMessagesToDownload.append( mIdsToSizeMap.value( id ) );
      }

      fetchJob->setFetchIds( idsToDownload, sizesOfMessagesToDownload );
      connect( fetchJob, SIGNAL(result(KJob*)),
               this, SLOT(fetchJobResult(KJob*)) );
      connect( fetchJob, SIGNAL(messageFinished(int,KMime::Message::Ptr)),
               this, SLOT(messageFinished(int,KMime::Message::Ptr)) );
      connect( fetchJob, SIGNAL(processedAmount(KJob*,KJob::Unit,qulonglong)),
               this, SLOT(messageDownloadProgress(KJob*,KJob::Unit,qulonglong)) );
      fetchJob->start();
    }
    break;
  case Save:
    {
396
      kDebug() << "================ Starting state Save ===========================";
397
398
      kDebug() << mPendingCreateJobs.size() << "item create jobs are pending";
      if ( mPendingCreateJobs.size() > 0 )
Thomas McGuire's avatar
Thomas McGuire committed
399
        emit status( Running, i18n( "Saving downloaded messages." ) );
400
401
402
403
404

      // It can happen that the create job map is empty, for example if there was no
      // mail to download or if all ItemCreateJob's finished before reaching this
      // stage
      if ( mPendingCreateJobs.isEmpty() ) {
405
        advanceState( Delete );
406
407
408
409
410
      }
    }
    break;
  case Delete:
    {
411
      kDebug() << "================ Starting state Delete =========================";
412
413
      QList<int> idsToKill = idsToDelete();
      if ( !idsToKill.isEmpty() ) {
414
        emit status( Running, i18n( "Deleting messages from the server.") );
415
416
417
418
419
420
421
        DeleteJob *deleteJob = new DeleteJob( mPopSession );
        deleteJob->setDeleteIds( idsToKill );
        connect( deleteJob, SIGNAL(result(KJob*)),
                 this, SLOT(deleteJobResult(KJob*)) );
        deleteJob->start();
      }
      else {
422
        advanceState( Quit );
423
424
425
426
427
      }
    }
    break;
  case Quit:
    {
428
      kDebug() << "================ Starting state Quit ===========================";
429
430
431
432
433
434
      QuitJob *quitJob = new QuitJob( mPopSession );
      connect( quitJob, SIGNAL(result(KJob*)),
               this, SLOT(quitJobResult(KJob*)) );
      quitJob->start();
    }
    break;
435
436
437
438
439
440
441
442
  case SavePassword:
    {
      kDebug() << "================ Starting state SavePassword ===================";
      if ( !mSavePassword )
        finish();
      else {
        kDebug() << "Writing password back to the wallet.";
        emit status( Running, i18n( "Saving password to the wallet." ) );
443
        mWallet = Wallet::openWallet( Wallet::NetworkWallet(), winIdForDialogs(),
444
                                      Wallet::Asynchronous );
445
446
447
448
449
450
        if ( mWallet ) {
          connect( mWallet, SIGNAL(walletOpened(bool)),
                   this, SLOT(walletOpenedForSaving(bool)) );
        } else {
          finish();
        }
451
452
453
      }
      break;
    }
454
455
456
457
458
459
460
  }
}

void POP3Resource::localFolderRequestJobFinished( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Error while trying to get the local inbox folder, "
Tobias Koenig's avatar
Tobias Koenig committed
461
                      "aborting mail check." ) + '\n' + job->errorString() );
462
463
    return;
  }
464
465
466
467
468
469
470
  if ( mTestLocalInbox ) {
    KMessageBox::information(0,
                             i18n("<qt>The folder you deleted was associated with the account "
                                  "<b>%1</b> which delivered mail into it. The folder the account "
                                  "delivers new mail into was reset to the main Inbox folder.</qt>", name()));
  }
  mTestLocalInbox = false;
471

Volker Krause's avatar
Volker Krause committed
472
  mTargetCollection = SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::Inbox );
473
  Q_ASSERT( mTargetCollection.isValid() );
474
  advanceState( Precommand );
475
476
477
478
479
}

void POP3Resource::targetCollectionFetchJobFinished( KJob *job )
{
  if ( job->error() ) {
480
481
482
483
484
485
486
    if ( !mTestLocalInbox ) {
      mTestLocalInbox = true;
      Settings::self()->setTargetCollection(  -1  );
      advanceState( FetchTargetCollection );
      return;
    } else {
      cancelSync( i18n( "Error while trying to get the folder for incoming mail, "
Tobias Koenig's avatar
Tobias Koenig committed
487
                        "aborting mail check." ) + '\n' + job->errorString() );
488
489
490
      mTestLocalInbox = false;
      return;
    }
491
  }
492
  mTestLocalInbox = false;
493
494
495
496
497
498
499
500
501
502
503
  Akonadi::CollectionFetchJob *fetchJob =
      dynamic_cast<Akonadi::CollectionFetchJob*>( job );
  Q_ASSERT( fetchJob );
  Q_ASSERT( fetchJob->collections().size() <= 1 );

  if ( fetchJob->collections().isEmpty() ) {
    cancelSync( i18n( "Could not find folder for incoming mail, aborting mail check.") );
    return;
  }
  else {
    mTargetCollection = fetchJob->collections().first();
504
    advanceState( Precommand );
505
506
507
508
509
510
511
512
513
514
515
  }
}

void POP3Resource::precommandResult( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Error while executing precommand." ) +
                '\n' + job->errorString() );
    return;
  }
  else {
516
    advanceState( RequestPassword );
517
518
519
520
521
522
  }
}

void POP3Resource::loginJobResult( KJob *job )
{
  if ( job->error() ) {
523
    kDebug() << job->error() << job->errorText();
524
    if ( job->error() == KIO::ERR_COULD_NOT_LOGIN )
525
526
527
      mAskAgain = true;
    cancelSync( i18n( "Unable to login to the server %1.", Settings::self()->host() ) +
                '\n' + job->errorString() );
528
529
  }
  else {
530
    advanceState( List );
531
532
533
534
535
536
537
538
539
540
541
542
543
544
  }
}

void POP3Resource::listJobResult( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Error while getting the list of messages on the server." ) +
                '\n' + job->errorString() );
  }
  else {
    ListJob *listJob = dynamic_cast<ListJob*>( job );
    Q_ASSERT( listJob );
    mIdsToSizeMap = listJob->idList();
    kDebug() << "IdsToSizeMap:" << mIdsToSizeMap;
545
    advanceState( UIDList );
546
547
548
549
550
551
552
553
554
555
556
557
558
  }
}

void POP3Resource::uidListJobResult( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Error while getting list of unique mail identifiers from the server." ) +
                '\n' + job->errorString() );
  }
  else {
    UIDListJob *listJob = dynamic_cast<UIDListJob*>( job );
    Q_ASSERT( listJob );
    mIdsToUidsMap = listJob->uidList();
559
    mUidsToIdsMap = listJob->idList();
560
    kDebug() << "IdToUidMap:" << mIdsToUidsMap;
561
    kDebug() << "UidToIdMap:" << mUidsToIdsMap;
562

563
    mUidListValid = !mIdsToUidsMap.isEmpty() || mIdsToSizeMap.isEmpty();
564
    if ( Settings::self()->leaveOnServer() && !mUidListValid ) {
565
566
567
568
569
570
571
572
573
      // FIXME: this needs a proper parent window
      KMessageBox::sorry( 0,
            i18n( "Your POP3 server (Account: %1) does not support "
                  "the UIDL command: this command is required to determine, in a reliable way, "
                  "which of the mails on the server KMail has already seen before;\n"
                  "the feature to leave the mails on the server will therefore not "
                  "work properly.", name() ) );
    }

574
    advanceState( Download );
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  }
}

void POP3Resource::fetchJobResult( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Error while fetching mails from the server." ) +
                '\n' + job->errorString() );
    return;
  }
  else {
    kDebug() << "Downloaded" << mDownloadedIDs.size() << "mails";

    if ( !mIdsToDownload.isEmpty() ) {
      kWarning() << "We did not download all messages, there are still some remaining "
                    "IDs, even though we requested their download:" << mIdsToDownload;
    }

593
    advanceState( Save );
594
595
596
597
598
  }
}

void POP3Resource::messageFinished( int messageId, KMime::Message::Ptr message )
{
599
  if ( mState != Download ) {
600
601
602
603
604
    // This can happen if the slave does not get notified in time about the fact
    // that the job was killed
    return;
  }

605
606
  //kDebug() << "Got message" << messageId
  //         << "with subject" << message->subject()->asUnicodeString();
607
608
609
610
611

  Akonadi::Item item;
  item.setMimeType( "message/rfc822" );
  item.setPayload<KMime::Message::Ptr>( message );

612
613
614
615
616
617
618
619
620
621
  // update status flags
  if ( KMime::isSigned( message.get() ) )
    item.setFlag( Akonadi::MessageFlags::Signed );
  if ( KMime::isEncrypted( message.get() ) )
    item.setFlag( Akonadi::MessageFlags::Encrypted );
  if ( KMime::isInvitation( message.get() ) )
    item.setFlag( Akonadi::MessageFlags::HasInvitation );
  if ( KMime::hasAttachment( message.get() ) )
    item.setFlag( Akonadi::MessageFlags::HasAttachment );

622
  ItemCreateJob *itemCreateJob = new ItemCreateJob( item, mTargetCollection );
623
624
625
626
627
628
629
630
631
632
633
634

  mPendingCreateJobs.insert( itemCreateJob, messageId );
  connect( itemCreateJob, SIGNAL(result(KJob*)),
           this, SLOT(itemCreateJobResult(KJob*)) );

  mDownloadedIDs.append( messageId );
  mIdsToDownload.removeAll( messageId );
}

void POP3Resource::messageDownloadProgress( KJob *job, KJob::Unit unit, qulonglong totalBytes )
{
  Q_UNUSED( totalBytes );
635
  Q_UNUSED( unit );
636
637
638
639
  Q_ASSERT( unit == KJob::Bytes );
  QString statusMessage;
  const int totalMessages = mIdsToDownload.size() + mDownloadedIDs.size();
  int bytesRemainingOnServer = 0;
640
  foreach( const QString &alreadyDownloadedUID, Settings::self()->seenUidList() ) {
641
    const int alreadyDownloadedID = mUidsToIdsMap.value( alreadyDownloadedUID, -1 );
642
643
644
645
    if ( alreadyDownloadedID != -1 )
      bytesRemainingOnServer += mIdsToSizeMap.value( alreadyDownloadedID );
  }

646
  if ( Settings::self()->leaveOnServer() && bytesRemainingOnServer > 0 ) {
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

    statusMessage = i18n( "Fetching message %1 of %2 (%3 of %4 KB) for %5 "
                          "(%6 KB remain on the server).",
                          mDownloadedIDs.size() + 1, totalMessages,
                          job->processedAmount( KJob::Bytes ) / 1024,
                          job->totalAmount( KJob::Bytes ) / 1024, name(),
                          bytesRemainingOnServer / 1024 );
  }
  else {
    statusMessage = i18n( "Fetching message %1 of %2 (%3 of %4 KB) for %5",
                          mDownloadedIDs.size() + 1, totalMessages,
                          job->processedAmount( KJob::Bytes ) / 1024,
                          job->totalAmount( KJob::Bytes ) / 1024, name() );
  }
  emit status( Running, statusMessage );
  emit percent( job->percent() );
}

void POP3Resource::itemCreateJobResult( KJob *job )
{
  if ( mState != Download && mState != Save ) {
    // This can happen if the slave does not get notified in time about the fact
    // that the job was killed
    return;
  }

  ItemCreateJob *createJob = dynamic_cast<ItemCreateJob*>( job );
  Q_ASSERT( createJob );

  if ( job->error() ) {
    cancelSync( i18n( "Unable to store downloaded mails." ) +
                '\n' + job->errorString() );
    return;
  }

  const int idOfMessageJustCreated = mPendingCreateJobs.value( createJob, -1 );
  Q_ASSERT( idOfMessageJustCreated != -1 );
  mPendingCreateJobs.remove( createJob );
  mIDsStored.append( idOfMessageJustCreated );
686
687
  //kDebug() << "Just stored message with ID" << idOfMessageJustCreated
  //         << "on the Akonadi server";
688
689
690

  // Have all create jobs finished? Go to the next state, then
  if ( mState == Save && mPendingCreateJobs.isEmpty() ) {
691
    advanceState( Delete );
692
693
694
695
  }
}

int POP3Resource::idToTime( int id ) const
696
{
697
698
  const QString uid = mIdsToUidsMap.value( id );
  if ( !uid.isEmpty() ) {
699
    const int index = Settings::self()->seenUidList().indexOf( uid );
700
    if ( index != -1 )
701
      return Settings::self()->seenUidTimeList().at( index );
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
  }

  // If we don't find any mail, either we have no UID, or it is not in the seen UID
  // list. In that case, we assume that the mail is new, i.e. from now
  return time( 0 );
}

int POP3Resource::idOfOldestMessage( QList<int> &idList ) const
{
  int timeOfOldestMessage = time( 0 ) + 999;
  int idOfOldestMessage = -1;
  foreach( int id, idList ) {
    const int idTime = idToTime( id );
    if ( idTime < timeOfOldestMessage ) {
      timeOfOldestMessage = idTime;
      idOfOldestMessage = id;
    }
  }
  Q_ASSERT( idList.isEmpty() || idOfOldestMessage != -1 );
  return idOfOldestMessage;
}

QList<int> POP3Resource::idsToDelete() const
{
  QList<int> idsToDeleteFromServer = mIdsToSizeMap.keys();
  QList<int> idsToSave;

  // Don't attempt to delete messages that weren't downloaded correctly
  foreach( int idNotDownloaded, mIdsToDownload )
    idsToDeleteFromServer.removeAll( idNotDownloaded );

  // By default, we delete all messages. But if we have "leave on server"
  // rules, we can save some messages.
735
  if ( Settings::self()->leaveOnServer() && !idsToDeleteFromServer.isEmpty() ) {
736
737
738

    // If the time-limited leave rule is checked, add the newer messages to
    // the list of messages to keep
739
    if ( Settings::self()->leaveOnServerDays() > 0 ) {
740
      const int secondsPerDay = 86400;
741
      time_t timeLimit = time( 0 ) - ( secondsPerDay * Settings::self()->leaveOnServerDays() );
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
      foreach( int idToDelete, idsToDeleteFromServer ) {
        const int msgTime = idToTime( idToDelete );
        if ( msgTime >= timeLimit ) {
          idsToSave.append( idToDelete );
        }
        else {
          kDebug() << "Message" << idToDelete << "is too old and will be deleted.";
        }
      }
    }

    // Otherwise, add all messages to the list of messages to keep - this may
    // be reduced in the following number-limited leave rule and size-limited
    // leave rule checks
    else {
      foreach ( int idToDelete, idsToDeleteFromServer ) {
        idsToSave.append( idToDelete );
      }
    }

    //
    // Delete more old messages if there are more than mLeaveOnServerCount
    //
765
766
    if ( Settings::self()->leaveOnServerCount() > 0 ) {
      const int numToDelete = idsToSave.count() - Settings::self()->leaveOnServerCount();
767
768
769
770
771
772
773
774
775
776
777
778
779
      if ( numToDelete > 0 && numToDelete < idsToSave.count() ) {
        // Get rid of the first numToDelete messages
        for ( int i = 0; i < numToDelete; i++ ) {
          idsToSave.removeAll( idOfOldestMessage( idsToSave ) );
        }
      }
      else if ( numToDelete >= idsToSave.count() )
        idsToSave.clear();
    }

    //
    // Delete more old messages until we're under mLeaveOnServerSize MBs
    //
780
781
    if ( Settings::self()->leaveOnServerSize() > 0 ) {
      const qint64 limitInBytes = Settings::self()->leaveOnServerSize() * ( 1024 * 1024 );
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
      qint64 sizeOnServerAfterDeletion = 0;
      foreach( int id, idsToSave ) {
        sizeOnServerAfterDeletion += mIdsToSizeMap.value( id );
      }
      while ( sizeOnServerAfterDeletion > limitInBytes ) {
        int oldestId = idOfOldestMessage( idsToSave );
        idsToSave.removeAll( oldestId );
        sizeOnServerAfterDeletion -= mIdsToSizeMap.value( oldestId );
      }
    }

    //
    // Now save the messages from deletion
    //
    foreach( int idToSave, idsToSave ) {
      idsToDeleteFromServer.removeAll( idToSave );
    }
  }

  kDebug() << "Going to delete" << idsToDeleteFromServer.size() << idsToDeleteFromServer;
802
  return idsToDeleteFromServer;
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
}

void POP3Resource::deleteJobResult( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Failed to delete the messages from the server.") +
                '\n' + job->errorString() );
    return;
  }

  DeleteJob *deleteJob = dynamic_cast<DeleteJob*>( job );
  Q_ASSERT( deleteJob );
  mDeletedIDs = deleteJob->deletedIDs();

  // Remove all deleted messages from the list of already downloaded messages,
  // as it is no longer necessary to store them (they just waste space)
819
820
  QList<QString> seenUIDs = Settings::self()->seenUidList();
  QList<int> timeOfSeenUids = Settings::self()->seenUidTimeList();
821
822
823
824
825
826
827
828
829
830
831
832
833
  Q_ASSERT( seenUIDs.size() == timeOfSeenUids.size() );
  foreach( int deletedId, mDeletedIDs ) {
    QString deletedUID = mIdsToUidsMap.value( deletedId );
    if ( !deletedUID.isEmpty() ) {
      int index = seenUIDs.indexOf( deletedUID );
      if ( index != -1 ) {
        // TEST
        kDebug() << "Removing UID" << deletedUID << "from the seen UID list, as it was deleted.";
        seenUIDs.removeAt( index );
        timeOfSeenUids.removeAt( index );
      }
    }
  }
834
835
  Settings::self()->setSeenUidList( seenUIDs );
  Settings::self()->setSeenUidTimeList( timeOfSeenUids );
836
837
  Settings::self()->writeConfig(),

838
  advanceState( Quit );
839
840
}

841
void POP3Resource::finish()
842
843
844
{
  kDebug() << "================= Mail check finished. =============================";
  saveSeenUIDList();
845
846
  if ( !mIntervalCheckInProgress )
    collectionsRetrieved( Akonadi::Collection::List() );
847
848
849
850
851
  if ( mDownloadedIDs.isEmpty() )
    emit status( Idle, i18n( "Finished mail check, no message downloaded." ) );
  else
    emit status( Idle, i18np( "Finished mail check, 1 message downloaded.",
                              "Finished mail check, %1 messages downloaded.",
Thomas McGuire's avatar
Thomas McGuire committed
852
                              mDownloadedIDs.size() ) );
853
854
855
856

  resetState();
}

857
858
859
860
861
862
863
864
void POP3Resource::quitJobResult( KJob *job )
{
  if ( job->error() ) {
    cancelSync( i18n( "Unable to complete the mail fetch." ) +
                '\n' + job->errorString() );
    return;
  }

865
  advanceState( SavePassword );
866
867
}

868
869
870
871
872
873
874
875
void POP3Resource::slotSessionError( int errorCode, const QString &errorMessage )
{
  kWarning() << "Error in our session, unrelated to a currently running job!";
  cancelSync( KIO::buildErrorString( errorCode, errorMessage ) );
}

void POP3Resource::saveSeenUIDList()
{
876
877
  QList<QString> seenUIDs = Settings::self()->seenUidList();
  QList<int> timeOfSeenUIDs = Settings::self()->seenUidTimeList();
878
879

  //
Volker Krause's avatar
Volker Krause committed
880
  // Find the messages that we have successfully stored, but did not actually get
881
882
883
  // deleted.
  // Those messages, we have to remember, so we don't download them again.
  //
884
885
886
887
888
889
890
891
892
893
894
  QList<int> idsOfMessagesDownloadedButNotDeleted = mIDsStored;
  foreach( int deletedId, mDeletedIDs )
    idsOfMessagesDownloadedButNotDeleted.removeAll( deletedId );
  QList<QString> uidsOfMessagesDownloadedButNotDeleted;
  foreach( int id, idsOfMessagesDownloadedButNotDeleted ) {
    QString uid = mIdsToUidsMap.value( id );
    if ( !uid.isEmpty() ) {
      uidsOfMessagesDownloadedButNotDeleted.append( uid );
    }
  }
  Q_ASSERT( seenUIDs.size() == timeOfSeenUIDs.size() );
Tobias Koenig's avatar
Tobias Koenig committed
895
  foreach( const QString &uid, uidsOfMessagesDownloadedButNotDeleted ) {
896
897
898
899
900
901
    if ( !seenUIDs.contains( uid ) ) {
      seenUIDs.append( uid );
      timeOfSeenUIDs.append( time( 0 ) );
    }
  }

902
903
904
905
906
907
908
909
910
911
912
  //
  // If we have a valid UID list from the server, delete those UIDs that are in
  // the seenUidList but are not on the server.
  // This can happen if someone else than this resource deleted the mails from the
  // server which we kept here.
  //
  if ( mUidListValid ) {
    QList<QString>::iterator uidIt = seenUIDs.begin();
    QList<int>::iterator timeIt = timeOfSeenUIDs.begin();
    while ( uidIt != seenUIDs.end() ) {
      const QString curSeenUID = *uidIt;
913
      if ( !mUidsToIdsMap.contains( curSeenUID ) ) {
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
        // Ok, we have a UID in the seen UID list that is not anymore on the server.
        // Therefore remove it from the seen UID list, it is not needed there anymore,
        // it just wastes space.
        uidIt = seenUIDs.erase( uidIt );
        timeIt = timeOfSeenUIDs.erase( timeIt );
      }
      else {
        uidIt++;
        timeIt++;
      }
    }
  }
  else
    kWarning() << "UID list from server is not valid.";


  //
  // Now save it in the settings
  //
933
  kDebug() << "The seen UID list has" << seenUIDs.size() << "entries";
934
935
  Settings::self()->setSeenUidList( seenUIDs );
  Settings::self()->setSeenUidTimeList( timeOfSeenUIDs );
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
  Settings::self()->writeConfig();
}

void POP3Resource::cancelSync( const QString &errorMessage, bool error )
{
  if ( error ) {
    cancelTask( errorMessage );
    kWarning() << "============== ERROR DURING POP3 SYNC ==========================";
    kWarning() << errorMessage;
  }
  else {
    kDebug() << "Canceled the sync, but no error.";
    cancelTask();
  }
  saveSeenUIDList();
  resetState();
}

void POP3Resource::resetState()
{
  mState = Idle;
  mTargetCollection = Collection( -1 );
  mIdsToSizeMap.clear();
  mIdsToUidsMap.clear();
960
  mUidsToIdsMap.clear();
961
962
963
964
965
  mDownloadedIDs.clear();
  mIdsToDownload.clear();
  mPendingCreateJobs.clear();
  mIDsStored.clear();
  mDeletedIDs.clear();
966
  mUidListValid = false;
967
  mIntervalCheckInProgress = false;
968
  mSavePassword = false;
969
  updateIntervalTimer();
970
971
  delete mWallet;
  mWallet = 0;
972

973
974
975
976
977
978
979
980
981
  if ( mPopSession ) {
    // Closing the POP session means the KIO slave will get disconnected, which
    // automatically issues the QUIT command.
    // Delete the POP session later, otherwise the scheduler makes us crash
    mPopSession->abortCurrentJob();
    mPopSession->deleteLater();
    mPopSession = 0;
  }
}
Thomas McGuire's avatar
Thomas McGuire committed
982

983
984
985
986
987
void POP3Resource::startMailCheck()
{
  resetState();
  mIntervalTimer->stop();
  emit percent( 0 ); // Otherwise the value from the last sync is taken
988
  advanceState( FetchTargetCollection );
989
990
}

991
992
void POP3Resource::retrieveCollections()
{
993
  if ( mState == Idle ) {
994
    startMailCheck();
995
996
997
998
999
1000
  }
  else {
    cancelSync(
        i18n( "Mail check already in progress, unable to start a second check." ) );
  }
}