class KUrl


Module kdecore
Namespace
Class KUrl
Inherits QUrl
Represents and parses a URL.

A prototypical URL looks like:

protocol://user:password\@hostname:port/path/to/file.ext#reference

KUrl handles escaping of URLs. This means that the specification of a full URL will differ from the corresponding string that would specify a local file or directory in file-operations like fopen. This is because an URL doesn't allow certain characters and escapes them. (e.g. '#'->"%23", space->"%20") (In a URL the hash-character '#' is used to specify a "reference", i.e. the position within a document).

The constructor KUrl(const QString&) expects a string properly escaped, or at least non-ambiguous. If you have the absolute path you should use KUrl.fromPath(const QString&).

KUrl kurl = KUrl.fromPath("/bar/#foo#");
QString url = kurl.url(); // . "file:bar/%23foo%23"

If you have the URL of a local file or directory and need the absolute path, you would use path().

KUrl url( "file:bar/%23foo%23" );
...
if ( url.isLocalFile() )
QString path = url.path(); // . "/bar/#foo#"

This must also be considered when you have separated directory and file strings and need to put them together. While you can simply concatenate normal path strings, you must take care if the directory-part is already an escaped URL. (This might be needed if the user specifies a relative path, and your program supplies the rest from elsewhere.)

Wrong:

QString dirUrl = "file:bar/";
QString fileName = "#foo#";
QString invalidURL = dirUrl + fileName; // . "file:bar/#foo#" won't behave like you would expect.
Instead you should use addPath(): Right:
KUrl url( "file:bar/" );
QString fileName = "#foo#";
url.addPath( fileName );
QString validURL = url.url(); // . "file:bar/%23foo%23"

Also consider that some URLs contain the password, but this shouldn't be visible. Your program should use prettyUrl() every time it displays a URL, whether in the GUI or in debug output or...

KUrl url( "ftp://name:password@ftp.faraway.org/bar/%23foo%23");
QString visibleURL = url.prettyUrl(); // . "ftp://name@ftp.faraway.org/bar/%23foo%23"
Note that prettyUrl() doesn't change the character escapes (like "%23"). Otherwise the URL would be invalid and the user wouldn't be able to use it in another context.


enums

enum details

methods