Rename createLocalFile to createNativeFile & deprecate createLocalFile

As it was confusing to use the term local file when referring
to a file that was accessible using native APIs and not just
a file that was on a hard disk somewhere already the function
name has been changed.

By renaming it to createNativeFile we keep it consistant with QFileInfo
which has an isNativeFile() function too.

Test also added.

Task-number: QTBUG-3169

Change-Id: I410e7ed28133d68fd312c6c0faf3f7191460d7ce
Reviewed-by: João Abecasis <joao@abecasis.name>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Andy Shaw 2012-09-25 13:14:52 +02:00 committed by The Qt Project
parent f20266f9f4
commit ef3a544436
6 changed files with 85 additions and 11 deletions

View File

@ -700,6 +700,21 @@ void QTemporaryFile::setFileTemplate(const QString &name)
/*!
\fn QTemporaryFile *QTemporaryFile::createLocalFile(const QString &fileName)
\overload
\obsolete
Use QTemporaryFile::createNativeFile(const QString &fileName) instead.
*/
/*!
\fn QTemporaryFile *QTemporaryFile::createLocalFile(QFile &file)
\obsolete
Use QTemporaryFile::createNativeFile(QFile &file) instead.
*/
/*!
\fn QTemporaryFile *QTemporaryFile::createNativeFile(const QString &fileName)
\overload
Works on the given \a fileName rather than an existing QFile
object.
@ -707,16 +722,27 @@ void QTemporaryFile::setFileTemplate(const QString &name)
/*!
If \a file is not on a local disk, a temporary file is created
on a local disk, \a file is copied into the temporary local file,
and a pointer to the temporary local file is returned. If \a file
is already on a local disk, a copy is not created and 0 is returned.
If \a file is not already a native file then a QTemporaryFile is created
in the tempPath() and \a file is copied into the temporary file, then a
pointer to the temporary file is returned. If \a file is already a native
file, a QTemporaryFile is not created, no copy is made and 0 is returned.
For example:
QFile f(":/resources/file.txt");
QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file
QFile f("/users/qt/file.txt");
QTemporaryFile::createNativeFile(f); // Returns 0
\sa QFileInfo::isNativePath()
*/
QTemporaryFile *QTemporaryFile::createLocalFile(QFile &file)
QTemporaryFile *QTemporaryFile::createNativeFile(QFile &file)
{
if (QAbstractFileEngine *engine = file.d_func()->engine()) {
if(engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::LocalDiskFlag)
return 0; //local already
return 0; //native already
//cache
bool wasOpen = file.isOpen();
qint64 old_off = 0;

View File

@ -83,10 +83,15 @@ public:
QString fileName() const;
QString fileTemplate() const;
void setFileTemplate(const QString &name);
inline static QTemporaryFile *createLocalFile(const QString &fileName)
{ QFile file(fileName); return createLocalFile(file); }
static QTemporaryFile *createLocalFile(QFile &file);
#if QT_DEPRECATED_SINCE(5,1)
QT_DEPRECATED inline static QTemporaryFile *createLocalFile(const QString &fileName)
{ return createNativeFile(fileName); }
QT_DEPRECATED inline static QTemporaryFile *createLocalFile(QFile &file)
{ return createNativeFile(file); }
#endif
inline static QTemporaryFile *createNativeFile(const QString &fileName)
{ QFile file(fileName); return createNativeFile(file); }
static QTemporaryFile *createNativeFile(QFile &file);
protected:
bool open(OpenMode flags);

View File

@ -5,3 +5,4 @@ QT = core testlib
SOURCES = tst_qtemporaryfile.cpp
TESTDATA += tst_qtemporaryfile.cpp
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
RESOURCES += qtemporaryfile.qrc

View File

@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>resources/test.txt</file>
</qresource>
</RCC>

View File

@ -0,0 +1 @@
This is a test

View File

@ -88,7 +88,8 @@ private slots:
void resetTemplateAfterError();
void setTemplateAfterOpen();
void autoRemoveAfterFailedRename();
void createNativeFile_data();
void createNativeFile();
void QTBUG_4796_data();
void QTBUG_4796();
};
@ -633,6 +634,41 @@ void tst_QTemporaryFile::autoRemoveAfterFailedRename()
cleaner.reset();
}
void tst_QTemporaryFile::createNativeFile_data()
{
QTest::addColumn<QString>("filePath");
QTest::addColumn<qint64>("currentPos");
QTest::addColumn<bool>("valid");
QTest::addColumn<QByteArray>("content");
QTest::newRow("nativeFile") << QFINDTESTDATA("resources/test.txt") << (qint64)-1 << false << QByteArray();
QTest::newRow("nativeFileWithPos") << QFINDTESTDATA("resources/test.txt") << (qint64)5 << false << QByteArray();
QTest::newRow("resourceFile") << ":/resources/test.txt" << (qint64)-1 << true << QByteArray("This is a test");
QTest::newRow("resourceFileWithPos") << ":/resources/test.txt" << (qint64)5 << true << QByteArray("This is a test");
}
void tst_QTemporaryFile::createNativeFile()
{
QFETCH(QString, filePath);
QFETCH(qint64, currentPos);
QFETCH(bool, valid);
QFETCH(QByteArray, content);
QFile f(filePath);
if (currentPos != -1) {
f.open(QIODevice::ReadOnly);
f.seek(currentPos);
}
QTemporaryFile *tempFile = QTemporaryFile::createNativeFile(f);
QVERIFY(valid == (bool)tempFile);
if (currentPos != -1)
QCOMPARE(currentPos, f.pos());
if (valid) {
QCOMPARE(content, tempFile->readAll());
delete tempFile;
}
}
void tst_QTemporaryFile::QTBUG_4796_data()
{
QTest::addColumn<QString>("prefix");