-
Notifications
You must be signed in to change notification settings - Fork 1
/
toggle.js
119 lines (99 loc) · 3.83 KB
/
toggle.js
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
import Gio from "gi://Gio"
import GObject from "gi://GObject"
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js'
import { QuickMenuToggle, SystemIndicator } from 'resource:///org/gnome/shell/ui/quickSettings.js'
import { spawnCommandLine } from "resource:///org/gnome/shell/misc/util.js"
import * as Main from 'resource:///org/gnome/shell/ui/main.js'
const statusPattern =
/(\(running\))/
const SyncthingStatus = Object.freeze({
Running: "(running)",
Error: "Error",
})
const ServiceToggle = GObject.registerClass(
class ServiceToggle extends QuickMenuToggle {
constructor(extensionObject, syncthingIcon) {
super({
title: _('Syncthing'),
gicon: syncthingIcon,
toggleMode: true,
subtitle: 'Loading'
})
// Add a header with an icon, title and optional subtitle. This is
// recommended for consistency with other quick settings menus.
this.menu.setHeader(syncthingIcon, _('Syncthing'))
this._settings = extensionObject.getSettings()
// Add a section of items to the menu
this._itemsSection = new PopupMenu.PopupMenuSection()
this._itemsSection.addAction(_('Open Web GUI'),
() => {
// Open the URL in the default browser
const webGuiUrl = 'http://localhost:' + this._settings.get_int('port')
try {
Gio.app_info_launch_default_for_uri(webGuiUrl, null)
} catch (e) {
logError(e, 'Failed to open URL')
}
})
// this.menu.addMenuItem(this._devicesSection)
this.menu.addMenuItem(this._itemsSection)
// Add an entry-point for more settings
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem())
const settingsItem = this.menu.addAction('Extension Settings',
() => extensionObject.openPreferences())
// Ensure the settings are unavailable when the screen is locked
settingsItem.visible = Main.sessionMode.allowSettings
this.menu._settingsActions[extensionObject.uuid] = settingsItem
}
})
export var ServiceIndicator = GObject.registerClass(
class ServiceIndicator extends SystemIndicator {
constructor(extensionObject) {
super()
const syncthingIcon = Gio.icon_new_for_string(
extensionObject.path + "/icons/syncthing-symbolic.svg"
)
this._indicator = this._addIndicator()
this._indicator.gicon = syncthingIcon
this._settings = extensionObject.getSettings()
this._toggle = new ServiceToggle(extensionObject, syncthingIcon)
this.quickSettingsItems.push(this._toggle)
this._toggle.connect("clicked", async () => {
let cmdline = `systemctl --user ${!this._toggle.checked ? "stop" : "start"} syncthing.service`
spawnCommandLine(cmdline)
await this.checkStatus()
// if the appropriate setting is enabled (default, also enable or disable the service)
// not using enable --now because it's way slower and bugs the status.
if (!this._settings.get_boolean('start-stop-only')) {
cmdline = `systemctl --user ${!this._toggle.checked ? "disable" : "enable"} syncthing.service`
spawnCommandLine(cmdline)
}
})
}
async checkStatus() {
try {
const proc = Gio.Subprocess.new(
["systemctl", "--user", "status", "syncthing.service"],
Gio.SubprocessFlags.STDOUT_PIPE
)
const stdout = await new Promise((resolve, reject) => {
proc.communicate_utf8_async(null, null, (proc, res) => {
let [, stdout, stderr] = proc.communicate_utf8_finish(res)
resolve(stdout)
})
})
const status = statusPattern.exec(stdout)?.[1]
this.updateStatus(status == "(running)")
return SyncthingStatus[status]
} catch (err) {
this.updateStatus(false, SyncthingStatus.Error)
logError("Err checking status:", err)
return SyncthingStatus.Error
}
}
updateStatus(isActive) {
this._indicator.visible = isActive
let optionalStatus = isActive ? "Running" : "Stopped"
this._toggle.set({ checked: isActive, subtitle: optionalStatus })
}
})