transportmanager.cpp 18 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  Copyright (c) 2006 - 2007 Volker Krause <vkrause@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) 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.
18 19
*/

20
#include "transportmanager.h"
21
#include "mailtransport_defs.h"
22
#include "smtpjob.h"
23
#include "transport.h"
24
#include "transport_p.h"
25
#include "transportjob.h"
26 27
#include "transporttype.h"
#include "transporttype_p.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include "widgets/addtransportdialog.h"
29 30 31
#include "widgets/transportconfigdialog.h"
#include "widgets/transportconfigwidget.h"
#include "widgets/smtpconfigwidget.h"
Laurent Montel's avatar
Laurent Montel committed
32
#include "helper_p.h"
33 34

#include <QApplication>
35 36
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>
Tobias Koenig's avatar
Tobias Koenig committed
37
#include <QtDBus/QDBusServiceWatcher>
38
#include <QPointer>
39 40 41
#include <QRegExp>
#include <QStringList>

42 43
#include <KConfig>
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
44
#include "mailtransport_debug.h"
45
#include <KEMailSettings>
46
#include <KLocalizedString>
47 48
#include <KMessageBox>
#include <KRandom>
Laurent Montel's avatar
Laurent Montel committed
49
#include <Kdelibs4ConfigMigrator>
50

Laurent Montel's avatar
Laurent Montel committed
51
#include <KWallet/kwallet.h>
52

53
using namespace MailTransport;
54 55
using namespace KWallet;

Laurent Montel's avatar
Laurent Montel committed
56
namespace MailTransport {
Tom Albers's avatar
Tom Albers committed
57 58 59 60
/**
 * Private class that helps to provide binary compatibility between releases.
 * @internal
 */
Volker Krause's avatar
Volker Krause committed
61
class TransportManagerPrivate
Allen Winter's avatar
Allen Winter committed
62
{
Laurent Montel's avatar
Laurent Montel committed
63 64
public:
    TransportManagerPrivate(TransportManager *parent)
Laurent Montel's avatar
Laurent Montel committed
65 66 67
        : config(nullptr)
        , wallet(nullptr)
        , q(parent)
Thomas McGuire's avatar
Thomas McGuire committed
68 69 70
    {
    }

Laurent Montel's avatar
Laurent Montel committed
71 72 73 74
    ~TransportManagerPrivate()
    {
        delete config;
        qDeleteAll(transports);
75
    }
76

Tom Albers's avatar
Tom Albers committed
77
    KConfig *config;
Allen Winter's avatar
Allen Winter committed
78
    QList<Transport *> transports;
79
    TransportType::List types;
Tom Albers's avatar
Tom Albers committed
80
    bool myOwnChange;
81
    bool appliedChange;
Tom Albers's avatar
Tom Albers committed
82 83 84 85 86
    KWallet::Wallet *wallet;
    bool walletOpenFailed;
    bool walletAsyncOpen;
    int defaultTransportId;
    bool isMainInstance;
Allen Winter's avatar
Allen Winter committed
87
    QList<TransportJob *> walletQueue;
Thomas McGuire's avatar
Thomas McGuire committed
88
    TransportManager *q;
89 90 91 92 93 94 95 96 97 98 99

    void readConfig();
    void writeConfig();
    void fillTypes();
    int createId() const;
    void prepareWallet();
    void validateDefault();
    void migrateToWallet();

    // Slots
    void slotTransportsChanged();
Laurent Montel's avatar
Laurent Montel committed
100
    void slotWalletOpened(bool success);
Tobias Koenig's avatar
Tobias Koenig committed
101
    void dbusServiceUnregistered();
Laurent Montel's avatar
Laurent Montel committed
102
    void jobResult(KJob *job);
103
};
Volker Krause's avatar
Volker Krause committed
104 105
}

106 107
class StaticTransportManager : public TransportManager
{
Laurent Montel's avatar
Laurent Montel committed
108
public:
Laurent Montel's avatar
Laurent Montel committed
109 110 111
    StaticTransportManager() : TransportManager()
    {
    }
Tom Albers's avatar
Tom Albers committed
112 113
};

Laurent Montel's avatar
Laurent Montel committed
114
StaticTransportManager *sSelf = nullptr;
115

