diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index 6f8f70f8d4..a647d21f40 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -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; diff --git a/src/corelib/io/qtemporaryfile.h b/src/corelib/io/qtemporaryfile.h index 96e6b1c779..b7c375ea41 100644 --- a/src/corelib/io/qtemporaryfile.h +++ b/src/corelib/io/qtemporaryfile.h @@ -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); diff --git a/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.pro b/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.pro index 64331daef9..8a91340e5f 100644 --- a/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.pro +++ b/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.pro @@ -5,3 +5,4 @@ QT = core testlib SOURCES = tst_qtemporaryfile.cpp TESTDATA += tst_qtemporaryfile.cpp DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 +RESOURCES += qtemporaryfile.qrc \ No newline at end of file diff --git a/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.qrc b/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.qrc new file mode 100644 index 0000000000..efadde8b4d --- /dev/null +++ b/tests/auto/corelib/io/qtemporaryfile/qtemporaryfile.qrc @@ -0,0 +1,5 @@ + + + resources/test.txt + + \ No newline at end of file diff --git a/tests/auto/corelib/io/qtemporaryfile/resources/test.txt b/tests/auto/corelib/io/qtemporaryfile/resources/test.txt new file mode 100644 index 0000000000..793aa682b0 --- /dev/null +++ b/tests/auto/corelib/io/qtemporaryfile/resources/test.txt @@ -0,0 +1 @@ +This is a test \ No newline at end of file diff --git a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp index e68594ebaf..6d7a6e76ef 100644 --- a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp @@ -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("filePath"); + QTest::addColumn("currentPos"); + QTest::addColumn("valid"); + QTest::addColumn("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("prefix");