qmacmime: convert UTF-16 using QTextCodec

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ø <tor.arne.vestbo@qt.io>
This commit is contained in:
Richard Moe Gustavsen 2016-10-26 11:19:19 +02:00
parent e8b55a6d2a
commit 4e19615907

View File

@ -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<const QChar *>(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<QByteArray> 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;
}