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