Laurent Montel's avatar
Laurent Montel committed
116 117 118
static void destroyStaticTransportManager()
{
    delete sSelf;
119
}
120 121

TransportManager::TransportManager()
Laurent Montel's avatar
Laurent Montel committed
122 123
    : QObject()
    , d(new TransportManagerPrivate(this))
124
{
Laurent Montel's avatar
Laurent Montel committed
125 126
    Kdelibs4ConfigMigrator migrate(QStringLiteral("transportmanager"));
    migrate.setConfigFiles(QStringList() << QStringLiteral("mailtransports"));
Laurent Montel's avatar
Laurent Montel committed
127 128
    migrate.migrate();

Laurent Montel's avatar
Laurent Montel committed
129 130 131
    qAddPostRoutine(destroyStaticTransportManager);
    d->myOwnChange = false;
    d->appliedChange = false;
Laurent Montel's avatar
Laurent Montel committed
132
    d->wallet = nullptr;
Laurent Montel's avatar
Laurent Montel committed
133 134 135
    d->walletOpenFailed = false;
    d->walletAsyncOpen = false;
    d->defaultTransportId = -1;
Laurent Montel's avatar
Laurent Montel committed
136
    d->config = new KConfig(QStringLiteral("mailtransports"));
137

Laurent Montel's avatar
Laurent Montel committed
138
    QDBusConnection::sessionBus().registerObject(DBUS_OBJECT_PATH, this,
Laurent Montel's avatar
Laurent Montel committed
139 140
                                                 QDBusConnection::ExportScriptableSlots
                                                 |QDBusConnection::ExportScriptableSignals);
Tobias Koenig's avatar
Tobias Koenig committed
141

Laurent Montel's avatar
Laurent Montel committed
142 143 144
    QDBusServiceWatcher *watcher
        = new QDBusServiceWatcher(DBUS_SERVICE_NAME, QDBusConnection::sessionBus(),
                                  QDBusServiceWatcher::WatchForUnregistration, this);
Laurent Montel's avatar
Laurent Montel committed
145 146
    connect(watcher, SIGNAL(serviceUnregistered(QString)),
            SLOT(dbusServiceUnregistered()));
147

Laurent Montel's avatar
Laurent Montel committed
148 149 150
    QDBusConnection::sessionBus().connect(QString(), QString(),
                                          DBUS_INTERFACE_NAME, DBUS_CHANGE_SIGNAL,
                                          this, SLOT(slotTransportsChanged()));
151

Laurent Montel's avatar
Laurent Montel committed
152
    d->isMainInstance = QDBusConnection::sessionBus().registerService(DBUS_SERVICE_NAME);
153

Laurent Montel's avatar
Laurent Montel committed
154
    d->fillTypes();
155 156 157 158
}

TransportManager::~TransportManager()
{
Laurent Montel's avatar
Laurent Montel committed
159 160
    qRemovePostRoutine(destroyStaticTransportManager);
    delete d;
161 162
}

Allen Winter's avatar
Allen Winter committed
163
TransportManager *TransportManager::self()
164
{
Laurent Montel's avatar
Laurent Montel committed
165 166 167 168 169
    if (!sSelf) {
        sSelf = new StaticTransportManager;
        sSelf->d->readConfig();
    }
    return sSelf;
170 171
}

Laurent Montel's avatar
Laurent Montel committed
172
Transport *TransportManager::transportById(int id, bool def) const
173
{
Laurent Montel's avatar
Laurent Montel committed
174 175 176 177
    foreach (Transport *t, d->transports) {
        if (t->id() == id) {
            return t;
        }
Allen Winter's avatar
Allen Winter committed
178
    }
179

Laurent Montel's avatar
Laurent Montel committed
180 181 182
    if (def || (id == 0 && d->defaultTransportId != id)) {
        return transportById(d->defaultTransportId, false);
    }
Laurent Montel's avatar
Laurent Montel committed
183
    return nullptr;
184 185
}

Laurent Montel's avatar
Laurent Montel committed
186
Transport *TransportManager::transportByName(const QString &name, bool def) const
187
{
Laurent Montel's avatar
Laurent Montel committed
188 189 190 191
    foreach (Transport *t, d->transports) {
        if (t->name() == name) {
            return t;
        }
Allen Winter's avatar
Allen Winter committed
192
    }
Laurent Montel's avatar
Laurent Montel committed
193 194 195
    if (def) {
        return transportById(0, false);
    }
Laurent Montel's avatar
Laurent Montel committed
196
    return nullptr;
197 198 199 200
}

