2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Fix pasting images from Firefox on Windows.

Fixes #10564.

Together with the image data Firefox sets to the clipboard an URLs list
which has a path to local temp file, created from that image.

Reading images from disk is slower + sometimes the content of the file
is wrong so for this case we prefer to read the image data directly.
This commit is contained in:
John Preston
2023-03-06 11:58:25 +04:00
parent c687882760
commit ff4af1b9bc
10 changed files with 70 additions and 34 deletions

View File

@@ -14,6 +14,28 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <QtCore/QMimeData>
namespace Core {
namespace {
[[nodiscard]] bool IsImageFromFirefox(not_null<const QMimeData*> data) {
// See https://bugs.telegram.org/c/6765/public
// See https://github.com/telegramdesktop/tdesktop/issues/10564
//
// Usually we prefer pasting from URLs list instead of pasting from
// image data, because sometimes a file is copied together with an
// image data of its File Explorer thumbnail or smth like that. In
// that case you end up sending this thumbnail instead of the file.
//
// But in case of "Copy Image" from Firefox on Windows we get both
// URLs list with a file path to some Temp folder in the list and
// the image data that was copied. The file is read slower + it may
// have incorrect content in case the URL can't be accessed without
// authorization. So in that case we want only image data and we
// check for a special Firefox mime type to check for that case.
return data->hasFormat(u"application/x-moz-nativeimage"_q)
&& data->hasImage();
}
} // namespace
MimeType::MimeType(const QMimeType &type) : _typeStruct(type) {
}
@@ -170,9 +192,10 @@ std::shared_ptr<QMimeData> ShareMimeMediaData(
result->setData(u"application/x-td-use-jpeg"_q, "1");
result->setData(u"image/jpeg"_q, original->data(u"image/jpeg"_q));
}
if (auto list = base::GetMimeUrls(original); !list.isEmpty()) {
if (auto list = Core::ReadMimeUrls(original); !list.isEmpty()) {
result->setUrls(std::move(list));
}
result->setText(Core::ReadMimeText(original));
return result;
}
@@ -192,4 +215,16 @@ MimeImageData ReadMimeImage(not_null<const QMimeData*> data) {
return {};
}
QString ReadMimeText(not_null<const QMimeData*> data) {
return IsImageFromFirefox(data) ? QString() : data->text();
}
QList<QUrl> ReadMimeUrls(not_null<const QMimeData*> data) {
return (data->hasUrls() && !IsImageFromFirefox(data))
? KUrlMimeData::urlsFromMimeData(
data,
KUrlMimeData::PreferLocalUrls)
: QList<QUrl>();
}
} // namespace Core

View File

@@ -65,5 +65,7 @@ struct MimeImageData {
}
};
[[nodiscard]] MimeImageData ReadMimeImage(not_null<const QMimeData*> data);
[[nodiscard]] QString ReadMimeText(not_null<const QMimeData*> data);
[[nodiscard]] QList<QUrl> ReadMimeUrls(not_null<const QMimeData*> data);
} // namespace Core

View File

@@ -38,18 +38,6 @@ inline bool in_range(Value &&value, From &&from, Till &&till) {
return (value >= from) && (value < till);
}
#if __has_include(<kurlmimedata.h>)
inline QList<QUrl> GetMimeUrls(const QMimeData *data) {
if (!data->hasUrls()) {
return {};
}
return KUrlMimeData::urlsFromMimeData(
data,
KUrlMimeData::PreferLocalUrls);
}
#endif
#if __has_include(<ksandbox.h>)
inline QString IconName() {
static const auto Result = KSandbox::isFlatpak()