forked from quickshell/quickshell
		
	core/qmlglobal: expose dataDir and stateDir
This commit is contained in:
		
							parent
							
								
									69c7f4fe77
								
							
						
					
					
						commit
						c1c24c2998
					
				
					 4 changed files with 96 additions and 30 deletions
				
			
		| 
						 | 
					@ -48,28 +48,6 @@ QString QsPaths::ipcPath(const QString& id) {
 | 
				
			||||||
	return QDir(QsPaths::basePath(id)).filePath("ipc.sock");
 | 
						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() {
 | 
					QDir* QsPaths::baseRunDir() {
 | 
				
			||||||
	if (this->baseRunState == DirState::Unknown) {
 | 
						if (this->baseRunState == DirState::Unknown) {
 | 
				
			||||||
		auto runtimeDir = qEnvironmentVariable("XDG_RUNTIME_DIR");
 | 
							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() {
 | 
					void QsPaths::createLock() {
 | 
				
			||||||
	if (auto* runDir = this->instanceRunDir()) {
 | 
						if (auto* runDir = this->instanceRunDir()) {
 | 
				
			||||||
		auto path = runDir->filePath("instance.lock");
 | 
							auto path = runDir->filePath("instance.lock");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,6 @@ public:
 | 
				
			||||||
	checkLock(const QString& path, InstanceLockInfo* info = nullptr, bool allowDead = false);
 | 
						checkLock(const QString& path, InstanceLockInfo* info = nullptr, bool allowDead = false);
 | 
				
			||||||
	static QVector<InstanceLockInfo> collectInstances(const QString& path, bool fallbackDead = false);
 | 
						static QVector<InstanceLockInfo> collectInstances(const QString& path, bool fallbackDead = false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QDir* cacheDir();
 | 
					 | 
				
			||||||
	QDir* baseRunDir();
 | 
						QDir* baseRunDir();
 | 
				
			||||||
	QDir* shellRunDir();
 | 
						QDir* shellRunDir();
 | 
				
			||||||
	QDir* instanceRunDir();
 | 
						QDir* instanceRunDir();
 | 
				
			||||||
| 
						 | 
					@ -32,6 +31,10 @@ public:
 | 
				
			||||||
	void linkPathDir();
 | 
						void linkPathDir();
 | 
				
			||||||
	void createLock();
 | 
						void createLock();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						QDir shellDataDir();
 | 
				
			||||||
 | 
						QDir shellStateDir();
 | 
				
			||||||
 | 
						QDir shellCacheDir();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	enum class DirState : quint8 {
 | 
						enum class DirState : quint8 {
 | 
				
			||||||
		Unknown = 0,
 | 
							Unknown = 0,
 | 
				
			||||||
| 
						 | 
					@ -41,12 +44,17 @@ private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QString shellId;
 | 
						QString shellId;
 | 
				
			||||||
	QString pathId;
 | 
						QString pathId;
 | 
				
			||||||
	QDir mCacheDir;
 | 
					 | 
				
			||||||
	QDir mBaseRunDir;
 | 
						QDir mBaseRunDir;
 | 
				
			||||||
	QDir mShellRunDir;
 | 
						QDir mShellRunDir;
 | 
				
			||||||
	QDir mInstanceRunDir;
 | 
						QDir mInstanceRunDir;
 | 
				
			||||||
	DirState cacheState = DirState::Unknown;
 | 
					 | 
				
			||||||
	DirState baseRunState = DirState::Unknown;
 | 
						DirState baseRunState = DirState::Unknown;
 | 
				
			||||||
	DirState shellRunState = DirState::Unknown;
 | 
						DirState shellRunState = DirState::Unknown;
 | 
				
			||||||
	DirState instanceRunState = DirState::Unknown;
 | 
						DirState instanceRunState = DirState::Unknown;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						QDir mShellDataDir;
 | 
				
			||||||
 | 
						QDir mShellStateDir;
 | 
				
			||||||
 | 
						QDir mShellCacheDir;
 | 
				
			||||||
 | 
						DirState shellDataState = DirState::Unknown;
 | 
				
			||||||
 | 
						DirState shellStateState = DirState::Unknown;
 | 
				
			||||||
 | 
						DirState shellCacheState = DirState::Unknown;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -190,12 +190,16 @@ void QuickshellGlobal::setWatchFiles(bool watchFiles) { // NOLINT
 | 
				
			||||||
	QuickshellSettings::instance()->setWatchFiles(watchFiles);
 | 
						QuickshellSettings::instance()->setWatchFiles(watchFiles);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QString QuickshellGlobal::cacheDir() const { // NOLINT
 | 
					QString QuickshellGlobal::dataDir() const { // NOLINT
 | 
				
			||||||
	auto* dir = QsPaths::instance()->cacheDir();
 | 
						return QsPaths::instance()->shellDataDir().path();
 | 
				
			||||||
	if (dir) return dir->path();
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	qCritical() << "Could not find cache dir.";
 | 
					QString QuickshellGlobal::stateDir() const { // NOLINT
 | 
				
			||||||
	return "/quickshell-cache-not-found";
 | 
						return QsPaths::instance()->shellStateDir().path();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QString QuickshellGlobal::cacheDir() const { // NOLINT
 | 
				
			||||||
 | 
						return QsPaths::instance()->shellCacheDir().path();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
 | 
					QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -108,6 +108,14 @@ class QuickshellGlobal: public QObject {
 | 
				
			||||||
	/// If true then the configuration will be reloaded whenever any files change.
 | 
						/// If true then the configuration will be reloaded whenever any files change.
 | 
				
			||||||
	/// Defaults to true.
 | 
						/// Defaults to true.
 | 
				
			||||||
	Q_PROPERTY(bool watchFiles READ watchFiles WRITE setWatchFiles NOTIFY watchFilesChanged);
 | 
						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.
 | 
						/// The per-shell cache directory.
 | 
				
			||||||
	///
 | 
						///
 | 
				
			||||||
	/// Usually `~/.cache/quickshell/by-shell/<shell-id>`
 | 
						/// Usually `~/.cache/quickshell/by-shell/<shell-id>`
 | 
				
			||||||
| 
						 | 
					@ -156,6 +164,8 @@ public:
 | 
				
			||||||
	[[nodiscard]] bool watchFiles() const;
 | 
						[[nodiscard]] bool watchFiles() const;
 | 
				
			||||||
	void setWatchFiles(bool watchFiles);
 | 
						void setWatchFiles(bool watchFiles);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						[[nodiscard]] QString dataDir() const;
 | 
				
			||||||
 | 
						[[nodiscard]] QString stateDir() const;
 | 
				
			||||||
	[[nodiscard]] QString cacheDir() const;
 | 
						[[nodiscard]] QString cacheDir() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	static QuickshellGlobal* create(QQmlEngine* engine, QJSEngine* /*unused*/);
 | 
						static QuickshellGlobal* create(QQmlEngine* engine, QJSEngine* /*unused*/);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue