Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F8239172
D246.1760598641.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
11 KB
Referenced Files
None
Subscribers
None
D246.1760598641.diff
View Options
diff --git a/src/contents/ui/LoginPage.qml b/src/contents/ui/LoginPage.qml
--- a/src/contents/ui/LoginPage.qml
+++ b/src/contents/ui/LoginPage.qml
@@ -13,6 +13,7 @@
id: loginPage
title: l10n.get('login-page-title')
property var isSwitchingAccount: false
+ property bool loading: loadingSession
ColumnLayout {
width: parent.width
@@ -33,13 +34,14 @@
Layout.fillWidth: true
model: restoreSessionPart.sessions
currentIndex: -1
+ enabled: !loginPage.loading
}
Button {
id: restoreButton
text: l10n.get('login-page-restore-session-button')
Layout.fillWidth: true
- enabled: sessionChooser.currentIndex != -1
+ enabled: sessionChooser.currentIndex != -1 && !loginPage.loading
onClicked: {
const sess = restoreSessionPart.sessions[sessionChooser.currentIndex];
console.log('Selected session ', sess);
@@ -60,6 +62,7 @@
Layout.fillWidth: true
placeholderText: l10n.get('login-page-userid-input-placeholder')
Kirigami.FormData.label: l10n.get('login-page-userid-prompt')
+ enabled: !loginPage.loading
}
Kirigami.PasswordField {
@@ -67,6 +70,7 @@
objectName: 'passwordField'
Layout.fillWidth: true
Kirigami.FormData.label: l10n.get('login-page-password-prompt')
+ enabled: !loginPage.loading
}
TextField {
@@ -75,6 +79,7 @@
Layout.fillWidth: true
placeholderText: l10n.get('login-page-server-url-placeholder')
Kirigami.FormData.label: l10n.get('login-page-server-url-prompt')
+ enabled: !loginPage.loading
}
RowLayout {
@@ -93,6 +98,7 @@
matrixSdk.login(userIdField.text, passwordField.text, serverUrlField.text);
console.log('Login job sent')
}
+ enabled: !loginPage.loading
}
property var closeButton: Button {
@@ -102,10 +108,17 @@
onClicked: {
pageStack.removePage(loginPage);
}
+ enabled: !loginPage.loading
}
data: isSwitchingAccount ? [closeButton, loginButton] : [loginButton]
}
}
+
+ ProgressBar {
+ visible: loginPage.loading
+ indeterminate: true
+ Layout.fillWidth: true
+ }
}
}
diff --git a/src/contents/ui/Main.qml b/src/contents/ui/Main.qml
--- a/src/contents/ui/Main.qml
+++ b/src/contents/ui/Main.qml
@@ -22,6 +22,8 @@
cacheDirectory: kazvConfig.cacheDirectory
}
+ property bool loadingSession: false
+
property var matrixSdk: MK.MatrixSdk {
onLoginSuccessful: {
switchToMainPage();
@@ -49,6 +51,18 @@
console.warn('Logout failed');
showPassiveNotification(l10n.get('logout-failed-prompt', { errorCode, errorMsg }));
}
+
+ onLoadSessionFinished: (sessionName, res) => {
+ root.loadingSession = false;
+ if (res === MK.MatrixSdk.SessionLoadSuccess) {
+ console.log('load session successful');
+ switchToMainPage();
+ recordLastSession();
+ matrixSdk.getSpecVersions();
+ } else {
+ showPassiveNotification(getSessionLoadError(res, sessionName));
+ }
+ }
}
property var loggedIn: !!matrixSdk.token
@@ -317,22 +331,12 @@
}
function loadSession(sessionName) {
- const res = matrixSdk.loadSession(sessionName);
-
- if (res === MK.MatrixSdk.SessionLoadSuccess) {
- console.log('load session successful');
- switchToMainPage();
- recordLastSession();
- matrixSdk.getSpecVersions();
- } else {
- showPassiveNotification(getSessionLoadError(res, sessionName));
- }
- return res;
+ root.loadingSession = true;
+ matrixSdk.loadSession(sessionName);
}
function loadLastSession() {
console.log('last session is:', kazvConfig.lastSession);
-
return loadSession(kazvConfig.lastSession);
}
diff --git a/src/matrix-sdk.hpp b/src/matrix-sdk.hpp
--- a/src/matrix-sdk.hpp
+++ b/src/matrix-sdk.hpp
@@ -134,6 +134,8 @@
void sessionChanged();
+ void loadSessionFinished(QString sessionName, MatrixSdk::LoadSessionResult result);
+
public Q_SLOTS:
void login(const QString &userId, const QString &password, const QString &homeserverUrl);
void logout();
@@ -146,13 +148,14 @@
void serializeToFile() const;
/**
- * Load session at <AppDataDir>/sessions/<sessionName> .
+ * Load session at <AppDataDir>/sessions/<sessionName> asynchronously.
*
- * @param sessionName A string in the form of <userid>/<deviceid> .
+ * When the session is loaded or when there is an error, loadSessionFinished
+ * will be called with the result.
*
- * @return true if successful, false otherwise.
+ * @param sessionName A string in the form of <userid>/<deviceid> .
*/
- LoadSessionResult loadSession(QString sessionName);
+ void loadSession(QString sessionName);
/**
* Delete session at <AppDataDir>/sessions/<sessionName> .
diff --git a/src/matrix-sdk.cpp b/src/matrix-sdk.cpp
--- a/src/matrix-sdk.cpp
+++ b/src/matrix-sdk.cpp
@@ -601,18 +601,18 @@
m_d->maybeSerialize();
}
-auto MatrixSdk::loadSession(QString sessionName) -> LoadSessionResult
+void MatrixSdk::loadSession(QString sessionName)
{
using StdPath = std::filesystem::path;
- std::unique_ptr<KazvSessionLockGuard> lockGuard;
- auto loadFromSession = [this, sessionName, &lockGuard](StdPath sessionDir) {
+ auto loadFromSession = [this, sessionName](StdPath sessionDir, std::unique_ptr<KazvSessionLockGuard> lockGuard) {
auto storeFile = sessionDir / "store";
auto metadataFile = sessionDir / "metadata";
if (! std::filesystem::exists(storeFile)) {
qDebug() << "storeFile does not exist, skip loading session " << sessionName;
- return SessionNotFound;
+ Q_EMIT loadSessionFinished(sessionName, SessionNotFound);
+ return;
}
if (std::filesystem::exists(metadataFile)) {
@@ -621,7 +621,8 @@
auto format = mdGroup.readEntry(u"archiveFormat"_s);
if (format != QStringLiteral("text")) {
qDebug() << "Unknown archive format:" << format;
- return SessionFormatUnknown;
+ Q_EMIT loadSessionFinished(sessionName, SessionFormatUnknown);
+ return;
}
auto version = mdGroup.readEntry(u"kazvVersion"_s);
auto curVersion = kazvVersionString();
@@ -635,7 +636,8 @@
if (! std::filesystem::create_directories(backupDir, err)
&& err) {
qDebug() << "Cannot create backup directory";
- return SessionCannotBackup;
+ Q_EMIT loadSessionFinished(sessionName, SessionCannotBackup);
+ return;
}
std::filesystem::copy_file(storeFile, backupDir / "store");
std::filesystem::copy_file(metadataFile, backupDir / "metadata");
@@ -647,7 +649,8 @@
auto storeStream = std::ifstream(storeFile);
if (! storeStream) {
qDebug() << "Unable to open storeFile";
- return SessionCannotOpenFile;
+ Q_EMIT loadSessionFinished(sessionName, SessionCannotOpenFile);
+ return;
}
using IAr = boost::archive::text_iarchive;
auto archive = IAr{storeStream};
@@ -655,10 +658,13 @@
qDebug() << "Finished loading session";
} catch (const std::exception &e) {
qDebug() << "Error when loading session:" << QString::fromStdString(e.what());
- return SessionDeserializeFailed;
+ Q_EMIT loadSessionFinished(sessionName, SessionDeserializeFailed);
+ return;
}
- emplace(std::move(model), std::move(lockGuard));
- return SessionLoadSuccess;
+ QMetaObject::invokeMethod(this, [this, model=std::move(model), lockGuard=std::move(lockGuard)]() mutable {
+ emplace(std::move(model), std::move(lockGuard));
+ });
+ Q_EMIT loadSessionFinished(sessionName, SessionLoadSuccess);
};
qDebug() << "in loadSession(), sessionName=" << sessionName;
@@ -668,16 +674,21 @@
auto userId = parts[0].toStdString();
auto deviceId = parts[1].toStdString();
auto sessionDir = sessionDirForUserAndDeviceId(userDataDir, userId, deviceId);
+ std::unique_ptr<KazvSessionLockGuard> lockGuard;
try {
lockGuard = std::make_unique<KazvSessionLockGuard>(sessionDir);
} catch (const std::runtime_error &e) {
qCWarning(kazvLog) << "Error locking session: " << e.what();
- return SessionLockFailed;
+ Q_EMIT loadSessionFinished(sessionName, SessionLockFailed);
+ return;
}
- return loadFromSession(sessionDir);
+ QThreadPool::globalInstance()->start([loadFromSession, sessionDir, lockGuard=std::move(lockGuard)]() mutable {
+ loadFromSession(sessionDir, std::move(lockGuard));
+ });
+ return;
}
qDebug(kazvLog) << "no session found for" << sessionName;
- return SessionNotFound;
+ Q_EMIT loadSessionFinished(sessionName, SessionNotFound);
}
bool MatrixSdk::deleteSession(QString sessionName) {
diff --git a/src/tests/matrix-sdk-session-loader.cpp b/src/tests/matrix-sdk-session-loader.cpp
--- a/src/tests/matrix-sdk-session-loader.cpp
+++ b/src/tests/matrix-sdk-session-loader.cpp
@@ -6,21 +6,24 @@
#include <kazv-defs.hpp>
#include <iostream>
+#include <cstdlib>
+#include <QCoreApplication>
#include <QString>
#include "matrix-sdk-sessions-test.hpp"
-int main([[maybe_unused]] int argc, char *argv[])
+int main(int argc, char *argv[])
{
std::string userDataDir = argv[1];
std::string sessionName = argv[2];
+ auto app = QCoreApplication(argc, argv);
auto sdk = MatrixSdkSessionsTest::makeMatrixSdkImpl(userDataDir);
- auto res = sdk->loadSession(QString::fromStdString(sessionName));
- if (res != MatrixSdk::SessionLoadSuccess) {
- std::cout << "cannot load session" << std::endl;
- return res == MatrixSdk::SessionLockFailed ? 1 : 2;
- }
-
- std::cout << "loaded session" << std::endl;
- while (1) {}
- return 0;
+ QObject::connect(sdk.get(), &MatrixSdk::loadSessionFinished, [](auto, auto res) {
+ if (res != MatrixSdk::SessionLoadSuccess) {
+ std::cout << "cannot load session" << std::endl;
+ std::exit(res == MatrixSdk::SessionLockFailed ? 1 : 2);
+ }
+ std::cout << "loaded session" << std::endl;
+ });
+ sdk->loadSession(QString::fromStdString(sessionName));
+ return app.exec();
}
diff --git a/src/tests/matrix-sdk-sessions-test.cpp b/src/tests/matrix-sdk-sessions-test.cpp
--- a/src/tests/matrix-sdk-sessions-test.cpp
+++ b/src/tests/matrix-sdk-sessions-test.cpp
@@ -101,7 +101,10 @@
createSession("@mew:example.com", "device4");
auto sdk = makeMatrixSdk();
- auto res = sdk->loadSession(u"@mew:example.com/device4"_s);
+ auto spy = QSignalSpy(sdk.get(), &MatrixSdk::loadSessionFinished);
+ sdk->loadSession(u"@mew:example.com/device4"_s);
+ spy.wait();
+ auto res = spy.first().at(1);
QCOMPARE(res, MatrixSdk::SessionLoadSuccess);
QCOMPARE(sdk->userId(), QStringLiteral("@mew:example.com"));
QCOMPARE(sdk->deviceId(), QStringLiteral("device4"));
@@ -175,7 +178,10 @@
QTest::qWait(100);
auto sdk = makeMatrixSdk();
- auto res = sdk->loadSession(QString::fromStdString(userId + "/" + deviceId));
+ auto spy = QSignalSpy(sdk.get(), &MatrixSdk::loadSessionFinished);
+ sdk->loadSession(QString::fromStdString(userId + "/" + deviceId));
+ spy.wait();
+ auto res = spy.first().at(1);
QCOMPARE(res, MatrixSdk::SessionLoadSuccess);
// verify that the original data is kept
QCOMPARE(sdk->serverUrl(), u""_s);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Oct 16, 12:10 AM (5 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
520103
Default Alt Text
D246.1760598641.diff (11 KB)
Attached To
Mode
D246: Load session asynchronously
Attached
Detach File
Event Timeline
Log In to Comment