diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-05-25 19:45:15 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-05-26 10:47:44 +0900 |
commit | fab6c26770827ce2b8ed8f5d06b3677b75a19cec (patch) | |
tree | 2487f1432c45829b152e7481077ad0fb08748aa6 | |
parent | 1cd8802c73ee3c0cc0095f8be30a9bbf3442c3ca (diff) | |
download | libquotient-fab6c26770827ce2b8ed8f5d06b3677b75a19cec.tar.gz libquotient-fab6c26770827ce2b8ed8f5d06b3677b75a19cec.zip |
Introducing EventList class + minor fix
Now you can parse a JSON array into a list of events with a one-liner.
Also, fromMSecsSinceEpoch accepts a qint64, not quint64 - fixed the respective cast.
-rw-r--r-- | events/event.cpp | 14 | ||||
-rw-r--r-- | events/event.h | 4 | ||||
-rw-r--r-- | jobs/roommessagesjob.cpp | 9 |
3 files changed, 18 insertions, 9 deletions
diff --git a/events/event.cpp b/events/event.cpp index c3e94ce1..9a48108c 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -18,7 +18,7 @@ #include "event.h" -#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> #include <QtCore/QJsonDocument> #include <QtCore/QDateTime> #include <QtCore/QDebug> @@ -134,7 +134,8 @@ bool Event::parseJson(const QJsonObject& obj) } if( obj.contains("origin_server_ts") ) { - d->timestamp = QDateTime::fromMSecsSinceEpoch( (quint64) obj.value("origin_server_ts").toDouble(), Qt::UTC ); + d->timestamp = QDateTime::fromMSecsSinceEpoch( + static_cast<qint64>(obj.value("origin_server_ts").toDouble()), Qt::UTC ); } else { correct = false; qDebug() << "Event: can't find ts"; @@ -146,3 +147,12 @@ bool Event::parseJson(const QJsonObject& obj) } return correct; } + +QList<Event*> QMatrixClient::eventListFromJson(const QJsonArray& json) +{ + QList<Event*> l; + l.reserve(json.size()); + for (auto event: json) + l.push_back(Event::fromJson(event.toObject())); + return l; +} diff --git a/events/event.h b/events/event.h index 6a8d0e89..a009aa99 100644 --- a/events/event.h +++ b/events/event.h @@ -25,6 +25,8 @@ #include <QtCore/QDateTime> #include <QtCore/QJsonObject> +class QJsonArray; + namespace QMatrixClient { enum class EventType @@ -56,6 +58,8 @@ namespace QMatrixClient Private* d; }; + QList<Event*> eventListFromJson(const QJsonArray& contents); + /** * Finds a place in the timeline where a new event/message could be inserted. * @return an iterator to an item with the earliest timestamp after diff --git a/jobs/roommessagesjob.cpp b/jobs/roommessagesjob.cpp index fc3f5f1e..5a97cc23 100644 --- a/jobs/roommessagesjob.cpp +++ b/jobs/roommessagesjob.cpp @@ -84,12 +84,7 @@ QUrlQuery RoomMessagesJob::query() void RoomMessagesJob::parseJson(const QJsonDocument& data) { QJsonObject obj = data.object(); - QJsonArray chunk = obj.value("chunk").toArray(); - for( const QJsonValue& val: chunk ) - { - Event* event = Event::fromJson(val.toObject()); - d->events.append(event); - } + d->events = eventListFromJson(obj.value("chunk").toArray()); d->end = obj.value("end").toString(); emitResult(); -}
\ No newline at end of file +} |