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 @@ -227,17 +227,42 @@ return userId + '/' + deviceId; } + function getSessionLoadError(res, sessionName) { + switch (res) { + case MK.MatrixSdk.SessionNotFound: + return l10n.get('session-load-failure-not-found', { sessionName }); + + case MK.MatrixSdk.SessionFormatUnknown: + return l10n.get('session-load-failure-format-unknown', { sessionName }); + + case MK.MatrixSdk.SessionCannotBackup: + return l10n.get('session-load-failure-cannot-backup', { sessionName }); + + case MK.MatrixSdk.SessionLockFailed: + return l10n.get('session-load-failure-lock-failed', { sessionName }); + + case MK.MatrixSdk.SessionCannotOpenFile: + return l10n.get('session-load-failure-cannot-open-file', { sessionName }); + + case MK.MatrixSdk.SessionDeserializeFailed: + return l10n.get('session-load-failure-deserialize-failed', { sessionName }); + + default: + console.error('loadSession: Encountered an unknown error code'); + } + } + function loadSession(sessionName) { - const succ = matrixSdk.loadSession(sessionName); + const res = matrixSdk.loadSession(sessionName); - if (succ) { + if (res === MK.MatrixSdk.SessionLoadSuccess) { console.log('load session successful'); switchToMainPage(); recordLastSession(); } else { - console.log('load session failed'); + showPassiveNotification(getSessionLoadError(res, sessionName)); } - return succ; + return res; } function loadLastSession() { diff --git a/src/l10n/cmn-Hans/100-ui.ftl b/src/l10n/cmn-Hans/100-ui.ftl --- a/src/l10n/cmn-Hans/100-ui.ftl +++ b/src/l10n/cmn-Hans/100-ui.ftl @@ -41,6 +41,13 @@ login-page-request-failed-prompt = 登录失败。错误代码:{ $errorCode }。错误讯息:{ $errorMsg }。 login-page-discover-failed-prompt = 不能检测此用户所在的服务器,或者服务器不可用。错误代码:{ $errorCode }。错误讯息:{ $errorMsg }。 +session-load-failure-not-found = 找不到会话 { $sessionName }。 +session-load-failure-format-unknown = 会话 { $sessionName } 包含不支持的格式。是由未来版本的 { -kt-app-name } 保存的吗? +session-load-failure-cannot-backup = 无法备份会话 { $sessionName }。 +session-load-failure-lock-failed = 会话 { $sessionName } 正在被别的程序使用。 +session-load-failure-cannot-open-file = 无法打开会话 { $sessionName } 的存档文件。 +session-load-failure-deserialize-failed = 无法打开会话 { $sessionName }。是被损坏了或者是由未来版本的 { -kt-app-name } 保存的吗? + main-page-title = { -kt-app-name } - { $userId } main-page-recent-tab-title = 最近 main-page-favourites-tab-title = 最爱 diff --git a/src/l10n/en/100-ui.ftl b/src/l10n/en/100-ui.ftl --- a/src/l10n/en/100-ui.ftl +++ b/src/l10n/en/100-ui.ftl @@ -41,6 +41,13 @@ login-page-request-failed-prompt = Login failed. Error code: { $errorCode }. Error message: { $errorMsg }. login-page-discover-failed-prompt = Unable to detect the server this user is on, or the server is unavailable. Error code: { $errorCode }. Error message: { $errorMsg }. +session-load-failure-not-found = The session { $sessionName } is not found. +session-load-failure-format-unknown = The session { $sessionName } contains an unsupported format. Is it saved using a future version of { -kt-app-name }? +session-load-failure-cannot-backup = Cannot make a backup of the session { $sessionName }. +session-load-failure-lock-failed = The session { $sessionName } is being used by another program. +session-load-failure-cannot-open-file = Unable to open the store file for the session { $sessionName }. +session-load-failure-deserialize-failed = The session { $sessionName } cannot be opened. Is it corrupted or saved using a future version of { -kt-app-name }? + main-page-title = { -kt-app-name } - { $userId } main-page-recent-tab-title = Recent main-page-favourites-tab-title = Favourites diff --git a/src/matrix-sdk.hpp b/src/matrix-sdk.hpp --- a/src/matrix-sdk.hpp +++ b/src/matrix-sdk.hpp @@ -61,6 +61,25 @@ Q_ENUM(CreateRoomPreset); + enum LoadSessionResult { + /// Successfully loaded the session + SessionLoadSuccess, + /// There is no store file + SessionNotFound, + /// The format of the store file is not supported + SessionFormatUnknown, + /// The store file cannot be backed up + SessionCannotBackup, + /// Cannot grab the lock on the session file + SessionLockFailed, + /// Cannot open store file + SessionCannotOpenFile, + /// Cannot deserialize the store file + SessionDeserializeFailed, + }; + + Q_ENUM(LoadSessionResult); + explicit MatrixSdk(QObject *parent = 0); ~MatrixSdk() override; @@ -109,7 +128,7 @@ * * @return true if successful, false otherwise. */ - bool loadSession(QString sessionName); + LoadSessionResult loadSession(QString sessionName); /** * Start an empty session. diff --git a/src/matrix-sdk.cpp b/src/matrix-sdk.cpp --- a/src/matrix-sdk.cpp +++ b/src/matrix-sdk.cpp @@ -447,7 +447,7 @@ m_d->maybeSerialize(); } -bool MatrixSdk::loadSession(QString sessionName) +auto MatrixSdk::loadSession(QString sessionName) -> LoadSessionResult { using StdPath = std::filesystem::path; std::unique_ptr<KazvSessionLockGuard> lockGuard; @@ -458,7 +458,7 @@ if (! std::filesystem::exists(storeFile)) { qDebug() << "storeFile does not exist, skip loading session " << sessionName; - return false; + return SessionNotFound; } if (std::filesystem::exists(metadataFile)) { @@ -467,7 +467,7 @@ auto format = mdGroup.readEntry("archiveFormat"); if (format != QStringLiteral("text")) { qDebug() << "Unknown archive format:" << format; - return false; + return SessionFormatUnknown; } auto version = mdGroup.readEntry("kazvVersion"); auto curVersion = kazvVersionString(); @@ -481,7 +481,7 @@ if (! std::filesystem::create_directories(backupDir, err) && err) { qDebug() << "Cannot create backup directory"; - return false; + return SessionCannotBackup; } std::filesystem::copy_file(storeFile, backupDir / "store"); std::filesystem::copy_file(metadataFile, backupDir / "metadata"); @@ -493,7 +493,7 @@ auto storeStream = std::ifstream(storeFile); if (! storeStream) { qDebug() << "Unable to open storeFile"; - return false; + return SessionCannotOpenFile; } using IAr = boost::archive::text_iarchive; auto archive = IAr{storeStream}; @@ -501,10 +501,10 @@ qDebug() << "Finished loading session"; } catch (const std::exception &e) { qDebug() << "Error when loading session:" << QString::fromStdString(e.what()); - return false; + return SessionDeserializeFailed; } emplace(std::move(model), std::move(lockGuard)); - return true; + return SessionLoadSuccess; }; qDebug() << "in loadSession(), sessionName=" << sessionName; @@ -519,12 +519,12 @@ lockGuard = std::make_unique<KazvSessionLockGuard>(sessionDir); } catch (const std::runtime_error &e) { qCWarning(kazvLog) << "Error locking session: " << e.what(); - return false; + return SessionLockFailed; } return loadFromSession(sessionDir); } qDebug(kazvLog) << "no session found for" << sessionName; - return false; + return SessionNotFound; } bool MatrixSdk::startNewSession() 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 @@ -15,9 +15,9 @@ std::string sessionName = argv[2]; auto sdk = MatrixSdkSessionsTest::makeMatrixSdkImpl(userDataDir); auto res = sdk->loadSession(QString::fromStdString(sessionName)); - if (!res) { + if (res != MatrixSdk::SessionLoadSuccess) { std::cout << "cannot load session" << std::endl; - return 1; + return res == MatrixSdk::SessionLockFailed ? 1 : 2; } std::cout << "loaded session" << std::endl; 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 @@ -99,7 +99,7 @@ auto sdk = makeMatrixSdk(); auto res = sdk->loadSession("@mew:example.com/device4"); - QVERIFY(res); + QCOMPARE(res, MatrixSdk::SessionLoadSuccess); QCOMPARE(sdk->userId(), QStringLiteral("@mew:example.com")); QCOMPARE(sdk->deviceId(), QStringLiteral("device4")); }