-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
142 lines (114 loc) · 3.4 KB
/
extension.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
import St from "gi://St";
import GObject from "gi://GObject";
import GLib from "gi://GLib";
import Shell from "gi://Shell";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
import * as MessageTray from "resource:///org/gnome/shell/ui/messageTray.js";
import * as Util from "resource:///org/gnome/shell/misc/util.js";
const Mainloop = imports.mainloop;
import {
Extension,
gettext as _,
} from "resource:///org/gnome/shell/extensions/extension.js";
const LIMIT = 80;
const INTERVAL = 5;
const COMMAND = `/bin/sh -c "LANG=C df ~ | tail -n1"`;
const QuotaMonitor = GObject.registerClass(
class QuotaMonitor extends PanelMenu.Button {
_init() {
super._init(0.0, "QuotaMonitor");
this.connect("button-press-event", this._openBaobab.bind(this));
this._initUI();
this.timer = Mainloop.timeout_add_seconds(
INTERVAL,
this._refresh.bind(this),
);
this._refresh();
}
_initUI() {
this.box = new St.BoxLayout();
this.icon = new St.Icon({
icon_name: "drive-harddisk-symbolic",
style_class: "system-status-icon",
});
this.percentage = new St.Label({
text: "0%",
style_class: "item",
});
this.box.add_child(this.icon);
this.box.add_child(this.percentage);
this.add_child(this.box);
}
destroy() {
if (this.timer) {
Mainloop.source_remove(this.timer);
this.timer = null;
}
super.destroy();
}
_openBaobab() {
const app = Shell.AppSystem.get_default().lookup_app(
"org.gnome.baobab.desktop",
);
if (app) {
app.activate();
} else {
Util.spawn(["baobab", GLib.get_home_dir()]);
}
}
_refresh() {
const [_in, out, _err] = GLib.spawn_command_line_sync(COMMAND);
const quota = new TextDecoder().decode(out).trim().split(/\s+/);
const current = Number.parseInt(quota[2]);
const maximum = Number.parseInt(quota[1]);
const percent = Math.round((100 * current) / maximum);
this.percentage.set_text(`${percent}%`);
if (percent >= LIMIT && !this.notified) {
this.notified = true;
notify(
_("Quota Alert"),
_("You are almost over your disk quota! Delete some files now."),
"drive-harddisk-symbolic",
);
}
if (percent < LIMIT && this.notified) {
this.notified = false;
}
if (this.timer) {
Mainloop.source_remove(this.timer);
this.timer = null;
}
this.timer = Mainloop.timeout_add_seconds(
INTERVAL,
this._refresh.bind(this),
);
}
},
);
const notify = (msg, details, icon) => {
const source = new MessageTray.Source({
title: msg,
iconName: icon,
});
Main.messageTray.add(source);
const notification = new MessageTray.Notification({
source: source,
title: msg,
body: details,
iconName: icon,
urgency: MessageTray.Urgency.HIGH,
});
source.addNotification(notification);
};
export default class QuotaMonitorExtension extends Extension {
enable() {
this.quotaMonitorIndicator = new QuotaMonitor();
Main.panel.addToStatusArea(this.metadata.uuid, this.quotaMonitorIndicator);
}
disable() {
this.quotaMonitorIndicator.destroy();
this.quotaMonitorIndicator = null;
}
}
/* vim:set sw=2 ts=2 et: */