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