QList< Transport * > TransportManager::transports() const
{
Laurent Montel's avatar
Laurent Montel committed
201
    return d->transports;
202 203
}

204
TransportType::List TransportManager::types() const
205
{
Laurent Montel's avatar
Laurent Montel committed
206
    return d->types;
207 208
}

Allen Winter's avatar
Allen Winter committed
209
Transport *TransportManager::createTransport() const
210
{
Laurent Montel's avatar
Laurent Montel committed
211 212 213 214
    int id = d->createId();
    Transport *t = new Transport(QString::number(id));
    t->setId(id);
    return t;
215 216
}

Laurent Montel's avatar
Laurent Montel committed
217
void TransportManager::addTransport(Transport *transport)
218
{
Laurent Montel's avatar
Laurent Montel committed
219
    if (d->transports.contains(transport)) {
Laurent Montel's avatar
Laurent Montel committed
220
        qCDebug(MAILTRANSPORT_LOG) << "Already have this transport.";
Laurent Montel's avatar
Laurent Montel committed
221 222
        return;
    }
Thomas McGuire's avatar
Thomas McGuire committed
223

Laurent Montel's avatar
Laurent Montel committed
224
    qCDebug(MAILTRANSPORT_LOG) << "Added transport" << transport;
Laurent Montel's avatar
Laurent Montel committed
225 226 227
    d->transports.append(transport);
    d->validateDefault();
    emitChangesCommitted();
228 229
}

Laurent Montel's avatar
Laurent Montel committed
230
void TransportManager::schedule(TransportJob *job)
231
{
Laurent Montel's avatar
Laurent Montel committed
232
    connect(job, SIGNAL(result(KJob *)), SLOT(jobResult(KJob *)));
233

Laurent Montel's avatar
Laurent Montel committed
234 235
    // check if the job is waiting for the wallet
    if (!job->transport()->isComplete()) {
Laurent Montel's avatar
Laurent Montel committed
236
        qCDebug(MAILTRANSPORT_LOG) << "job waits for wallet:" << job;
Laurent Montel's avatar
Laurent Montel committed
237 238 239 240
        d->walletQueue << job;
        loadPasswordsAsync();
        return;
    }
241

Laurent Montel's avatar
Laurent Montel committed
242
    job->start();
243 244
}

Volker Krause's avatar
Volker Krause committed
245 246
void TransportManager::createDefaultTransport()
{
Laurent Montel's avatar
Laurent Montel committed
247 248 249 250 251 252 253 254
    KEMailSettings kes;
    Transport *t = createTransport();
    t->setName(i18n("Default Transport"));
    t->setHost(kes.getSetting(KEMailSettings::OutServer));
    if (t->isValid()) {
        t->save();
        addTransport(t);
    } else {
Laurent Montel's avatar
Laurent Montel committed
255
        qCWarning(MAILTRANSPORT_LOG) << "KEMailSettings does not contain a valid transport.";
Laurent Montel's avatar
Laurent Montel committed
256
    }
Volker Krause's avatar
Volker Krause committed
257 258
}

Laurent Montel's avatar
Laurent Montel committed
259
bool TransportManager::showTransportCreationDialog(QWidget *parent, ShowCondition showCondition)
260
{
Laurent Montel's avatar
Laurent Montel committed
261 262 263 264
    if (showCondition == IfNoTransportExists) {
        if (!isEmpty()) {
            return true;
        }
265

Laurent Montel's avatar
Laurent Montel committed
266
        const int response = KMessageBox::messageBox(parent,
Laurent Montel's avatar
Laurent Montel committed
267 268 269 270
                                                     KMessageBox::WarningContinueCancel,
                                                     i18n("You must create an outgoing account before sending."),
                                                     i18n("Create Account Now?"),
                                                     KGuiItem(i18n("Create Account Now")));
Laurent Montel's avatar
Laurent Montel committed
271 272 273
        if (response != KMessageBox::Continue) {
            return false;
        }
274 275
    }

Laurent Montel's avatar
Laurent Montel committed
276 277 278 279
    QPointer<AddTransportDialog> dialog = new AddTransportDialog(parent);
    const bool accepted = (dialog->exec() == QDialog::Accepted);
    delete dialog;
    return accepted;
280 281
}

