Skip to content

Commit 6f3275b

Browse files
committed
core/qmlglobal: expose dataDir and stateDir
1 parent 5cf56f4 commit 6f3275b

File tree

4 files changed

+96
-30
lines changed

4 files changed

+96
-30
lines changed

src/core/paths.cpp

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,6 @@ QString QsPaths::ipcPath(const QString& id) {
4848
return QDir(QsPaths::basePath(id)).filePath("ipc.sock");
4949
}
5050

51-
QDir* QsPaths::cacheDir() {
52-
if (this->cacheState == DirState::Unknown) {
53-
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
54-
dir = QDir(dir.filePath("by-shell"));
55-
dir = QDir(dir.filePath(this->shellId));
56-
this->mCacheDir = dir;
57-
58-
qCDebug(logPaths) << "Initialized cache path:" << dir.path();
59-
60-
if (!dir.mkpath(".")) {
61-
qCCritical(logPaths) << "Could not create cache directory at" << dir.path();
62-
63-
this->cacheState = DirState::Failed;
64-
} else {
65-
this->cacheState = DirState::Ready;
66-
}
67-
}
68-
69-
if (this->cacheState == DirState::Failed) return nullptr;
70-
else return &this->mCacheDir;
71-
}
72-
7351
QDir* QsPaths::baseRunDir() {
7452
if (this->baseRunState == DirState::Unknown) {
7553
auto runtimeDir = qEnvironmentVariable("XDG_RUNTIME_DIR");
@@ -230,6 +208,72 @@ void QsPaths::linkPathDir() {
230208
}
231209
}
232210

211+
QDir QsPaths::shellDataDir() {
212+
if (this->shellDataState == DirState::Unknown) {
213+
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
214+
dir = QDir(dir.filePath("by-shell"));
215+
dir = QDir(dir.filePath(this->shellId));
216+
this->mShellDataDir = dir;
217+
218+
qCDebug(logPaths) << "Initialized data path:" << dir.path();
219+
220+
if (!dir.mkpath(".")) {
221+
qCCritical(logPaths) << "Could not create data directory at" << dir.path();
222+
223+
this->shellDataState = DirState::Failed;
224+
} else {
225+
this->shellDataState = DirState::Ready;
226+
}
227+
}
228+
229+
// Returning no path on fail might result in files being written in unintended locations.
230+
return this->mShellDataDir;
231+
}
232+
233+
QDir QsPaths::shellStateDir() {
234+
if (this->shellStateState == DirState::Unknown) {
235+
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::StateLocation));
236+
dir = QDir(dir.filePath("by-shell"));
237+
dir = QDir(dir.filePath(this->shellId));
238+
this->mShellStateDir = dir;
239+
240+
qCDebug(logPaths) << "Initialized state path:" << dir.path();
241+
242+
if (!dir.mkpath(".")) {
243+
qCCritical(logPaths) << "Could not create state directory at" << dir.path();
244+
245+
this->shellStateState = DirState::Failed;
246+
} else {
247+
this->shellStateState = DirState::Ready;
248+
}
249+
}
250+
251+
// Returning no path on fail might result in files being written in unintended locations.
252+
return this->mShellStateDir;
253+
}
254+
255+
QDir QsPaths::shellCacheDir() {
256+
if (this->shellCacheState == DirState::Unknown) {
257+
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
258+
dir = QDir(dir.filePath("by-shell"));
259+
dir = QDir(dir.filePath(this->shellId));
260+
this->mShellCacheDir = dir;
261+
262+
qCDebug(logPaths) << "Initialized cache path:" << dir.path();
263+
264+
if (!dir.mkpath(".")) {
265+
qCCritical(logPaths) << "Could not create cache directory at" << dir.path();
266+
267+
this->shellCacheState = DirState::Failed;
268+
} else {
269+
this->shellCacheState = DirState::Ready;
270+
}
271+
}
272+
273+
// Returning no path on fail might result in files being written in unintended locations.
274+
return this->mShellCacheDir;
275+
}
276+
233277
void QsPaths::createLock() {
234278
if (auto* runDir = this->instanceRunDir()) {
235279
auto path = runDir->filePath("instance.lock");

src/core/paths.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ class QsPaths {
2424
checkLock(const QString& path, InstanceLockInfo* info = nullptr, bool allowDead = false);
2525
static QVector<InstanceLockInfo> collectInstances(const QString& path, bool fallbackDead = false);
2626

27-
QDir* cacheDir();
2827
QDir* baseRunDir();
2928
QDir* shellRunDir();
3029
QDir* instanceRunDir();
3130
void linkRunDir();
3231
void linkPathDir();
3332
void createLock();
3433

34+
QDir shellDataDir();
35+
QDir shellStateDir();
36+
QDir shellCacheDir();
37+
3538
private:
3639
enum class DirState : quint8 {
3740
Unknown = 0,
@@ -41,12 +44,17 @@ class QsPaths {
4144

4245
QString shellId;
4346
QString pathId;
44-
QDir mCacheDir;
4547
QDir mBaseRunDir;
4648
QDir mShellRunDir;
4749
QDir mInstanceRunDir;
48-
DirState cacheState = DirState::Unknown;
4950
DirState baseRunState = DirState::Unknown;
5051
DirState shellRunState = DirState::Unknown;
5152
DirState instanceRunState = DirState::Unknown;
53+
54+
QDir mShellDataDir;
55+
QDir mShellStateDir;
56+
QDir mShellCacheDir;
57+
DirState shellDataState = DirState::Unknown;
58+
DirState shellStateState = DirState::Unknown;
59+
DirState shellCacheState = DirState::Unknown;
5260
};

src/core/qmlglobal.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,16 @@ void QuickshellGlobal::setWatchFiles(bool watchFiles) { // NOLINT
190190
QuickshellSettings::instance()->setWatchFiles(watchFiles);
191191
}
192192

193-
QString QuickshellGlobal::cacheDir() const { // NOLINT
194-
auto* dir = QsPaths::instance()->cacheDir();
195-
if (dir) return dir->path();
193+
QString QuickshellGlobal::dataDir() const { // NOLINT
194+
return QsPaths::instance()->shellDataDir().path();
195+
}
196196

197-
qCritical() << "Could not find cache dir.";
198-
return "/quickshell-cache-not-found";
197+
QString QuickshellGlobal::stateDir() const { // NOLINT
198+
return QsPaths::instance()->shellStateDir().path();
199+
}
200+
201+
QString QuickshellGlobal::cacheDir() const { // NOLINT
202+
return QsPaths::instance()->shellCacheDir().path();
199203
}
200204

201205
QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT

src/core/qmlglobal.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class QuickshellGlobal: public QObject {
108108
/// If true then the configuration will be reloaded whenever any files change.
109109
/// Defaults to true.
110110
Q_PROPERTY(bool watchFiles READ watchFiles WRITE setWatchFiles NOTIFY watchFilesChanged);
111+
/// The per-shell data directory.
112+
///
113+
/// Usually `~/.local/share/quickshell/by-shell/<shell-id>`
114+
Q_PROPERTY(QString dataDir READ dataDir CONSTANT);
115+
/// The per-shell state directory.
116+
///
117+
/// Usually `~/.local/state/quickshell/by-shell/<shell-id>`
118+
Q_PROPERTY(QString stateDir READ stateDir CONSTANT);
111119
/// The per-shell cache directory.
112120
///
113121
/// Usually `~/.cache/quickshell/by-shell/<shell-id>`
@@ -156,6 +164,8 @@ class QuickshellGlobal: public QObject {
156164
[[nodiscard]] bool watchFiles() const;
157165
void setWatchFiles(bool watchFiles);
158166

167+
[[nodiscard]] QString dataDir() const;
168+
[[nodiscard]] QString stateDir() const;
159169
[[nodiscard]] QString cacheDir() const;
160170

161171
static QuickshellGlobal* create(QQmlEngine* engine, QJSEngine* /*unused*/);

0 commit comments

Comments
 (0)