2016-08-29 15:41:11 +02:00
|
|
|
#include "./desktoputils.h"
|
|
|
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2019-10-09 12:54:35 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include <filesystem>
|
|
|
|
#endif
|
|
|
|
|
2019-06-10 21:57:46 +02:00
|
|
|
namespace QtUtilities {
|
2016-08-29 15:41:11 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Shows the specified file or directory using the default file browser.
|
2019-10-09 12:54:35 +02:00
|
|
|
* \remarks
|
|
|
|
* - The specified \a path must *not* be specified as URL. (The conversion to a URL suitable for
|
|
|
|
* QDesktopServices::openUrl() is the whole purpose of this function).
|
|
|
|
* - The Qt documentation suggests to use
|
|
|
|
* `QDesktopServices::openUrl(QUrl("file:///C:/Documents and Settings/All Users/Desktop", QUrl::TolerantMode));`
|
|
|
|
* under Windows. However, that does not work if the path contains a '#'. It is also better to use
|
|
|
|
* QUrl::DecodedMode to prevent QUrl from interpreting any of the paths characters in a special way.
|
2016-08-29 15:41:11 +02:00
|
|
|
*/
|
|
|
|
bool openLocalFileOrDir(const QString &path)
|
|
|
|
{
|
2019-10-09 12:54:35 +02:00
|
|
|
QUrl url(QStringLiteral("file://"));
|
|
|
|
|
2016-08-29 15:41:11 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
2019-10-09 12:54:35 +02:00
|
|
|
// replace backslashes with regular slashes
|
2016-08-29 15:41:11 +02:00
|
|
|
QString tmp(path);
|
|
|
|
tmp.replace(QChar('\\'), QChar('/'));
|
2019-10-09 12:54:35 +02:00
|
|
|
|
|
|
|
// add a slash before the drive letter of an absolute path
|
|
|
|
if (std::filesystem::path(path.toStdString()).is_absolute()) {
|
|
|
|
tmp = QStringLiteral("/") + tmp;
|
|
|
|
}
|
2016-12-27 20:22:20 +01:00
|
|
|
url.setPath(tmp, QUrl::DecodedMode);
|
2019-10-09 12:54:35 +02:00
|
|
|
|
2016-08-29 15:41:11 +02:00
|
|
|
#else
|
2016-12-27 20:22:20 +01:00
|
|
|
url.setPath(path, QUrl::DecodedMode);
|
2016-08-29 15:41:11 +02:00
|
|
|
#endif
|
2019-10-09 12:54:35 +02:00
|
|
|
return QDesktopServices::openUrl(url);
|
2016-08-29 15:41:11 +02:00
|
|
|
}
|
2019-06-10 21:57:46 +02:00
|
|
|
} // namespace QtUtilities
|