summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-29 15:42:40 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-29 15:42:40 +0300
commit95120c602b2888c244eb15b475483c523a3d4dbd (patch)
tree08eb9b05ee24210808789cb1c416ff2dc1ed4a97 /include
parentInstall qt5-svg in travis (diff)
downloadnheko-95120c602b2888c244eb15b475483c523a3d4dbd.tar.xz
Refactor utils::getMessageDescription
Diffstat (limited to 'include')
-rw-r--r--include/Cache.h3
-rw-r--r--include/RoomList.h3
-rw-r--r--include/Utils.h63
-rw-r--r--include/timeline/TimelineItem.h27
4 files changed, 74 insertions, 22 deletions
diff --git a/include/Cache.h b/include/Cache.h
index a3e2c6c1..a92f6bc5 100644
--- a/include/Cache.h
+++ b/include/Cache.h
@@ -19,12 +19,11 @@
 
 #include <QDebug>
 #include <QDir>
+#include <QImage>
 #include <json.hpp>
 #include <lmdb++.h>
 #include <mtx/responses.hpp>
 
-#include "Utils.h"
-
 struct SearchResult
 {
         QString user_id;
diff --git a/include/RoomList.h b/include/RoomList.h
index a2df0380..bdfb7218 100644
--- a/include/RoomList.h
+++ b/include/RoomList.h
@@ -23,7 +23,6 @@
 #include <QVBoxLayout>
 #include <QWidget>
 
-#include "Cache.h"
 #include <mtx.hpp>
 
 class LeaveRoomDialog;
@@ -32,7 +31,9 @@ class OverlayModal;
 class RoomInfoListItem;
 class Sync;
 class UserSettings;
+class Cache;
 struct DescInfo;
+struct RoomInfo;
 
 class RoomList : public QWidget
 {
diff --git a/include/Utils.h b/include/Utils.h
index 35c5eec1..c9dc460a 100644
--- a/include/Utils.h
+++ b/include/Utils.h
@@ -1,6 +1,11 @@
 #pragma once
 
+#include "Cache.h"
 #include "RoomInfoListItem.h"
+#include "timeline/widgets/AudioItem.h"
+#include "timeline/widgets/FileItem.h"
+#include "timeline/widgets/ImageItem.h"
+#include "timeline/widgets/VideoItem.h"
 
 #include <QDateTime>
 #include <mtx/events/collections.hpp>
@@ -27,6 +32,64 @@ firstChar(const QString &input);
 QString
 humanReadableFileSize(uint64_t bytes);
 
+//! Match widgets/events with a description message.
+template<class T>
+QString
+messageDescription(const QString &username = "", const QString &body = "")
+{
+        using Audio   = mtx::events::RoomEvent<mtx::events::msg::Audio>;
+        using Emote   = mtx::events::RoomEvent<mtx::events::msg::Emote>;
+        using File    = mtx::events::RoomEvent<mtx::events::msg::File>;
+        using Image   = mtx::events::RoomEvent<mtx::events::msg::Image>;
+        using Notice  = mtx::events::RoomEvent<mtx::events::msg::Notice>;
+        using Sticker = mtx::events::Sticker;
+        using Text    = mtx::events::RoomEvent<mtx::events::msg::Text>;
+        using Video   = mtx::events::RoomEvent<mtx::events::msg::Video>;
+
+        if (std::is_same<T, AudioItem>::value || std::is_same<T, Audio>::value)
+                return QString("sent an audio clip");
+        else if (std::is_same<T, ImageItem>::value || std::is_same<T, Image>::value)
+                return QString("sent an image");
+        else if (std::is_same<T, FileItem>::value || std::is_same<T, File>::value)
+                return QString("sent a file");
+        else if (std::is_same<T, VideoItem>::value || std::is_same<T, Video>::value)
+                return QString("sent a video clip");
+        else if (std::is_same<T, StickerItem>::value || std::is_same<T, Sticker>::value)
+                return QString("sent a sticker");
+        else if (std::is_same<T, Notice>::value)
+                return QString("sent a notification");
+        else if (std::is_same<T, Text>::value)
+                return QString(": %1").arg(body);
+        else if (std::is_same<T, Emote>::value)
+                return QString("* %1 %2").arg(username).arg(body);
+}
+
+template<class T, class Event>
+DescInfo
+createDescriptionInfo(const Event &event, const QString &localUser, const QString &room_id)
+{
+        using Text  = mtx::events::RoomEvent<mtx::events::msg::Text>;
+        using Emote = mtx::events::RoomEvent<mtx::events::msg::Emote>;
+
+        const auto msg    = mpark::get<T>(event);
+        const auto sender = QString::fromStdString(msg.sender);
+
+        const auto username = Cache::displayName(room_id, sender);
+        const auto ts       = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
+
+        bool isText  = std::is_same<T, Text>::value;
+        bool isEmote = std::is_same<T, Emote>::value;
+
+        return DescInfo{
+          isEmote ? "" : (sender == localUser ? "You" : username),
+          sender,
+          (isText || isEmote)
+            ? messageDescription<T>(username, QString::fromStdString(msg.content.body).trimmed())
+            : QString(" %1").arg(messageDescription<T>()),
+          utils::descriptiveTime(ts),
+          ts};
+}
+
 //! Scale down an image to fit to the given width & height limitations.
 template<class ImageType>
 ImageType
diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h
index ac512ab8..24d7726b 100644
--- a/include/timeline/TimelineItem.h
+++ b/include/timeline/TimelineItem.h
@@ -26,11 +26,12 @@
 #include <QStyleOption>
 
 #include "AvatarProvider.h"
-#include "Cache.h"
 #include "ChatPage.h"
 #include "RoomInfoListItem.h"
 #include "Utils.h"
 
+#include "Cache.h"
+
 class ImageItem;
 class StickerItem;
 class AudioItem;
@@ -136,16 +137,10 @@ private:
         void addSaveImageAction(ImageItem *image);
 
         template<class Widget>
-        void setupLocalWidgetLayout(Widget *widget,
-                                    const QString &userid,
-                                    const QString &msgDescription,
-                                    bool withSender);
+        void setupLocalWidgetLayout(Widget *widget, const QString &userid, bool withSender);
 
         template<class Event, class Widget>
-        void setupWidgetLayout(Widget *widget,
-                               const Event &event,
-                               const QString &msgDescription,
-                               bool withSender);
+        void setupWidgetLayout(Widget *widget, const Event &event, bool withSender);
 
         void generateBody(const QString &body);
         void generateBody(const QString &userid, const QString &body);
@@ -184,17 +179,14 @@ private:
 
 template<class Widget>
 void
-TimelineItem::setupLocalWidgetLayout(Widget *widget,
-                                     const QString &userid,
-                                     const QString &msgDescription,
-                                     bool withSender)
+TimelineItem::setupLocalWidgetLayout(Widget *widget, const QString &userid, bool withSender)
 {
         auto displayName = Cache::displayName(room_id_, userid);
         auto timestamp   = QDateTime::currentDateTime();
 
         descriptionMsg_ = {"You",
                            userid,
-                           QString(" %1").arg(msgDescription),
+                           QString(" %1").arg(utils::messageDescription<Widget>()),
                            utils::descriptiveTime(timestamp),
                            timestamp};
 
@@ -227,10 +219,7 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget,
 
 template<class Event, class Widget>
 void
-TimelineItem::setupWidgetLayout(Widget *widget,
-                                const Event &event,
-                                const QString &msgDescription,
-                                bool withSender)
+TimelineItem::setupWidgetLayout(Widget *widget, const Event &event, bool withSender)
 {
         init();
 
@@ -243,7 +232,7 @@ TimelineItem::setupWidgetLayout(Widget *widget,
         QSettings settings;
         descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName,
                            sender,
-                           msgDescription,
+                           QString(" %1").arg(utils::messageDescription<Widget>()),
                            utils::descriptiveTime(timestamp),
                            timestamp};