diff options
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | events/event.h | 18 | ||||
-rw-r--r-- | events/roomaliasesevent.cpp | 2 | ||||
-rw-r--r-- | events/roommessageevent.cpp | 111 | ||||
-rw-r--r-- | events/roommessageevent.h | 75 | ||||
-rw-r--r-- | events/roomnameevent.cpp | 3 | ||||
-rw-r--r-- | jobs/basejob.cpp | 4 | ||||
-rw-r--r-- | room.cpp | 23 | ||||
-rw-r--r-- | room.h | 2 |
9 files changed, 209 insertions, 33 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bb5a47cb..32792435 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,10 @@ if ( CMAKE_VERSION VERSION_LESS "3.1" ) else ( CMAKE_VERSION VERSION_LESS "3.1" ) target_compile_features(qmatrixclient PRIVATE cxx_range_for) target_compile_features(qmatrixclient PRIVATE cxx_override) + target_compile_features(qmatrixclient PRIVATE cxx_strong_enums) + target_compile_features(qmatrixclient PRIVATE cxx_lambdas) + target_compile_features(qmatrixclient PRIVATE cxx_auto_type) + target_compile_features(qmatrixclient PRIVATE cxx_generalized_initializers) endif ( CMAKE_VERSION VERSION_LESS "3.1" ) target_link_libraries(qmatrixclient Qt5::Core Qt5::Network Qt5::Gui) diff --git a/events/event.h b/events/event.h index b25b1378..6a8d0e89 100644 --- a/events/event.h +++ b/events/event.h @@ -19,6 +19,8 @@ #ifndef QMATRIXCLIENT_EVENT_H #define QMATRIXCLIENT_EVENT_H +#include <algorithm> + #include <QtCore/QString> #include <QtCore/QDateTime> #include <QtCore/QJsonObject> @@ -53,6 +55,22 @@ namespace QMatrixClient class Private; Private* d; }; + + /** + * 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 + * the one of 'item'; or timeline.end(), if all events are earlier + */ + template <class ItemT, template <typename> class ContT> + typename ContT<ItemT *>::iterator + findInsertionPos(ContT<ItemT *> & timeline, const ItemT *item) + { + return std::lower_bound (timeline.begin(), timeline.end(), item, + [](const ItemT * a, const ItemT * b) { + return a->timestamp() < b->timestamp(); + } + ); + } } #endif // QMATRIXCLIENT_EVENT_H diff --git a/events/roomaliasesevent.cpp b/events/roomaliasesevent.cpp index 6bbe0aff..54fb5682 100644 --- a/events/roomaliasesevent.cpp +++ b/events/roomaliasesevent.cpp @@ -64,7 +64,7 @@ QStringList RoomAliasesEvent::aliases() const RoomAliasesEvent* RoomAliasesEvent::fromJson(const QJsonObject& obj) { - qDebug() << "!!!!"; + qDebug() << "RoomAliasesEvent::fromJson"; RoomAliasesEvent* e = new RoomAliasesEvent(); e->parseJson(obj); const QJsonObject contents = obj.value("content").toObject(); diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp index ea03986b..99d17e5c 100644 --- a/events/roommessageevent.cpp +++ b/events/roommessageevent.cpp @@ -30,16 +30,16 @@ class RoomMessageEvent::Private Private() {} QString userId; - QString body; - QString msgtype; + MessageEventType msgtype; QDateTime hsob_ts; + MessageEventContent* content; }; RoomMessageEvent::RoomMessageEvent() : Event(EventType::RoomMessage) , d(new Private) { - + d->content = 0; } RoomMessageEvent::~RoomMessageEvent() @@ -52,14 +52,14 @@ QString RoomMessageEvent::userId() const return d->userId; } -QString RoomMessageEvent::msgtype() const +MessageEventType RoomMessageEvent::msgtype() const { return d->msgtype; } QString RoomMessageEvent::body() const { - return d->body; + return d->content->body; } QDateTime RoomMessageEvent::hsob_ts() const @@ -67,6 +67,11 @@ QDateTime RoomMessageEvent::hsob_ts() const return d->hsob_ts; } +MessageEventContent* RoomMessageEvent::content() const +{ + return d->content; +} + RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) { RoomMessageEvent* e = new RoomMessageEvent(); @@ -80,15 +85,99 @@ RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) if( obj.contains("content") ) { QJsonObject content = obj.value("content").toObject(); - if( content.contains("msgtype") ) + QString msgtype = content.value("msgtype").toString(); + + if( msgtype == "m.text" ) { - e->d->msgtype = content.value("msgtype").toString(); - } else { - qDebug() << "RoomMessageEvent: msgtype not found"; + e->d->msgtype = MessageEventType::Text; + e->d->content = new MessageEventContent(); + } + else if( msgtype == "m.emote" ) + { + e->d->msgtype = MessageEventType::Emote; + e->d->content = new MessageEventContent(); + } + else if( msgtype == "m.notice" ) + { + e->d->msgtype = MessageEventType::Notice; + e->d->content = new MessageEventContent(); + } + else if( msgtype == "m.image" ) + { + e->d->msgtype = MessageEventType::Image; + ImageEventContent* c = new ImageEventContent; + c->url = QUrl(content.value("url").toString()); + QJsonObject info = content.value("info").toObject(); + c->height = info.value("h").toInt(); + c->width = info.value("w").toInt(); + c->size = info.value("size").toInt(); + c->mimetype = info.value("mimetype").toString(); + e->d->content = c; } + else if( msgtype == "m.file" ) + { + e->d->msgtype = MessageEventType::File; + FileEventContent* c = new FileEventContent; + c->filename = content.value("filename").toString(); + c->url = QUrl(content.value("url").toString()); + QJsonObject info = content.value("info").toObject(); + c->size = info.value("size").toInt(); + c->mimetype = info.value("mimetype").toString(); + e->d->content = c; + } + else if( msgtype == "m.location" ) + { + e->d->msgtype = MessageEventType::Location; + LocationEventContent* c = new LocationEventContent; + c->geoUri = content.value("geo_uri").toString(); + c->thumbnailUrl = QUrl(content.value("thumbnail_url").toString()); + QJsonObject info = content.value("thumbnail_info").toObject(); + c->thumbnailHeight = info.value("h").toInt(); + c->thumbnailWidth = info.value("w").toInt(); + c->thumbnailSize = info.value("size").toInt(); + c->thumbnailMimetype = info.value("mimetype").toString(); + e->d->content = c; + } + else if( msgtype == "m.video" ) + { + e->d->msgtype = MessageEventType::Video; + VideoEventContent* c = new VideoEventContent; + c->url = QUrl(content.value("url").toString()); + QJsonObject info = content.value("info").toObject(); + c->height = info.value("h").toInt(); + c->width = info.value("w").toInt(); + c->duration = info.value("duration").toInt(); + c->size = info.value("size").toInt(); + c->thumbnailUrl = QUrl(info.value("thumnail_url").toString()); + QJsonObject thumbnailInfo = content.value("thumbnail_info").toObject(); + c->thumbnailHeight = thumbnailInfo.value("h").toInt(); + c->thumbnailWidth = thumbnailInfo.value("w").toInt(); + c->thumbnailSize = thumbnailInfo.value("size").toInt(); + c->thumbnailMimetype = thumbnailInfo.value("mimetype").toString(); + e->d->content = c; + } + else if( msgtype == "m.audio" ) + { + e->d->msgtype = MessageEventType::Audio; + AudioEventContent* c = new AudioEventContent; + c->url = QUrl(content.value("url").toString()); + QJsonObject info = content.value("info").toObject(); + c->duration = info.value("duration").toInt(); + c->mimetype = info.value("mimetype").toString(); + c->size = info.value("size").toInt(); + e->d->content = c; + } + else + { + qDebug() << "RoomMessageEvent: unknown msgtype: " << msgtype; + qDebug() << obj; + e->d->msgtype = MessageEventType::Unkown; + e->d->content = new MessageEventContent; + } + if( content.contains("body") ) { - e->d->body = content.value("body").toString(); + e->d->content->body = content.value("body").toString(); } else { qDebug() << "RoomMessageEvent: body not found"; } @@ -98,4 +187,4 @@ RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) // } } return e; -}
\ No newline at end of file +} diff --git a/events/roommessageevent.h b/events/roommessageevent.h index dcc5b547..939113d1 100644 --- a/events/roommessageevent.h +++ b/events/roommessageevent.h @@ -19,10 +19,25 @@ #ifndef QMATRIXCLIENT_ROOMMESSAGEEVENT_H #define QMATRIXCLIENT_ROOMMESSAGEEVENT_H +#include <QtCore/QUrl> + #include "event.h" namespace QMatrixClient { + enum class MessageEventType + { + Text, Emote, Notice, Image, File, Location, Video, Audio, Unkown + }; + + class MessageEventContent + { + public: + virtual ~MessageEventContent() {} + + QString body; + }; + class RoomMessageEvent: public Event { public: @@ -30,9 +45,11 @@ namespace QMatrixClient virtual ~RoomMessageEvent(); QString userId() const; - QString msgtype() const; + MessageEventType msgtype() const; QString body() const; QDateTime hsob_ts() const; + + MessageEventContent* content() const; static RoomMessageEvent* fromJson( const QJsonObject& obj ); @@ -40,6 +57,62 @@ namespace QMatrixClient class Private; Private* d; }; + + class ImageEventContent: public MessageEventContent + { + public: + QUrl url; + int height; + int width; + int size; + QString mimetype; + }; + + class FileEventContent: public MessageEventContent + { + public: + QString filename; + QString mimetype; + int size; + QUrl url; + }; + + class LocationEventContent: public MessageEventContent + { + public: + QString geoUri; + int thumbnailHeight; + int thumbnailWidth; + QString thumbnailMimetype; + int thumbnailSize; + QUrl thumbnailUrl; + }; + + class VideoEventContent: public MessageEventContent + { + public: + QUrl url; + int duration; + int width; + int height; + int size; + QString mimetype; + int thumbnailWidth; + int thumbnailHeight; + int thumbnailSize; + QString thumbnailMimetype; + QUrl thumbnailUrl; + }; + + class AudioEventContent: public MessageEventContent + { + public: + QUrl url; + int size; + int duration; + QString mimetype; + }; + } #endif // QMATRIXCLIENT_ROOMMESSAGEEVENT_H
\ No newline at end of file diff --git a/events/roomnameevent.cpp b/events/roomnameevent.cpp index f4c31edf..c5bcf011 100644 --- a/events/roomnameevent.cpp +++ b/events/roomnameevent.cpp @@ -20,8 +20,9 @@ using namespace QMatrixClient;
-struct RoomNameEvent::Private
+class RoomNameEvent::Private
{
+public:
QString name;
};
diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 86109feb..519e1517 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -81,7 +81,7 @@ void BaseJob::start() QNetworkRequest req = QNetworkRequest(url); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) - req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);\ + req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); req.setMaximumRedirectsAllowed(10); #endif QJsonDocument data = QJsonDocument(this->data()); @@ -125,7 +125,7 @@ void BaseJob::gotReply() { if( d->reply->error() != QNetworkReply::NoError ) { - qDebug() << "NetworkError:" << d->reply->errorString(); + qDebug() << "NetworkError!!!" << d->reply->error(); fail( NetworkError, d->reply->errorString() ); return; } @@ -71,14 +71,14 @@ Room::Room(Connection* connection, QString id) d->connection = connection; d->joinState = JoinState::Join; d->gettingNewContent = false; - qDebug() << "New Room: " << id; + qDebug() << "New Room:" << id; //connection->getMembers(this); // I don't think we need this anymore in r0.0.1 } Room::~Room() { - qDebug() << "deconstructing room " << this; + qDebug() << "deconstructing room" << this; delete d; } @@ -87,7 +87,7 @@ QString Room::id() const return d->id; } -QList< Event* > Room::messages() const +QList< Event* > Room::messageEvents() const { return d->messageEvents; } @@ -284,15 +284,7 @@ Connection* Room::connection() void Room::processMessageEvent(Event* event) { - for( int i=0; i<d->messageEvents.count(); i++ ) - { - if( event->timestamp() < d->messageEvents.at(i)->timestamp() ) - { - d->messageEvents.insert(i, event); - return; - } - } - d->messageEvents.append(event); + d->messageEvents.insert(findInsertionPos(d->messageEvents, event), event); } void Room::processStateEvent(Event* event) @@ -302,7 +294,7 @@ void Room::processStateEvent(Event* event) if (RoomNameEvent* nameEvent = static_cast<RoomNameEvent*>(event)) { d->name = nameEvent->name(); - qDebug() << "room name: " << d->name; + qDebug() << "room name:" << d->name; emit namesChanged(this); } else { @@ -314,14 +306,14 @@ void Room::processStateEvent(Event* event) { RoomAliasesEvent* aliasesEvent = static_cast<RoomAliasesEvent*>(event); d->aliases = aliasesEvent->aliases(); - qDebug() << "room aliases: " << d->aliases; + qDebug() << "room aliases:" << d->aliases; emit namesChanged(this); } if( event->type() == EventType::RoomCanonicalAlias ) { RoomCanonicalAliasEvent* aliasEvent = static_cast<RoomCanonicalAliasEvent*>(event); d->canonicalAlias = aliasEvent->alias(); - qDebug() << "room canonical alias: " << d->canonicalAlias; + qDebug() << "room canonical alias:" << d->canonicalAlias; emit namesChanged(this); } if( event->type() == EventType::RoomTopic ) @@ -334,7 +326,6 @@ void Room::processStateEvent(Event* event) { RoomMemberEvent* memberEvent = static_cast<RoomMemberEvent*>(event); User* u = d->connection->user(memberEvent->userId()); - if( !u ) qDebug() << "addState: invalid user!" << u << memberEvent->userId(); u->processEvent(event); if( memberEvent->membership() == MembershipType::Join and !d->users.contains(u) ) { @@ -41,7 +41,7 @@ namespace QMatrixClient virtual ~Room(); QString id() const; - QList<Event*> messages() const; + QList<Event*> messageEvents() const; QString name() const; QStringList aliases() const; QString canonicalAlias() const; |