Aurélien Gâteau

Common user interface mistakes in KDE applications, part 6: User-friendly file paths

written on Sunday, October 7, 2012

Hey, long time no addition to this article series...

This one is a quick tip: paths and urls in KDE applications are handled with the KUrl class. When one wants to show an URL to the user, it is tempting to use the prettyUrl() method, but the string returned by this method is prefixed with "file://" for local urls, which is not something we should show to the user. Instead, it is nicer to use pathOrUrl(). pathOrUrl() calls prettyUrl() for remote urls and toLocalFile() for local ones.

This short code snippet shows the different behaviors:

KUrl local("/home/user/Documents/foo.odt");

kDebug() << local.prettyUrl();
// prints "file:///home/user/Documents/foo.odt"

kDebug() << local.pathOrUrl();
// prints "/home/user/Documents/foo.odt"

KUrl remote("");

kDebug() << remote.prettyUrl();
// prints ""

kDebug() << remote.pathOrUrl();
// prints ""

I recently fixed this in Kate code base, it is rather easy to grep your code for calls to prettyUrl() and check if you would be better off with pathOrUrl(). There are actually very few situations where one should use prettyUrl() instead of pathOrUrl().

I am going to file a request to update KUrl documentation so that this mistake is less likely to happen.

Flattr this

This post was tagged KDE and ui
blog comments powered by Disqus