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
This commit is contained in:
parent
c8bfb02211
commit
cd9d1a2ec6
25 changed files with 552 additions and 399 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue