QResource: deprecate isCompressed()

Current codebases assume isCompressed() implies ZlibCompression, since
there was no compressionAlgorithm() getter. In order to force codebases
to change, deprecate isCompressed() and force handling of the algorithm.

The replacement API is being introduced in 5.14, which is why the
warning is being emitted in 5.15 only.

Change-Id: Ief874765cd7b43798de3fffd15a9f5d978951ea5
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2019-06-20 09:44:20 -07:00
parent a5da01c044
commit d80bd2f548
4 changed files with 41 additions and 23 deletions

View File

@ -1,7 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Copyright (C) 2018 Intel Corporation.
** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -279,14 +279,14 @@ static inline QStringList *resourceSearchPaths()
This enum is used by compressionAlgorithm() to indicate which algorithm the
RCC tool used to compress the payload.
\value NoCompression Contents are not compressed (isCompressed() is false).
\value NoCompression Contents are not compressed
\value ZlibCompression Contents are compressed using \l{zlib}{https://zlib.net} and can
be decompressed using the qUncompress() function.
\value ZstdCompression Contents are compressed using \l{zstd}{https://zstd.net}. To
decompress, use the \c{ZSTD_decompress} function from the zstd
library.
\sa compressionAlgorithm(), isCompressed()
\sa compressionAlgorithm()
*/
class QResourcePrivate {
@ -551,13 +551,20 @@ bool QResource::isValid() const
\sa isDir()
*/
#if QT_DEPRECATED_SINCE(5, 13)
/*!
\obsolete
Returns \c true if the resource represents a file and the data backing it
is in a compressed format, false otherwise. If the data is compressed,
check compressionAlgorithm() to verify what algorithm to use to decompress
the data.
\note This function is deprecated and can be replaced with
\code
compressionAlgorithm() != NoCompression
\endcode
\sa data(), compressionAlgorithm(), isFile()
*/
@ -565,6 +572,7 @@ bool QResource::isCompressed() const
{
return compressionAlgorithm() != NoCompression;
}
#endif
/*!
\since 5.13
@ -582,7 +590,7 @@ bool QResource::isCompressed() const
See \l{http://facebook.github.io/zstd/zstd_manual.html}{Zstandard manual}.
\sa isCompressed(), data(), isFile()
\sa data(), isFile()
*/
QResource::Compression QResource::compressionAlgorithm() const
{
@ -606,11 +614,11 @@ qint64 QResource::size() const
/*!
Returns direct access to a read only segment of data that this resource
represents. If the resource is compressed the data returns is
compressed and qUncompress() must be used to access the data. If the
resource is a directory \nullptr is returned.
represents. If the resource is compressed the data returned is compressed
and the appropriate library functions must be used to access the data. If
the resource is a directory \nullptr is returned.
\sa size(), isCompressed(), isFile()
\sa size(), compressionAlgorithm(), isFile()
*/
const uchar *QResource::data() const
@ -1366,9 +1374,15 @@ bool QResourceFileEngine::open(QIODevice::OpenMode flags)
qWarning("QResourceFileEngine::open: Missing file name");
return false;
}
if(flags & QIODevice::WriteOnly)
if (flags & QIODevice::WriteOnly)
return false;
d->uncompress();
if (d->resource.compressionAlgorithm() != QResource::NoCompression) {
d->uncompress();
if (d->uncompressed.isNull()) {
d->errorString = QSystemError::stdString(EIO);
return false;
}
}
if (!d->resource.isValid()) {
d->errorString = QSystemError::stdString(ENOENT);
return false;
@ -1395,7 +1409,7 @@ qint64 QResourceFileEngine::read(char *data, qint64 len)
len = size()-d->offset;
if(len <= 0)
return 0;
if(d->resource.isCompressed())
if (!d->uncompressed.isNull())
memcpy(data, d->uncompressed.constData()+d->offset, len);
else
memcpy(data, d->resource.data()+d->offset, len);
@ -1431,9 +1445,9 @@ bool QResourceFileEngine::link(const QString &)
qint64 QResourceFileEngine::size() const
{
Q_D(const QResourceFileEngine);
if(!d->resource.isValid())
if (!d->resource.isValid())
return 0;
if (d->resource.isCompressed()) {
if (d->resource.compressionAlgorithm() != QResource::NoCompression) {
d->uncompress();
return d->uncompressed.size();
}
@ -1596,7 +1610,7 @@ uchar *QResourceFileEnginePrivate::map(qint64 offset, qint64 size, QFile::Memory
Q_UNUSED(flags);
qint64 max = resource.size();
if (resource.isCompressed()) {
if (resource.compressionAlgorithm() != QResource::NoCompression) {
uncompress();
max = uncompressed.size();
}
@ -1609,7 +1623,7 @@ uchar *QResourceFileEnginePrivate::map(qint64 offset, qint64 size, QFile::Memory
}
const uchar *address = resource.data();
if (resource.isCompressed())
if (resource.compressionAlgorithm() != QResource::NoCompression)
address = reinterpret_cast<const uchar *>(uncompressed.constData());
return const_cast<uchar *>(address) + offset;

View File

@ -72,7 +72,6 @@ public:
bool isValid() const;
bool isCompressed() const;
Compression compressionAlgorithm() const;
qint64 size() const;
const uchar *data() const;
@ -84,6 +83,10 @@ public:
QT_DEPRECATED_X("Use QDir::searchPaths() instead")
static QStringList searchPaths();
#endif
#if QT_DEPRECATED_SINCE(5, 15)
QT_DEPRECATED_VERSION_X_5_15("Use QResource::compressionAlgorithm() instead")
bool isCompressed() const;
#endif
static bool registerResource(const QString &rccFilename, const QString &resourceRoot=QString());
static bool unregisterResource(const QString &rccFilename, const QString &resourceRoot=QString());

View File

@ -528,7 +528,8 @@ bool QTranslatorPrivate::do_load(const QString &realname, const QString &directo
// memory, so no need to use QFile to copy it again.
Q_ASSERT(!d->resource);
d->resource = new QResource(realname);
if (resource->isValid() && !resource->isCompressed() && resource->size() >= MagicLength
if (resource->isValid() && resource->compressionAlgorithm() == QResource::NoCompression
&& resource->size() >= MagicLength
&& !memcmp(resource->data(), magic, MagicLength)) {
d->unmapLength = resource->size();
d->unmapPointer = reinterpret_cast<char *>(const_cast<uchar *>(resource->data()));

View File

@ -1,7 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2018 Intel Corporation.
** Copyright (C) 2018 The Qt Company Ltd.
** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@ -563,15 +563,15 @@ void tst_QResourceEngine::setLocale()
// default constructed QResource gets the default locale
QResource resource;
resource.setFileName("aliasdir/aliasdir.txt");
QVERIFY(!resource.isCompressed());
QCOMPARE(resource.compressionAlgorithm(), QResource::NoCompression);
// change the default locale and make sure it doesn't affect the resource
QLocale::setDefault(QLocale("de_CH"));
QVERIFY(!resource.isCompressed());
QCOMPARE(resource.compressionAlgorithm(), QResource::NoCompression);
// then explicitly set the locale on qresource
resource.setLocale(QLocale("de_CH"));
QVERIFY(resource.isCompressed());
QVERIFY(resource.compressionAlgorithm() != QResource::NoCompression);
// the reset the default locale back
QLocale::setDefault(QLocale::system());