diff --git a/Quotient/connection.cpp b/Quotient/connection.cpp index aae963ce8..edc923265 100644 --- a/Quotient/connection.cpp +++ b/Quotient/connection.cpp @@ -392,31 +392,44 @@ void Connection::Private::completeSetup(const QString& mxId, bool newLogin, const std::optional& deviceId, const std::optional& accessToken) { - data->setIdentity(mxId, deviceId.value_or(u""_s), accessToken.value_or(u""_s).toLatin1()); - q->setObjectName(data->userId() % u'/' % data->deviceId()); - qCDebug(MAIN) << "Using server" << data->baseUrl().toDisplayString() - << "by user" << data->userId() - << "from device" << data->deviceId(); - connect(qApp, &QCoreApplication::aboutToQuit, q, &Connection::saveState); - setupPicklingKey(); - - if (accessToken.has_value()) { - q->loadVersions(); - q->loadCapabilities(); - q->user()->load(); // Load the local user's profile - } + auto doCompleteSetup = [this, mxId, deviceId, accessToken](){ + data->setIdentity(mxId, deviceId.value_or(u""_s), accessToken.value_or(u""_s).toLatin1()); + q->setObjectName(data->userId() % u'/' % data->deviceId()); + qCDebug(MAIN) << "Using server" << data->baseUrl().toDisplayString() + << "by user" << data->userId() + << "from device" << data->deviceId(); + connect(qApp, &QCoreApplication::aboutToQuit, q, &Connection::saveState); + setupPicklingKey(); + + if (accessToken.has_value()) { + q->loadVersions(); + q->loadCapabilities(); + q->user()->load(); // Load the local user's profile + } - emit q->stateChanged(); // Technically connected to the homeserver but no E2EE yet + emit q->stateChanged(); // Technically connected to the homeserver but no E2EE yet - if (useEncryption) { - emit q->encryptionChanged(useEncryption); - emit q->stateChanged(); - emit q->ready(); - emit q->connected(); + if (useEncryption) { + emit q->encryptionChanged(useEncryption); + emit q->stateChanged(); + emit q->ready(); + emit q->connected(); + } else { + qCInfo(E2EE) << "End-to-end encryption (E2EE) support is off for" << q->objectName(); + emit q->ready(); + emit q->connected(); + } + }; + if (newLogin) { + auto mxIdForDb = q->userId(); + mxIdForDb.replace(u':', u'_'); + const QString databasePath{ QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) % u'/' % mxIdForDb % u'/' % q->deviceId() }; + QDir(databasePath).removeRecursively(); + runKeychainJob(new QKeychain::DeletePasswordJob(qAppName()), q->userId() % u"-Pickle"_s).then([doCompleteSetup](const auto &) { + doCompleteSetup(); + }); } else { - qCInfo(E2EE) << "End-to-end encryption (E2EE) support is off for" << q->objectName(); - emit q->ready(); - emit q->connected(); + doCompleteSetup(); } }