summaryrefslogtreecommitdiff
path: root/src/common-lib/utilities.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common-lib/utilities.h')
-rw-r--r--src/common-lib/utilities.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/common-lib/utilities.h b/src/common-lib/utilities.h
index ba8c7006..86d5b6ba 100644
--- a/src/common-lib/utilities.h
+++ b/src/common-lib/utilities.h
@@ -46,6 +46,11 @@
#include <QByteArray>
#include <QMultiMap>
#include <QVariant>
+#include <QString>
+#include <QUrl>
+#include <QDir>
+#include <QResource>
+#include <QLibrary>
#include <QtAppManCommon/global.h>
@@ -70,6 +75,28 @@ inline QStringList variantToStringList(const QVariant &v)
: v.toStringList();
}
+// Translate between QFile and QUrl (resource) representations.
+// For some weird reason, QFile cannot cope with "qrc:" and QUrl cannot cope with ":".
+inline QUrl filePathToUrl(const QString &path, const QString &baseDir)
+{
+ return path.startsWith(qSL(":")) ? QUrl(qSL("qrc") + path)
+ : QUrl::fromUserInput(path, baseDir, QUrl::AssumeLocalFile);
+}
+
+inline QString urlToLocalFilePath(const QUrl &url)
+{
+ if (url.isLocalFile())
+ return url.toLocalFile();
+ else if (url.scheme() == qSL("qrc"))
+ return qL1C(':') + url.path();
+ return QString();
+}
+
+inline QString toAbsoluteFilePath(const QString &path, const QString &baseDir = QDir::currentPath())
+{
+ return path.startsWith(qSL("qrc:")) ? path.mid(3) : QDir(baseDir).absoluteFilePath(path);
+}
+
/*! \internal
Recursively merge the second QVariantMap into the first one
*/
@@ -120,4 +147,12 @@ QVector<T *> loadPlugins(const char *type, const QStringList &files) Q_DECL_NOEX
return result;
}
+// Load a Qt resource, either in the form of a resource file or a plugin
+inline bool loadResource(const QString &resource)
+{
+ return QLibrary::isLibrary(resource)
+ ? (QLibrary(QDir().absoluteFilePath(resource)).load() || QResource::registerResource(resource))
+ : (QResource::registerResource(resource) || QLibrary(QDir().absoluteFilePath(resource)).load());
+}
+
QT_END_NAMESPACE_AM