6 public class LeftLabel : Label {
7 public LeftLabel(string? text = null) {
9 set_markup("<b>%s</b>".printf(text));
15 public class RightLabel : Label {
16 public RightLabel(string? text = null) {
19 set_ellipsize(EllipsizeMode.START);
23 public void set_text_or_na(string? text = null) {
24 if (text == null || text == "")
25 set_markup("<i>n/a</i>");
31 public class MainWindow : Window {
33 private TreeView unit_view;
34 private TreeView job_view;
36 private ListStore unit_model;
37 private ListStore job_model;
39 private Button start_button;
40 private Button stop_button;
41 private Button restart_button;
42 private Button reload_button;
43 private Button cancel_button;
45 private Connection bus;
46 private Manager manager;
48 private RightLabel unit_id_label;
49 private RightLabel unit_description_label;
50 private RightLabel unit_load_state_label;
51 private RightLabel unit_active_state_label;
52 private RightLabel unit_load_path_label;
53 private RightLabel unit_active_enter_timestamp_label;
54 private RightLabel unit_active_exit_timestamp_label;
55 private RightLabel unit_can_start_label;
56 private RightLabel unit_can_reload_label;
58 private RightLabel job_id_label;
59 private RightLabel job_state_label;
60 private RightLabel job_type_label;
62 public MainWindow() throws DBus.Error {
64 position = WindowPosition.CENTER;
65 set_default_size(1000, 700);
67 destroy.connect(Gtk.main_quit);
69 Notebook notebook = new Notebook();
72 Box unit_vbox = new VBox(false, 6);
73 notebook.append_page(unit_vbox, new Label("Units"));
74 unit_vbox.set_border_width(12);
76 Box job_vbox = new VBox(false, 6);
77 notebook.append_page(job_vbox, new Label("Jobs"));
78 job_vbox.set_border_width(12);
81 unit_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string));
82 job_model = new ListStore(5, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string));
84 unit_view = new TreeView.with_model(unit_model);
85 job_view = new TreeView.with_model(job_model);
87 unit_view.cursor_changed.connect(unit_changed);
88 job_view.cursor_changed.connect(job_changed);
90 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
91 unit_view.insert_column_with_attributes(-1, "Description", new CellRendererText(), "text", 1);
92 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
93 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
94 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 4);
96 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
97 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
98 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
99 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
101 ScrolledWindow scroll = new ScrolledWindow(null, null);
102 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
103 scroll.set_shadow_type(ShadowType.IN);
104 scroll.add(unit_view);
105 unit_vbox.pack_start(scroll, true, true, 0);
107 scroll = new ScrolledWindow(null, null);
108 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
109 scroll.set_shadow_type(ShadowType.IN);
110 scroll.add(job_view);
111 job_vbox.pack_start(scroll, true, true, 0);
113 unit_id_label = new RightLabel();
114 unit_description_label = new RightLabel();
115 unit_load_state_label = new RightLabel();
116 unit_active_state_label = new RightLabel();
117 unit_load_path_label = new RightLabel();
118 unit_active_enter_timestamp_label = new RightLabel();
119 unit_active_exit_timestamp_label = new RightLabel();
120 unit_can_start_label = new RightLabel();
121 unit_can_reload_label = new RightLabel();
123 job_id_label = new RightLabel();
124 job_state_label = new RightLabel();
125 job_type_label = new RightLabel();
127 Table unit_table = new Table(9, 2, false);
128 unit_table.set_row_spacings(6);
129 unit_table.set_border_width(12);
130 unit_vbox.pack_start(unit_table, false, true, 0);
132 Table job_table = new Table(2, 2, false);
133 job_table.set_row_spacings(6);
134 job_table.set_border_width(12);
135 job_vbox.pack_start(job_table, false, true, 0);
137 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
138 unit_table.attach(unit_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
139 unit_table.attach(new LeftLabel("Description:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
140 unit_table.attach(unit_description_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
141 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
142 unit_table.attach(unit_load_state_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
143 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
144 unit_table.attach(unit_active_state_label, 1, 2, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
145 unit_table.attach(new LeftLabel("Load Path:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
146 unit_table.attach(unit_load_path_label, 1, 2, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
147 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
148 unit_table.attach(unit_active_enter_timestamp_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
149 unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
150 unit_table.attach(unit_active_exit_timestamp_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
151 unit_table.attach(new LeftLabel("Can Start/Stop:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
152 unit_table.attach(unit_can_start_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
153 unit_table.attach(new LeftLabel("Can Reload:"), 0, 1, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
154 unit_table.attach(unit_can_reload_label, 1, 2, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
156 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
157 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
158 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
159 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
160 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
161 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
163 ButtonBox bbox = new HButtonBox();
164 bbox.set_layout(ButtonBoxStyle.START);
166 unit_vbox.pack_start(bbox, false, true, 0);
168 start_button = new Button.with_mnemonic("_Start");
169 stop_button = new Button.with_mnemonic("Sto_p");
170 reload_button = new Button.with_mnemonic("_Reload");
171 restart_button = new Button.with_mnemonic("Res_tart");
173 start_button.clicked.connect(on_start);
174 stop_button.clicked.connect(on_stop);
175 reload_button.clicked.connect(on_reload);
176 restart_button.clicked.connect(on_restart);
178 bbox.pack_start(start_button, false, true, 0);
179 bbox.pack_start(stop_button, false, true, 0);
180 bbox.pack_start(restart_button, false, true, 0);
181 bbox.pack_start(reload_button, false, true, 0);
183 bbox = new HButtonBox();
184 bbox.set_layout(ButtonBoxStyle.START);
186 job_vbox.pack_start(bbox, false, true, 0);
188 cancel_button = new Button.with_mnemonic("_Cancel");
190 cancel_button.clicked.connect(on_cancel);
192 bbox.pack_start(cancel_button, false, true, 0);
194 bus = Bus.get(BusType.SESSION);
196 manager = bus.get_object (
197 "org.freedesktop.systemd1",
198 "/org/freedesktop/systemd1",
199 "org.freedesktop.systemd1") as Manager;
202 populate_unit_model();
203 populate_job_model();
206 public void populate_unit_model() throws DBus.Error {
209 var list = manager.list_units();
211 foreach (var i in list) {
214 unit_model.append(out iter);
220 4, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
225 public void populate_job_model() throws DBus.Error {
228 var list = manager.list_jobs();
230 foreach (var i in list) {
233 job_model.append(out iter);
235 0, "%u".printf(i.id),
237 2, "→ %s".printf(i.type),
243 public Unit? get_current_unit() {
245 unit_view.get_cursor(out p, null);
253 unit_model.get_iter(out iter, p);
254 unit_model.get(iter, 5, out path);
256 return bus.get_object (
257 "org.freedesktop.systemd1",
259 "org.freedesktop.systemd1.Unit") as Unit;
262 public void unit_changed() {
263 Unit u = get_current_unit();
271 public void clear_unit() {
272 start_button.set_sensitive(false);
273 stop_button.set_sensitive(false);
274 reload_button.set_sensitive(false);
275 restart_button.set_sensitive(false);
277 unit_id_label.set_text_or_na();
278 unit_description_label.set_text_or_na();
279 unit_load_state_label.set_text_or_na();
280 unit_active_state_label.set_text_or_na();
281 unit_load_path_label.set_text_or_na();
282 unit_active_enter_timestamp_label.set_text_or_na();
283 unit_active_exit_timestamp_label.set_text_or_na();
284 unit_can_reload_label.set_text_or_na();
285 unit_can_start_label.set_text_or_na();
288 public void show_unit(Unit unit) {
289 unit_id_label.set_text_or_na(unit.id);
290 unit_description_label.set_text_or_na(unit.description);
291 unit_load_state_label.set_text_or_na(unit.load_state);
292 unit_active_state_label.set_text_or_na(unit.active_state);
293 unit_load_path_label.set_text_or_na(unit.load_path);
295 uint64 t = unit.active_enter_timestamp;
297 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
298 unit_active_enter_timestamp_label.set_text_or_na(tv.to_iso8601());
300 unit_active_enter_timestamp_label.set_text_or_na();
302 t = unit.active_exit_timestamp;
304 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
305 unit_active_exit_timestamp_label.set_text_or_na(tv.to_iso8601());
307 unit_active_exit_timestamp_label.set_text_or_na();
309 bool b = unit.can_start;
310 start_button.set_sensitive(b);
311 stop_button.set_sensitive(b);
312 restart_button.set_sensitive(b);
313 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
316 reload_button.set_sensitive(b);
317 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
320 public Job? get_current_job() {
322 job_view.get_cursor(out p, null);
330 job_model.get_iter(out iter, p);
331 job_model.get(iter, 4, out path);
333 return bus.get_object (
334 "org.freedesktop.systemd1",
336 "org.freedesktop.systemd1.Job") as Job;
339 public void job_changed() {
340 Job j = get_current_job();
348 public void clear_job() {
349 job_id_label.set_text_or_na();
350 job_state_label.set_text_or_na();
351 job_type_label.set_text_or_na();
354 public void show_job(Job job) {
355 job_id_label.set_text_or_na("%u".printf(job.id));
356 job_state_label.set_text_or_na(job.state);
357 job_type_label.set_text_or_na(job.job_type);
360 public void on_start() {
361 Unit u = get_current_unit();
368 } catch (DBus.Error e) {
369 message("%s", e.message);
373 public void on_stop() {
374 Unit u = get_current_unit();
381 } catch (DBus.Error e) {
382 message("%s", e.message);
386 public void on_reload() {
387 Unit u = get_current_unit();
394 } catch (DBus.Error e) {
395 message("%s", e.message);
399 public void on_restart() {
400 Unit u = get_current_unit();
406 u.restart("replace");
407 } catch (DBus.Error e) {
408 message("%s", e.message);
412 public void on_cancel() {
413 Job j = get_current_job();
420 } catch (DBus.Error e) {
421 message("%s", e.message);
426 int main (string[] args) {
430 MainWindow window = new MainWindow();
432 } catch (DBus.Error e) {
433 message("%s", e.message);