From 4e196159077a820e99bc8bd306d31d0ccaa17c57 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 26 Oct 2016 11:19:19 +0200 Subject: qmacmime: convert UTF-16 using QTextCodec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current implementation of QMacPasteboardMimeUnicodeText didn't contain any logic to handle unicode text with a byte order mark (BOM). According to the docs (*), 'public.utf16-plain-text' can have an optional BOM. Because of that, Qt would fail encoding UTF-16 text from the pasteboard if it had a BOM. Additionally, perhaps because of a bug in iOS 10, UTF-16 text placed on the pasteboard by Qt ends up being encoded wrong by native apps, unless the text has a BOM. Rather than hard-coding UTF-16 encoding/decoding in qmacmime, we now leave it to QTextCodec. QTextCodec will add a BOM by default, and can handle decoding of UTF-16 both with, and without, a BOM. *: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html Task-number: QTBUG-56229 Change-Id: I3a08deb0262350c67e5622cf23eb3c3a4907ec39 Reviewed-by: Tor Arne Vestbø --- src/platformsupport/clipboard/qmacmime.mm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/platformsupport/clipboard/qmacmime.mm') diff --git a/src/platformsupport/clipboard/qmacmime.mm b/src/platformsupport/clipboard/qmacmime.mm index 2e3257cfcf..cdb5d6b08e 100644 --- a/src/platformsupport/clipboard/qmacmime.mm +++ b/src/platformsupport/clipboard/qmacmime.mm @@ -405,8 +405,7 @@ QVariant QMacPasteboardMimeUnicodeText::convertToMime(const QString &mimetype, Q if (flavor == QLatin1String("public.utf8-plain-text")) { ret = QString::fromUtf8(firstData); } else if (flavor == QLatin1String("public.utf16-plain-text")) { - ret = QString(reinterpret_cast(firstData.constData()), - firstData.size() / sizeof(QChar)); + ret = QTextCodec::codecForName("UTF-16")->toUnicode(firstData); } else { qWarning("QMime::convertToMime: unhandled mimetype: %s", qPrintable(mimetype)); } @@ -420,7 +419,7 @@ QList QMacPasteboardMimeUnicodeText::convertFromMime(const QString & if (flavor == QLatin1String("public.utf8-plain-text")) ret.append(string.toUtf8()); else if (flavor == QLatin1String("public.utf16-plain-text")) - ret.append(QByteArray((char*)string.utf16(), string.length()*2)); + ret.append(QTextCodec::codecForName("UTF-16")->fromUnicode(string)); return ret; } -- cgit v1.2.3