transportmanager.cpp 18.1 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"
Laurent Montel's avatar
Laurent Montel committed
22
#include "smtp/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 29 30
#include "plugins/transportpluginmanager.h"
#include "plugins/transportabstractplugin.h"
#include "widgets/addtransportdialogng.h"
31
#include "smtp/smtpconfigwidget.h"
32

33
#include <QApplication>
Laurent Montel's avatar
Laurent Montel committed
34 35 36
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <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
    QPointer<AddTransportDialogNG> dialog = new AddTransportDialogNG(parent);
Laurent Montel's avatar
Laurent Montel committed
276 277 278
    const bool accepted = (dialog->exec() == QDialog::Accepted);
    delete dialog;
    return accepted;
279 280
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
466 467 468 469 470 471 472
    for (MailTransport::TransportAbstractPlugin * plugin : MailTransport::TransportPluginManager::self()->pluginsList()) {
        for (const MailTransport::TransportAbstractPluginInfo &info : plugin->names()) {
            TransportType type;
            type.d->mName = info.name;
            type.d->mDescription = info.description;
            types << type;
        }
473 474 475
    }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

697
#include "moc_transportmanager.cpp"
Laurent Montel's avatar
Laurent Montel committed
698 699

#include <MailTransport/TransportAbstractPlugin>