Laurent Montel's avatar
Laurent Montel committed
282
bool TransportManager::configureTransport(Transport *transport, QWidget *parent)
283
{
Laurent Montel's avatar
Laurent Montel committed
284 285
    QPointer<TransportConfigDialog> transportConfigDialog
        = new TransportConfigDialog(transport, parent);
Laurent Montel's avatar
Laurent Montel committed
286 287 288 289
    transportConfigDialog->setWindowTitle(i18n("Configure account"));
    bool okClicked = (transportConfigDialog->exec() == QDialog::Accepted);
    delete transportConfigDialog;
    return okClicked;
290 291
}

Laurent Montel's avatar
Laurent Montel committed
292
TransportJob *TransportManager::createTransportJob(int transportId)
293
{
Laurent Montel's avatar
Laurent Montel committed
294 295
    Transport *t = transportById(transportId, false);
    if (!t) {
Laurent Montel's avatar
Laurent Montel committed
296
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
297 298 299 300 301 302 303 304
    }
    t = t->clone(); // Jobs delete their transports.
    t->updatePasswordState();
    switch (t->type()) {
    case Transport::EnumType::SMTP:
        return new SmtpJob(t, this);
    }
    Q_ASSERT(false);
Laurent Montel's avatar
Laurent Montel committed
305
    return nullptr;
306 307
}

Laurent Montel's avatar
Laurent Montel committed
308
TransportJob *TransportManager::createTransportJob(const QString &transport)
309
{
Laurent Montel's avatar
Laurent Montel committed
310
    bool ok = false;
Laurent Montel's avatar
Laurent Montel committed
311
    Transport *t = nullptr;
312

Laurent Montel's avatar
Laurent Montel committed
313 314 315 316
    int transportId = transport.toInt(&ok);
    if (ok) {
        t = transportById(transportId);
    }
317

Laurent Montel's avatar
Laurent Montel committed
318 319 320
    if (!t) {
        t = transportByName(transport, false);
    }
321

Laurent Montel's avatar
Laurent Montel committed
322 323 324
    if (t) {
        return createTransportJob(t->id());
    }
325

Laurent Montel's avatar
Laurent Montel committed
326
    return nullptr;
327 328 329
}

bool TransportManager::isEmpty() const
330
{
Laurent Montel's avatar
Laurent Montel committed
331
    return d->transports.isEmpty();
332 333
}

334
QVector<int> TransportManager::transportIds() const
335
{
336
    QVector<int> rv;
337
    rv.reserve(d->transports.count());
Laurent Montel's avatar
Laurent Montel committed
338 339 340 341
    foreach (Transport *t, d->transports) {
        rv << t->id();
    }
    return rv;
342 343 344 345
}

QStringList TransportManager::transportNames() const
{
Laurent Montel's avatar
Laurent Montel committed
346
    QStringList rv;
347
    rv.reserve(d->transports.count());
Laurent Montel's avatar
Laurent Montel committed
348 349 350 351
    foreach (Transport *t, d->transports) {
        rv << t->name();
    }
    return rv;
352 353 354 355
}

QString TransportManager::defaultTransportName() const
{
Laurent Montel's avatar
Laurent Montel committed
356 357 358 359 360
    Transport *t = transportById(d->defaultTransportId, false);
    if (t) {
        return t->name();
    }
    return QString();
361 362 363 364
}

int TransportManager::defaultTransportId() const
{
Laurent Montel's avatar
Laurent Montel committed
365
    return d->defaultTransportId;
366 367
}

Laurent Montel's avatar
Laurent Montel committed
368
void TransportManager::setDefaultTransport(int id)
369
{
Laurent Montel's avatar
Laurent Montel committed
370 371 372 373 374
    if (id == d->defaultTransportId || !transportById(id, false)) {
        return;
    }
    d->defaultTransportId = id;
    d->writeConfig();
375 376
}

