summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Faure <faure@kde.org>2013-07-14 00:45:14 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-07-20 21:04:16 +0200
commit0f062f42b9817205aab657dcfaeaa8c3ed302889 (patch)
treee922533f68e046990ed16f991940e99f6e3bc5f1 /src
parent9d0ff90760bed65451fb665fecf9f770e3b05967 (diff)
QUrl: add fileName() method. Complements QUrl::RemoveFilename.
Change-Id: Ieda43364214c3b7aee43040e176e29ad48c14271 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp6
-rw-r--r--src/corelib/io/qurl.cpp30
-rw-r--r--src/corelib/io/qurl.h1
3 files changed, 37 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp
index 532a0e1f59..91eaf298e1 100644
--- a/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_io_qurl.cpp
@@ -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]
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index 3d7e18fdfa..00588da15f 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -2416,6 +2416,36 @@ QString QUrl::path(ComponentFormattingOptions options) const
*/
/*!
+ \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
Returns true if this URL contains a Query (i.e., if ? was seen on it).
diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h
index 5678c7813b..3018f6d0e4 100644
--- a/src/corelib/io/qurl.h
+++ b/src/corelib/io/qurl.h
@@ -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);