diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-06-22 16:50:24 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-06-22 17:25:00 +0900 |
commit | 631b322ba2b7e7ffc44b2a1ab653b851be86fd33 (patch) | |
tree | bd4eb7770229ff1cff4c183fffb6323f1eb850c5 /events | |
parent | 2a51e977575387001eb025e09eba77d9afbd9981 (diff) | |
download | libquotient-631b322ba2b7e7ffc44b2a1ab653b851be86fd33.tar.gz libquotient-631b322ba2b7e7ffc44b2a1ab653b851be86fd33.zip |
MessageEventContent: generalise mimeType
mimeType is relevant to most of the content types, and at the same time getting a MIME type in a generic way is handy for clients to uniformly detect whether they can display the content and what renderer to use for it.
Diffstat (limited to 'events')
-rw-r--r-- | events/roommessageevent.cpp | 29 | ||||
-rw-r--r-- | events/roommessageevent.h | 21 |
2 files changed, 32 insertions, 18 deletions
diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp index 7697c5c3..ccaa6226 100644 --- a/events/roommessageevent.cpp +++ b/events/roommessageevent.cpp @@ -92,8 +92,8 @@ RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj) if (_msgtype == MsgType::Unknown) { - qCDebug(EVENTS) << "RoomMessageEvent: unknown msgtype" << msgtype - << ", full content dump follows"; + qCDebug(EVENTS) << "RoomMessageEvent: couldn't load content," + << " full content dump follows"; qCDebug(EVENTS) << formatJson << content; } } @@ -104,6 +104,12 @@ RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj) } } +QMimeType RoomMessageEvent::mimeType() const +{ + return _content ? _content->mimeType : + QMimeDatabase().mimeTypeForName("text/plain"); +} + QJsonObject RoomMessageEvent::toJson() const { QJsonObject obj = _content ? _content->toJson() : QJsonObject(); @@ -112,6 +118,13 @@ QJsonObject RoomMessageEvent::toJson() const return obj; } +QJsonObject Base::toJson() const +{ + QJsonObject o; + fillJson(&o); + return o; +} + QJsonObject InfoBase::toInfoJson() const { QJsonObject info; @@ -120,7 +133,7 @@ QJsonObject InfoBase::toInfoJson() const } TextContent::TextContent(const QString& text, const QString& contentType) - : mimeType(QMimeDatabase().mimeTypeForName(contentType)), body(text) + : Base(QMimeDatabase().mimeTypeForName(contentType)), body(text) { } TextContent::TextContent(const QJsonObject& json) @@ -136,8 +149,8 @@ TextContent::TextContent(const QJsonObject& json) } else { // Falling back to plain text, as there's no standard way to describe // rich text in messages. - body = json["body"].toString(); mimeType = db.mimeTypeForName("text/plain"); + body = json["body"].toString(); } } @@ -150,7 +163,7 @@ void TextContent::fillJson(QJsonObject* json) const FileInfo::FileInfo(const QUrl& u, int payloadSize, const QMimeType& mimeType, const QString& originalFilename) - : url(u), payloadSize(payloadSize), mimetype(mimeType) + : InfoBase(mimeType), url(u), payloadSize(payloadSize) , originalName(originalFilename) { } @@ -160,15 +173,15 @@ FileInfo::FileInfo(const QUrl& u, const QJsonObject& infoJson, QMimeDatabase().mimeTypeForName(infoJson["mimetype"].toString()), originalFilename) { - if (!mimetype.isValid()) - mimetype = QMimeDatabase().mimeTypeForData(QByteArray()); + if (!mimeType.isValid()) + mimeType = QMimeDatabase().mimeTypeForData(QByteArray()); } void FileInfo::fillInfoJson(QJsonObject* infoJson) const { Q_ASSERT(infoJson); infoJson->insert("size", payloadSize); - infoJson->insert("mimetype", mimetype.name()); + infoJson->insert("mimetype", mimeType.name()); } void FileInfo::fillJson(QJsonObject* json) const diff --git a/events/roommessageevent.h b/events/roommessageevent.h index 308ce742..edc11bee 100644 --- a/events/roommessageevent.h +++ b/events/roommessageevent.h @@ -45,14 +45,15 @@ namespace QMatrixClient { public: virtual ~Base() = default; - QJsonObject toJson() const - { - QJsonObject o; - fillJson(&o); - return o; - } + + QJsonObject toJson() const; + + QMimeType mimeType; protected: + Base() = default; + explicit Base(const QMimeType& type) : mimeType(type) { } + virtual void fillJson(QJsonObject* o) const = 0; }; @@ -73,6 +74,8 @@ namespace QMatrixClient QJsonObject toInfoJson() const; protected: + using Base::Base; + virtual void fillInfoJson(QJsonObject* infoJson) const { } }; } // namespace MessageEventContent @@ -106,8 +109,8 @@ namespace QMatrixClient MsgType msgtype() const { return _msgtype; } const QString& plainBody() const { return _plainBody; } - const MessageEventContent::Base* content() const - { return _content.data(); } + const MessageEventContent::Base* content() const { return _content.data(); } + QMimeType mimeType() const; QJsonObject toJson() const; @@ -143,7 +146,6 @@ namespace QMatrixClient void fillJson(QJsonObject* json) const override; - QMimeType mimeType; QString body; }; @@ -165,7 +167,6 @@ namespace QMatrixClient QUrl url; int payloadSize; - QMimeType mimetype; QString originalName; protected: |