Add support to set text/uri-list mimedata via setData()

Fixed issue that text/uri-list mimedata got from QMimeData::data()
was corrupted after setting it back via QMimeData::setData()

Change-Id: I2377523a9286519402ab9127ed7f3fa66e39a679
Task-number: QTBUG-45486
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Marko Kangas 2015-04-16 16:11:06 +03:00
parent f15d6c3fa9
commit 0abf5ec7c4
2 changed files with 32 additions and 2 deletions

View File

@ -566,7 +566,22 @@ QByteArray QMimeData::data(const QString &mimeType) const
void QMimeData::setData(const QString &mimeType, const QByteArray &data)
{
Q_D(QMimeData);
d->setData(mimeType, QVariant(data));
if (mimeType == QLatin1String("text/uri-list")) {
QByteArray ba = data;
if (ba.endsWith('\0'))
ba.chop(1);
QList<QByteArray> urls = ba.split('\n');
QList<QVariant> list;
for (int i = 0; i < urls.size(); ++i) {
QByteArray ba = urls.at(i).trimmed();
if (!ba.isEmpty())
list.append(QUrl::fromEncoded(ba));
}
d->setData(mimeType, list);
} else {
d->setData(mimeType, QVariant(data));
}
}
/*!

View File

@ -297,9 +297,16 @@ void tst_QMimeData::setText() const
QVERIFY(mimeData.hasText() == false);
}
// Publish retrieveData for verifying content validity
class TstMetaData : public QMimeData
{
public:
using QMimeData::retrieveData;
};
void tst_QMimeData::setUrls() const
{
QMimeData mimeData;
TstMetaData mimeData;
QList<QUrl> shortUrlList;
QList<QUrl> longUrlList;
@ -321,6 +328,14 @@ void tst_QMimeData::setUrls() const
QCOMPARE(mimeData.urls(), longUrlList);
QCOMPARE(mimeData.text(), QString("http://qt-project.org\nhttp://www.google.com\n"));
// test and verify that setData doesn't corrupt url content
foreach (const QString &format, mimeData.formats()) {
QVariant before = mimeData.retrieveData(format, QVariant::ByteArray);
mimeData.setData(format, mimeData.data(format));
QVariant after = mimeData.retrieveData(format, QVariant::ByteArray);
QCOMPARE(after, before);
}
// clear, verify
mimeData.clear();
QCOMPARE(mimeData.hasUrls(), false);