forked from JoshuaFurman/omarchy-cloud-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.qml
More file actions
323 lines (279 loc) · 10.8 KB
/
Copy pathService.qml
File metadata and controls
323 lines (279 loc) · 10.8 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import QtQuick
import Quickshell
import Quickshell.Io
import "Model.js" as Model
// Headless singleton behind the Cloud plugin.
//
// A bar widget is instantiated once per monitor, so the polling lives here --
// the shell mounts exactly one service per plugin, and a two-monitor setup
// should not double the work.
//
// The service deliberately owns no mounts. `rclone mount` runs under systemd
// (see bin/omarchy-cloud-mount), because a mount parented to the shell process
// would be torn down by `omarchy restart shell` and by every plugin
// hot-reload during development, taking any open file with it. Everything
// here is therefore either a read of local state or a short-lived control
// command.
Item {
id: root
// Injected by the shell.
property var shell: null
property var settings: ({})
// ---------------------------------------------------------------- state
property bool rcloneInstalled: true
property string rcloneVersion: ""
property string mountRoot: ""
property var remotes: []
property bool refreshing: false
property string lastError: ""
property string actionStatus: ""
readonly property string aggregateState: Model.aggregateState(remotes)
readonly property string barSummary: Model.barSummary(remotes)
readonly property bool busy: statusProcess.running || controlProcess.running
// ---------------------------------------------------------------- settings
function setting(name, fallback) {
var value = settings ? settings[name] : undefined
return value === undefined || value === null ? fallback : value
}
function intSetting(name, fallback, min, max) {
var n = parseInt(String(setting(name, fallback)), 10)
if (!isFinite(n)) n = fallback
return Math.max(min, Math.min(max, n))
}
readonly property string configuredRoot: String(setting("mountRoot", "~/Cloud"))
readonly property int refreshIntervalSec: intSetting("refreshIntervalSec", 15, 5, 300)
readonly property int quotaIntervalMin: intSetting("quotaIntervalMin", 20, 5, 240)
readonly property string vfsCacheMode: String(setting("vfsCacheMode", "full"))
readonly property int cacheMaxSizeGB: intSetting("cacheMaxSizeGB", 8, 1, 512)
// ---------------------------------------------------------------- paths
//
// The shell injects `settings` into widgets but not the plugin's own
// directory, and first-party plugins cheat by reading OMARCHY_PATH -- which
// resolves to the packaged tree, not here. Qt.resolvedUrl is relative to
// this file, so it works wherever the plugin is installed.
function scriptPath(name) {
return Qt.resolvedUrl("bin/" + name).toString().replace(/^file:\/\//, "")
}
readonly property string statusScript: scriptPath("omarchy-cloud-status")
readonly property string mountScript: scriptPath("omarchy-cloud-mount")
readonly property string connectScript: scriptPath("omarchy-cloud-connect")
// ---------------------------------------------------------------- reading
property bool _statusIncludesQuota: false
property bool _quotaPending: false
function refresh(withQuota) {
if (statusProcess.running) {
// A cheap poll may already be running when the slow quota timer fires.
// Remember that request; otherwise two aligned repeating timers can
// starve quota refreshes forever.
if (withQuota === true && !_statusIncludesQuota) _quotaPending = true
return
}
var includeQuota = withQuota === true || _quotaPending
_quotaPending = false
_statusIncludesQuota = includeQuota
refreshing = true
var args = [statusScript, "--root", configuredRoot]
if (includeQuota) args.push("--with-quota")
args.push("--quota-age", String(quotaIntervalMin * 60))
statusProcess.command = ["python3"].concat(args)
statusProcess.running = true
}
function applyStatus(raw) {
var parsed = Model.parseStatus(raw)
if (!parsed.ok) {
lastError = String(parsed.error || "Could not read cloud status")
return
}
rcloneInstalled = parsed.rcloneInstalled === true
rcloneVersion = String(parsed.rcloneVersion || "")
mountRoot = String(parsed.mountRoot || "")
remotes = parsed.remotes || []
lastError = ""
}
// ---------------------------------------------------------------- control
// Reflect a mount/unmount immediately rather than waiting for the next poll:
// systemd takes a beat to settle, and a toggle that appears to do nothing
// for two seconds reads as broken. The poll corrects any optimism.
function setPending(name, state) {
var next = []
for (var i = 0; i < remotes.length; i++) {
var row = remotes[i]
if (row.name === name) {
var copy = {}
for (var key in row) copy[key] = row[key]
copy.state = state
next.push(copy)
} else {
next.push(row)
}
}
remotes = next
}
function runControl(args, failureMessage) {
if (controlProcess.running) return false
controlProcess.failureMessage = failureMessage || "Command failed"
controlProcess.command = ["bash", mountScript].concat(args)
controlProcess.running = true
return true
}
function mount(name) {
setPending(name, "mounting")
runControl(["start", name], "Could not mount " + name)
}
function unmount(name) {
setPending(name, "stopped")
runControl(["stop", name], "Could not unmount " + name)
}
function toggleMount(remote) {
if (!remote || !remote.name) return
if (remote.state === "mounted" || remote.state === "mounting") unmount(remote.name)
else if (remote.state !== "needs-auth") mount(remote.name)
}
function setAutoMount(remote, enabled) {
if (!remote || !remote.name) return
runControl([enabled ? "enable" : "disable", remote.name],
"Could not change login setting for " + remote.name)
}
// Pushes the plugin's settings to disk for systemd's benefit. Units start at
// login with no access to shell.json, so the mount flags have to be readable
// from a file the unit can source.
function pushSettings() {
if (controlProcess.running) return
runControl(["settings",
"MOUNT_ROOT=" + configuredRoot,
"VFS_CACHE_MODE=" + vfsCacheMode,
"CACHE_MAX_SIZE_GB=" + String(cacheMaxSizeGB)],
"Could not save mount settings")
}
// ---------------------------------------------------------------- actions
function openInFiles(remote) {
if (!remote || !remote.mountPath) return
Quickshell.execDetached(["uwsm-app", "--", "nautilus", Model.fileUri(remote.mountPath)])
}
readonly property string termScript: scriptPath("omarchy-cloud-term")
// The interactive scripts open a browser for OAuth and ask questions, so
// they run in a real terminal rather than being driven headlessly from here.
function runInTerminal(script, args) {
var command = [termScript, script]
if (args) command = command.concat(args)
Quickshell.execDetached(command)
settleTimer.restart()
}
function connectService() {
actionStatus = "Opening setup…"
actionStatusTimer.restart()
runInTerminal("omarchy-cloud-connect", [])
}
function configure(remote) {
if (!remote || !remote.name) return
actionStatus = "Opening settings…"
actionStatusTimer.restart()
runInTerminal("omarchy-cloud-configure", [remote.name])
}
function reconnect(remote) {
if (!remote || !remote.name) return
actionStatus = "Opening sign-in…"
actionStatusTimer.restart()
runInTerminal("omarchy-cloud-reconnect", [remote.name])
}
function installRclone() {
actionStatus = "Installing rclone…"
actionStatusTimer.restart()
runInTerminal("omarchy-cloud-install", [])
}
// ---------------------------------------------------------------- timers
Timer {
// Cheap: local files and one batched systemctl call, no network.
id: pollTimer
interval: root.refreshIntervalSec * 1000
repeat: true
running: true
triggeredOnStart: true
onTriggered: root.refresh(false)
}
Timer {
// Expensive: one network round trip per mounted remote, so it runs on its
// own slow schedule and the helper caches the answer on disk besides.
id: quotaTimer
interval: root.quotaIntervalMin * 60 * 1000
repeat: true
running: true
triggeredOnStart: true
onTriggered: root.refresh(true)
}
Timer {
// After an action that happens outside the shell (the wizard, a sign-in),
// poll a few times so the panel catches up without waiting a full cycle.
id: settleTimer
property int ticks: 0
interval: 3000
repeat: true
running: false
onTriggered: {
ticks += 1
root.refresh(false)
if (ticks >= 8) {
ticks = 0
settleTimer.running = false
}
}
onRunningChanged: if (running) ticks = 0
}
Timer {
id: actionStatusTimer
interval: 2600
repeat: false
onTriggered: root.actionStatus = ""
}
Timer {
// Settings arrive one property at a time as the shell applies them; wait
// for the burst to finish before writing the file and regenerating the
// unit.
id: settingsDebounce
interval: 800
repeat: false
onTriggered: root.pushSettings()
}
onConfiguredRootChanged: settingsDebounce.restart()
onVfsCacheModeChanged: settingsDebounce.restart()
onCacheMaxSizeGBChanged: settingsDebounce.restart()
// Write the settings file once at startup too. Leaving every default
// untouched changes no property, so the handlers above would never fire and
// systemd would be left with no settings file at all -- which works only for
// as long as the shell defaults and the script defaults agree.
Component.onCompleted: settingsDebounce.restart()
// ---------------------------------------------------------------- processes
Process {
id: statusProcess
running: false
command: []
stdout: StdioCollector { id: statusOut; waitForEnd: true }
stderr: StdioCollector { id: statusErr; waitForEnd: true }
onExited: function(exitCode) {
root.refreshing = false
if (exitCode === 0) root.applyStatus(statusOut.text)
else root.lastError = String(statusErr.text || "Could not read cloud status").substring(0, 160)
root._statusIncludesQuota = false
if (root._quotaPending) Qt.callLater(function() { root.refresh(true) })
}
}
Process {
id: controlProcess
property string failureMessage: ""
running: false
command: []
stdout: StdioCollector { id: controlOut; waitForEnd: true }
stderr: StdioCollector { id: controlErr; waitForEnd: true }
onExited: function(exitCode) {
if (exitCode !== 0) {
var reason = String(controlErr.text || controlOut.text || "").trim()
root.lastError = (failureMessage + (reason ? ": " + reason : "")).substring(0, 200)
root.actionStatus = ""
} else {
root.lastError = ""
}
// Either way, find out what actually happened.
settleTimer.restart()
}
}
}