Changeset View
Changeset View
Standalone View
Standalone View
src/matrix-sdk.cpp
| Show First 20 Lines • Show All 595 Lines • ▼ Show 20 Lines | QStringList MatrixSdk::allSessions() const | ||||
| return sessionNames; | return sessionNames; | ||||
| } | } | ||||
| void MatrixSdk::serializeToFile() const | void MatrixSdk::serializeToFile() const | ||||
| { | { | ||||
| m_d->maybeSerialize(); | m_d->maybeSerialize(); | ||||
| } | } | ||||
| auto MatrixSdk::loadSession(QString sessionName) -> LoadSessionResult | void MatrixSdk::loadSession(QString sessionName) | ||||
| { | { | ||||
| using StdPath = std::filesystem::path; | using StdPath = std::filesystem::path; | ||||
| std::unique_ptr<KazvSessionLockGuard> lockGuard; | auto loadFromSession = [this, sessionName](StdPath sessionDir, std::unique_ptr<KazvSessionLockGuard> lockGuard) { | ||||
| auto loadFromSession = [this, sessionName, &lockGuard](StdPath sessionDir) { | |||||
| auto storeFile = sessionDir / "store"; | auto storeFile = sessionDir / "store"; | ||||
| auto metadataFile = sessionDir / "metadata"; | auto metadataFile = sessionDir / "metadata"; | ||||
| if (! std::filesystem::exists(storeFile)) { | if (! std::filesystem::exists(storeFile)) { | ||||
| qDebug() << "storeFile does not exist, skip loading session " << sessionName; | qDebug() << "storeFile does not exist, skip loading session " << sessionName; | ||||
| return SessionNotFound; | Q_EMIT loadSessionFinished(sessionName, SessionNotFound); | ||||
| return; | |||||
| } | } | ||||
| if (std::filesystem::exists(metadataFile)) { | if (std::filesystem::exists(metadataFile)) { | ||||
| KConfig metadata(QString::fromStdString(metadataFile.string())); | KConfig metadata(QString::fromStdString(metadataFile.string())); | ||||
| KConfigGroup mdGroup(&metadata, u"Metadata"_s); | KConfigGroup mdGroup(&metadata, u"Metadata"_s); | ||||
| auto format = mdGroup.readEntry(u"archiveFormat"_s); | auto format = mdGroup.readEntry(u"archiveFormat"_s); | ||||
| if (format != QStringLiteral("text")) { | if (format != QStringLiteral("text")) { | ||||
| qDebug() << "Unknown archive format:" << format; | qDebug() << "Unknown archive format:" << format; | ||||
| return SessionFormatUnknown; | Q_EMIT loadSessionFinished(sessionName, SessionFormatUnknown); | ||||
| return; | |||||
| } | } | ||||
| auto version = mdGroup.readEntry(u"kazvVersion"_s); | auto version = mdGroup.readEntry(u"kazvVersion"_s); | ||||
| auto curVersion = kazvVersionString(); | auto curVersion = kazvVersionString(); | ||||
| if (version != QString::fromStdString(curVersion)) { | if (version != QString::fromStdString(curVersion)) { | ||||
| qDebug() << "A different version from the current one, making a backup"; | qDebug() << "A different version from the current one, making a backup"; | ||||
| std::error_code err; | std::error_code err; | ||||
| auto now = std::chrono::system_clock::now(); | auto now = std::chrono::system_clock::now(); | ||||
| auto backupName = | auto backupName = | ||||
| std::to_string(std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count()); | std::to_string(std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count()); | ||||
| auto backupDir = sessionDir / "backup" / backupName; | auto backupDir = sessionDir / "backup" / backupName; | ||||
| if (! std::filesystem::create_directories(backupDir, err) | if (! std::filesystem::create_directories(backupDir, err) | ||||
| && err) { | && err) { | ||||
| qDebug() << "Cannot create backup directory"; | qDebug() << "Cannot create backup directory"; | ||||
| return SessionCannotBackup; | Q_EMIT loadSessionFinished(sessionName, SessionCannotBackup); | ||||
| return; | |||||
| } | } | ||||
| std::filesystem::copy_file(storeFile, backupDir / "store"); | std::filesystem::copy_file(storeFile, backupDir / "store"); | ||||
| std::filesystem::copy_file(metadataFile, backupDir / "metadata"); | std::filesystem::copy_file(metadataFile, backupDir / "metadata"); | ||||
| } | } | ||||
| } | } | ||||
| SdkModel model; | SdkModel model; | ||||
| try { | try { | ||||
| auto storeStream = std::ifstream(storeFile); | auto storeStream = std::ifstream(storeFile); | ||||
| if (! storeStream) { | if (! storeStream) { | ||||
| qDebug() << "Unable to open storeFile"; | qDebug() << "Unable to open storeFile"; | ||||
| return SessionCannotOpenFile; | Q_EMIT loadSessionFinished(sessionName, SessionCannotOpenFile); | ||||
| return; | |||||
| } | } | ||||
| using IAr = boost::archive::text_iarchive; | using IAr = boost::archive::text_iarchive; | ||||
| auto archive = IAr{storeStream}; | auto archive = IAr{storeStream}; | ||||
| archive >> model; | archive >> model; | ||||
| qDebug() << "Finished loading session"; | qDebug() << "Finished loading session"; | ||||
| } catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
| qDebug() << "Error when loading session:" << QString::fromStdString(e.what()); | qDebug() << "Error when loading session:" << QString::fromStdString(e.what()); | ||||
| return SessionDeserializeFailed; | Q_EMIT loadSessionFinished(sessionName, SessionDeserializeFailed); | ||||
| return; | |||||
| } | } | ||||
| QMetaObject::invokeMethod(this, [this, model=std::move(model), lockGuard=std::move(lockGuard)]() mutable { | |||||
| emplace(std::move(model), std::move(lockGuard)); | emplace(std::move(model), std::move(lockGuard)); | ||||
| return SessionLoadSuccess; | }); | ||||
| Q_EMIT loadSessionFinished(sessionName, SessionLoadSuccess); | |||||
| }; | }; | ||||
| qDebug() << "in loadSession(), sessionName=" << sessionName; | qDebug() << "in loadSession(), sessionName=" << sessionName; | ||||
| auto userDataDir = StdPath(m_d->userDataDir); | auto userDataDir = StdPath(m_d->userDataDir); | ||||
| auto parts = sessionName.split(u'/'); | auto parts = sessionName.split(u'/'); | ||||
| if (parts.size() == 2) { | if (parts.size() == 2) { | ||||
| auto userId = parts[0].toStdString(); | auto userId = parts[0].toStdString(); | ||||
| auto deviceId = parts[1].toStdString(); | auto deviceId = parts[1].toStdString(); | ||||
| auto sessionDir = sessionDirForUserAndDeviceId(userDataDir, userId, deviceId); | auto sessionDir = sessionDirForUserAndDeviceId(userDataDir, userId, deviceId); | ||||
| std::unique_ptr<KazvSessionLockGuard> lockGuard; | |||||
| try { | try { | ||||
| lockGuard = std::make_unique<KazvSessionLockGuard>(sessionDir); | lockGuard = std::make_unique<KazvSessionLockGuard>(sessionDir); | ||||
| } catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
| qCWarning(kazvLog) << "Error locking session: " << e.what(); | 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; | qDebug(kazvLog) << "no session found for" << sessionName; | ||||
| return SessionNotFound; | Q_EMIT loadSessionFinished(sessionName, SessionNotFound); | ||||
| } | } | ||||
| bool MatrixSdk::deleteSession(QString sessionName) { | bool MatrixSdk::deleteSession(QString sessionName) { | ||||
| using StdPath = std::filesystem::path; | using StdPath = std::filesystem::path; | ||||
| qDebug() << "in deleteSession(), sessionName=" << sessionName; | qDebug() << "in deleteSession(), sessionName=" << sessionName; | ||||
| auto userDataDir = StdPath(kazvUserDataDir().toStdString()); | auto userDataDir = StdPath(kazvUserDataDir().toStdString()); | ||||
| auto parts = sessionName.split(u'/'); | auto parts = sessionName.split(u'/'); | ||||
| if (parts.size() == 2) { | if (parts.size() == 2) { | ||||
| ▲ Show 20 Lines • Show All 207 Lines • Show Last 20 Lines | |||||