chiark / gitweb /
b5985faf21184a61edaee17cb12803f12572de20
[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 static bool session = false;
26
27 public class LeftLabel : Label {
28         public LeftLabel(string? text = null) {
29                 if (text != null)
30                         set_markup("<b>%s</b>".printf(text));
31                 set_alignment(0, 0);
32                 set_padding(6, 0);
33         }
34 }
35
36 public class RightLabel : Label {
37         public RightLabel(string? text = null) {
38                 set_text_or_na(text);
39                 set_alignment(0, 0);
40                 set_ellipsize(EllipsizeMode.START);
41                 set_selectable(true);
42         }
43
44         public void set_text_or_na(string? text = null) {
45                 if (text == null || text == "")
46                         set_markup("<i>n/a</i>");
47                 else
48                         set_text(text);
49         }
50 }
51
52 public class MainWindow : Window {
53
54         private string? current_unit_id;
55         private uint32 current_job_id;
56
57         private TreeView unit_view;
58         private TreeView job_view;
59
60         private ListStore unit_model;
61         private ListStore job_model;
62
63         private Button start_button;
64         private Button stop_button;
65         private Button restart_button;
66         private Button reload_button;
67         private Button cancel_button;
68
69         private Entry unit_load_entry;
70         private Button unit_load_button;
71
72         private Button server_snapshot_button;
73         private Button server_reload_button;
74
75         private Connection bus;
76         private Manager manager;
77
78         private RightLabel unit_id_label;
79         private RightLabel unit_aliases_label;
80         private RightLabel unit_description_label;
81         private RightLabel unit_load_state_label;
82         private RightLabel unit_active_state_label;
83         private RightLabel unit_sub_state_label;
84         private RightLabel unit_fragment_path_label;
85         private RightLabel unit_active_enter_timestamp_label;
86         private RightLabel unit_active_exit_timestamp_label;
87         private RightLabel unit_can_start_label;
88         private RightLabel unit_can_reload_label;
89         private RightLabel unit_cgroup_label;
90
91         private RightLabel job_id_label;
92         private RightLabel job_state_label;
93         private RightLabel job_type_label;
94
95         private ComboBox unit_type_combo_box;
96
97         public MainWindow() throws DBus.Error {
98                 title = "systemadm";
99                 position = WindowPosition.CENTER;
100                 set_default_size(1000, 700);
101                 set_border_width(12);
102                 destroy += Gtk.main_quit;
103
104                 Notebook notebook = new Notebook();
105                 add(notebook);
106
107                 Box unit_vbox = new VBox(false, 12);
108                 notebook.append_page(unit_vbox, new Label("Units"));
109                 unit_vbox.set_border_width(12);
110
111                 Box job_vbox = new VBox(false, 12);
112                 notebook.append_page(job_vbox, new Label("Jobs"));
113                 job_vbox.set_border_width(12);
114
115                 unit_type_combo_box = new ComboBox.text();
116                 Box type_hbox = new HBox(false, 6);
117                 type_hbox.pack_start(unit_type_combo_box, false, false, 0);
118                 unit_vbox.pack_start(type_hbox, false, false, 0);
119
120                 unit_type_combo_box.append_text("Show All Units");
121                 unit_type_combo_box.append_text("Show Only Live Units");
122                 unit_type_combo_box.append_text("Services");
123                 unit_type_combo_box.append_text("Sockets");
124                 unit_type_combo_box.append_text("Devices");
125                 unit_type_combo_box.append_text("Mounts");
126                 unit_type_combo_box.append_text("Automounts");
127                 unit_type_combo_box.append_text("Targets");
128                 unit_type_combo_box.append_text("Snapshots");
129                 unit_type_combo_box.set_active(1);
130                 unit_type_combo_box.changed += unit_type_changed;
131
132                 unit_load_entry = new Entry();
133                 unit_load_button = new Button.with_mnemonic("_Load");
134                 unit_load_button.set_sensitive(false);
135
136                 unit_load_entry.changed += on_unit_load_entry_changed;
137                 unit_load_entry.activate += on_unit_load;
138                 unit_load_button.clicked += on_unit_load;
139
140                 Gtk.Alignment unit_load_button_alignment = new Gtk.Alignment(0.5f, 0.5f, 1f, 1f);
141                 unit_load_button_alignment.right_padding = 24;
142                 unit_load_button_alignment.add(unit_load_button);
143
144                 server_snapshot_button = new Button.with_mnemonic("Take S_napshot");
145                 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
146
147                 server_snapshot_button.clicked += on_server_snapshot;
148                 server_reload_button.clicked += on_server_reload;
149
150                 type_hbox.pack_end(server_snapshot_button, false, true, 0);
151                 type_hbox.pack_end(server_reload_button, false, true, 0);
152                 type_hbox.pack_end(unit_load_button_alignment, false, true, 0);
153                 type_hbox.pack_end(unit_load_entry, false, true, 0);
154
155                 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
156                 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
157
158                 TreeModelFilter unit_model_filter;
159                 unit_model_filter = new TreeModelFilter(unit_model, null);
160                 unit_model_filter.set_visible_func(unit_filter);
161
162                 unit_view = new TreeView.with_model(unit_model_filter);
163                 job_view = new TreeView.with_model(job_model);
164
165                 unit_view.cursor_changed += unit_changed;
166                 job_view.cursor_changed += job_changed;
167
168                 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
169                 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
170                 unit_view.insert_column_with_attributes(-1, "Unit State", new CellRendererText(), "text", 4);
171                 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
172                 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 5);
173
174                 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
175                 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
176                 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
177                 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
178
179                 ScrolledWindow scroll = new ScrolledWindow(null, null);
180                 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
181                 scroll.set_shadow_type(ShadowType.IN);
182                 scroll.add(unit_view);
183                 unit_vbox.pack_start(scroll, true, true, 0);
184
185                 scroll = new ScrolledWindow(null, null);
186                 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
187                 scroll.set_shadow_type(ShadowType.IN);
188                 scroll.add(job_view);
189                 job_vbox.pack_start(scroll, true, true, 0);
190
191                 unit_id_label = new RightLabel();
192                 unit_aliases_label = new RightLabel();
193                 unit_description_label = new RightLabel();
194                 unit_load_state_label = new RightLabel();
195                 unit_active_state_label = new RightLabel();
196                 unit_sub_state_label = new RightLabel();
197                 unit_fragment_path_label = new RightLabel();
198                 unit_active_enter_timestamp_label = new RightLabel();
199                 unit_active_exit_timestamp_label = new RightLabel();
200                 unit_can_start_label = new RightLabel();
201                 unit_can_reload_label = new RightLabel();
202                 unit_cgroup_label = new RightLabel();
203
204                 job_id_label = new RightLabel();
205                 job_state_label = new RightLabel();
206                 job_type_label = new RightLabel();
207
208                 Table unit_table = new Table(8, 6, false);
209                 unit_table.set_row_spacings(6);
210                 unit_table.set_border_width(0);
211                 unit_vbox.pack_start(unit_table, false, true, 0);
212
213                 Table job_table = new Table(2, 2, false);
214                 job_table.set_row_spacings(6);
215                 job_table.set_border_width(0);
216                 job_vbox.pack_start(job_table, false, true, 0);
217
218                 unit_table.attach(new LeftLabel("Id:"),                     0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
219                 unit_table.attach(unit_id_label,                            1, 6, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
220                 unit_table.attach(new LeftLabel("Aliases:"),                0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
221                 unit_table.attach(unit_aliases_label,                       1, 6, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
222                 unit_table.attach(new LeftLabel("Description:"),            0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
223                 unit_table.attach(unit_description_label,                   1, 6, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
224                 unit_table.attach(new LeftLabel("Fragment Path:"),          0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
225                 unit_table.attach(unit_fragment_path_label,                 1, 6, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
226                 unit_table.attach(new LeftLabel("Control Group:"),          0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
227                 unit_table.attach(unit_cgroup_label,                        1, 6, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
228
229                 unit_table.attach(new LeftLabel("Load State:"),             0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
230                 unit_table.attach(unit_load_state_label,                    1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
231                 unit_table.attach(new LeftLabel("Active State:"),           0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
232                 unit_table.attach(unit_active_state_label,                  1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
233                 unit_table.attach(new LeftLabel("Unit State:"),             0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
234                 unit_table.attach(unit_sub_state_label,                     1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
235
236                 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 2, 3, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
237                 unit_table.attach(unit_active_enter_timestamp_label,        3, 4, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
238                 unit_table.attach(new LeftLabel("Active Exit Timestamp:"),  2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
239                 unit_table.attach(unit_active_exit_timestamp_label,         3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
240
241                 unit_table.attach(new LeftLabel("Can Start/Stop:"),         4, 5, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
242                 unit_table.attach(unit_can_start_label,                     5, 6, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
243                 unit_table.attach(new LeftLabel("Can Reload:"),             4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
244                 unit_table.attach(unit_can_reload_label,                    5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
245
246                 job_table.attach(new LeftLabel("Id:"),                      0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
247                 job_table.attach(job_id_label,                              1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
248                 job_table.attach(new LeftLabel("State:"),                   0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
249                 job_table.attach(job_state_label,                           1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
250                 job_table.attach(new LeftLabel("Type:"),                    0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
251                 job_table.attach(job_type_label,                            1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
252
253                 ButtonBox bbox = new HButtonBox();
254                 bbox.set_layout(ButtonBoxStyle.START);
255                 bbox.set_spacing(6);
256                 unit_vbox.pack_start(bbox, false, true, 0);
257
258                 start_button = new Button.with_mnemonic("_Start");
259                 stop_button = new Button.with_mnemonic("Sto_p");
260                 reload_button = new Button.with_mnemonic("_Reload");
261                 restart_button = new Button.with_mnemonic("Res_tart");
262
263                 start_button.clicked += on_start;
264                 stop_button.clicked += on_stop;
265                 reload_button.clicked += on_reload;
266                 restart_button.clicked += on_restart;
267
268                 bbox.pack_start(start_button, false, true, 0);
269                 bbox.pack_start(stop_button, false, true, 0);
270                 bbox.pack_start(restart_button, false, true, 0);
271                 bbox.pack_start(reload_button, false, true, 0);
272
273                 bbox = new HButtonBox();
274                 bbox.set_layout(ButtonBoxStyle.START);
275                 bbox.set_spacing(6);
276                 job_vbox.pack_start(bbox, false, true, 0);
277
278                 cancel_button = new Button.with_mnemonic("_Cancel");
279
280                 cancel_button.clicked += on_cancel;
281
282                 bbox.pack_start(cancel_button, false, true, 0);
283
284                 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
285
286                 manager = bus.get_object(
287                                 "org.freedesktop.systemd1",
288                                 "/org/freedesktop/systemd1",
289                                 "org.freedesktop.systemd1.Manager") as Manager;
290
291                 manager.unit_new += on_unit_new;
292                 manager.job_new += on_job_new;
293                 manager.unit_removed += on_unit_removed;
294                 manager.job_removed += on_job_removed;
295
296                 manager.subscribe();
297
298                 clear_unit();
299                 clear_job();
300                 populate_unit_model();
301                 populate_job_model();
302         }
303
304         public void populate_unit_model() throws DBus.Error {
305                 unit_model.clear();
306
307                 var list = manager.list_units();
308
309                 foreach (var i in list) {
310                         TreeIter iter;
311
312                         Unit u = bus.get_object(
313                                         "org.freedesktop.systemd1",
314                                         i.unit_path,
315                                         "org.freedesktop.systemd1.Unit") as Unit;
316
317                         u.changed += on_unit_changed;
318
319                         unit_model.append(out iter);
320                         unit_model.set(iter,
321                                        0, i.id,
322                                        1, i.description,
323                                        2, i.load_state,
324                                        3, i.active_state,
325                                        4, i.sub_state,
326                                        5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
327                                        6, u);
328                 }
329         }
330
331         public void populate_job_model() throws DBus.Error {
332                 job_model.clear();
333
334                 var list = manager.list_jobs();
335
336                 foreach (var i in list) {
337                         TreeIter iter;
338
339                         Job j = bus.get_object(
340                                         "org.freedesktop.systemd1",
341                                         i.job_path,
342                                         "org.freedesktop.systemd1.Job") as Job;
343
344                         j.changed += on_job_changed;
345
346                         job_model.append(out iter);
347                         job_model.set(iter,
348                                       0, "%u".printf(i.id),
349                                       1, i.name,
350                                       2, "→ %s".printf(i.type),
351                                       3, i.state,
352                                       4, j,
353                                       5, i.id);
354                 }
355         }
356
357         public Unit? get_current_unit() {
358                 TreePath p;
359                 unit_view.get_cursor(out p, null);
360
361                 if (p == null)
362                         return null;
363
364                 TreeModel model = unit_view.get_model();
365                 TreeIter iter;
366                 Unit u;
367
368                 model.get_iter(out iter, p);
369                 model.get(iter, 6, out u);
370
371                 return u;
372         }
373
374         public void unit_changed() {
375                 Unit u = get_current_unit();
376
377                 if (u == null)
378                         clear_unit();
379                 else
380                         show_unit(u);
381         }
382
383         public void clear_unit() {
384                 current_unit_id = null;
385
386                 start_button.set_sensitive(false);
387                 stop_button.set_sensitive(false);
388                 reload_button.set_sensitive(false);
389                 restart_button.set_sensitive(false);
390
391                 unit_id_label.set_text_or_na();
392                 unit_aliases_label.set_text_or_na();
393                 unit_description_label.set_text_or_na();
394                 unit_load_state_label.set_text_or_na();
395                 unit_active_state_label.set_text_or_na();
396                 unit_sub_state_label.set_text_or_na();
397                 unit_fragment_path_label.set_text_or_na();
398                 unit_active_enter_timestamp_label.set_text_or_na();
399                 unit_active_exit_timestamp_label.set_text_or_na();
400                 unit_can_reload_label.set_text_or_na();
401                 unit_can_start_label.set_text_or_na();
402                 unit_cgroup_label.set_text_or_na();
403         }
404
405         public void show_unit(Unit unit) {
406                 current_unit_id = unit.id;
407
408                 unit_id_label.set_text_or_na(current_unit_id);
409
410                 string a = "";
411                 foreach (string i in unit.names) {
412                         if (i == current_unit_id)
413                                 continue;
414
415                         if (a == "")
416                                 a = i;
417                         else
418                                 a += "\n" + i;
419                 }
420
421                 unit_aliases_label.set_text_or_na(a);
422                 unit_description_label.set_text_or_na(unit.description);
423                 unit_load_state_label.set_text_or_na(unit.load_state);
424                 unit_active_state_label.set_text_or_na(unit.active_state);
425                 unit_sub_state_label.set_text_or_na(unit.sub_state);
426                 unit_fragment_path_label.set_text_or_na(unit.fragment_path);
427
428                 uint64 t = unit.active_enter_timestamp;
429                 if (t > 0) {
430                         Time timestamp = Time.local((time_t) (t / 1000000));
431                         unit_active_enter_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
432                 } else
433                         unit_active_enter_timestamp_label.set_text_or_na();
434
435                 t = unit.active_exit_timestamp;
436                 if (t > 0) {
437                         Time timestamp = Time.local((time_t) (t / 1000000));
438                         unit_active_exit_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
439                 } else
440                         unit_active_exit_timestamp_label.set_text_or_na();
441
442                 bool b = unit.can_start;
443                 start_button.set_sensitive(b);
444                 stop_button.set_sensitive(b);
445                 restart_button.set_sensitive(b);
446                 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
447
448                 b = unit.can_reload;
449                 reload_button.set_sensitive(b);
450                 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
451
452                 unit_cgroup_label.set_text_or_na(unit.default_control_group);
453         }
454
455         public Job? get_current_job() {
456                 TreePath p;
457                 job_view.get_cursor(out p, null);
458
459                 if (p == null)
460                         return null;
461
462                 TreeIter iter;
463                 TreeModel model = job_view.get_model();
464                 Job *j;
465
466                 model.get_iter(out iter, p);
467                 model.get(iter, 4, out j);
468
469                 return j;
470         }
471
472         public void job_changed() {
473                 Job j = get_current_job();
474
475                 if (j == null)
476                         clear_job();
477                 else
478                         show_job(j);
479         }
480
481         public void clear_job() {
482                 current_job_id = 0;
483
484                 job_id_label.set_text_or_na();
485                 job_state_label.set_text_or_na();
486                 job_type_label.set_text_or_na();
487
488                 cancel_button.set_sensitive(false);
489         }
490
491         public void show_job(Job job) {
492                 current_job_id = job.id;
493
494                 job_id_label.set_text_or_na("%u".printf(current_job_id));
495                 job_state_label.set_text_or_na(job.state);
496                 job_type_label.set_text_or_na(job.job_type);
497
498                 cancel_button.set_sensitive(true);
499         }
500
501         public void on_start() {
502                 Unit u = get_current_unit();
503
504                 if (u == null)
505                         return;
506
507                 try {
508                         u.start("replace");
509                 } catch (DBus.Error e) {
510                         show_error(e.message);
511                 }
512         }
513
514         public void on_stop() {
515                 Unit u = get_current_unit();
516
517                 if (u == null)
518                         return;
519
520                 try {
521                         u.stop("replace");
522                 } catch (DBus.Error e) {
523                         show_error(e.message);
524                 }
525         }
526
527         public void on_reload() {
528                 Unit u = get_current_unit();
529
530                 if (u == null)
531                         return;
532
533                 try {
534                         u.reload("replace");
535                 } catch (DBus.Error e) {
536                         show_error(e.message);
537                 }
538         }
539
540         public void on_restart() {
541                 Unit u = get_current_unit();
542
543                 if (u == null)
544                         return;
545
546                 try {
547                         u.restart("replace");
548                 } catch (DBus.Error e) {
549                         show_error(e.message);
550                 }
551         }
552
553         public void on_cancel() {
554                 Job j = get_current_job();
555
556                 if (j == null)
557                         return;
558
559                 try {
560                         j.cancel();
561                 } catch (DBus.Error e) {
562                         show_error(e.message);
563                 }
564         }
565
566         public void update_unit_iter(TreeIter iter, string id, Unit u) {
567
568                 string t = "";
569                 Unit.JobLink jl = u.job;
570
571                 if (jl.id != 0) {
572                         Job j = bus.get_object(
573                                         "org.freedesktop.systemd1",
574                                         jl.path,
575                                         "org.freedesktop.systemd1.Job") as Job;
576
577                         t = j.job_type;
578                 }
579
580                 unit_model.set(iter,
581                                0, id,
582                                1, u.description,
583                                2, u.load_state,
584                                3, u.active_state,
585                                4, u.sub_state,
586                                5, t != "" ? "→ %s".printf(t) : "",
587                                6, u);
588         }
589
590         public void on_unit_new(string id, ObjectPath path) {
591                 Unit u = bus.get_object(
592                                 "org.freedesktop.systemd1",
593                                 path,
594                                 "org.freedesktop.systemd1.Unit") as Unit;
595
596                 u.changed += on_unit_changed;
597
598                 TreeIter iter;
599                 unit_model.append(out iter);
600                 update_unit_iter(iter, id, u);
601         }
602
603         public void update_job_iter(TreeIter iter, uint32 id, Job j) {
604                 job_model.set(iter,
605                               0, "%u".printf(id),
606                               1, j.unit.id,
607                               2, "→ %s".printf(j.job_type),
608                               3, j.state,
609                               4, j,
610                               5, id);
611         }
612
613         public void on_job_new(uint32 id, ObjectPath path) {
614                 Job j = bus.get_object(
615                                 "org.freedesktop.systemd1",
616                                 path,
617                                 "org.freedesktop.systemd1.Job") as Job;
618
619                 j.changed += on_job_changed;
620
621                 TreeIter iter;
622                 job_model.append(out iter);
623                 update_job_iter(iter, id, j);
624         }
625
626         public void on_unit_removed(string id, ObjectPath path) {
627                 TreeIter iter;
628                 if (!(unit_model.get_iter_first(out iter)))
629                         return;
630
631                 do {
632                         string name;
633
634                         unit_model.get(iter, 0, out name);
635
636                         if (id == name) {
637                                 if (current_unit_id == name)
638                                         clear_unit();
639
640                                 unit_model.remove(iter);
641                                 break;
642                         }
643
644                 } while (unit_model.iter_next(ref iter));
645         }
646
647         public void on_job_removed(uint32 id, ObjectPath path) {
648                 TreeIter iter;
649                 if (!(job_model.get_iter_first(out iter)))
650                         return;
651
652                 do {
653                         uint32 j;
654
655                         job_model.get(iter, 5, out j);
656
657                         if (id == j) {
658                                 if (current_job_id == j)
659                                         clear_job();
660
661                                 job_model.remove(iter);
662
663                                 break;
664                         }
665
666                 } while (job_model.iter_next(ref iter));
667         }
668
669         public void on_unit_changed(Unit u) {
670                 TreeIter iter;
671                 string id;
672
673                 if (!(unit_model.get_iter_first(out iter)))
674                         return;
675
676                 id = u.id;
677
678                 do {
679                         string name;
680
681                         unit_model.get(iter, 0, out name);
682
683                         if (id == name) {
684                                 update_unit_iter(iter, id, u);
685
686                                 if (current_unit_id == id)
687                                         show_unit(u);
688
689                                 break;
690                         }
691
692                 } while (unit_model.iter_next(ref iter));
693         }
694
695         public void on_job_changed(Job j) {
696                 TreeIter iter;
697                 uint32 id;
698
699                 if (!(job_model.get_iter_first(out iter)))
700                         return;
701
702                 id = j.id;
703
704                 do {
705                         uint32 k;
706
707                         job_model.get(iter, 5, out k);
708
709                         if (id == k) {
710                                 update_job_iter(iter, id, j);
711
712                                 if (current_job_id == id)
713                                         show_job(j);
714
715                                 break;
716                         }
717
718                 } while (job_model.iter_next(ref iter));
719         }
720
721         public bool unit_filter(TreeModel model, TreeIter iter) {
722                 string id, active_state, job;
723
724                 model.get(iter, 0, out id, 3, out active_state, 5, out job);
725
726                 if (id == null)
727                         return false;
728
729                 switch (unit_type_combo_box.get_active()) {
730
731                         case 0:
732                                 return true;
733
734                         case 1:
735                                 return active_state != "inactive" || job != "";
736
737                         case 2:
738                                 return id.has_suffix(".service");
739
740                         case 3:
741                                 return id.has_suffix(".socket");
742
743                         case 4:
744                                 return id.has_suffix(".device");
745
746                         case 5:
747                                 return id.has_suffix(".mount");
748
749                         case 6:
750                                 return id.has_suffix(".automount");
751
752                         case 7:
753                                 return id.has_suffix(".target");
754
755                         case 8:
756                                 return id.has_suffix(".snapshot");
757                 }
758
759                 return false;
760         }
761
762         public void unit_type_changed() {
763                 TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
764
765                 model.refilter();
766         }
767
768         public void on_server_reload() {
769                 try {
770                         manager.reload();
771                 } catch (DBus.Error e) {
772                         show_error(e.message);
773                 }
774         }
775
776         public void on_server_snapshot() {
777                 try {
778                         manager.create_snapshot();
779
780                         if (unit_type_combo_box.get_active() != 0)
781                                 unit_type_combo_box.set_active(8);
782
783                 } catch (DBus.Error e) {
784                         show_error(e.message);
785                 }
786         }
787
788         public void on_unit_load() {
789                 try {
790                         var path = manager.load_unit(unit_load_entry.get_text());
791
792                         Unit u = bus.get_object(
793                                         "org.freedesktop.systemd1",
794                                         path,
795                                         "org.freedesktop.systemd1.Unit") as Unit;
796
797                         var m = new MessageDialog(this,
798                                                   DialogFlags.DESTROY_WITH_PARENT,
799                                                   MessageType.INFO,
800                                                   ButtonsType.CLOSE,
801                                                   "Unit available as id %s", u.id);
802                         m.title = "Unit";
803                         m.run();
804                         m.destroy();
805
806                 } catch (DBus.Error e) {
807                         show_error(e.message);
808                 }
809         }
810
811         public void on_unit_load_entry_changed() {
812                 unit_load_button.set_sensitive(unit_load_entry.get_text() != "");
813         }
814
815         public void show_error(string e) {
816                 var m = new MessageDialog(this,
817                                           DialogFlags.DESTROY_WITH_PARENT,
818                                           MessageType.ERROR,
819                                           ButtonsType.CLOSE, "%s", e);
820                 m.title = "Error";
821                 m.run();
822                 m.destroy();
823         }
824
825 }
826
827 static const OptionEntry entries[] = {
828         { "session", 0,   0,                   OptionArg.NONE,   out session, "Connect to session bus", null },
829         { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE,   out session, "Connect to system bus", null },
830         { null }
831 };
832
833 void show_error(string e) {
834         var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
835         m.run();
836         m.destroy();
837 }
838
839 int main (string[] args) {
840
841         try {
842                 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
843
844                 MainWindow window = new MainWindow();
845                 window.show_all();
846
847                 Gtk.main();
848         } catch (DBus.Error e) {
849                 show_error(e.message);
850         } catch (GLib.Error e) {
851                 show_error(e.message);
852         }
853
854         return 0;
855 }