Laurent Montel's avatar
Laurent Montel committed
377
void TransportManager::removeTransport(int id)
378
{
Laurent Montel's avatar
Laurent Montel committed
379 380 381 382 383 384 385 386 387
    Transport *t = transportById(id, false);
    if (!t) {
        return;
    }
    emit transportRemoved(t->id(), t->name());

    d->transports.removeAll(t);
    d->validateDefault();
    QString group = t->currentGroup();
388 389
    if (t->storePassword()) {
        Wallet *currentWallet = wallet();
Laurent Montel's avatar
Laurent Montel committed
390 391
        if (currentWallet) {
            currentWallet->removeEntry(QString::number(t->id()));
392 393
        }
    }
Laurent Montel's avatar
Laurent Montel committed
394 395 396
    delete t;
    d->config->deleteGroup(group);
    d->writeConfig();
397 398
}

Volker Krause's avatar
Volker Krause committed
399
void TransportManagerPrivate::readConfig()
400
{
Laurent Montel's avatar
Laurent Montel committed
401 402 403
    QList<Transport *> oldTransports = transports;
    transports.clear();

404
    QRegExp re(QStringLiteral("^Transport (.+)$"));
Laurent Montel's avatar
Laurent Montel committed
405 406
    const QStringList groups = config->groupList().filter(re);
    for (const QString &s : groups) {
Laurent Montel's avatar
Laurent Montel committed
407 408 409
        if (re.indexIn(s) == -1) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
410
        Transport *t = nullptr;
Laurent Montel's avatar
Laurent Montel committed
411 412 413 414

        // see if we happen to have that one already
        foreach (Transport *old, oldTransports) {
            if (old->currentGroup() == QLatin1String("Transport ") + re.cap(1)) {
Laurent Montel's avatar
Laurent Montel committed
415
                qCDebug(MAILTRANSPORT_LOG) << "reloading existing transport:" << s;
Laurent Montel's avatar
Laurent Montel committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
                t = old;
                t->d->passwordNeedsUpdateFromWallet = true;
                t->load();
                oldTransports.removeAll(old);
                break;
            }
        }

        if (!t) {
            t = new Transport(re.cap(1));
        }
        if (t->id() <= 0) {
            t->setId(createId());
            t->save();
        }
        transports.append(t);
    }
433

Laurent Montel's avatar
Laurent Montel committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    qDeleteAll(oldTransports);
    oldTransports.clear();

    // read default transport
    KConfigGroup group(config, "General");
    defaultTransportId = group.readEntry("default-transport", 0);
    if (defaultTransportId == 0) {
        // migrated default transport contains the name instead
        QString name = group.readEntry("default-transport", QString());
        if (!name.isEmpty()) {
            Transport *t = q->transportByName(name, false);
            if (t) {
                defaultTransportId = t->id();
                writeConfig();
            }
        }
    }
    validateDefault();
    migrateToWallet();
    q->loadPasswordsAsync();
454 455
}

Volker Krause's avatar
Volker Krause committed
456
void TransportManagerPrivate::writeConfig()
457
{
Laurent Montel's avatar
Laurent Montel committed
458 459 460 461
    KConfigGroup group(config, "General");
    group.writeEntry("default-transport", defaultTransportId);
    config->sync();
    q->emitChangesCommitted();
462 463
}

Volker Krause's avatar
Volker Krause committed
464
void TransportManagerPrivate::fillTypes()
465
{
Laurent Montel's avatar
Laurent Montel committed
466 467 468 469
    Q_ASSERT(types.isEmpty());

    // SMTP.
    {
470
        TransportType type;
Laurent Montel's avatar
Laurent Montel committed
471 472 473
        type.d->mType = Transport::EnumType::SMTP;
        type.d->mName = i18nc("@option SMTP transport", "SMTP");
        type.d->mDescription = i18n("An SMTP server on the Internet");
474
        types << type;
475 476 477
    }
}

478 479
void TransportManager::emitChangesCommitted()
{
Laurent Montel's avatar
Laurent Montel committed
480 481 482 483
    d->myOwnChange = true; // prevent us from reading our changes again
    d->appliedChange = false; // but we have to read them at least once
    emit transportsChanged();
    emit changesCommitted();
484 485
}

Volker Krause's avatar
Volker Krause committed
486
void TransportManagerPrivate::slotTransportsChanged()
487
{
Laurent Montel's avatar
Laurent Montel committed
488 489 490 491 492
    if (myOwnChange && appliedChange) {
        myOwnChange = false;
        appliedChange = false;
        return;
    }
493

Laurent Montel's avatar
Laurent Montel committed
494
    qCDebug(MAILTRANSPORT_LOG);
Laurent Montel's avatar
Laurent Montel committed
495 496 497 498 499
    config->reparseConfiguration();
    // FIXME: this deletes existing transport objects!
    readConfig();
    appliedChange = true; // to prevent recursion
    emit q->transportsChanged();
500 501
}

