transportmanager.cpp 19.1 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
Allen Winter's avatar
Allen Winter committed
3

4
  SPDX-License-Identifier: LGPL-2.0-or-later
5 6
*/

7
#include "transportmanager.h"
8 9
#include "mailtransport_defs.h"
#include "transport.h"
10
#include "transport_p.h"
11
#include "transportjob.h"
12 13
#include "transporttype.h"
#include "transporttype_p.h"
Laurent Montel's avatar
Laurent Montel committed
14 15 16
#include "plugins/transportpluginmanager.h"
#include "plugins/transportabstractplugin.h"
#include "widgets/addtransportdialogng.h"
Laurent Montel's avatar
Laurent Montel committed
17
#include <MailTransport/TransportAbstractPlugin>
18

19
#include <QApplication>
Laurent Montel's avatar
Laurent Montel committed
20 21 22
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
23
#include <QPointer>
24
#include <QRandomGenerator>
Laurent Montel's avatar
Laurent Montel committed
25
#include <QRegularExpression>
26 27
#include <QStringList>

28 29
#include <KConfig>
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
30
#include "mailtransport_debug.h"
31
#include <KEMailSettings>
32
#include <KLocalizedString>
33
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
34
#include <Kdelibs4ConfigMigrator>
35 36
#include <qt5keychain/keychain.h>
using namespace QKeychain;
Laurent Montel's avatar
Laurent Montel committed
37
#include <KWallet>
38

39
using namespace MailTransport;
40 41
using namespace KWallet;

Laurent Montel's avatar
Laurent Montel committed
42
namespace MailTransport {
Tom Albers's avatar
Tom Albers committed
43 44 45 46
/**
 * Private class that helps to provide binary compatibility between releases.
 * @internal
 */
Volker Krause's avatar
Volker Krause committed
47
class TransportManagerPrivate
Allen Winter's avatar
Allen Winter committed
48
{
Laurent Montel's avatar
Laurent Montel committed
49 50
public:
    TransportManagerPrivate(TransportManager *parent)
Laurent Montel's avatar
Minor  
Laurent Montel committed
51
        : q(parent)
Thomas McGuire's avatar
Thomas McGuire committed
52 53 54
    {
    }

Laurent Montel's avatar
Laurent Montel committed
55 56 57 58
    ~TransportManagerPrivate()
    {
        delete config;
        qDeleteAll(transports);
59
    }
60

Laurent Montel's avatar
Laurent Montel committed
61
    KConfig *config = nullptr;
Allen Winter's avatar
Allen Winter committed
62
    QList<Transport *> transports;
63
    TransportType::List types;
Tom Albers's avatar
Tom Albers committed
64
    bool myOwnChange;
65
    bool appliedChange;
Laurent Montel's avatar
Laurent Montel committed
66
    KWallet::Wallet *wallet = nullptr;
Tom Albers's avatar
Tom Albers committed
67 68 69 70
    bool walletOpenFailed;
    bool walletAsyncOpen;
    int defaultTransportId;
    bool isMainInstance;
Allen Winter's avatar
Allen Winter committed
71
    QList<TransportJob *> walletQueue;
Laurent Montel's avatar
Laurent Montel committed
72
    TransportManager *const q;
73 74 75 76 77 78 79 80

    void readConfig();
    void writeConfig();
    void fillTypes();
    int createId() const;
    void prepareWallet();
    void validateDefault();
    void migrateToWallet();
Laurent Montel's avatar
Laurent Montel committed
81
    void updatePluginList();
82 83 84

    // Slots
    void slotTransportsChanged();
Laurent Montel's avatar
Laurent Montel committed
85
    void slotWalletOpened(bool success);
Tobias Koenig's avatar
Tobias Koenig committed
86
    void dbusServiceUnregistered();
Laurent Montel's avatar
Laurent Montel committed
87
    void jobResult(KJob *job);
88
};
Volker Krause's avatar
Volker Krause committed
89 90
}

91 92
class StaticTransportManager : public TransportManager
{
Laurent Montel's avatar
Laurent Montel committed
93
public:
Laurent Montel's avatar
Laurent Montel committed
94 95 96
    StaticTransportManager() : TransportManager()
    {
    }
Tom Albers's avatar
Tom Albers committed
97 98
};

Laurent Montel's avatar
Laurent Montel committed
99
StaticTransportManager *sSelf = nullptr;
100

Laurent Montel's avatar
Laurent Montel committed
101 102 103
static void destroyStaticTransportManager()
{
    delete sSelf;
104
}
105 106

