2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 public class LeftLabel : Label {
26 public LeftLabel(string? text = null) {
28 set_markup("<b>%s</b>".printf(text));
34 public class RightLabel : Label {
35 public RightLabel(string? text = null) {
38 set_ellipsize(EllipsizeMode.START);
42 public void set_text_or_na(string? text = null) {
43 if (text == null || text == "")
44 set_markup("<i>n/a</i>");
50 public class MainWindow : Window {
52 private TreeView unit_view;
53 private TreeView job_view;
55 private ListStore unit_model;
56 private ListStore job_model;
58 private Button start_button;
59 private Button stop_button;
60 private Button restart_button;
61 private Button reload_button;
62 private Button cancel_button;
64 private Connection bus;
65 private Manager manager;
67 private RightLabel unit_id_label;
68 private RightLabel unit_description_label;
69 private RightLabel unit_load_state_label;
70 private RightLabel unit_active_state_label;
71 private RightLabel unit_load_path_label;
72 private RightLabel unit_active_enter_timestamp_label;
73 private RightLabel unit_active_exit_timestamp_label;
74 private RightLabel unit_can_start_label;
75 private RightLabel unit_can_reload_label;
77 private RightLabel job_id_label;
78 private RightLabel job_state_label;
79 private RightLabel job_type_label;
81 public MainWindow() throws DBus.Error {
83 position = WindowPosition.CENTER;
84 set_default_size(1000, 700);
86 destroy.connect(Gtk.main_quit);
88 Notebook notebook = new Notebook();
91 Box unit_vbox = new VBox(false, 6);
92 notebook.append_page(unit_vbox, new Label("Units"));
93 unit_vbox.set_border_width(12);
95 Box job_vbox = new VBox(false, 6);
96 notebook.append_page(job_vbox, new Label("Jobs"));
97 job_vbox.set_border_width(12);
100 unit_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string));
101 job_model = new ListStore(5, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string));
103 unit_view = new TreeView.with_model(unit_model);
104 job_view = new TreeView.with_model(job_model);
106 unit_view.cursor_changed.connect(unit_changed);
107 job_view.cursor_changed.connect(job_changed);
109 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
110 unit_view.insert_column_with_attributes(-1, "Description", new CellRendererText(), "text", 1);
111 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
112 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
113 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 4);
115 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
116 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
117 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
118 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
120 ScrolledWindow scroll = new ScrolledWindow(null, null);
121 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
122 scroll.set_shadow_type(ShadowType.IN);
123 scroll.add(unit_view);
124 unit_vbox.pack_start(scroll, true, true, 0);
126 scroll = new ScrolledWindow(null, null);
127 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
128 scroll.set_shadow_type(ShadowType.IN);
129 scroll.add(job_view);
130 job_vbox.pack_start(scroll, true, true, 0);
132 unit_id_label = new RightLabel();
133 unit_description_label = new RightLabel();
134 unit_load_state_label = new RightLabel();
135 unit_active_state_label = new RightLabel();
136 unit_load_path_label = new RightLabel();
137 unit_active_enter_timestamp_label = new RightLabel();
138 unit_active_exit_timestamp_label = new RightLabel();
139 unit_can_start_label = new RightLabel();
140 unit_can_reload_label = new RightLabel();
142 job_id_label = new RightLabel();
143 job_state_label = new RightLabel();
144 job_type_label = new RightLabel();
146 Table unit_table = new Table(9, 2, false);
147 unit_table.set_row_spacings(6);
148 unit_table.set_border_width(12);
149 unit_vbox.pack_start(unit_table, false, true, 0);
151 Table job_table = new Table(2, 2, false);
152 job_table.set_row_spacings(6);
153 job_table.set_border_width(12);
154 job_vbox.pack_start(job_table, false, true, 0);
156 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
157 unit_table.attach(unit_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
158 unit_table.attach(new LeftLabel("Description:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
159 unit_table.attach(unit_description_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
160 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
161 unit_table.attach(unit_load_state_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
162 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
163 unit_table.attach(unit_active_state_label, 1, 2, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
164 unit_table.attach(new LeftLabel("Load Path:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
165 unit_table.attach(unit_load_path_label, 1, 2, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
166 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
167 unit_table.attach(unit_active_enter_timestamp_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
168 unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
169 unit_table.attach(unit_active_exit_timestamp_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
170 unit_table.attach(new LeftLabel("Can Start/Stop:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
171 unit_table.attach(unit_can_start_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
172 unit_table.attach(new LeftLabel("Can Reload:"), 0, 1, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
173 unit_table.attach(unit_can_reload_label, 1, 2, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
175 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
176 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
177 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
178 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
179 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
180 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
182 ButtonBox bbox = new HButtonBox();
183 bbox.set_layout(ButtonBoxStyle.START);
185 unit_vbox.pack_start(bbox, false, true, 0);
187 start_button = new Button.with_mnemonic("_Start");
188 stop_button = new Button.with_mnemonic("Sto_p");
189 reload_button = new Button.with_mnemonic("_Reload");
190 restart_button = new Button.with_mnemonic("Res_tart");
192 start_button.clicked.connect(on_start);
193 stop_button.clicked.connect(on_stop);
194 reload_button.clicked.connect(on_reload);
195 restart_button.clicked.connect(on_restart);
197 bbox.pack_start(start_button, false, true, 0);
198 bbox.pack_start(stop_button, false, true, 0);
199 bbox.pack_start(restart_button, false, true, 0);
200 bbox.pack_start(reload_button, false, true, 0);
202 bbox = new HButtonBox();
203 bbox.set_layout(ButtonBoxStyle.START);
205 job_vbox.pack_start(bbox, false, true, 0);
207 cancel_button = new Button.with_mnemonic("_Cancel");
209 cancel_button.clicked.connect(on_cancel);
211 bbox.pack_start(cancel_button, false, true, 0);
213 bus = Bus.get(BusType.SESSION);
215 manager = bus.get_object (
216 "org.freedesktop.systemd1",
217 "/org/freedesktop/systemd1",
218 "org.freedesktop.systemd1") as Manager;
221 populate_unit_model();
222 populate_job_model();
225 public void populate_unit_model() throws DBus.Error {
228 var list = manager.list_units();
230 foreach (var i in list) {
233 unit_model.append(out iter);
239 4, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
244 public void populate_job_model() throws DBus.Error {
247 var list = manager.list_jobs();
249 foreach (var i in list) {
252 job_model.append(out iter);
254 0, "%u".printf(i.id),
256 2, "→ %s".printf(i.type),
262 public Unit? get_current_unit() {
264 unit_view.get_cursor(out p, null);
272 unit_model.get_iter(out iter, p);
273 unit_model.get(iter, 5, out path);
275 return bus.get_object (
276 "org.freedesktop.systemd1",
278 "org.freedesktop.systemd1.Unit") as Unit;
281 public void unit_changed() {
282 Unit u = get_current_unit();
290 public void clear_unit() {
291 start_button.set_sensitive(false);
292 stop_button.set_sensitive(false);
293 reload_button.set_sensitive(false);
294 restart_button.set_sensitive(false);
296 unit_id_label.set_text_or_na();
297 unit_description_label.set_text_or_na();
298 unit_load_state_label.set_text_or_na();
299 unit_active_state_label.set_text_or_na();
300 unit_load_path_label.set_text_or_na();
301 unit_active_enter_timestamp_label.set_text_or_na();
302 unit_active_exit_timestamp_label.set_text_or_na();
303 unit_can_reload_label.set_text_or_na();
304 unit_can_start_label.set_text_or_na();
307 public void show_unit(Unit unit) {
308 unit_id_label.set_text_or_na(unit.id);
309 unit_description_label.set_text_or_na(unit.description);
310 unit_load_state_label.set_text_or_na(unit.load_state);
311 unit_active_state_label.set_text_or_na(unit.active_state);
312 unit_load_path_label.set_text_or_na(unit.load_path);
314 uint64 t = unit.active_enter_timestamp;
316 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
317 unit_active_enter_timestamp_label.set_text_or_na(tv.to_iso8601());
319 unit_active_enter_timestamp_label.set_text_or_na();
321 t = unit.active_exit_timestamp;
323 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
324 unit_active_exit_timestamp_label.set_text_or_na(tv.to_iso8601());
326 unit_active_exit_timestamp_label.set_text_or_na();
328 bool b = unit.can_start;
329 start_button.set_sensitive(b);
330 stop_button.set_sensitive(b);
331 restart_button.set_sensitive(b);
332 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
335 reload_button.set_sensitive(b);
336 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
339 public Job? get_current_job() {
341 job_view.get_cursor(out p, null);
349 job_model.get_iter(out iter, p);
350 job_model.get(iter, 4, out path);
352 return bus.get_object (
353 "org.freedesktop.systemd1",
355 "org.freedesktop.systemd1.Job") as Job;
358 public void job_changed() {
359 Job j = get_current_job();
367 public void clear_job() {
368 job_id_label.set_text_or_na();
369 job_state_label.set_text_or_na();
370 job_type_label.set_text_or_na();
373 public void show_job(Job job) {
374 job_id_label.set_text_or_na("%u".printf(job.id));
375 job_state_label.set_text_or_na(job.state);
376 job_type_label.set_text_or_na(job.job_type);
379 public void on_start() {
380 Unit u = get_current_unit();
387 } catch (DBus.Error e) {
388 message("%s", e.message);
392 public void on_stop() {
393 Unit u = get_current_unit();
400 } catch (DBus.Error e) {
401 message("%s", e.message);
405 public void on_reload() {
406 Unit u = get_current_unit();
413 } catch (DBus.Error e) {
414 message("%s", e.message);
418 public void on_restart() {
419 Unit u = get_current_unit();
425 u.restart("replace");
426 } catch (DBus.Error e) {
427 message("%s", e.message);
431 public void on_cancel() {
432 Job j = get_current_job();
439 } catch (DBus.Error e) {
440 message("%s", e.message);
445 int main (string[] args) {
449 MainWindow window = new MainWindow();
451 } catch (DBus.Error e) {
452 message("%s", e.message);