Volker Krause's avatar
Volker Krause committed
502
int TransportManagerPrivate::createId() const
503
{
504
    QVector<int> usedIds;
505
    usedIds.reserve(1 + transports.count());
Laurent Montel's avatar
Laurent Montel committed
506
    for (Transport *t : qAsConst(transports)) {
Laurent Montel's avatar
Laurent Montel committed
507 508 509 510 511 512 513 514
        usedIds << t->id();
    }
    usedIds << 0; // 0 is default for unknown
    int newId;
    do {
        newId = KRandom::random();
    } while (usedIds.contains(newId));
    return newId;
515 516
}

Laurent Montel's avatar
Laurent Montel committed
517
KWallet::Wallet *TransportManager::wallet()
518
{
Laurent Montel's avatar
Laurent Montel committed
519 520 521
    if (d->wallet && d->wallet->isOpen()) {
        return d->wallet;
    }
522

Laurent Montel's avatar
Laurent Montel committed
523
    if (!Wallet::isEnabled() || d->walletOpenFailed) {
Laurent Montel's avatar
Laurent Montel committed
524
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
525
    }
526

Laurent Montel's avatar
Laurent Montel committed
527 528 529 530 531 532
    WId window = 0;
    if (qApp->activeWindow()) {
        window = qApp->activeWindow()->winId();
    } else if (!QApplication::topLevelWidgets().isEmpty()) {
        window = qApp->topLevelWidgets().first()->winId();
    }
533

Laurent Montel's avatar
Laurent Montel committed
534 535
    delete d->wallet;
    d->wallet = Wallet::openWallet(Wallet::NetworkWallet(), window);
536

Laurent Montel's avatar
Laurent Montel committed
537 538
    if (!d->wallet) {
        d->walletOpenFailed = true;
Laurent Montel's avatar
Laurent Montel committed
539
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
540
    }
541

Laurent Montel's avatar
Laurent Montel committed
542 543
    d->prepareWallet();
    return d->wallet;
544 545
}

Volker Krause's avatar
Volker Krause committed
546
void TransportManagerPrivate::prepareWallet()
547
{
Laurent Montel's avatar
Laurent Montel committed
548 549 550 551 552 553 554
    if (!wallet) {
        return;
    }
    if (!wallet->hasFolder(WALLET_FOLDER)) {
        wallet->createFolder(WALLET_FOLDER);
    }
    wallet->setFolder(WALLET_FOLDER);
555 556 557 558
}

void TransportManager::loadPasswords()
{
Laurent Montel's avatar
Laurent Montel committed
559 560 561
    foreach (Transport *t, d->transports) {
        t->readPassword();
    }
562

Laurent Montel's avatar
Laurent Montel committed
563 564 565
    // flush the wallet queue
    const QList<TransportJob *> copy = d->walletQueue;
    d->walletQueue.clear();
Laurent Montel's avatar
Laurent Montel committed
566
    for (TransportJob *job : copy) {
Laurent Montel's avatar
Laurent Montel committed
567 568
        job->start();
    }
569

Laurent Montel's avatar
Laurent Montel committed
570
    emit passwordsChanged();
571 572 573 574
}

void TransportManager::loadPasswordsAsync()
{
Laurent Montel's avatar
Laurent Montel committed
575
    qCDebug(MAILTRANSPORT_LOG);
576

Laurent Montel's avatar
Laurent Montel committed
577 578 579 580 581 582 583
    // check if there is anything to do at all
    bool found = false;
    foreach (Transport *t, d->transports) {
        if (!t->isComplete()) {
            found = true;
            break;
        }
584
    }
Laurent Montel's avatar
Laurent Montel committed
585 586
    if (!found) {
        return;
Allen Winter's avatar
Allen Winter committed
587 588
    }

Laurent Montel's avatar
Laurent Montel committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    // async wallet opening
    if (!d->wallet && !d->walletOpenFailed) {
        WId window = 0;
        if (qApp->activeWindow()) {
            window = qApp->activeWindow()->winId();
        } else if (!QApplication::topLevelWidgets().isEmpty()) {
            window = qApp->topLevelWidgets().first()->winId();
        }

        d->wallet = Wallet::openWallet(Wallet::NetworkWallet(), window,
                                       Wallet::Asynchronous);
        if (d->wallet) {
            connect(d->wallet, SIGNAL(walletOpened(bool)), SLOT(slotWalletOpened(bool)));
            d->walletAsyncOpen = true;
        } else {
            d->walletOpenFailed = true;
            loadPasswords();
        }
        return;
    }
    if (d->wallet && !d->walletAsyncOpen) {
        loadPasswords();
611 612 613
    }
}

