transportmanager.cpp 18.5 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 22
#include "mailtransport_defs.h"
#include "transport.h"
23
#include "transport_p.h"
24
#include "transportjob.h"
25 26
#include "transporttype.h"
#include "transporttype_p.h"
Laurent Montel's avatar
Laurent Montel committed
27 28 29
#include "plugins/transportpluginmanager.h"
#include "plugins/transportabstractplugin.h"
#include "widgets/addtransportdialogng.h"
30

31
#include <QApplication>
Laurent Montel's avatar
Laurent Montel committed
32 33 34
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
35
#include <QPointer>
36 37 38
#include <QRegExp>
#include <QStringList>

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

Laurent Montel's avatar
Laurent Montel committed
48
#include <KWallet/kwallet.h>
49

50
using namespace MailTransport;
51 52
using namespace KWallet;

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
111
StaticTransportManager *sSelf = nullptr;
112

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
239
    job->start();
240 241
}

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

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

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

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

279 280
bool TransportManager::configureTransport(const QString &identifier, Transport *transport, QWidget *parent)
{
Laurent Montel's avatar
Laurent Montel committed
281 282 283 284
    TransportAbstractPlugin *plugin = TransportPluginManager::self()->plugin(identifier);
    if (plugin) {
        return plugin->configureTransport(identifier, transport, parent);
    }
285 286 287
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
288
TransportJob *TransportManager::createTransportJob(int transportId)
289
{
Laurent Montel's avatar
Laurent Montel committed
290 291
    Transport *t = transportById(transportId, false);
    if (!t) {
Laurent Montel's avatar
Laurent Montel committed
292
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
293 294 295
    }
    t = t->clone(); // Jobs delete their transports.
    t->updatePasswordState();
Laurent Montel's avatar
Laurent Montel committed
296 297
    TransportAbstractPlugin *plugin = TransportPluginManager::self()->plugin(t->identifier());
    if (plugin) {
298
        return plugin->createTransportJob(t, t->identifier());
Laurent Montel's avatar
Laurent Montel committed
299 300
    }
    Q_ASSERT(false);
Laurent Montel's avatar
Laurent Montel committed
301
    return nullptr;
302 303
}

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

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

Laurent Montel's avatar
Laurent Montel committed
314 315 316
    if (!t) {
        t = transportByName(transport, false);
    }
317

Laurent Montel's avatar
Laurent Montel committed
318 319 320
    if (t) {
        return createTransportJob(t->id());
    }
321

Laurent Montel's avatar
Laurent Montel committed
322
    return nullptr;
323 324 325
}

bool TransportManager::isEmpty() const
326
{
Laurent Montel's avatar
Laurent Montel committed
327
    return d->transports.isEmpty();
328 329
}

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

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

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

int TransportManager::defaultTransportId() const
{
Laurent Montel's avatar
Laurent Montel committed
361
    return d->defaultTransportId;
362 363
}

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

Laurent Montel's avatar
Laurent Montel committed
373
void TransportManager::removeTransport(int id)
374
{
Laurent Montel's avatar
Laurent Montel committed
375 376 377 378
    Transport *t = transportById(id, false);
    if (!t) {
        return;
    }
379 380 381 382
    auto plugin = MailTransport::TransportPluginManager::self()->plugin(t->identifier());
    if (plugin) {
        plugin->cleanUp(t->identifier());
    }
Laurent Montel's avatar
Laurent Montel committed
383 384 385 386 387
    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
    Q_ASSERT(types.isEmpty());

Laurent Montel's avatar
Laurent Montel committed
468
    for (MailTransport::TransportAbstractPlugin * plugin : MailTransport::TransportPluginManager::self()->pluginsList()) {
Laurent Montel's avatar
Laurent Montel committed
469 470 471 472
        qDebug() << " plugin "<< plugin;
        if (plugin->names().isEmpty()) {
            qCDebug(MAILTRANSPORT_LOG) << "Plugin " << plugin << " doesn't provide plugin";
        }
Laurent Montel's avatar
Laurent Montel committed
473 474 475 476
        for (const MailTransport::TransportAbstractPluginInfo &info : plugin->names()) {
            TransportType type;
            type.d->mName = info.name;
            type.d->mDescription = info.description;
Laurent Montel's avatar
Laurent Montel committed
477
            type.d->mIdentifier = info.identifier;
478
            type.d->mIsAkonadiResource = info.isAkonadi;
Laurent Montel's avatar
Laurent Montel committed
479 480
            types << type;
        }
481 482 483
    }
}

484 485
void TransportManager::emitChangesCommitted()
{
Laurent Montel's avatar
Laurent Montel committed
486 487 488 489
    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();
490 491
}

Volker Krause's avatar
Volker Krause committed
492
void TransportManagerPrivate::slotTransportsChanged()
493
{
Laurent Montel's avatar
Laurent Montel committed
494 495 496 497 498
    if (myOwnChange && appliedChange) {
        myOwnChange = false;
        appliedChange = false;
        return;
    }
499

Laurent Montel's avatar
Laurent Montel committed
500
    qCDebug(MAILTRANSPORT_LOG);
Laurent Montel's avatar
Laurent Montel committed
501 502 503 504 505
    config->reparseConfiguration();
    // FIXME: this deletes existing transport objects!
    readConfig();
    appliedChange = true; // to prevent recursion
    emit q->transportsChanged();
506 507
}

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

