From 3d2a196830e9a71590100594595d89591c048b06 Mon Sep 17 00:00:00 2001 From: Meru Alagalingam Date: Wed, 16 Dec 2020 11:09:25 +0100 Subject: [PATCH 1/2] FEAT(client): Easier pasting of images Currently pasting or dragging and dropping images in the Mumble chat is difficult because: 1) They are rejected because they exceed the allowed size. As a user I would not expect it to transmitted losslessly but I would hope that the client compresses it to a size the server allows. 2) Images are rejected because the server does not allow html. When inserting an image via drag and drop you aren't notified what went wrong. --- src/mumble/CustomElements.cpp | 59 +++++++++++++++++++---------------- src/mumble/CustomElements.h | 1 + src/mumble/Log.cpp | 24 +++++--------- src/mumble/Log.h | 2 +- 4 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/mumble/CustomElements.cpp b/src/mumble/CustomElements.cpp index 5ca858f6a..9e6ca3109 100644 --- a/src/mumble/CustomElements.cpp +++ b/src/mumble/CustomElements.cpp @@ -180,43 +180,48 @@ void ChatbarTextEdit::insertFromMimeData(const QMimeData *source) { } bool ChatbarTextEdit::sendImagesFromMimeData(const QMimeData *source) { - if (source->hasImage()) { - // Process the image pasted onto the chatbar. - if (g.bAllowHTML) { + if (g.bAllowHTML) { + if (source->hasImage()) { + // Process the image pasted onto the chatbar. QImage image = qvariant_cast< QImage >(source->imageData()); - - QString imgHtml = QLatin1String("
") + Log::imageToImg(image); - - if ((g.uiImageLength == 0) || static_cast< unsigned int >(imgHtml.length()) < g.uiImageLength) { - emit pastedImage(imgHtml); + if (emitPastedImage(image)) { return true; } else { g.l->log(Log::Information, tr("Unable to send image: too large.")); + return false; } - } - } else if (source->hasUrls()) { - // Process the files dropped onto the chatbar. URLs here should be understood as the URIs of files. - QList< QUrl > urlList = source->urls(); - int count = 0; - for (int i = 0; i < urlList.size(); ++i) { - QString path = urlList[i].toLocalFile(); - QImage image(path); + } else if (source->hasUrls()) { + // Process the files dropped onto the chatbar. URLs here should be understood as the URIs of files. + QList< QUrl > urlList = source->urls(); - if (image.isNull()) - continue; + int count = 0; + for (int i = 0; i < urlList.size(); ++i) { + QString path = urlList[i].toLocalFile(); + QImage image(path); - QString imgHtml = QLatin1String("
") + Log::imageToImg(image); - - if (static_cast< unsigned int >(imgHtml.length()) < g.uiImageLength) { - emit pastedImage(imgHtml); - ++count; - } else { - g.l->log(Log::Information, tr("Unable to send image %1: too large.").arg(path)); + if (image.isNull()) + continue; + if (emitPastedImage(image)) { + ++count; + } else { + g.l->log(Log::Information, tr("Unable to send image %1: too large.").arg(path)); + } } - } - return (count > 0); + return (count > 0); + } + } + g.l->log(Log::Information, tr("This server does not allow sending images.")); + return false; +} + +bool ChatbarTextEdit::emitPastedImage(QImage image) { + QString processedImage = Log::imageToImg(image, g.uiImageLength); + if (processedImage.length() > 0) { + QString imgHtml = QLatin1String("
") + processedImage; + emit pastedImage(imgHtml); + return true; } return false; } diff --git a/src/mumble/CustomElements.h b/src/mumble/CustomElements.h index e05fa9b99..de225c185 100644 --- a/src/mumble/CustomElements.h +++ b/src/mumble/CustomElements.h @@ -53,6 +53,7 @@ protected: void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE; void insertFromMimeData(const QMimeData *source) Q_DECL_OVERRIDE; bool sendImagesFromMimeData(const QMimeData *source); + bool emitPastedImage(QImage image); public: void setDefaultText(const QString &, bool = false); diff --git a/src/mumble/Log.cpp b/src/mumble/Log.cpp index d30ed83e3..acb8363af 100644 --- a/src/mumble/Log.cpp +++ b/src/mumble/Log.cpp @@ -522,38 +522,30 @@ QString Log::imageToImg(const QByteArray &format, const QByteArray &image) { return QString::fromLatin1("").arg(fmt).arg(QLatin1String(encoded)); } -QString Log::imageToImg(QImage img) { +QString Log::imageToImg(QImage img, int maxSize) { if ((img.width() > 480) || (img.height() > 270)) { img = img.scaled(480, 270, Qt::KeepAspectRatio, Qt::SmoothTransformation); } int quality = 100; - QByteArray format = "PNG"; + QByteArray format = "JPEG"; QByteArray qba; - { - QBuffer qb(&qba); - qb.open(QIODevice::WriteOnly); - - QImageWriter imgwrite(&qb, format); - imgwrite.write(img); - } - - while ((qba.length() >= 65536) && (quality > 0)) { + QString result; + while (quality > 0) { qba.clear(); QBuffer qb(&qba); qb.open(QIODevice::WriteOnly); - format = "JPEG"; - QImageWriter imgwrite(&qb, format); imgwrite.setQuality(quality); imgwrite.write(img); + result = imageToImg(format, qba); + if (result.length() < maxSize || maxSize == 0) { + return result; + } quality -= 10; } - if (qba.length() < 65536) { - return imageToImg(format, qba); - } return QString(); } diff --git a/src/mumble/Log.h b/src/mumble/Log.h index 4b21df177..761a0f268 100644 --- a/src/mumble/Log.h +++ b/src/mumble/Log.h @@ -125,7 +125,7 @@ public: void clearIgnore(); static QString validHtml(const QString &html, QTextCursor *tc = nullptr); static QString imageToImg(const QByteArray &format, const QByteArray &image); - static QString imageToImg(QImage img); + static QString imageToImg(QImage img, int maxSize = 0); static QString msgColor(const QString &text, LogColorType t); static QString formatClientUser(ClientUser *cu, LogColorType t, const QString &displayName = QString()); static QString formatChannel(::Channel *c); From 38215622f48f85e5b7cd9d7dec164fea5edbafee Mon Sep 17 00:00:00 2001 From: Meru Alagalingam Date: Thu, 17 Dec 2020 18:29:44 +0100 Subject: [PATCH 2/2] TRANSLATION: Update translation files Scanning directory './src'... Scanning directory './src/mumble'... Updating 'src/mumble/mumble_en.ts'... Found 1911 source text(s) (1 new and 1910 already existing) --- src/mumble/mumble_en.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mumble/mumble_en.ts b/src/mumble/mumble_en.ts index bf0fc6fea..60b8cda11 100644 --- a/src/mumble/mumble_en.ts +++ b/src/mumble/mumble_en.ts @@ -2694,6 +2694,10 @@ Are you sure you wish to replace your certificate? Unable to send image %1: too large. + + This server does not allow sending images. + + ClientUser