summary refs log tree commit diff
path: root/include/timeline
diff options
context:
space:
mode:
authorchristarazi <christarazi@users.noreply.github.com>2018-02-18 12:52:31 -0800
committermujx <mujx@users.noreply.github.com>2018-02-18 22:52:31 +0200
commitcd9d1a2ec69bf4c459f9e845293d5fd7dc1398d4 (patch)
treea3669f25c8ecaf716061b3080ae9f9073134da05 /include/timeline
parentShow loading indicator while waiting for /login & /logout (diff)
downloadnheko-cd9d1a2ec69bf4c459f9e845293d5fd7dc1398d4.tar.xz
Support audio, video, generic file for pasting (#220)
* Refactor widget items to use same interface

* Support audio, video, generic file for pasting

* Add utils function for human readable file sizes

* Set correct MIME type for media messages

This change also determines the size of the upload once from the
ContentLengthHeader, rather than seeking the QIODevice and asking for
its size. This prevents any future trouble in case the QIODevice is
sequential (cannot be seeked). The MIME type is also determined at
upload once, rather than using the QIODevice and the underlying data
inside.

* Allow for file urls to be used as fall-back

This fixes an issue on macOS which uses `text/uri-list` for copying
files to the clipboard.

fixes #228 
Diffstat (limited to 'include/timeline')
-rw-r--r--include/timeline/TimelineView.h22
-rw-r--r--include/timeline/TimelineViewManager.h22
-rw-r--r--include/timeline/widgets/AudioItem.h3
-rw-r--r--include/timeline/widgets/FileItem.h3
-rw-r--r--include/timeline/widgets/ImageItem.h2
-rw-r--r--include/timeline/widgets/VideoItem.h3
6 files changed, 37 insertions, 18 deletions
diff --git a/include/timeline/TimelineView.h b/include/timeline/TimelineView.h
index 6e1287c8..782f3afd 100644
--- a/include/timeline/TimelineView.h
+++ b/include/timeline/TimelineView.h
@@ -44,6 +44,8 @@ struct PendingMessage
         int txn_id;
         QString body;
         QString filename;
+        QString mime;
+        int64_t media_size;
         QString event_id;
         TimelineItem *widget;
 
@@ -51,12 +53,16 @@ struct PendingMessage
                        int txn_id,
                        QString body,
                        QString filename,
+                       QString mime,
+                       int64_t media_size,
                        QString event_id,
                        TimelineItem *widget)
           : ty(ty)
           , txn_id(txn_id)
           , body(body)
           , filename(filename)
+          , mime(mime)
+          , media_size(media_size)
           , event_id(event_id)
           , widget(widget)
         {}
@@ -87,10 +93,10 @@ public:
         void addUserMessage(mtx::events::MessageType ty, const QString &msg);
 
         template<class Widget, mtx::events::MessageType MsgType>
-        void addUserMessage(
-          const QString &url,
-          const QString &filename,
-          const QSharedPointer<QIODevice> data = QSharedPointer<QIODevice>(nullptr));
+        void addUserMessage(const QString &url,
+                            const QString &filename,
+                            const QString &mime,
+                            const int64_t size);
         void updatePendingMessage(int txn_id, QString event_id);
         void scrollDown();
         QLabel *createDateSeparator(QDateTime datetime);
@@ -236,11 +242,13 @@ template<class Widget, mtx::events::MessageType MsgType>
 void
 TimelineView::addUserMessage(const QString &url,
                              const QString &filename,
-                             const QSharedPointer<QIODevice> data)
+                             const QString &mime,
+                             const int64_t size)
 {
         auto with_sender = lastSender_ != local_user_;
+        auto trimmed     = QFileInfo{filename}.fileName(); // Trim file path.
 
-        auto widget = new Widget(client_, url, data, filename, this);
+        auto widget = new Widget(client_, url, trimmed, size, this);
 
         TimelineItem *view_item =
           new TimelineItem(widget, local_user_, with_sender, scroll_widget_);
@@ -255,7 +263,7 @@ TimelineView::addUserMessage(const QString &url,
 
         int txn_id = client_->incrementTransactionId();
 
-        PendingMessage message(MsgType, txn_id, url, filename, "", view_item);
+        PendingMessage message(MsgType, txn_id, url, trimmed, mime, size, "", view_item);
         handleNewUserMessage(message);
 }
 
diff --git a/include/timeline/TimelineViewManager.h b/include/timeline/TimelineViewManager.h
index 74d8588c..a531be88 100644
--- a/include/timeline/TimelineViewManager.h
+++ b/include/timeline/TimelineViewManager.h
@@ -64,11 +64,25 @@ public slots:
         void queueTextMessage(const QString &msg);
         void queueEmoteMessage(const QString &msg);
         void queueImageMessage(const QString &roomid,
-                               const QSharedPointer<QIODevice> data,
                                const QString &filename,
-                               const QString &url);
-        void queueFileMessage(const QString &roomid, const QString &filename, const QString &url);
-        void queueAudioMessage(const QString &roomid, const QString &filename, const QString &url);
+                               const QString &url,
+                               const QString &mime,
+                               const int64_t dsize);
+        void queueFileMessage(const QString &roomid,
+                              const QString &filename,
+                              const QString &url,
+                              const QString &mime,
+                              const int64_t dsize);
+        void queueAudioMessage(const QString &roomid,
+                               const QString &filename,
+                               const QString &url,
+                               const QString &mime,
+                               const int64_t dsize);
+        void queueVideoMessage(const QString &roomid,
+                               const QString &filename,
+                               const QString &url,
+                               const QString &mime,
+                               const int64_t dsize);
 
 private slots:
         void messageSent(const QString &eventid, const QString &roomid, int txnid);
diff --git a/include/timeline/widgets/AudioItem.h b/include/timeline/widgets/AudioItem.h
index ca81f498..06f4b8ff 100644
--- a/include/timeline/widgets/AudioItem.h
+++ b/include/timeline/widgets/AudioItem.h
@@ -48,8 +48,8 @@ public:
 
         AudioItem(QSharedPointer<MatrixClient> client,
                   const QString &url,
-                  const QSharedPointer<QIODevice> data,
                   const QString &filename,
+                  const int64_t size,
                   QWidget *parent = nullptr);
 
         QSize sizeHint() const override;
@@ -76,7 +76,6 @@ private slots:
         void fileDownloaded(const QString &event_id, const QByteArray &data);
 
 private:
-        QString calculateFileSize(int nbytes) const;
         void init();
 
         enum class AudioState
diff --git a/include/timeline/widgets/FileItem.h b/include/timeline/widgets/FileItem.h
index 72589189..36d72f25 100644
--- a/include/timeline/widgets/FileItem.h
+++ b/include/timeline/widgets/FileItem.h
@@ -42,8 +42,8 @@ public:
 
         FileItem(QSharedPointer<MatrixClient> client,
                  const QString &url,
-                 const QSharedPointer<QIODevice> data,
                  const QString &filename,
+                 const int64_t size,
                  QWidget *parent = nullptr);
 
         QSize sizeHint() const override;
@@ -64,7 +64,6 @@ private slots:
         void fileDownloaded(const QString &event_id, const QByteArray &data);
 
 private:
-        QString calculateFileSize(int nbytes) const;
         void openUrl();
         void init();
 
diff --git a/include/timeline/widgets/ImageItem.h b/include/timeline/widgets/ImageItem.h
index d24b7239..9e5fa82b 100644
--- a/include/timeline/widgets/ImageItem.h
+++ b/include/timeline/widgets/ImageItem.h
@@ -36,8 +36,8 @@ public:
 
         ImageItem(QSharedPointer<MatrixClient> client,
                   const QString &url,
-                  const QSharedPointer<QIODevice> data,
                   const QString &filename,
+                  const int64_t size,
                   QWidget *parent = nullptr);
 
         void setImage(const QPixmap &image);
diff --git a/include/timeline/widgets/VideoItem.h b/include/timeline/widgets/VideoItem.h
index 53c3e21a..65b87c95 100644
--- a/include/timeline/widgets/VideoItem.h
+++ b/include/timeline/widgets/VideoItem.h
@@ -37,13 +37,12 @@ public:
 
         VideoItem(QSharedPointer<MatrixClient> client,
                   const QString &url,
-                  const QSharedPointer<QIODevice> data,
                   const QString &filename,
+                  const int64_t size,
                   QWidget *parent = nullptr);
 
 private:
         void init();
-        QString calculateFileSize(int nbytes) const;
 
         QUrl url_;
         QString text_;