Laurent Montel's avatar
Laurent Montel committed
614
void TransportManagerPrivate::slotWalletOpened(bool success)
615
{
Laurent Montel's avatar
Laurent Montel committed
616
    qCDebug(MAILTRANSPORT_LOG);
Laurent Montel's avatar
Laurent Montel committed
617 618 619 620
    walletAsyncOpen = false;
    if (!success) {
        walletOpenFailed = true;
        delete wallet;
Laurent Montel's avatar
Laurent Montel committed
621
        wallet = nullptr;
Laurent Montel's avatar
Laurent Montel committed
622 623 624 625
    } else {
        prepareWallet();
    }
    q->loadPasswords();
626 627
}

Volker Krause's avatar
Volker Krause committed
628
void TransportManagerPrivate::validateDefault()
629
{
Laurent Montel's avatar
Laurent Montel committed
630 631 632 633 634 635 636
    if (!q->transportById(defaultTransportId, false)) {
        if (q->isEmpty()) {
            defaultTransportId = -1;
        } else {
            defaultTransportId = transports.first()->id();
            writeConfig();
        }
637 638 639
    }
}

Volker Krause's avatar
Volker Krause committed
640
void TransportManagerPrivate::migrateToWallet()
641
{
Laurent Montel's avatar
Laurent Montel committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655
    // check if we tried this already
    static bool firstRun = true;
    if (!firstRun) {
        return;
    }
    firstRun = false;

    // check if we are the main instance
    if (!isMainInstance) {
        return;
    }

    // check if migration is needed
    QStringList names;
Laurent Montel's avatar
Laurent Montel committed
656
    for (Transport *t : qAsConst(transports)) {
Laurent Montel's avatar
Laurent Montel committed
657 658 659 660 661 662 663 664 665 666
        if (t->needsWalletMigration()) {
            names << t->name();
        }
    }
    if (names.isEmpty()) {
        return;
    }

    // ask user if he wants to migrate
    int result = KMessageBox::questionYesNoList(
Laurent Montel's avatar
Laurent Montel committed
667 668 669 670 671 672 673 674 675 676
        nullptr,
        i18n("The following mail transports store their passwords in an "
             "unencrypted configuration file.\nFor security reasons, "
             "please consider migrating these passwords to KWallet, the "
             "KDE Wallet management tool,\nwhich stores sensitive data "
             "for you in a strongly encrypted file.\n"
             "Do you want to migrate your passwords to KWallet?"),
        names, i18n("Question"),
        KGuiItem(i18n("Migrate")), KGuiItem(i18n("Keep")),
        QStringLiteral("WalletMigrate"));
Laurent Montel's avatar
Laurent Montel committed
677 678 679 680 681 682 683 684 685 686
    if (result != KMessageBox::Yes) {
        return;
    }

    // perform migration
    foreach (Transport *t, transports) {
        if (t->needsWalletMigration()) {
            t->migrateToWallet();
        }
    }
687 688
}

Tobias Koenig's avatar
Tobias Koenig committed
689
void TransportManagerPrivate::dbusServiceUnregistered()
690
{
Laurent Montel's avatar
Laurent Montel committed
691
    QDBusConnection::sessionBus().registerService(DBUS_SERVICE_NAME);
692 693
}

Laurent Montel's avatar
Laurent Montel committed
694
void TransportManagerPrivate::jobResult(KJob *job)
695
{
Laurent Montel's avatar
Laurent Montel committed
696
    walletQueue.removeAll(static_cast<TransportJob *>(job));
697 698
}

699
#include "moc_transportmanager.cpp"