Laurent Montel's avatar
Laurent Montel committed
523
KWallet::Wallet *TransportManager::wallet()
524
{
Laurent Montel's avatar
Laurent Montel committed
525 526 527
    if (d->wallet && d->wallet->isOpen()) {
        return d->wallet;
    }
528

Laurent Montel's avatar
Laurent Montel committed
529
    if (!Wallet::isEnabled() || d->walletOpenFailed) {
Laurent Montel's avatar
Laurent Montel committed
530
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
531
    }
532

Laurent Montel's avatar
Laurent Montel committed
533 534 535 536 537 538
    WId window = 0;
    if (qApp->activeWindow()) {
        window = qApp->activeWindow()->winId();
    } else if (!QApplication::topLevelWidgets().isEmpty()) {
        window = qApp->topLevelWidgets().first()->winId();
    }
539

Laurent Montel's avatar
Laurent Montel committed
540 541
    delete d->wallet;
    d->wallet = Wallet::openWallet(Wallet::NetworkWallet(), window);
542

Laurent Montel's avatar
Laurent Montel committed
543 544
    if (!d->wallet) {
        d->walletOpenFailed = true;
Laurent Montel's avatar
Laurent Montel committed
545
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
546
    }
547

Laurent Montel's avatar
Laurent Montel committed
548 549
    d->prepareWallet();
    return d->wallet;
550 551
}

Volker Krause's avatar
Volker Krause committed
552
void TransportManagerPrivate::prepareWallet()
553
{
Laurent Montel's avatar
Laurent Montel committed
554 555 556 557 558 559 560
    if (!wallet) {
        return;
    }
    if (!wallet->hasFolder(WALLET_FOLDER)) {
        wallet->createFolder(WALLET_FOLDER);
    }
    wallet->setFolder(WALLET_FOLDER);
561 562 563 564
}

void TransportManager::loadPasswords()
{
Laurent Montel's avatar
Laurent Montel committed
565 566 567
    foreach (Transport *t, d->transports) {
        t->readPassword();
    }
568

Laurent Montel's avatar
Laurent Montel committed
569 570 571
    // flush the wallet queue
    const QList<TransportJob *> copy = d->walletQueue;
    d->walletQueue.clear();
Laurent Montel's avatar
Laurent Montel committed
572
    for (TransportJob *job : copy) {
Laurent Montel's avatar
Laurent Montel committed
573 574
        job->start();
    }
575

Laurent Montel's avatar
Laurent Montel committed
576
    emit passwordsChanged();
577 578 579 580
}

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

Laurent Montel's avatar
Laurent Montel committed
583 584 585 586 587 588 589
    // check if there is anything to do at all
    bool found = false;
    foreach (Transport *t, d->transports) {
        if (!t->isComplete()) {
            found = true;
            break;
        }
590
    }
Laurent Montel's avatar
Laurent Montel committed
591 592
    if (!found) {
        return;
Allen Winter's avatar
Allen Winter committed
593 594
    }

Laurent Montel's avatar
Laurent Montel committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    // 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();
617 618 619
    }
}

Laurent Montel's avatar
Laurent Montel committed
620
void TransportManagerPrivate::slotWalletOpened(bool success)
621
{
Laurent Montel's avatar
Laurent Montel committed
622
    qCDebug(MAILTRANSPORT_LOG);
Laurent Montel's avatar
Laurent Montel committed
623 624 625 626
    walletAsyncOpen = false;
    if (!success) {
        walletOpenFailed = true;
        delete wallet;
Laurent Montel's avatar
Laurent Montel committed
627
        wallet = nullptr;
Laurent Montel's avatar
Laurent Montel committed
628 629 630 631
    } else {
        prepareWallet();
    }
    q->loadPasswords();
632 633
}

Volker Krause's avatar
Volker Krause committed
634
void TransportManagerPrivate::validateDefault()
635
{
Laurent Montel's avatar
Laurent Montel committed
636 637 638 639 640 641 642
    if (!q->transportById(defaultTransportId, false)) {
        if (q->isEmpty()) {
            defaultTransportId = -1;
        } else {
            defaultTransportId = transports.first()->id();
            writeConfig();
        }
643 644 645
    }
}

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

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

Tobias Koenig's avatar
Tobias Koenig committed
695
void TransportManagerPrivate::dbusServiceUnregistered()
696
{
Laurent Montel's avatar
Laurent Montel committed
697
    QDBusConnection::sessionBus().registerService(DBUS_SERVICE_NAME);
698 699
}

Laurent Montel's avatar
Laurent Montel committed
700
void TransportManagerPrivate::jobResult(KJob *job)
701
{
Laurent Montel's avatar
Laurent Montel committed
702
    walletQueue.removeAll(static_cast<TransportJob *>(job));
703 704
}

705
#include "moc_transportmanager.cpp"
Laurent Montel's avatar
Laurent Montel committed
706 707

#include <MailTransport/TransportAbstractPlugin>