summaryrefslogtreecommitdiff
path: root/utils/Directories.qml
blob: b71277c477a00478512ba0d3b04dcb50d68c3f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pragma Singleton
// pragma ComponentBehavior: Bound

import Qt.labs.platform
import QtQuick
import Quickshell
import Quickshell.Hyprland

Singleton {
	/**
 * Trims the File protocol off the input string
 * @param {string} str
 * @returns {string}
 */
	function trimFileProtocol(str) {
		return str.startsWith("file://") ? str.slice(7) : str;
	}

	/**
 * Extracts the file name from a file path
 * @param {string} str
 * @returns {string}
 */
	function fileNameForPath(str) {
		if (typeof str !== "string")
			return "";
		const trimmed = trimFileProtocol(str);
		return trimmed.split(/[\\/]/).pop();
	}

	/**
 * Removes the file extension from a file path or name
 * @param {string} str
 * @returns {string}
 */
	function trimFileExt(str) {
		if (typeof str !== "string")
			return "";
		const trimmed = trimFileProtocol(str);
		const lastDot = trimmed.lastIndexOf(".");
		if (lastDot > -1 && lastDot > trimmed.lastIndexOf("/")) {
			return trimmed.slice(0, lastDot);
		}
		return trimmed;
	}

	// XDG Dirs, with "file://"
	readonly property string config: StandardPaths.standardLocations(StandardPaths.ConfigLocation)[0]
	readonly property string state: StandardPaths.standardLocations(StandardPaths.StateLocation)[0]
	readonly property string cache: StandardPaths.standardLocations(StandardPaths.CacheLocation)[0]
	readonly property string pictures: StandardPaths.standardLocations(StandardPaths.PicturesLocation)[0]
	readonly property string downloads: StandardPaths.standardLocations(StandardPaths.DownloadLocation)[0]

	property string clipHistMetaDataPath: `${state}/cliphist.json`

	// Cleanup on init
	Component.onCompleted: {
		Quickshell.execDetached(["touch", trimFileProtocol(clipHistMetaDataPath)]);
	}
}