chiark / gitweb /
fabf8b25e9ba0832529da496df7168b543e3edef
[elogind.git] / systemadm.vala
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 using Gtk;
21 using GLib;
22 using DBus;
23 using Pango;
24
25 public class LeftLabel : Label {
26         public LeftLabel(string? text = null) {
27                 if (text != null)
28                         set_markup("<b>%s</b>".printf(text));
29                 set_alignment(1, 0);
30                 set_padding(6, 0);
31         }
32 }
33
34 public class RightLabel : Label {
35         public RightLabel(string? text = null) {
36                 set_text_or_na(text);
37                 set_alignment(0, 1);
38                 set_ellipsize(EllipsizeMode.START);
39                 set_selectable(true);
40         }
41
42         public void set_text_or_na(string? text = null) {
43                 if (text == null || text == "")
44                         set_markup("<i>n/a</i>");
45                 else
46                         set_text(text);
47         }
48 }
49
50 public class MainWindow : Window {
51
52         private TreeView unit_view;
53         private TreeView job_view;
54
55         private ListStore unit_model;
56         private ListStore job_model;
57
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;
63
64         private Connection bus;
65         private Manager manager;
66
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;
76
77         private RightLabel job_id_label;
78         private RightLabel job_state_label;
79         private RightLabel job_type_label;
80
81         public MainWindow() throws DBus.Error {
82                 title = "systemdadm";
83                 position = WindowPosition.CENTER;
84                 set_default_size(1000, 700);
85                 set_border_width(12);
86                 destroy.connect(Gtk.main_quit);
87
88                 Notebook notebook = new Notebook();
89                 add(notebook);
90
91                 Box unit_vbox = new VBox(false, 6);
92                 notebook.append_page(unit_vbox, new Label("Units"));
93                 unit_vbox.set_border_width(12);
94
95                 Box job_vbox = new VBox(false, 6);
96                 notebook.append_page(job_vbox, new Label("Jobs"));
97                 job_vbox.set_border_width(12);
98
99
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));
102
103                 unit_view = new TreeView.with_model(unit_model);
104                 job_view = new TreeView.with_model(job_model);
105
106                 unit_view.cursor_changed.connect(unit_changed);
107                 job_view.cursor_changed.connect(job_changed);
108
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);
114
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);
119
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);
125
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);
131
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();
141
142                 job_id_label = new RightLabel();
143                 job_state_label = new RightLabel();
144                 job_type_label = new RightLabel();
145
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);
150
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);
155
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);
174
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);
181
182                 ButtonBox bbox = new HButtonBox();
183                 bbox.set_layout(ButtonBoxStyle.START);
184                 bbox.set_spacing(6);
185                 unit_vbox.pack_start(bbox, false, true, 0);
186
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");
191
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);
196
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);
201
202                 bbox = new HButtonBox();
203                 bbox.set_layout(ButtonBoxStyle.START);
204                 bbox.set_spacing(6);
205                 job_vbox.pack_start(bbox, false, true, 0);
206
207                 cancel_button = new Button.with_mnemonic("_Cancel");
208
209                 cancel_button.clicked.connect(on_cancel);
210
211                 bbox.pack_start(cancel_button, false, true, 0);
212
213                 bus = Bus.get(BusType.SESSION);
214
215                 manager = bus.get_object (
216                                 "org.freedesktop.systemd1",
217                                 "/org/freedesktop/systemd1",
218                                 "org.freedesktop.systemd1") as Manager;
219
220                 clear_unit();
221                 populate_unit_model();
222                 populate_job_model();
223         }
224
225         public void populate_unit_model() throws DBus.Error {
226                 unit_model.clear();
227
228                 var list = manager.list_units();
229
230                 foreach (var i in list) {
231                         TreeIter iter;
232
233                         unit_model.append(out iter);
234                         unit_model.set(iter,
235                                        0, i.id,
236                                        1, i.description,
237                                        2, i.load_state,
238                                        3, i.active_state,
239                                        4, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
240                                        5, i.unit_path);
241                 }
242         }
243
244         public void populate_job_model() throws DBus.Error {
245                 job_model.clear();
246
247                 var list = manager.list_jobs();
248
249                 foreach (var i in list) {
250                         TreeIter iter;
251
252                         job_model.append(out iter);
253                         job_model.set(iter,
254                                       0, "%u".printf(i.id),
255                                       1, i.name,
256                                       2, "→ %s".printf(i.type),
257                                       3, i.state,
258                                       4, i.job_path);
259                 }
260         }
261
262         public Unit? get_current_unit() {
263                 TreePath p;
264                 unit_view.get_cursor(out p, null);
265
266                 if (p == null)
267                         return null;
268
269                 TreeIter iter;
270                 string path;
271
272                 unit_model.get_iter(out iter, p);
273                 unit_model.get(iter, 5, out path);
274
275                 return bus.get_object (
276                                 "org.freedesktop.systemd1",
277                                 path,
278                                 "org.freedesktop.systemd1.Unit") as Unit;
279         }
280
281         public void unit_changed() {
282                 Unit u = get_current_unit();
283
284                 if (u == null)
285                         clear_unit();
286                 else
287                         show_unit(u);
288         }
289
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);
295
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();
305         }
306
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);
313
314                 uint64 t = unit.active_enter_timestamp;
315                 if (t > 0) {
316                         TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
317                         unit_active_enter_timestamp_label.set_text_or_na(tv.to_iso8601());
318                 } else
319                         unit_active_enter_timestamp_label.set_text_or_na();
320
321                 t = unit.active_exit_timestamp;
322                 if (t > 0) {
323                         TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
324                         unit_active_exit_timestamp_label.set_text_or_na(tv.to_iso8601());
325                 } else
326                         unit_active_exit_timestamp_label.set_text_or_na();
327
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");
333
334                 b = unit.can_reload;
335                 reload_button.set_sensitive(b);
336                 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
337         }
338
339         public Job? get_current_job() {
340                 TreePath p;
341                 job_view.get_cursor(out p, null);
342
343                 if (p == null)
344                         return null;
345
346                 TreeIter iter;
347                 string path;
348
349                 job_model.get_iter(out iter, p);
350                 job_model.get(iter, 4, out path);
351
352                 return bus.get_object (
353                                 "org.freedesktop.systemd1",
354                                 path,
355                                 "org.freedesktop.systemd1.Job") as Job;
356         }
357
358         public void job_changed() {
359                 Job j = get_current_job();
360
361                 if (j == null)
362                         clear_job();
363                 else
364                         show_job(j);
365         }
366
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();
371         }
372
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);
377         }
378
379         public void on_start() {
380                 Unit u = get_current_unit();
381
382                 if (u == null)
383                         return;
384
385                 try {
386                         u.start("replace");
387                 } catch (DBus.Error e) {
388                         message("%s", e.message);
389                 }
390         }
391
392         public void on_stop() {
393                 Unit u = get_current_unit();
394
395                 if (u == null)
396                         return;
397
398                 try {
399                         u.stop("replace");
400                 } catch (DBus.Error e) {
401                         message("%s", e.message);
402                 }
403         }
404
405         public void on_reload() {
406                 Unit u = get_current_unit();
407
408                 if (u == null)
409                         return;
410
411                 try {
412                         u.reload("replace");
413                 } catch (DBus.Error e) {
414                         message("%s", e.message);
415                 }
416         }
417
418         public void on_restart() {
419                 Unit u = get_current_unit();
420
421                 if (u == null)
422                         return;
423
424                 try {
425                         u.restart("replace");
426                 } catch (DBus.Error e) {
427                         message("%s", e.message);
428                 }
429         }
430
431         public void on_cancel() {
432                 Job j = get_current_job();
433
434                 if (j == null)
435                         return;
436
437                 try {
438                         j.cancel();
439                 } catch (DBus.Error e) {
440                         message("%s", e.message);
441                 }
442         }
443 }
444
445 int main (string[] args) {
446         Gtk.init(ref args);
447
448         try {
449                 MainWindow window = new MainWindow();
450                 window.show_all();
451         } catch (DBus.Error e) {
452                 message("%s", e.message);
453         }
454
455         Gtk.main();
456         return 0;
457 }