diff options
-rw-r--r-- | connection.cpp | 34 | ||||
-rw-r--r-- | connection.h | 3 | ||||
-rw-r--r-- | room.cpp | 64 |
3 files changed, 56 insertions, 45 deletions
diff --git a/connection.cpp b/connection.cpp index 7650b4dd..4d76f7cd 100644 --- a/connection.cpp +++ b/connection.cpp @@ -59,6 +59,7 @@ class Connection::Private QString userId; SyncJob* syncJob; + QString stateSaveFile; }; Connection::Connection(const QUrl& server, QObject* parent) @@ -175,7 +176,6 @@ void Connection::sync(int timeout) void Connection::onSyncSuccess(SyncData &data) { d->data->setLastEvent(data.nextBatch()); - qInfo() << "last event " << d->data->lastEvent(); for( auto&& roomData: data.takeRoomData() ) { if ( auto* r = provideRoom(roomData.roomId) ) @@ -330,7 +330,11 @@ QByteArray Connection::generateTxnId() } QString Connection::getStateSaveFile() const { - return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/state.json"; + return d->stateSaveFile; +} + +void Connection::setStateSaveFile(const QString &path) { + d->stateSaveFile = path; } void Connection::saveState() { @@ -342,21 +346,25 @@ void Connection::saveState() { rooms[i->id()] = roomObj; } - QJsonObject rootObj{ - {"next_batch", QJsonValue(d->data->lastEvent())}, - {"presence", QJsonValue(QJsonObject())}, - {"rooms", QJsonValue({ - qMakePair(QString("leave"), QJsonValue(QJsonObject())), - qMakePair(QString("join"), QJsonValue(rooms)), - qMakePair(QString("invite"), QJsonValue(QJsonObject())) - })} - }; + QJsonObject roomObj; + roomObj["leave"] = QJsonObject(); + roomObj["join"] = rooms; + roomObj["invite"] = QJsonObject(); + + QJsonObject rootObj; + rootObj["next_batch"] = d->data->lastEvent(); + rootObj["presence"] = QJsonObject(); + rootObj["rooms"] = roomObj; + QJsonDocument doc { rootObj }; QByteArray data = doc.toJson(); - QFile outfile { getStateSaveFile() }; + QString filepath = getStateSaveFile(); + if (filepath.isEmpty()) return; + + QFile outfile { filepath }; outfile.open(QFile::WriteOnly); - qInfo() << "Writing state to file=" << outfile.fileName(); + qCDebug(MAIN) << "Writing state to file=" << outfile.fileName(); outfile.write(data.data(), data.size()); } diff --git a/connection.h b/connection.h index 58a3de6b..063018f9 100644 --- a/connection.h +++ b/connection.h @@ -39,6 +39,7 @@ namespace QMatrixClient class Connection: public QObject { Q_OBJECT + Q_PROPERTY(QString stateSaveFile READ getStateSaveFile WRITE setStateSaveFile) public: explicit Connection(const QUrl& server, QObject* parent = nullptr); Connection(); @@ -148,11 +149,13 @@ namespace QMatrixClient * Returns the path to file for saving state (rooms, presence, ...) */ QString getStateSaveFile() const; + void setStateSaveFile(const QString &path); /** * Completes loading sync data. */ void onSyncSuccess(SyncData &data); + private: class Private; Private* d; @@ -881,25 +881,26 @@ void Room::Private::toJson(QJsonObject &out) { QJsonValue nowTimestamp { QDateTime::currentMSecsSinceEpoch() }; QJsonArray stateEvents; - QJsonObject nameEvent { - {"type", QJsonValue("m.room.name")}, - {"content", QJsonValue({qMakePair(QString("name"), QJsonValue(this->name))})}}; + QJsonObject nameEvent; + nameEvent["type"] = QString("m.room.name"); + QJsonObject nameEventContent; + nameEventContent["name"] = this->name; + nameEvent["content"] = nameEventContent; stateEvents.append(QJsonValue(nameEvent)); for (auto i : this->membersMap) { - QJsonObject content { - {"membership", QJsonValue("join")}, - {"displayname", QJsonValue(i->displayname())} - // avatar URL is not available - }; - QJsonObject memberEvent { - {"type", QJsonValue("m.room.member")}, - {"sender", QJsonValue(i->id())}, - {"state_key", QJsonValue(i->id())}, - {"content", QJsonValue(content)}, - {"membership", QJsonValue("join")}, - {"origin_server_ts", nowTimestamp} - }; + QJsonObject content; + content["membership"] = QString("join"); + content["displayname"] = i->displayname(); + // avatar URL is not available + + QJsonObject memberEvent; + memberEvent["type"] = QString("m.room.member"); + memberEvent["sender"] = (i->id()); + memberEvent["state_key"] = (i->id()); + memberEvent["content"] = (content); + memberEvent["membership"] = QString("join"); + memberEvent["origin_server_ts"] = nowTimestamp; stateEvents.append(QJsonValue(memberEvent)); } @@ -909,31 +910,30 @@ void Room::Private::toJson(QJsonObject &out) { aliases.append(QJsonValue(i)); } - QJsonObject content { - {"aliases", QJsonValue(aliases)} - }; + QJsonObject content; + content["aliases"] = QJsonValue(aliases); - QJsonObject aliasEvent { - {"type", QJsonValue("m.room.aliases")}, - {"origin_server_ts", nowTimestamp}, - {"content", QJsonValue(content)} - }; + QJsonObject aliasEvent; + aliasEvent["type"] = QString("m.room.aliases"); + aliasEvent["origin_server_ts"] = nowTimestamp; + aliasEvent["content"] = (content); stateEvents.append(QJsonValue(aliasEvent)); } { - QJsonObject content { - {"alias", QJsonValue(this->canonicalAlias)} - }; - QJsonObject canonicalAliasEvent { - {"type", QJsonValue("m.room.canonical_alias")}, - {"origin_server_ts", nowTimestamp} - }; + QJsonObject content; + content["alias"] = (this->canonicalAlias); + + QJsonObject canonicalAliasEvent; + canonicalAliasEvent["type"] = QString("m.room.canonical_alias"); + canonicalAliasEvent["origin_server_ts"] = nowTimestamp; stateEvents.append(QJsonValue(canonicalAliasEvent)); } - out["state"] = QJsonValue({qMakePair(QString("events"), QJsonValue(stateEvents))}); + QJsonObject roomStateObj; + roomStateObj["events"] = QJsonValue(stateEvents); + out["state"] = roomStateObj; } void Room::toJson(QJsonObject &out) const { |