TransportManager::TransportManager()
Laurent Montel's avatar
Laurent Montel committed
107 108
    : QObject()
    , d(new TransportManagerPrivate(this))
109
{
Laurent Montel's avatar
Laurent Montel committed
110 111
    Kdelibs4ConfigMigrator migrate(QStringLiteral("transportmanager"));
    migrate.setConfigFiles(QStringList() << QStringLiteral("mailtransports"));
Laurent Montel's avatar
Laurent Montel committed
112 113
    migrate.migrate();

Laurent Montel's avatar
Laurent Montel committed
114 115 116
    qAddPostRoutine(destroyStaticTransportManager);
    d->myOwnChange = false;
    d->appliedChange = false;
Laurent Montel's avatar
Laurent Montel committed
117
    d->wallet = nullptr;
Laurent Montel's avatar
Laurent Montel committed
118 119 120
    d->walletOpenFailed = false;
    d->walletAsyncOpen = false;
    d->defaultTransportId = -1;
Laurent Montel's avatar
Laurent Montel committed
121
    d->config = new KConfig(QStringLiteral("mailtransports"));
122

Laurent Montel's avatar
Laurent Montel committed
123
    QDBusConnection::sessionBus().registerObject(DBUS_OBJECT_PATH, this,
Laurent Montel's avatar
Laurent Montel committed
124 125
                                                 QDBusConnection::ExportScriptableSlots
                                                 |QDBusConnection::ExportScriptableSignals);
Tobias Koenig's avatar
Tobias Koenig committed
126

Laurent Montel's avatar
Laurent Montel committed
127 128 129
    QDBusServiceWatcher *watcher
        = new QDBusServiceWatcher(DBUS_SERVICE_NAME, QDBusConnection::sessionBus(),
                                  QDBusServiceWatcher::WatchForUnregistration, this);
Laurent Montel's avatar
Laurent Montel committed
130 131 132
    connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, [this]() {
        d->dbusServiceUnregistered();
    });
133

Laurent Montel's avatar
Laurent Montel committed
134 135 136
    QDBusConnection::sessionBus().connect(QString(), QString(),
                                          DBUS_INTERFACE_NAME, DBUS_CHANGE_SIGNAL,
                                          this, SLOT(slotTransportsChanged()));
137

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

Laurent Montel's avatar
Laurent Montel committed
140
    d->fillTypes();
141 142 143 144
}

TransportManager::~TransportManager()
{
Laurent Montel's avatar
Laurent Montel committed
145 146
    qRemovePostRoutine(destroyStaticTransportManager);
    delete d;
147 148
}

Allen Winter's avatar
Allen Winter committed
149
TransportManager *TransportManager::self()
150
{
Laurent Montel's avatar
Laurent Montel committed
151 152 153 154 155
    if (!sSelf) {
        sSelf = new StaticTransportManager;
        sSelf->d->readConfig();
    }
    return sSelf;
156 157
}

Laurent Montel's avatar
Laurent Montel committed
158
Transport *TransportManager::transportById(int id, bool def) const
159
{
Laurent Montel's avatar
Laurent Montel committed
160
    for (Transport *t : qAsConst(d->transports)) {
Laurent Montel's avatar
Laurent Montel committed
161 162 163
        if (t->id() == id) {
            return t;
        }
Allen Winter's avatar
Allen Winter committed
164
    }
165

Laurent Montel's avatar
Laurent Montel committed
166 167 168
    if (def || (id == 0 && d->defaultTransportId != id)) {
        return transportById(d->defaultTransportId, false);
    }
Laurent Montel's avatar
Laurent Montel committed
169
    return nullptr;
170 171
}

Laurent Montel's avatar
Laurent Montel committed
172
Transport *TransportManager::transportByName(const QString &name, bool def) const
173
{
Laurent Montel's avatar
Laurent Montel committed
174
    for (Transport *t : qAsConst(d->transports)) {
Laurent Montel's avatar
Laurent Montel committed
175 176 177
        if (t->name() == name) {
            return t;
        }
Allen Winter's avatar
Allen Winter committed
178
    }
Laurent Montel's avatar
Laurent Montel committed
179 180 181
    if (def) {
        return transportById(0, false);
    }
Laurent Montel's avatar
Laurent Montel committed
182
    return nullptr;
183 184 185 186
}

