QUrl: add fileName() method. Complements QUrl::RemoveFilename.
Change-Id: Ieda43364214c3b7aee43040e176e29ad48c14271 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
9d0ff90760
commit
0f062f42b9
|
|
@ -85,3 +85,9 @@ QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s");
|
|||
qDebug(ba.constData());
|
||||
// prints "{a fi%73hy %73tring%3F}"
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
QUrl url("http://qt-project.org/support/file.html");
|
||||
// url.adjusted(RemoveFilename) == "http://qt-project.org/support/"
|
||||
// url.fileName() == "file.html"
|
||||
//! [7]
|
||||
|
|
|
|||
|
|
@ -2415,6 +2415,36 @@ QString QUrl::path(ComponentFormattingOptions options) const
|
|||
\sa setEncodedPath(), toEncoded()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\since 5.2
|
||||
|
||||
Returns the name of the file, excluding the directory path.
|
||||
|
||||
Note that, if this QUrl object is given a path ending in a slash, the name of the file is considered empty.
|
||||
|
||||
If the path doesn't contain any slash, it is fully returned as the fileName.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet code/src_corelib_io_qurl.cpp 7
|
||||
|
||||
The \a options argument controls how to format the file name component. All
|
||||
values produce an unambiguous result. With QUrl::FullyDecoded, all
|
||||
percent-encoded sequences are decoded; otherwise, the returned value may
|
||||
contain some percent-encoded sequences for some control sequences not
|
||||
representable in decoded form in QString.
|
||||
|
||||
\sa path()
|
||||
*/
|
||||
QString QUrl::fileName(ComponentFormattingOptions options) const
|
||||
{
|
||||
const QString ourPath = path(options);
|
||||
const int slash = ourPath.lastIndexOf(QLatin1Char('/'));
|
||||
if (slash == -1)
|
||||
return ourPath;
|
||||
return ourPath.mid(slash + 1);
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 4.2
|
||||
|
||||
|
|
|
|||
|
|
@ -224,6 +224,7 @@ public:
|
|||
|
||||
void setPath(const QString &path, ParsingMode mode = TolerantMode);
|
||||
QString path(ComponentFormattingOptions options = PrettyDecoded) const;
|
||||
QString fileName(ComponentFormattingOptions options = PrettyDecoded) const;
|
||||
|
||||
bool hasQuery() const;
|
||||
void setQuery(const QString &query, ParsingMode mode = TolerantMode);
|
||||
|
|
|
|||
|
|
@ -162,6 +162,8 @@ private slots:
|
|||
void binaryData();
|
||||
void fromUserInput_data();
|
||||
void fromUserInput();
|
||||
void fileName_data();
|
||||
void fileName();
|
||||
void isEmptyForEncodedUrl();
|
||||
void toEncodedNotUsingUninitializedPath();
|
||||
void emptyAuthorityRemovesExistingAuthority();
|
||||
|
|
@ -2826,6 +2828,43 @@ void tst_QUrl::fromUserInput()
|
|||
QCOMPARE(url, guessUrlFromString);
|
||||
}
|
||||
|
||||
void tst_QUrl::fileName_data()
|
||||
{
|
||||
QTest::addColumn<QString>("urlStr");
|
||||
QTest::addColumn<QString>("expectedDirPath");
|
||||
QTest::addColumn<QString>("expectedPrettyDecodedFileName");
|
||||
QTest::addColumn<QString>("expectedFullyDecodedFileName");
|
||||
|
||||
QTest::newRow("fromDocu") << "http://qt-project.org/support/file.html"
|
||||
<< "/support/" << "file.html" << "file.html";
|
||||
QTest::newRow("absoluteFile") << "file:///temp/tmp.txt"
|
||||
<< "/temp/" << "tmp.txt" << "tmp.txt";
|
||||
QTest::newRow("absoluteDir") << "file:///temp/"
|
||||
<< "/temp/" << QString() << QString();
|
||||
QTest::newRow("absoluteInRoot") << "file:///temp"
|
||||
<< "/" << "temp" << "temp";
|
||||
QTest::newRow("relative") << "temp/tmp.txt"
|
||||
<< "temp/" << "tmp.txt" << "tmp.txt";
|
||||
QTest::newRow("relativeNoSlash") << "tmp.txt"
|
||||
<< QString() << "tmp.txt" << "tmp.txt";
|
||||
QTest::newRow("encoded") << "print:/specials/Print%20To%20File%20(PDF%252FAcrobat)"
|
||||
<< "/specials/" << "Print To File (PDF%252FAcrobat)" << "Print To File (PDF%2FAcrobat)";
|
||||
}
|
||||
|
||||
void tst_QUrl::fileName()
|
||||
{
|
||||
QFETCH(QString, urlStr);
|
||||
QFETCH(QString, expectedDirPath);
|
||||
QFETCH(QString, expectedPrettyDecodedFileName);
|
||||
QFETCH(QString, expectedFullyDecodedFileName);
|
||||
|
||||
QUrl url(urlStr);
|
||||
QVERIFY(url.isValid());
|
||||
QCOMPARE(url.adjusted(QUrl::RemoveFilename).path(), expectedDirPath);
|
||||
QCOMPARE(url.fileName(QUrl::PrettyDecoded), expectedPrettyDecodedFileName);
|
||||
QCOMPARE(url.fileName(QUrl::FullyDecoded), expectedFullyDecodedFileName);
|
||||
}
|
||||
|
||||
// This is a regression test for a previously fixed bug where isEmpty didn't
|
||||
// work for an encoded URL that was yet to be decoded. The test checks that
|
||||
// isEmpty works for an encoded URL both after and before decoding.
|
||||
|
|
|
|||
Loading…
Reference in New Issue