aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/roommessageevent.cpp53
-rw-r--r--lib/events/roommessageevent.h8
2 files changed, 61 insertions, 0 deletions
diff --git a/lib/events/roommessageevent.cpp b/lib/events/roommessageevent.cpp
index 69969c0f..d63ae2fe 100644
--- a/lib/events/roommessageevent.cpp
+++ b/lib/events/roommessageevent.cpp
@@ -21,6 +21,8 @@
#include "logging.h"
#include <QtCore/QMimeDatabase>
+#include <QtCore/QFileInfo>
+#include <QtGui/QImageReader>
using namespace QMatrixClient;
using namespace EventContent;
@@ -95,6 +97,38 @@ RoomMessageEvent::RoomMessageEvent(const QString& plainBody,
: RoomMessageEvent(plainBody, msgTypeToJson(msgType), content)
{ }
+TypedBase* contentFromFile(const QFileInfo& file, bool asGenericFile)
+{
+ auto filePath = file.absoluteFilePath();
+ auto localUrl = QUrl::fromLocalFile(filePath);
+ auto mimeType = QMimeDatabase().mimeTypeForFile(file);
+ auto payloadSize = file.size();
+ if (!asGenericFile)
+ {
+ auto mimeTypeName = mimeType.name();
+ if (mimeTypeName.startsWith("image/"))
+ return new ImageContent(localUrl, payloadSize, mimeType,
+ QImageReader(filePath).size());
+
+ // duration can only be obtained asynchronously and can only be reliably
+ // done by starting to play the file. Left for a future implementation.
+ if (mimeTypeName.startsWith("video/"))
+ return new VideoContent(localUrl, payloadSize, mimeType);
+
+ if (mimeTypeName.startsWith("audio/"))
+ return new AudioContent(localUrl, payloadSize, mimeType);
+
+ }
+ return new FileContent(localUrl, payloadSize, mimeType);
+}
+
+RoomMessageEvent::RoomMessageEvent(const QString& plainBody,
+ const QFileInfo& file, bool asGenericFile)
+ : RoomMessageEvent(plainBody,
+ asGenericFile ? QStringLiteral("m.file") : rawMsgTypeForFile(file),
+ contentFromFile(file, asGenericFile))
+{ }
+
RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj)
: RoomEvent(typeId(), obj), _content(nullptr)
{
@@ -162,6 +196,25 @@ bool RoomMessageEvent::hasThumbnail() const
return content() && content()->thumbnailInfo();
}
+QString rawMsgTypeForMimeType(const QMimeType& mimeType)
+{
+ auto name = mimeType.name();
+ return name.startsWith("image/") ? QStringLiteral("m.image") :
+ name.startsWith("video/") ? QStringLiteral("m.video") :
+ name.startsWith("audio/") ? QStringLiteral("m.audio") :
+ QStringLiteral("m.file");
+}
+
+QString RoomMessageEvent::rawMsgTypeForUrl(const QUrl& url)
+{
+ return rawMsgTypeForMimeType(QMimeDatabase().mimeTypeForUrl(url));
+}
+
+QString RoomMessageEvent::rawMsgTypeForFile(const QFileInfo& fi)
+{
+ return rawMsgTypeForMimeType(QMimeDatabase().mimeTypeForFile(fi));
+}
+
TextContent::TextContent(const QString& text, const QString& contentType)
: mimeType(QMimeDatabase().mimeTypeForName(contentType)), body(text)
{
diff --git a/lib/events/roommessageevent.h b/lib/events/roommessageevent.h
index 5657135b..d5b570f5 100644
--- a/lib/events/roommessageevent.h
+++ b/lib/events/roommessageevent.h
@@ -21,6 +21,8 @@
#include "roomevent.h"
#include "eventcontent.h"
+class QFileInfo;
+
namespace QMatrixClient
{
namespace MessageEventContent = EventContent; // Back-compatibility
@@ -49,6 +51,9 @@ namespace QMatrixClient
explicit RoomMessageEvent(const QString& plainBody,
MsgType msgType = MsgType::Text,
EventContent::TypedBase* content = nullptr);
+ explicit RoomMessageEvent(const QString& plainBody,
+ const QFileInfo& file,
+ bool asGenericFile = false);
explicit RoomMessageEvent(const QJsonObject& obj);
MsgType msgtype() const;
@@ -68,6 +73,9 @@ namespace QMatrixClient
bool hasFileContent() const;
bool hasThumbnail() const;
+ static QString rawMsgTypeForUrl(const QUrl& url);
+ static QString rawMsgTypeForFile(const QFileInfo& fi);
+
private:
QScopedPointer<EventContent::TypedBase> _content;