Add a convenience function QTemporaryDir::filePath()

It allows to write more readable code:
    QString filePath = tmpDir.filePath(fileName);
instead of
    QString filePath = tmpDir.path() + QLatin1Char('/') + fileName;

Change-Id: I85aa54fd365e3bdd3ca41018ead7ed8741352b16
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Alexander Volkov 2016-05-25 14:05:55 +03:00
parent 590ca43603
commit 6b7c1f5b82
3 changed files with 53 additions and 0 deletions

View File

@ -304,6 +304,33 @@ QString QTemporaryDir::path() const
return d_ptr->success ? d_ptr->pathOrError : QString();
}
/*!
\since 5.8
Returns the path name of a file in the temporary directory.
Does \e not check if the file actually exists in the directory.
Redundant multiple separators or "." and ".." directories in
\a fileName are not removed (see QDir::cleanPath()). Absolute
paths are not allowed.
*/
QString QTemporaryDir::filePath(const QString &fileName) const
{
if (QDir::isAbsolutePath(fileName)) {
qWarning("QTemporaryDir::filePath: Absolute paths are not allowed: %s", qUtf8Printable(fileName));
return QString();
}
if (!d_ptr->success)
return QString();
QString ret = d_ptr->pathOrError;
if (!fileName.isEmpty()) {
ret += QLatin1Char('/');
ret += fileName;
}
return ret;
}
/*!
Returns \c true if the QTemporaryDir is in auto remove
mode. Auto-remove mode will automatically delete the directory from

View File

@ -65,6 +65,7 @@ public:
bool remove();
QString path() const;
QString filePath(const QString &fileName) const;
private:
QScopedPointer<QTemporaryDirPrivate> d_ptr;

View File

@ -57,6 +57,8 @@ private slots:
void fileTemplate_data();
void getSetCheck();
void fileName();
void filePath_data();
void filePath();
void autoRemove();
void nonWritableCurrentDir();
void openOnRootDrives();
@ -204,6 +206,29 @@ void tst_QTemporaryDir::fileName()
QCOMPARE(absoluteFilePath, absoluteTempPath);
}
void tst_QTemporaryDir::filePath_data()
{
QTest::addColumn<QString>("templatePath");
QTest::addColumn<QString>("fileName");
QTest::newRow("0") << QString() << "/tmpfile";
QTest::newRow("1") << QString() << "tmpfile";
QTest::newRow("2") << "XXXXX" << "tmpfile";
QTest::newRow("3") << "YYYYY" << "subdir/file";
}
void tst_QTemporaryDir::filePath()
{
QFETCH(QString, templatePath);
QFETCH(QString, fileName);
QTemporaryDir dir(templatePath);
const QString filePath = dir.filePath(fileName);
const QString expectedFilePath = QDir::isAbsolutePath(fileName) ?
QString() : dir.path() + QLatin1Char('/') + fileName;
QCOMPARE(filePath, expectedFilePath);
}
void tst_QTemporaryDir::autoRemove()
{
// Test auto remove