aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2016-08-24 18:01:21 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2016-08-24 18:01:21 +0900
commit596533a1840961857a51652f89c3a51435794d02 (patch)
tree5a202a92bd69bd1e6c429bb95cf04a5e6340afaa
parent51dfcc732301b5fe0d8b8b5966b751ff35295ef4 (diff)
downloadlibquotient-596533a1840961857a51652f89c3a51435794d02.tar.gz
libquotient-596533a1840961857a51652f89c3a51435794d02.zip
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".
-rw-r--r--events/roommessageevent.cpp32
-rw-r--r--events/roommessageevent.h10
2 files changed, 35 insertions, 7 deletions
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 <QtCore/QJsonObject>
+#include <QtCore/QMimeDatabase>
#include <QtCore/QDebug>
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());
+ }
+}
diff --git a/events/roommessageevent.h b/events/roommessageevent.h
index af3b6797..7b6394ba 100644
--- a/events/roommessageevent.h
+++ b/events/roommessageevent.h
@@ -20,6 +20,7 @@
#define QMATRIXCLIENT_ROOMMESSAGEEVENT_H
#include <QtCore/QUrl>
+#include <QtCore/QMimeType>
#include "event.h"
@@ -65,6 +66,15 @@ namespace QMatrixClient
namespace MessageEventContent
{
+ class TextContent: public Base
+ {
+ public:
+ TextContent(const QJsonObject& json);
+
+ QMimeType mimeType;
+ QString body;
+ };
+
class ImageContent: public Base
{
public: