1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/******************************************************************************
* Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "roommessageevent.h"
#include "util.h"
#include <QtCore/QMimeDatabase>
#include <QtCore/QDebug>
using namespace QMatrixClient;
class RoomMessageEvent::Private
{
public:
Private() : msgtype(MessageEventType::Unknown), content(nullptr) {}
~Private() { if (content) delete content; }
QString userId;
MessageEventType msgtype;
QString plainBody;
MessageEventContent::Base* content;
};
RoomMessageEvent::RoomMessageEvent()
: Event(EventType::RoomMessage)
, d(new Private)
{ }
RoomMessageEvent::~RoomMessageEvent()
{
delete d;
}
QString RoomMessageEvent::userId() const
{
return d->userId;
}
MessageEventType RoomMessageEvent::msgtype() const
{
return d->msgtype;
}
QString RoomMessageEvent::plainBody() const
{
return d->plainBody;
}
QString RoomMessageEvent::body() const
{
return plainBody();
}
using namespace MessageEventContent;
Base* RoomMessageEvent::content() const
{
return d->content;
}
using ContentPair = std::pair<MessageEventType, MessageEventContent::Base*>;
template <MessageEventType EnumType, typename ContentT>
ContentPair make(const QJsonObject& json)
{
return { EnumType, new ContentT(json) };
}
ContentPair makeVideo(const QJsonObject& json)
{
auto c = new VideoContent(json);
// Only for m.video, the spec puts a thumbnail inside "info" JSON key. Once
// this is fixed, VideoContent creation will switch to make<>().
const QJsonObject infoJson = json["info"].toObject();
if (infoJson.contains("thumbnail_url"))
{
c->thumbnail = ImageInfo(infoJson["thumbnail_url"].toString(),
infoJson["thumbnail_info"].toObject());
}
return { MessageEventType::Video, c };
};
ContentPair makeUnknown(const QJsonObject& json)
{
qCDebug(EVENTS) << "RoomMessageEvent: couldn't resolve msgtype, JSON follows:";
qCDebug(EVENTS) << json;
return { MessageEventType::Unknown, new Base };
}
RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj)
{
RoomMessageEvent* e = new RoomMessageEvent();
e->parseJson(obj);
if( obj.contains("sender") )
{
e->d->userId = obj.value("sender").toString();
} else {
qCDebug(EVENTS) << "RoomMessageEvent: user_id not found";
}
if( obj.contains("content") )
{
const QJsonObject content = obj["content"].toObject();
if ( content.contains("msgtype") && content.contains("body") )
{
e->d->plainBody = content["body"].toString();
auto delegate = lookup(content.value("msgtype").toString(),
"m.text", make<MessageEventType::Text, TextContent>,
"m.emote", make<MessageEventType::Emote, TextContent>,
"m.notice", make<MessageEventType::Notice, TextContent>,
"m.image", make<MessageEventType::Image, ImageContent>,
"m.file", make<MessageEventType::File, FileContent>,
"m.location", make<MessageEventType::Location, LocationContent>,
"m.video", makeVideo,
"m.audio", make<MessageEventType::Audio, AudioContent>,
// Insert new message types before this line
makeUnknown
);
std::tie(e->d->msgtype, e->d->content) = delegate(content);
}
else
{
qCWarning(EVENTS) << "RoomMessageEvent(" << e->id() << "): no body or msgtype";
qCDebug(EVENTS) << 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 {
// 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");
}
}
FileInfo::FileInfo(QUrl u, const QJsonObject& infoJson, QString originalFilename)
: url(u)
, fileSize(infoJson["size"].toInt())
, mimetype(QMimeDatabase().mimeTypeForName(infoJson["mimetype"].toString()))
, originalName(originalFilename)
{
if (!mimetype.isValid())
mimetype = QMimeDatabase().mimeTypeForData(QByteArray());
}
ImageInfo::ImageInfo(QUrl u, const QJsonObject& infoJson)
: FileInfo(u, infoJson)
, imageSize(infoJson["w"].toInt(), infoJson["h"].toInt())
{ }
LocationContent::LocationContent(const QJsonObject& json)
: geoUri(json["geo_uri"].toString())
, thumbnail(json["thumbnail_url"].toString(),
json["thumbnail_info"].toObject())
{ }
VideoInfo::VideoInfo(QUrl u, const QJsonObject& infoJson)
: FileInfo(u, infoJson)
, duration(infoJson["duration"].toInt())
, imageSize(infoJson["w"].toInt(), infoJson["h"].toInt())
{ }
AudioInfo::AudioInfo(QUrl u, const QJsonObject& infoJson)
: FileInfo(u, infoJson)
, duration(infoJson["duration"].toInt())
{ }
|