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