forked from ziyagenc/phi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpihole.js
323 lines (272 loc) · 10.7 KB
/
mpihole.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
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 Gio from "gi://Gio";
import GLib from "gi://GLib";
import GObject from "gi://GObject";
import St from "gi://St";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as MessageTray from "resource:///org/gnome/shell/ui/messageTray.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
// PiholeClient v5
import { PiholeClient } from "./client.js";
// PiholeClient v6
import { PiholeClient6 } from "./client6.js";
import { HeaderItem, Line, MultiStatItem, TailItem } from "./mitems.js";
export const MPihole = GObject.registerClass(
{
GTypeName: "MPihole",
},
class MPihole extends GObject.Object {
constructor(me, settings, command) {
super();
this._me = me;
this._settings = settings;
this._configure();
this._makeMenu();
if (this._version1 == 0) {
this._piholeClient1 = new PiholeClient(this._url1, this._token1);
} else {
this._piholeClient1 = new PiholeClient6(this._url1, this._token1);
}
if (this._version2 == 0) {
this._piholeClient2 = new PiholeClient(this._url2, this._token2);
} else {
this._piholeClient2 = new PiholeClient6(this._url2, this._token2);
}
this._setHandlers();
this._notificationSource = null;
this._checkedUpdate = false;
if (command === "start") {
this._startUpdating();
this._isRunning = true;
}
}
_configure() {
this._url1 = this._settings.get_string("url1");
this._url2 = this._settings.get_string("url2");
this._token1 = this._settings.get_string("token1");
this._token2 = this._settings.get_string("token2");
this._name1 = this._settings.get_string("instance1");
this._name2 = this._settings.get_string("instance2");
this._version1 = this._settings.get_uint("version1");
this._version2 = this._settings.get_uint("version2");
this._interval = this._settings.get_uint("interval");
this._checkNewVersion = this._settings.get_boolean("check-new-version");
this._showSensorData = this._settings.get_boolean("show-sensor-data");
}
_makeMenu() {
this._menuButton = new PanelMenu.Button(
0.0,
this._me.metadata.name,
false
);
this._menuButton.icon = new St.Icon({
style_class: "system-status-icon",
});
this._menuButton.icon.gicon = Gio.icon_new_for_string(
this._me.path + "/icons/phi-symbolic.svg"
);
this._menuButton.add_child(this._menuButton.icon);
this._headerItem = new HeaderItem(this._name1, this._name2);
this._line1 = new Line();
this._totalQueriesItem = new MultiStatItem(_("Total Queries"));
this._queriesBlockedItem = new MultiStatItem(_("Queries Blocked"));
this._percentageBlockedItem = new MultiStatItem(_("Percentage Blocked"));
this._domainsOnAdlistsItem = new MultiStatItem(_("Domains on AdLists"));
if (
(this._version1 == 1 || this._version2 == 1) &&
this._showSensorData
) {
this._line2 = new Line();
this._cpuUtilItem = new MultiStatItem(_("CPU"));
this._memoryUsageItem = new MultiStatItem(_("Memory Usage"));
this._temperatureItem = new MultiStatItem(_("Temperature"));
}
this._line3 = new Line();
this._settingsItem = new TailItem();
const statsBox = new St.BoxLayout({ vertical: true });
this._menuButton.menu.box.add_child(statsBox);
statsBox.add_child(this._headerItem);
statsBox.add_child(this._line1);
statsBox.add_child(this._totalQueriesItem);
statsBox.add_child(this._queriesBlockedItem);
statsBox.add_child(this._percentageBlockedItem);
statsBox.add_child(this._domainsOnAdlistsItem);
if (
(this._version1 == 1 || this._version2 == 1) &&
this._showSensorData
) {
statsBox.add_child(this._line2);
statsBox.add_child(this._cpuUtilItem);
statsBox.add_child(this._memoryUsageItem);
statsBox.add_child(this._temperatureItem);
}
statsBox.add_child(this._line3);
statsBox.add_child(this._settingsItem);
Main.panel.addToStatusArea(this._me.uuid, this._menuButton);
}
_setHandlers() {
this._headerItem._button1.connect("clicked", () => {
const newState = !this._headerItem.state1;
this._piholeClient1.togglePihole(newState);
this._headerItem.state1 = newState;
this._updateFirstInstance(newState);
this._updatePanelIcon();
});
this._headerItem._button2.connect("clicked", () => {
const newState = !this._headerItem.state2;
this._piholeClient2.togglePihole(newState);
this._headerItem.state2 = newState;
this._updateSecondInstance(newState);
this._updatePanelIcon();
});
this._settingsItem._button.connect("clicked", () => {
this._me.openPreferences();
});
}
_updateFirstInstance(newState) {
const newButtonStyle = newState ? "phi-button" : "phi-button disabled";
const newLabelStyle = newState ? "stat-value" : "stat-value ver-value";
this._headerItem._button1.set_style_class_name(newButtonStyle);
this._totalQueriesItem._label2.set_style_class_name(newLabelStyle);
this._queriesBlockedItem._label2.set_style_class_name(newLabelStyle);
this._percentageBlockedItem._label2.set_style_class_name(newLabelStyle);
this._domainsOnAdlistsItem._label2.set_style_class_name(newLabelStyle);
if (
(this._version1 == 1 || this._version2 == 1) &&
this._showSensorData
) {
this._cpuUtilItem._label2.set_style_class_name(newLabelStyle);
this._memoryUsageItem._label2.set_style_class_name(newLabelStyle);
this._temperatureItem._label2.set_style_class_name(newLabelStyle);
}
}
_updateSecondInstance(newState) {
const newButtonStyle = newState ? "phi-button" : "phi-button disabled";
const newLabelStyle = newState ? "stat-value" : "stat-value ver-value";
this._headerItem._button2.set_style_class_name(newButtonStyle);
this._totalQueriesItem._label3.set_style_class_name(newLabelStyle);
this._queriesBlockedItem._label3.set_style_class_name(newLabelStyle);
this._percentageBlockedItem._label3.set_style_class_name(newLabelStyle);
this._domainsOnAdlistsItem._label3.set_style_class_name(newLabelStyle);
if (
(this._version1 == 1 || this._version2 == 1) &&
this._showSensorData
) {
this._cpuUtilItem._label3.set_style_class_name(newLabelStyle);
this._memoryUsageItem._label3.set_style_class_name(newLabelStyle);
this._temperatureItem._label3.set_style_class_name(newLabelStyle);
}
}
_updatePanelIcon() {
const state = this._headerItem.state1 && this._headerItem.state2;
const iconStyle = state
? "system-status-icon"
: "system-status-icon icon-disabled";
this._menuButton.icon.set_style_class_name(iconStyle);
}
_getNotificationSource() {
if (!this._notificationSource) {
this._notificationSource = new MessageTray.Source({
title: "Phi",
icon: this._menuButton.icon.gicon,
});
this._notificationSource.connect("destroy", (_source) => {
this._notificationSource = null;
});
Main.messageTray.add(this._notificationSource);
}
return this._notificationSource;
}
_showNotification(message) {
const notification = new MessageTray.Notification({
source: this._getNotificationSource(),
title: message,
isTransient: true,
});
this._getNotificationSource().addNotification(notification);
}
async _updateUI() {
await Promise.all([
this._piholeClient1.fetchData(),
this._piholeClient2.fetchData(),
]);
// Make sure that the menu is there.
if (!this._menuButton) return;
const data1 = this._piholeClient1.data;
const data2 = this._piholeClient2.data;
const state1 = data1.blocking === "enabled";
const state2 = data2.blocking === "enabled";
this._headerItem.state1 = state1;
this._headerItem.state2 = state2;
this._updateFirstInstance(state1);
this._updateSecondInstance(state2);
this._updatePanelIcon();
this._totalQueriesItem.text1 = data1.dns_queries_today;
this._queriesBlockedItem.text1 = data1.ads_blocked_today;
this._percentageBlockedItem.text1 = data1.ads_percentage_today + " %";
this._domainsOnAdlistsItem.text1 = data1.domains_being_blocked;
this._totalQueriesItem.text2 = data2.dns_queries_today;
this._queriesBlockedItem.text2 = data2.ads_blocked_today;
this._percentageBlockedItem.text2 = data2.ads_percentage_today + " %";
this._domainsOnAdlistsItem.text2 = data2.domains_being_blocked;
if (this._showSensorData) {
if (this._version1 == 1) {
this._cpuUtilItem.text1 = data1.cpu + " %";
this._memoryUsageItem.text1 = data1.memory + " %";
this._temperatureItem.text1 = data1.temp;
} else {
this._cpuUtilItem.text1 = "n/a";
this._memoryUsageItem.text1 = "n/a";
this._temperatureItem.text1 = "n/a";
}
if (this._version2 == 1) {
this._cpuUtilItem.text2 = data2.cpu + " %";
this._memoryUsageItem.text2 = data2.memory + " %";
this._temperatureItem.text2 = data2.temp;
} else {
this._cpuUtilItem.text2 = "n/a";
this._memoryUsageItem.text2 = "n/a";
this._temperatureItem.text2 = "n/a";
}
}
this._settingsItem.text1 = data1.version;
this._settingsItem.text2 = data2.version;
if (this._checkNewVersion && !this._checkedUpdate) {
if (data1.updateExists) {
this._showNotification(`Update available for ${this._name1}.`);
}
if (data2.updateExists) {
this._showNotification(`Update available for ${this._name2}.`);
}
this._checkedUpdate = true;
}
}
_startUpdating() {
this._updateUI();
if (this._fetchTimeoutId) {
GLib.Source.remove(this._fetchTimeoutId);
}
this._fetchTimeoutId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
this._interval,
() => {
this._updateUI();
return GLib.SOURCE_CONTINUE;
}
);
}
destroy() {
if (this._fetchTimeoutId) {
GLib.Source.remove(this._fetchTimeoutId);
}
this._settings = null;
this._notificationSource?.destroy();
this._notificationSource = null;
this._piholeClient1?.destroy();
this._piholeClient1 = null;
this._piholeClient2?.destroy();
this._piholeClient2 = null;
this._menuButton.destroy();
this._menuButton = null;
}
}
);