QList< Transport * > TransportManager::transports() const
{
Laurent Montel's avatar
Laurent Montel committed
187
    return d->transports;
188 189
}

190
TransportType::List TransportManager::types() const
191
{
Laurent Montel's avatar
Laurent Montel committed
192
    return d->types;
193 194
}

Allen Winter's avatar
Allen Winter committed
195
Transport *TransportManager::createTransport() const
196
{
Laurent Montel's avatar
Laurent Montel committed
197 198 199 200
    int id = d->createId();
    Transport *t = new Transport(QString::number(id));
    t->setId(id);
    return t;
201 202
}

Laurent Montel's avatar
Laurent Montel committed
203
void TransportManager::addTransport(Transport *transport)
204
{
Laurent Montel's avatar
Laurent Montel committed
205
    if (d->transports.contains(transport)) {
Laurent Montel's avatar
Laurent Montel committed
206
        qCDebug(MAILTRANSPORT_LOG) << "Already have this transport.";
Laurent Montel's avatar
Laurent Montel committed
207 208
        return;
    }
Thomas McGuire's avatar
Thomas McGuire committed
209

Laurent Montel's avatar
Laurent Montel committed
210
    qCDebug(MAILTRANSPORT_LOG) << "Added transport" << transport;
Laurent Montel's avatar
Laurent Montel committed
211 212 213
    d->transports.append(transport);
    d->validateDefault();
    emitChangesCommitted();
214 215
}

Laurent Montel's avatar
Laurent Montel committed
216
void TransportManager::schedule(TransportJob *job)
217
{
Laurent Montel's avatar
Laurent Montel committed
218 219 220
    connect(job, &TransportJob::result, this, [this](KJob *job) {
        d->jobResult(job);
    });
221

Laurent Montel's avatar
Laurent Montel committed
222 223
    // check if the job is waiting for the wallet
    if (!job->transport()->isComplete()) {
Laurent Montel's avatar
Laurent Montel committed
224
        qCDebug(MAILTRANSPORT_LOG) << "job waits for wallet:" << job;
Laurent Montel's avatar
Laurent Montel committed
225 226 227 228
        d->walletQueue << job;
        loadPasswordsAsync();
        return;
    }
229

Laurent Montel's avatar
Laurent Montel committed
230
    job->start();
231 232
}

Volker Krause's avatar
Volker Krause committed
233 234
void TransportManager::createDefaultTransport()
{
Laurent Montel's avatar
Laurent Montel committed
235 236 237 238 239 240 241 242
    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
243
        qCWarning(MAILTRANSPORT_LOG) << "KEMailSettings does not contain a valid transport.";
Laurent Montel's avatar
Laurent Montel committed
244
    }
Volker Krause's avatar
Volker Krause committed
245 246
}

