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("http://www.kde.org");
kDebug() << remote.prettyUrl();
// prints "http://www.kde.org"
kDebug() << remote.pathOrUrl();
// prints "http://www.kde.org"

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.

This post was tagged kde and ui