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