core/qmlglobal: add dataPath(), statePath() and cachePath()

This commit is contained in:
outfoxxed 2025-05-16 00:11:09 -07:00
parent 6f3275bdaf
commit b61fc91033
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
3 changed files with 30 additions and 0 deletions

View file

@ -11,6 +11,7 @@
#include <qloggingcategory.h>
#include <qstandardpaths.h>
#include <qtenvironmentvariables.h>
#include <qtversionchecks.h>
#include <unistd.h>
#include "instanceinfo.hpp"
@ -232,7 +233,18 @@ QDir QsPaths::shellDataDir() {
QDir QsPaths::shellStateDir() {
if (this->shellStateState == DirState::Unknown) {
#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
QDir dir;
if (qEnvironmentVariableIsSet("XDG_STATE_HOME")) {
dir = QDir(qEnvironmentVariable("XDG_STATE_HOME"));
} else {
auto home = QDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
dir = QDir(home.filePath(".local/state"));
}
#else
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::StateLocation));
#endif
dir = QDir(dir.filePath("by-shell"));
dir = QDir(dir.filePath(this->shellId));
this->mShellStateDir = dir;

View file

@ -202,6 +202,18 @@ QString QuickshellGlobal::cacheDir() const { // NOLINT
return QsPaths::instance()->shellCacheDir().path();
}
QString QuickshellGlobal::dataPath(const QString& path) const {
return this->dataDir() % '/' % path;
}
QString QuickshellGlobal::statePath(const QString& path) const {
return this->stateDir() % '/' % path;
}
QString QuickshellGlobal::cachePath(const QString& path) const {
return this->cacheDir() % '/' % path;
}
QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
auto vstr = variable.toStdString();
if (!qEnvironmentVariableIsSet(vstr.data())) return QVariant::fromValue(nullptr);

View file

@ -155,6 +155,12 @@ public:
/// Setting the `fallback` parameter of `iconPath` will attempt to load the fallback
/// icon if the requested one could not be loaded.
Q_INVOKABLE static QString iconPath(const QString& icon, const QString& fallback);
/// Equivalent to `${Quickshell.dataDir}/${path}`
Q_INVOKABLE [[nodiscard]] QString dataPath(const QString& path) const;
/// Equivalent to `${Quickshell.stateDir}/${path}`
Q_INVOKABLE [[nodiscard]] QString statePath(const QString& path) const;
/// Equivalent to `${Quickshell.cacheDir}/${path}`
Q_INVOKABLE [[nodiscard]] QString cachePath(const QString& path) const;
[[nodiscard]] QString shellRoot() const;