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