core/qmlglobal: expose dataDir and stateDir

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

View file

@ -48,28 +48,6 @@ QString QsPaths::ipcPath(const QString& id) {
return QDir(QsPaths::basePath(id)).filePath("ipc.sock");
}
QDir* QsPaths::cacheDir() {
if (this->cacheState == DirState::Unknown) {
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
dir = QDir(dir.filePath("by-shell"));
dir = QDir(dir.filePath(this->shellId));
this->mCacheDir = dir;
qCDebug(logPaths) << "Initialized cache path:" << dir.path();
if (!dir.mkpath(".")) {
qCCritical(logPaths) << "Could not create cache directory at" << dir.path();
this->cacheState = DirState::Failed;
} else {
this->cacheState = DirState::Ready;
}
}
if (this->cacheState == DirState::Failed) return nullptr;
else return &this->mCacheDir;
}
QDir* QsPaths::baseRunDir() {
if (this->baseRunState == DirState::Unknown) {
auto runtimeDir = qEnvironmentVariable("XDG_RUNTIME_DIR");
@ -230,6 +208,72 @@ void QsPaths::linkPathDir() {
}
}
QDir QsPaths::shellDataDir() {
if (this->shellDataState == DirState::Unknown) {
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
dir = QDir(dir.filePath("by-shell"));
dir = QDir(dir.filePath(this->shellId));
this->mShellDataDir = dir;
qCDebug(logPaths) << "Initialized data path:" << dir.path();
if (!dir.mkpath(".")) {
qCCritical(logPaths) << "Could not create data directory at" << dir.path();
this->shellDataState = DirState::Failed;
} else {
this->shellDataState = DirState::Ready;
}
}
// Returning no path on fail might result in files being written in unintended locations.
return this->mShellDataDir;
}
QDir QsPaths::shellStateDir() {
if (this->shellStateState == DirState::Unknown) {
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::StateLocation));
dir = QDir(dir.filePath("by-shell"));
dir = QDir(dir.filePath(this->shellId));
this->mShellStateDir = dir;
qCDebug(logPaths) << "Initialized state path:" << dir.path();
if (!dir.mkpath(".")) {
qCCritical(logPaths) << "Could not create state directory at" << dir.path();
this->shellStateState = DirState::Failed;
} else {
this->shellStateState = DirState::Ready;
}
}
// Returning no path on fail might result in files being written in unintended locations.
return this->mShellStateDir;
}
QDir QsPaths::shellCacheDir() {
if (this->shellCacheState == DirState::Unknown) {
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
dir = QDir(dir.filePath("by-shell"));
dir = QDir(dir.filePath(this->shellId));
this->mShellCacheDir = dir;
qCDebug(logPaths) << "Initialized cache path:" << dir.path();
if (!dir.mkpath(".")) {
qCCritical(logPaths) << "Could not create cache directory at" << dir.path();
this->shellCacheState = DirState::Failed;
} else {
this->shellCacheState = DirState::Ready;
}
}
// Returning no path on fail might result in files being written in unintended locations.
return this->mShellCacheDir;
}
void QsPaths::createLock() {
if (auto* runDir = this->instanceRunDir()) {
auto path = runDir->filePath("instance.lock");

View file

@ -24,7 +24,6 @@ public:
checkLock(const QString& path, InstanceLockInfo* info = nullptr, bool allowDead = false);
static QVector<InstanceLockInfo> collectInstances(const QString& path, bool fallbackDead = false);
QDir* cacheDir();
QDir* baseRunDir();
QDir* shellRunDir();
QDir* instanceRunDir();
@ -32,6 +31,10 @@ public:
void linkPathDir();
void createLock();
QDir shellDataDir();
QDir shellStateDir();
QDir shellCacheDir();
private:
enum class DirState : quint8 {
Unknown = 0,
@ -41,12 +44,17 @@ private:
QString shellId;
QString pathId;
QDir mCacheDir;
QDir mBaseRunDir;
QDir mShellRunDir;
QDir mInstanceRunDir;
DirState cacheState = DirState::Unknown;
DirState baseRunState = DirState::Unknown;
DirState shellRunState = DirState::Unknown;
DirState instanceRunState = DirState::Unknown;
QDir mShellDataDir;
QDir mShellStateDir;
QDir mShellCacheDir;
DirState shellDataState = DirState::Unknown;
DirState shellStateState = DirState::Unknown;
DirState shellCacheState = DirState::Unknown;
};

View file

@ -190,12 +190,16 @@ void QuickshellGlobal::setWatchFiles(bool watchFiles) { // NOLINT
QuickshellSettings::instance()->setWatchFiles(watchFiles);
}
QString QuickshellGlobal::cacheDir() const { // NOLINT
auto* dir = QsPaths::instance()->cacheDir();
if (dir) return dir->path();
QString QuickshellGlobal::dataDir() const { // NOLINT
return QsPaths::instance()->shellDataDir().path();
}
qCritical() << "Could not find cache dir.";
return "/quickshell-cache-not-found";
QString QuickshellGlobal::stateDir() const { // NOLINT
return QsPaths::instance()->shellStateDir().path();
}
QString QuickshellGlobal::cacheDir() const { // NOLINT
return QsPaths::instance()->shellCacheDir().path();
}
QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT

View file

@ -108,6 +108,14 @@ class QuickshellGlobal: public QObject {
/// If true then the configuration will be reloaded whenever any files change.
/// Defaults to true.
Q_PROPERTY(bool watchFiles READ watchFiles WRITE setWatchFiles NOTIFY watchFilesChanged);
/// The per-shell data directory.
///
/// Usually `~/.local/share/quickshell/by-shell/<shell-id>`
Q_PROPERTY(QString dataDir READ dataDir CONSTANT);
/// The per-shell state directory.
///
/// Usually `~/.local/state/quickshell/by-shell/<shell-id>`
Q_PROPERTY(QString stateDir READ stateDir CONSTANT);
/// The per-shell cache directory.
///
/// Usually `~/.cache/quickshell/by-shell/<shell-id>`
@ -156,6 +164,8 @@ public:
[[nodiscard]] bool watchFiles() const;
void setWatchFiles(bool watchFiles);
[[nodiscard]] QString dataDir() const;
[[nodiscard]] QString stateDir() const;
[[nodiscard]] QString cacheDir() const;
static QuickshellGlobal* create(QQmlEngine* engine, QJSEngine* /*unused*/);