Laurent Montel's avatar
Laurent Montel committed
247
bool TransportManager::showTransportCreationDialog(QWidget *parent, ShowCondition showCondition)
248
{
Laurent Montel's avatar
Laurent Montel committed
249 250 251 252
    if (showCondition == IfNoTransportExists) {
        if (!isEmpty()) {
            return true;
        }
253

Laurent Montel's avatar
Laurent Montel committed
254
        const int response = KMessageBox::messageBox(parent,
Laurent Montel's avatar
Laurent Montel committed
255 256 257 258
                                                     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
259 260 261
        if (response != KMessageBox::Continue) {
            return false;
        }
262 263
    }

Laurent Montel's avatar
Laurent Montel committed
264
    QPointer<AddTransportDialogNG> dialog = new AddTransportDialogNG(parent);
Laurent Montel's avatar
Laurent Montel committed
265 266 267
    const bool accepted = (dialog->exec() == QDialog::Accepted);
    delete dialog;
    return accepted;
268 269
}

Laurent Montel's avatar
Laurent Montel committed
270 271 272 273
void TransportManager::initializeTransport(const QString &identifier, Transport *transport)
{
    TransportAbstractPlugin *plugin = TransportPluginManager::self()->plugin(identifier);
    if (plugin) {
Laurent Montel's avatar
Laurent Montel committed
274
        plugin->initializeTransport(transport, identifier);
Laurent Montel's avatar
Laurent Montel committed
275 276 277
    }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

372 373
void TransportManager::removePasswordFromWallet(int id)
{
374 375 376
    auto deleteJob = new DeletePasswordJob(WALLET_FOLDER);
    deleteJob->setKey(QString::number(id));
    deleteJob->start();
377 378
}

Laurent Montel's avatar
Laurent Montel committed
379
void TransportManager::removeTransport(int id)
380
{
Laurent Montel's avatar
Laurent Montel committed
381 382 383 384
    Transport *t = transportById(id, false);
    if (!t) {
        return;
    }
385 386
    auto plugin = MailTransport::TransportPluginManager::self()->plugin(t->identifier());
    if (plugin) {
Laurent Montel's avatar
Laurent Montel committed
387
        plugin->cleanUp(t);
388
    }
389
    Q_EMIT transportRemoved(t->id(), t->name());
Laurent Montel's avatar
Laurent Montel committed
390 391 392 393

    d->transports.removeAll(t);
    d->validateDefault();
    QString group = t->currentGroup();
394
    if (t->storePassword()) {
Laurent Montel's avatar
Laurent Montel committed
395
        //Move async
396
        Wallet *currentWallet = wallet();
Laurent Montel's avatar
Laurent Montel committed
397 398
        if (currentWallet) {
            currentWallet->removeEntry(QString::number(t->id()));
399 400
        }
    }
Laurent Montel's avatar
Laurent Montel committed
401 402 403
    delete t;
    d->config->deleteGroup(group);
    d->writeConfig();
404 405
}

Volker Krause's avatar
Volker Krause committed
406
void TransportManagerPrivate::readConfig()
407
{
408 409 410 411 412 413 414 415 416 417 418 419
    QList<Transport *> oldTransports = transports;
    transports.clear();

    static QRegularExpression re(QStringLiteral("^Transport (.+)$"));
    const QStringList groups = config->groupList().filter(re);
    for (const QString &s : groups) {
        const QRegularExpressionMatch match = re.match(s);
        if (!match.hasMatch()) {
            continue;
        }
        Transport *t = nullptr;
        // see if we happen to have that one already
Laurent Montel's avatar
Laurent Montel committed
420 421
        const QString capturedString = match.captured(1);
        const QString checkString = QLatin1String("Transport ") + capturedString;
422
        for (Transport *old : oldTransports) {
Laurent Montel's avatar
Laurent Montel committed
423
            if (old->currentGroup() == checkString) {
424 425 426 427 428 429 430 431 432
                qCDebug(MAILTRANSPORT_LOG) << "reloading existing transport:" << s;
                t = old;
                t->load();
                oldTransports.removeAll(old);
                break;
            }
        }

        if (!t) {
Laurent Montel's avatar
Laurent Montel committed
433
            t = new Transport(capturedString);
434 435 436 437 438 439 440 441 442 443
        }
        if (t->id() <= 0) {
            t->setId(createId());
            t->save();
        }
        transports.append(t);
    }

    qDeleteAll(oldTransports);
    oldTransports.clear();
Laurent Montel's avatar
Laurent Montel committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    // 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();
461 462
}

Volker Krause's avatar
Volker Krause committed
463
void TransportManagerPrivate::writeConfig()
464
{
Laurent Montel's avatar
Laurent Montel committed
465 466 467 468
    KConfigGroup group(config, "General");
    group.writeEntry("default-transport", defaultTransportId);
    config->sync();
    q->emitChangesCommitted();
469 470
}

Volker Krause's avatar
Volker Krause committed
471
void TransportManagerPrivate::fillTypes()
472
{
Laurent Montel's avatar
Laurent Montel committed
473 474
    Q_ASSERT(types.isEmpty());

Laurent Montel's avatar
Laurent Montel committed
475 476 477 478 479 480 481
    updatePluginList();
    QObject::connect(MailTransport::TransportPluginManager::self(), &TransportPluginManager::updatePluginList, q, &TransportManager::updatePluginList);
}

void TransportManagerPrivate::updatePluginList()
{
    types.clear();
Laurent Montel's avatar
Laurent Montel committed
482 483
    const QVector<MailTransport::TransportAbstractPlugin *> lstPlugins = MailTransport::TransportPluginManager::self()->pluginsList();
    for (MailTransport::TransportAbstractPlugin *plugin : lstPlugins) {
Laurent Montel's avatar
Laurent Montel committed
484 485 486
        if (plugin->names().isEmpty()) {
            qCDebug(MAILTRANSPORT_LOG) << "Plugin " << plugin << " doesn't provide plugin";
        }
Laurent Montel's avatar
Laurent Montel committed
487 488
        const QVector<TransportAbstractPluginInfo> lstInfos = plugin->names();
        for (const MailTransport::TransportAbstractPluginInfo &info : lstInfos) {
Laurent Montel's avatar
Laurent Montel committed
489 490 491
            TransportType type;
            type.d->mName = info.name;
            type.d->mDescription = info.description;
Laurent Montel's avatar
Laurent Montel committed
492
            type.d->mIdentifier = info.identifier;
493
            type.d->mIsAkonadiResource = info.isAkonadi;
Laurent Montel's avatar
Laurent Montel committed
494 495
            types << type;
        }
496 497 498
    }
}

Laurent Montel's avatar
Laurent Montel committed
499 500 501 502 503
void TransportManager::updatePluginList()
{
    d->updatePluginList();
}

504 505
void TransportManager::emitChangesCommitted()
{
Laurent Montel's avatar
Laurent Montel committed
506 507
    d->myOwnChange = true; // prevent us from reading our changes again
    d->appliedChange = false; // but we have to read them at least once
508 509
    Q_EMIT transportsChanged();
    Q_EMIT changesCommitted();
510 511
}

Volker Krause's avatar
Volker Krause committed
512
void TransportManagerPrivate::slotTransportsChanged()
513
{
Laurent Montel's avatar
Laurent Montel committed
514 515 516 517 518
    if (myOwnChange && appliedChange) {
        myOwnChange = false;
        appliedChange = false;
        return;
    }
519

Laurent Montel's avatar
Laurent Montel committed
520
    qCDebug(MAILTRANSPORT_LOG);
Laurent Montel's avatar
Laurent Montel committed
521 522 523 524
    config->reparseConfiguration();
    // FIXME: this deletes existing transport objects!
    readConfig();
    appliedChange = true; // to prevent recursion
525
    Q_EMIT q->transportsChanged();
526 527
}

Volker Krause's avatar
Volker Krause committed
528
int TransportManagerPrivate::createId() const
529
{
530
    QVector<int> usedIds;
531
    usedIds.reserve(1 + transports.count());
Laurent Montel's avatar
Laurent Montel committed
532
    for (Transport *t : qAsConst(transports)) {
Laurent Montel's avatar
Laurent Montel committed
533 534 535 536 537
        usedIds << t->id();
    }
    usedIds << 0; // 0 is default for unknown
    int newId;
    do {
538
        newId = QRandomGenerator::global()->generate();
Laurent Montel's avatar
Laurent Montel committed
539 540
    } while (usedIds.contains(newId));
    return newId;
541 542
}

Laurent Montel's avatar
Laurent Montel committed
543
KWallet::Wallet *TransportManager::wallet()
544
{
Laurent Montel's avatar
Laurent Montel committed
545 546 547
    if (d->wallet && d->wallet->isOpen()) {
        return d->wallet;
    }
548

Laurent Montel's avatar
Laurent Montel committed
549
    if (!Wallet::isEnabled() || d->walletOpenFailed) {
Laurent Montel's avatar
Laurent Montel committed
550
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
551
    }
552

Laurent Montel's avatar
Laurent Montel committed
553 554 555 556 557 558
    WId window = 0;
    if (qApp->activeWindow()) {
        window = qApp->activeWindow()->winId();
    } else if (!QApplication::topLevelWidgets().isEmpty()) {
        window = qApp->topLevelWidgets().first()->winId();
    }
559

Laurent Montel's avatar
Laurent Montel committed
560 561
    delete d->wallet;
    d->wallet = Wallet::openWallet(Wallet::NetworkWallet(), window);
562

Laurent Montel's avatar
Laurent Montel committed
563 564
    if (!d->wallet) {
        d->walletOpenFailed = true;
Laurent Montel's avatar
Laurent Montel committed
565
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
566
    }
567

Laurent Montel's avatar
Laurent Montel committed
568 569
    d->prepareWallet();
    return d->wallet;
570 571
}

Volker Krause's avatar
Volker Krause committed
572
void TransportManagerPrivate::prepareWallet()
573
{
Laurent Montel's avatar
Laurent Montel committed
574 575 576 577 578 579 580
    if (!wallet) {
        return;
    }
    if (!wallet->hasFolder(WALLET_FOLDER)) {
        wallet->createFolder(WALLET_FOLDER);
    }
    wallet->setFolder(WALLET_FOLDER);
581 582 583 584
}

void TransportManager::loadPasswords()
{
585
    for (Transport *t : qAsConst(d->transports)) {
Laurent Montel's avatar
Laurent Montel committed
586 587
        t->readPassword();
    }
588

Laurent Montel's avatar
Laurent Montel committed
589 590 591
    // flush the wallet queue
    const QList<TransportJob *> copy = d->walletQueue;
    d->walletQueue.clear();
Laurent Montel's avatar
Laurent Montel committed
592
    for (TransportJob *job : copy) {
Laurent Montel's avatar
Laurent Montel committed
593 594
        job->start();
    }
595

596
    Q_EMIT passwordsChanged();
597 598 599 600
}

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

Laurent Montel's avatar
Laurent Montel committed
603 604
    // check if there is anything to do at all
    bool found = false;
605
    for (Transport *t : qAsConst(d->transports)) {
Laurent Montel's avatar
Laurent Montel committed
606 607 608 609
        if (!t->isComplete()) {
            found = true;
            break;
        }
610
    }
Laurent Montel's avatar
Laurent Montel committed
611 612
    if (!found) {
        return;
Allen Winter's avatar
Allen Winter committed
613 614
    }

Laurent Montel's avatar
Laurent Montel committed
615 616 617 618 619 620 621 622 623 624 625
    // 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);
Laurent Montel's avatar
Laurent Montel committed
626
        //Already async. It will be easy to port to qt5keychain
Laurent Montel's avatar
Laurent Montel committed
627
        if (d->wallet) {
Laurent Montel's avatar
Laurent Montel committed
628 629 630
            connect(d->wallet, &KWallet::Wallet::walletOpened, this, [this](bool status) {
                d->slotWalletOpened(status);
            });
Laurent Montel's avatar
Laurent Montel committed
631 632 633 634 635 636 637 638 639
            d->walletAsyncOpen = true;
        } else {
            d->walletOpenFailed = true;
            loadPasswords();
        }
        return;
    }
    if (d->wallet && !d->walletAsyncOpen) {
        loadPasswords();
640 641 642
    }
}

