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