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"
32

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

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

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

52
using namespace MailTransport;
53 54
using namespace KWallet;

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

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

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

    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
99
    void slotWalletOpened(bool success);
Tobias Koenig's avatar
Tobias Koenig committed
100
    void dbusServiceUnregistered();
Laurent Montel's avatar
Laurent Montel committed
101
    void jobResult(KJob *job);
102
};
Volker Krause's avatar
Volker Krause committed
103 104
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Volker Krause's avatar
Volker Krause committed
244 245
void TransportManager::createDefaultTransport()
{
Laurent Montel's avatar
Laurent Montel committed
246 247 248 249 250 251 252 253
    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
254
        qCWarning(MAILTRANSPORT_LOG) << "KEMailSettings does not contain a valid transport.";
Laurent Montel's avatar
Laurent Montel committed
255
    }
Volker Krause's avatar
Volker Krause committed
256 257
}

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

Laurent Montel's avatar
Laurent Montel committed
265
        const int response = KMessageBox::messageBox(parent,
Laurent Montel's avatar
Laurent Montel committed
266 267 268 269
                                                     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
270 271 272
        if (response != KMessageBox::Continue) {
            return false;
        }
273 274
    }

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

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

Laurent Montel's avatar
Laurent Montel committed
291
TransportJob *TransportManager::createTransportJob(int transportId)
292
{
Laurent Montel's avatar
Laurent Montel committed
293 294
    Transport *t = transportById(transportId, false);
    if (!t) {
Laurent Montel's avatar
Laurent Montel committed
295
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
296 297 298 299 300 301 302 303
    }
    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
304
    return nullptr;
305 306
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        // 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
414
                qCDebug(MAILTRANSPORT_LOG) << "reloading existing transport:" << s;
Laurent Montel's avatar
Laurent Montel committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                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);
    }
432

Laurent Montel's avatar
Laurent Montel committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    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();
453 454
}

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

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

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

477 478
void TransportManager::emitChangesCommitted()
{
Laurent Montel's avatar
Laurent Montel committed
479 480 481 482
    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();
483 484
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    // 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();
610 611 612
    }
}

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

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

Volker Krause's avatar
Volker Krause committed
639
void TransportManagerPrivate::migrateToWallet()
640
{
Laurent Montel's avatar
Laurent Montel committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654
    // 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
655
    for (Transport *t : qAsConst(transports)) {
Laurent Montel's avatar
Laurent Montel committed
656 657 658 659 660 661 662 663 664 665
        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
666 667 668 669 670 671 672 673 674 675
        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
676 677 678 679 680 681 682 683 684 685
    if (result != KMessageBox::Yes) {
        return;
    }

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

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

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

698
#include "moc_transportmanager.cpp"