From 596533a1840961857a51652f89c3a51435794d02 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 24 Aug 2016 18:01:21 +0900 Subject: Introduce TextContent + minor cleanup TextContent is a class to deal with formatted (HTML, RTF, Markdown) text messages. Right now it only supports Vector's non-standard "formatted_body". --- events/roommessageevent.cpp | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'events/roommessageevent.cpp') diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp index f02922b5..0da35527 100644 --- a/events/roommessageevent.cpp +++ b/events/roommessageevent.cpp @@ -19,6 +19,7 @@ #include "roommessageevent.h" #include +#include #include using namespace QMatrixClient; @@ -26,7 +27,7 @@ using namespace QMatrixClient; class RoomMessageEvent::Private { public: - Private() {} + Private() : msgtype(MessageEventType::Unknown), content(nullptr) {} QString userId; MessageEventType msgtype; @@ -37,9 +38,7 @@ class RoomMessageEvent::Private RoomMessageEvent::RoomMessageEvent() : Event(EventType::RoomMessage) , d(new Private) -{ - d->content = nullptr; -} +{ } RoomMessageEvent::~RoomMessageEvent() { @@ -91,17 +90,17 @@ RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) if( msgtype == "m.text" ) { e->d->msgtype = MessageEventType::Text; - e->d->content = new Base(); + e->d->content = new TextContent(obj); } else if( msgtype == "m.emote" ) { e->d->msgtype = MessageEventType::Emote; - e->d->content = new Base(); + e->d->content = new TextContent(obj); } else if( msgtype == "m.notice" ) { e->d->msgtype = MessageEventType::Notice; - e->d->content = new Base(); + e->d->content = new TextContent(obj); } else if( msgtype == "m.image" ) { @@ -185,3 +184,22 @@ RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) } return e; } + +using namespace MessageEventContent; + +TextContent::TextContent(const QJsonObject& json) +{ + QMimeDatabase db; + + // Special-casing the custom matrix.org's (actually, Vector's) way + // of sending HTML messages. + if (json["format"].toString() == "org.matrix.custom.html") + { + mimeType = db.mimeTypeForName("text/html"); + body = json["formatted_body"].toString(); + } else { + // Best-guessing from the content + body = json["body"].toString(); + mimeType = db.mimeTypeForData(body.toUtf8()); + } +} -- cgit v1.2.3