diff options
author | Felix Rohrbach <fxrh@gmx.de> | 2016-08-23 18:35:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-23 18:35:40 +0200 |
commit | 77d45e9afa195f7d715a5074b16f16c6b2d15235 (patch) | |
tree | b8c9f1d73e0eba48cc55194b06bbbeb97bc4e258 | |
parent | da52097f1c90d7286aff27a84fc14b6b0793ff97 (diff) | |
parent | fb6df81fa9f224c94118fd79fea91ae15559c7e3 (diff) | |
download | libquotient-77d45e9afa195f7d715a5074b16f16c6b2d15235.tar.gz libquotient-77d45e9afa195f7d715a5074b16f16c6b2d15235.zip |
Merge pull request #18 from Fxrh/kitsune-use-qvector-with-nonpointers
Replaced QList<> with QVector<> where appropriate + minor code cleanup
-rw-r--r-- | connection.cpp | 6 | ||||
-rw-r--r-- | connectionprivate.cpp | 9 | ||||
-rw-r--r-- | connectionprivate.h | 2 | ||||
-rw-r--r-- | events/receiptevent.cpp | 24 | ||||
-rw-r--r-- | events/receiptevent.h | 6 | ||||
-rw-r--r-- | jobs/syncjob.cpp | 10 | ||||
-rw-r--r-- | jobs/syncjob.h | 8 | ||||
-rw-r--r-- | room.cpp | 6 |
8 files changed, 32 insertions, 39 deletions
diff --git a/connection.cpp b/connection.cpp index d6338191..904649bc 100644 --- a/connection.cpp +++ b/connection.cpp @@ -113,7 +113,11 @@ SyncJob* Connection::sync(int timeout) syncJob->setTimeout(timeout); connect( syncJob, &SyncJob::success, [=] () { d->data->setLastEvent(syncJob->nextBatch()); - d->processRooms(syncJob->roomData()); + for( const auto roomData: syncJob->roomData() ) + { + if ( Room* r = d->provideRoom(roomData.roomId) ) + r->updateData(roomData); + } emit syncDone(); }); connect( syncJob, &SyncJob::failure, [=] () { diff --git a/connectionprivate.cpp b/connectionprivate.cpp index 0f9f4539..6556a8ee 100644 --- a/connectionprivate.cpp +++ b/connectionprivate.cpp @@ -85,15 +85,6 @@ void ConnectionPrivate::processState(State* state) r->addInitialState(state); } -void ConnectionPrivate::processRooms(const QList<SyncRoomData>& data) -{ - for( const SyncRoomData& roomData: data ) - { - if ( Room* r = provideRoom(roomData.roomId) ) - r->updateData(roomData); - } -} - Room* ConnectionPrivate::provideRoom(QString id) { if (id.isEmpty()) diff --git a/connectionprivate.h b/connectionprivate.h index d1199081..424ef998 100644 --- a/connectionprivate.h +++ b/connectionprivate.h @@ -45,7 +45,7 @@ namespace QMatrixClient void resolveServer( QString domain ); void processState( State* state ); - void processRooms( const QList<SyncRoomData>& data ); + /** Finds a room with this id or creates a new one and adds it to roomMap. */ Room* provideRoom( QString id ); diff --git a/events/receiptevent.cpp b/events/receiptevent.cpp index d2a27079..29d78a20 100644 --- a/events/receiptevent.cpp +++ b/events/receiptevent.cpp @@ -41,17 +41,11 @@ Example of a Receipt Event: using namespace QMatrixClient; -Receipt::Receipt(QString event, QString user, QDateTime time) - : eventId(event) - , userId(user) - , timestamp(time) -{ -} class ReceiptEvent::Private { public: - QHash<QString, QList<Receipt>> eventToReceipts; + QHash<QString, Receipts> eventToReceipts; }; ReceiptEvent::ReceiptEvent() @@ -65,7 +59,7 @@ ReceiptEvent::~ReceiptEvent() delete d; } -QList<Receipt> ReceiptEvent::receiptsForEvent(QString eventId) const +Receipts ReceiptEvent::receiptsForEvent(QString eventId) const { return d->eventToReceipts.value(eventId); } @@ -79,17 +73,17 @@ ReceiptEvent* ReceiptEvent::fromJson(const QJsonObject& obj) { ReceiptEvent* e = new ReceiptEvent(); e->parseJson(obj); - const QJsonObject contents = obj.value("content").toObject(); + const QJsonObject contents = obj["content"].toObject(); + e->d->eventToReceipts.reserve(contents.size()); for( const QString& eventId: contents.keys() ) { - QJsonObject reads = contents.value(eventId).toObject().value("m.read").toObject(); - QList<Receipt> receipts; + const QJsonObject reads = contents[eventId].toObject().value("m.read").toObject(); + Receipts receipts(reads.size()); for( const QString& userId: reads.keys() ) { - QJsonObject user = reads.value(userId).toObject(); - QDateTime time = QDateTime::fromMSecsSinceEpoch( (quint64) user.value("ts").toDouble(), Qt::UTC ); - Receipt receipt(eventId, userId, time); - receipts.append(receipt); + const QJsonObject user = reads[userId].toObject(); + const QDateTime time = QDateTime::fromMSecsSinceEpoch( (quint64) user["ts"].toDouble(), Qt::UTC ); + receipts.push_back({ eventId, userId, time }); } e->d->eventToReceipts.insert(eventId, receipts); } diff --git a/events/receiptevent.h b/events/receiptevent.h index aa72ba90..028bf0f6 100644 --- a/events/receiptevent.h +++ b/events/receiptevent.h @@ -31,9 +31,8 @@ namespace QMatrixClient QString eventId; QString userId; QDateTime timestamp; - - Receipt(QString event, QString user, QDateTime time); }; + using Receipts = QVector<Receipt>; class ReceiptEvent: public Event { @@ -41,7 +40,7 @@ namespace QMatrixClient ReceiptEvent(); virtual ~ReceiptEvent(); - QList<Receipt> receiptsForEvent(QString eventId) const; + Receipts receiptsForEvent(QString eventId) const; QStringList events() const; @@ -52,5 +51,6 @@ namespace QMatrixClient Private* d; }; } +Q_DECLARE_TYPEINFO(QMatrixClient::Receipt, Q_MOVABLE_TYPE); #endif // QMATRIXCLIENT_RECEIPTEVENT_H diff --git a/jobs/syncjob.cpp b/jobs/syncjob.cpp index 3bd1f0c8..a9865b55 100644 --- a/jobs/syncjob.cpp +++ b/jobs/syncjob.cpp @@ -40,7 +40,7 @@ class SyncJob::Private int timeout; QString nextBatch; - QList<SyncRoomData> roomData; + SyncData roomData; }; static size_t jobId = 0; @@ -84,7 +84,7 @@ QString SyncJob::nextBatch() const return d->nextBatch; } -QList<SyncRoomData> SyncJob::roomData() const +const SyncData& SyncJob::roomData() const { return d->roomData; } @@ -128,9 +128,9 @@ BaseJob::Status SyncJob::parseJson(const QJsonDocument& data) { const QJsonObject rs = rooms.value(roomState.jsonKey).toObject(); d->roomData.reserve(rs.size()); - for( auto r = rs.begin(); r != rs.end(); ++r ) + for( auto rkey: rs.keys() ) { - d->roomData.push_back({r.key(), r.value().toObject(), roomState.enumVal}); + d->roomData.push_back({rkey, roomState.enumVal, rs[rkey].toObject()}); } } @@ -143,7 +143,7 @@ void SyncRoomData::EventList::fromJson(const QJsonObject& roomContents) swap(l); } -SyncRoomData::SyncRoomData(QString roomId_, const QJsonObject& room_, JoinState joinState_) +SyncRoomData::SyncRoomData(QString roomId_, JoinState joinState_, const QJsonObject& room_) : roomId(roomId_) , joinState(joinState_) , state("state") diff --git a/jobs/syncjob.h b/jobs/syncjob.h index 6e6b4f7c..f08a6b56 100644 --- a/jobs/syncjob.h +++ b/jobs/syncjob.h @@ -52,8 +52,11 @@ namespace QMatrixClient int highlightCount; int notificationCount; - SyncRoomData(QString roomId_, const QJsonObject& room_, JoinState joinState_); + SyncRoomData(QString roomId_ = QString(), + JoinState joinState_ = JoinState::Join, + const QJsonObject& room_ = QJsonObject()); }; + using SyncData = QVector<SyncRoomData>; class ConnectionData; class SyncJob: public BaseJob @@ -67,7 +70,7 @@ namespace QMatrixClient void setPresence(QString presence); void setTimeout(int timeout); - QList<SyncRoomData> roomData() const; + const SyncData& roomData() const; QString nextBatch() const; protected: @@ -80,5 +83,6 @@ namespace QMatrixClient Private* d; }; } +Q_DECLARE_TYPEINFO(QMatrixClient::SyncRoomData, Q_MOVABLE_TYPE); #endif // QMATRIXCLIENT_SYNCJOB_H @@ -482,11 +482,11 @@ void Room::processEphemeralEvent(Event* event) } if( event->type() == EventType::Receipt ) { - ReceiptEvent* receiptEvent = static_cast<ReceiptEvent*>(event); + auto receiptEvent = static_cast<ReceiptEvent*>(event); for( QString eventId: receiptEvent->events() ) { - QList<Receipt> receipts = receiptEvent->receiptsForEvent(eventId); - for( Receipt r: receipts ) + const auto receipts = receiptEvent->receiptsForEvent(eventId); + for( const Receipt r: receipts ) { d->lastReadEvent.insert(d->connection->user(r.userId), eventId); } |