-
Notifications
You must be signed in to change notification settings - Fork 53
/
panel-shell.vala
157 lines (144 loc) · 4.66 KB
/
panel-shell.vala
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
using Gtk;
[DBus (name = "org.gnome.SessionManager.EndSessionDialog")]
public class PanelEndSessionDialog: Object {
public signal void canceled();
public signal void confirmed_logout();
public signal void confirmed_reboot();
public signal void confirmed_shutdown();
public signal void closed();
private Label label = null;
private int countdown = 10;
private uint update_title_source = -1;
private string current_title;
private uint default_action = 0;
private Dialog dialog = null;
private bool action_called = false;
public PanelEndSessionDialog() {
label = new Label(null);
label.justify = Gtk.Justification.CENTER;
}
void start_update_title (string title) {
countdown = 10;
current_title = title;
update_title();
update_title_source = GLib.Timeout.add_seconds(1, update_title);
}
bool update_title() {
label.set_markup("<span font-weight='heavy' font_size='xx-large'>%d</span>\n".printf(countdown) + current_title);
if (countdown <= 0) {
countdown = 0;
launch_action(default_action);
if (dialog != null) {
dialog.destroy();
}
return false;
}
countdown = countdown - 1;
return true;
}
void show_dialog (uint type, string title, string button_text) {
default_action = type;
var d = new Dialog.with_buttons(_("Confirmation"), null, Gtk.DialogFlags.MODAL, null);
dialog = d;
d.fullscreen();
if (update_title_source >= 0) {
GLib.Source.remove(update_title_source);
update_title_source = -1;
}
var cancel_title = _("\nYou can prevent it by pressing <b>Cancel</b> button");
start_update_title(title + cancel_title);
(d.get_content_area() as Gtk.Box).pack_start(label, true, true);
label.show();
d.add_button(button_text, 1);
d.add_button(_("Cancel"), 2);
d.set_default_response(2);
d.response.connect((id) => {
GLib.Source.remove(update_title_source);
update_title_source = -1;
if (id == -4 || id == 2) { // closed or canceled
canceled();
if (id == 2) {
d.destroy();
}
} else {
launch_action(type);
d.destroy();
}
});
d.key_press_event.connect ((e) => {
if (Gdk.keyval_name(e.keyval) == "Escape") {
canceled();
d.destroy();
}
return false;
});
d.run();
}
void launch_action(uint type) {
if (action_called == true) {
return;
}
action_called = true;
switch (type) {
case 3:
case 2:
confirmed_reboot();
break;
case 1:
confirmed_shutdown();
break;
case 0:
default:
confirmed_logout();
break;
}
}
public async void open (uint type, uint timestamp, uint waiting_time, ObjectPath[] inhibitors) {
var title = "";
var button_text = "";
switch(type) {
case 1: // shutdown
button_text = _("Shutdown Now");
title = _("This computer will go to a shutdown.");
break;
case 2: // restart
button_text = _("Restart Now");
title = _("This computer will be restarted.");
break;
case 3: // restart and install
button_text = _("Restart Now");
title = _("This computer will be restarted.");
break;
case 0: // logout
default: // fallthrough
button_text = _("Logout Now");
title = _("Your account will be logged-out.");
break;
}
show_dialog(type, title, button_text);
}
public void close () {
}
}
public class PanelShell : Object {
public PanelShell() {
Bus.own_name(
BusType.SESSION,
"org.gnome.Shell",
0,
() => {},
on_name_acquired,
() => {
stdout.printf ("Unable to claim Shell from gnome-session");
}
);
}
void on_name_acquired(DBusConnection conn, string name) {
try {
conn.register_object("/org/gnome/SessionManager/EndSessionDialog", new PanelEndSessionDialog());
stderr.printf("EndSessionDialog hooked\n");
} catch (IOError e) {
stderr.printf("Unable to hook EndSessionDialog\n");
}
}
}