Laurent Montel's avatar
Laurent Montel committed
643
void TransportManagerPrivate::slotWalletOpened(bool success)
644
{
Laurent Montel's avatar
Laurent Montel committed
645
    qCDebug(MAILTRANSPORT_LOG);
Laurent Montel's avatar
Laurent Montel committed
646 647 648 649
    walletAsyncOpen = false;
    if (!success) {
        walletOpenFailed = true;
        delete wallet;
Laurent Montel's avatar
Laurent Montel committed
650
        wallet = nullptr;
Laurent Montel's avatar
Laurent Montel committed
651 652 653 654
    } else {
        prepareWallet();
    }
    q->loadPasswords();
655 656
}

Volker Krause's avatar
Volker Krause committed
657
void TransportManagerPrivate::validateDefault()
658
{
Laurent Montel's avatar
Laurent Montel committed
659 660 661 662
    if (!q->transportById(defaultTransportId, false)) {
        if (q->isEmpty()) {
            defaultTransportId = -1;
        } else {
Laurent Montel's avatar
Laurent Montel committed
663
            defaultTransportId = transports.constFirst()->id();
Laurent Montel's avatar
Laurent Montel committed
664 665
            writeConfig();
        }
666 667 668
    }
}

Volker Krause's avatar
Volker Krause committed
669
void TransportManagerPrivate::migrateToWallet()
670
{
Laurent Montel's avatar
Laurent Montel committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684
    // 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
685
    for (Transport *t : qAsConst(transports)) {
Laurent Montel's avatar
Laurent Montel committed
686 687 688 689 690 691 692 693 694 695
        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
696 697 698 699 700 701 702 703 704 705
        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
706 707 708 709 710
    if (result != KMessageBox::Yes) {
        return;
    }

    // perform migration
711
    for (Transport *t : qAsConst(transports)) {
Laurent Montel's avatar
Laurent Montel committed
712 713 714 715
        if (t->needsWalletMigration()) {
            t->migrateToWallet();
        }
    }
716 717
}

Tobias Koenig's avatar
Tobias Koenig committed
718
void TransportManagerPrivate::dbusServiceUnregistered()
719
{
Laurent Montel's avatar
Laurent Montel committed
720
    QDBusConnection::sessionBus().registerService(DBUS_SERVICE_NAME);
721 722
}

Laurent Montel's avatar
Laurent Montel committed
723
void TransportManagerPrivate::jobResult(KJob *job)
724
{
Laurent Montel's avatar
Laurent Montel committed
725
    walletQueue.removeAll(static_cast<TransportJob *>(job));
726 727
}

728
#include "moc_transportmanager.cpp"