chiark / gitweb /
systemadm: allow sorting of jobs and units
[elogind.git] / src / systemadm.vala
index bd0062a0692f6b1182c9a678f3acacda90893558..21177bf396b832674cad22b1e657c0fc7f7ca052 100644 (file)
 
 using Gtk;
 using GLib;
-using DBus;
 using Pango;
 
-static bool session = false;
+static bool user = false;
+
+public string format_time(uint64 time_ns) {
+        if (time_ns <= 0)
+                return "";
+        Time timestamp = Time.local((time_t) (time_ns / 1000000));
+        return timestamp.format("%a, %d %b %Y %H:%M:%S");
+}
+
+public void new_column(TreeView view, int column_id, string title) {
+        TreeViewColumn col;
+        col = new TreeViewColumn.with_attributes(title, new CellRendererText(), "text", column_id);
+        col.set_sort_column_id(column_id);
+        view.insert_column(col, -1);
+}
 
 public class LeftLabel : Label {
         public LeftLabel(string? text = null) {
@@ -79,7 +92,6 @@ public class MainWindow : Window {
         private Button server_snapshot_button;
         private Button server_reload_button;
 
-        private Connection bus;
         private Manager manager;
 
         private RightLabel unit_id_label;
@@ -102,12 +114,12 @@ public class MainWindow : Window {
 
         private ComboBox unit_type_combo_box;
 
-        public MainWindow() throws DBus.Error {
-                title = session ? "systemd Session Manager" : "systemd System Manager";
-                position = WindowPosition.CENTER;
+        public MainWindow() throws IOError {
+                title = user ? "systemd User Service Manager" : "systemd System Manager";
+                set_position(WindowPosition.CENTER);
                 set_default_size(1000, 700);
                 set_border_width(12);
-                destroy += Gtk.main_quit;
+                destroy.connect(Gtk.main_quit);
 
                 Notebook notebook = new Notebook();
                 add(notebook);
@@ -135,15 +147,15 @@ public class MainWindow : Window {
                 unit_type_combo_box.append_text("Targets");
                 unit_type_combo_box.append_text("Snapshots");
                 unit_type_combo_box.set_active(1);
-                unit_type_combo_box.changed += unit_type_changed;
+                unit_type_combo_box.changed.connect(unit_type_changed);
 
                 unit_load_entry = new Entry();
                 unit_load_button = new Button.with_mnemonic("_Load");
                 unit_load_button.set_sensitive(false);
 
-                unit_load_entry.changed += on_unit_load_entry_changed;
-                unit_load_entry.activate += on_unit_load;
-                unit_load_button.clicked += on_unit_load;
+                unit_load_entry.changed.connect(on_unit_load_entry_changed);
+                unit_load_entry.activate.connect(on_unit_load);
+                unit_load_button.clicked.connect(on_unit_load);
 
                 Box unit_load_hbox = new HBox(false, 6);
                 unit_load_hbox.pack_start(unit_load_entry, false, true, 0);
@@ -152,8 +164,8 @@ public class MainWindow : Window {
                 server_snapshot_button = new Button.with_mnemonic("Take S_napshot");
                 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
 
-                server_snapshot_button.clicked += on_server_snapshot;
-                server_reload_button.clicked += on_server_reload;
+                server_snapshot_button.clicked.connect(on_server_snapshot);
+                server_reload_button.clicked.connect(on_server_reload);
 
                 type_hbox.pack_end(server_snapshot_button, false, true, 0);
                 type_hbox.pack_end(server_reload_button, false, true, 0);
@@ -166,22 +178,24 @@ public class MainWindow : Window {
                 unit_model_filter = new TreeModelFilter(unit_model, null);
                 unit_model_filter.set_visible_func(unit_filter);
 
-                unit_view = new TreeView.with_model(unit_model_filter);
+                TreeModelSort unit_model_sort = new TreeModelSort.with_model(unit_model_filter);
+
+                unit_view = new TreeView.with_model(unit_model_sort);
                 job_view = new TreeView.with_model(job_model);
 
-                unit_view.cursor_changed += unit_changed;
-                job_view.cursor_changed += job_changed;
+                unit_view.cursor_changed.connect(unit_changed);
+                job_view.cursor_changed.connect(job_changed);
 
-                unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
-                unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
-                unit_view.insert_column_with_attributes(-1, "Unit State", new CellRendererText(), "text", 4);
-                unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
-                unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 5);
+                new_column(unit_view, 2, "Load State");
+                new_column(unit_view, 3, "Active State");
+                new_column(unit_view, 4, "Unit State");
+                new_column(unit_view, 0, "Unit");
+                new_column(unit_view, 5, "Job");
 
-                job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
-                job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
-                job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
-                job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
+                new_column(job_view, 0, "Job");
+                new_column(job_view, 1, "Unit");
+                new_column(job_view, 2, "Type");
+                new_column(job_view, 3, "State");
 
                 ScrolledWindow scroll = new ScrolledWindow(null, null);
                 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
@@ -215,7 +229,9 @@ public class MainWindow : Window {
 
                 unit_dependency_label.set_track_visited_links(false);
                 unit_dependency_label.set_selectable(false);
-                unit_dependency_label.activate_link += on_activate_link;
+                unit_dependency_label.activate_link.connect(on_activate_link);
+
+                unit_fragment_path_label.set_track_visited_links(false);
 
                 Table unit_table = new Table(8, 6, false);
                 unit_table.set_row_spacings(6);
@@ -274,10 +290,10 @@ public class MainWindow : Window {
                 reload_button = new Button.with_mnemonic("_Reload");
                 restart_button = new Button.with_mnemonic("Res_tart");
 
-                start_button.clicked += on_start;
-                stop_button.clicked += on_stop;
-                reload_button.clicked += on_reload;
-                restart_button.clicked += on_restart;
+                start_button.clicked.connect(on_start);
+                stop_button.clicked.connect(on_stop);
+                reload_button.clicked.connect(on_reload);
+                restart_button.clicked.connect(on_restart);
 
                 bbox.pack_start(start_button, false, true, 0);
                 bbox.pack_start(stop_button, false, true, 0);
@@ -291,21 +307,19 @@ public class MainWindow : Window {
 
                 cancel_button = new Button.with_mnemonic("_Cancel");
 
-                cancel_button.clicked += on_cancel;
+                cancel_button.clicked.connect(on_cancel);
 
                 bbox.pack_start(cancel_button, false, true, 0);
 
-                bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
-
-                manager = bus.get_object(
+                manager = Bus.get_proxy_sync(
+                                user ? BusType.SESSION : BusType.SYSTEM,
                                 "org.freedesktop.systemd1",
-                                "/org/freedesktop/systemd1",
-                                "org.freedesktop.systemd1.Manager") as Manager;
+                                "/org/freedesktop/systemd1");
 
-                manager.unit_new += on_unit_new;
-                manager.job_new += on_job_new;
-                manager.unit_removed += on_unit_removed;
-                manager.job_removed += on_job_removed;
+                manager.unit_new.connect(on_unit_new);
+                manager.job_new.connect(on_job_new);
+                manager.unit_removed.connect(on_unit_removed);
+                manager.job_removed.connect(on_job_removed);
 
                 manager.subscribe();
 
@@ -315,7 +329,7 @@ public class MainWindow : Window {
                 populate_job_model();
         }
 
-        public void populate_unit_model() throws DBus.Error {
+        public void populate_unit_model() throws IOError {
                 unit_model.clear();
 
                 var list = manager.list_units();
@@ -323,12 +337,17 @@ public class MainWindow : Window {
                 foreach (var i in list) {
                         TreeIter iter;
 
-                        Unit u = bus.get_object(
+                        Properties p = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
                                         "org.freedesktop.systemd1",
-                                        i.unit_path,
-                                        "org.freedesktop.systemd1.Unit") as Unit;
+                                        i.unit_path);
 
-                        u.changed += on_unit_changed;
+                        p.properties_changed.connect(on_unit_changed);
+
+                        Unit u = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        "org.freedesktop.systemd1",
+                                        i.unit_path);
 
                         unit_model.append(out iter);
                         unit_model.set(iter,
@@ -342,7 +361,7 @@ public class MainWindow : Window {
                 }
         }
 
-        public void populate_job_model() throws DBus.Error {
+        public void populate_job_model() throws IOError {
                 job_model.clear();
 
                 var list = manager.list_jobs();
@@ -350,12 +369,17 @@ public class MainWindow : Window {
                 foreach (var i in list) {
                         TreeIter iter;
 
-                        Job j = bus.get_object(
+                        Properties p = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
                                         "org.freedesktop.systemd1",
-                                        i.job_path,
-                                        "org.freedesktop.systemd1.Job") as Job;
+                                        i.job_path);
+
+                        p.properties_changed.connect(on_job_changed);
 
-                        j.changed += on_job_changed;
+                        Job j = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        "org.freedesktop.systemd1",
+                                        i.job_path);
 
                         job_model.append(out iter);
                         job_model.set(iter,
@@ -500,21 +524,17 @@ public class MainWindow : Window {
                 unit_load_state_label.set_text_or_na(unit.load_state);
                 unit_active_state_label.set_text_or_na(unit.active_state);
                 unit_sub_state_label.set_text_or_na(unit.sub_state);
-                unit_fragment_path_label.set_text_or_na(unit.fragment_path);
-
-                uint64 t = unit.active_enter_timestamp;
-                if (t > 0) {
-                        Time timestamp = Time.local((time_t) (t / 1000000));
-                        unit_active_enter_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
-                } else
-                        unit_active_enter_timestamp_label.set_text_or_na();
-
-                t = unit.active_exit_timestamp;
-                if (t > 0) {
-                        Time timestamp = Time.local((time_t) (t / 1000000));
-                        unit_active_exit_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
-                } else
-                        unit_active_exit_timestamp_label.set_text_or_na();
+
+                string fp = unit.fragment_path;
+                if (fp != "")
+                        unit_fragment_path_label.set_markup_or_na("<a href=\"file://" + fp +"\">" + fp + "</a>" );
+                else
+                        unit_fragment_path_label.set_text_or_na();
+
+
+                unit_active_enter_timestamp_label.set_text_or_na(format_time(unit.active_enter_timestamp));
+
+                unit_active_exit_timestamp_label.set_text_or_na(format_time(unit.active_exit_timestamp));
 
                 bool b = unit.can_start;
                 start_button.set_sensitive(b);
@@ -583,7 +603,7 @@ public class MainWindow : Window {
 
                 try {
                         u.start("replace");
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -596,7 +616,7 @@ public class MainWindow : Window {
 
                 try {
                         u.stop("replace");
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -609,7 +629,7 @@ public class MainWindow : Window {
 
                 try {
                         u.reload("replace");
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -622,7 +642,7 @@ public class MainWindow : Window {
 
                 try {
                         u.restart("replace");
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -635,46 +655,61 @@ public class MainWindow : Window {
 
                 try {
                         j.cancel();
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
 
         public void update_unit_iter(TreeIter iter, string id, Unit u) {
 
-                string t = "";
-                Unit.JobLink jl = u.job;
+                try  {
+                        string t = "";
+                        Unit.JobLink jl = u.job;
 
-                if (jl.id != 0) {
-                        Job j = bus.get_object(
-                                        "org.freedesktop.systemd1",
-                                        jl.path,
-                                        "org.freedesktop.systemd1.Job") as Job;
+                        if (jl.id != 0) {
+                                Job j = Bus.get_proxy_sync(
+                                                user ? BusType.SESSION : BusType.SYSTEM,
+                                                "org.freedesktop.systemd1",
+                                                jl.path);
 
-                        t = j.job_type;
-                }
+                                t = j.job_type;
+                        }
 
-                unit_model.set(iter,
-                               0, id,
-                               1, u.description,
-                               2, u.load_state,
-                               3, u.active_state,
-                               4, u.sub_state,
-                               5, t != "" ? "→ %s".printf(t) : "",
-                               6, u);
+                        unit_model.set(iter,
+                                       0, id,
+                                       1, u.description,
+                                       2, u.load_state,
+                                       3, u.active_state,
+                                       4, u.sub_state,
+                                       5, t != "" ? "→ %s".printf(t) : "",
+                                       6, u);
+                } catch (IOError e) {
+                        show_error(e.message);
+                }
         }
 
         public void on_unit_new(string id, ObjectPath path) {
-                Unit u = bus.get_object(
-                                "org.freedesktop.systemd1",
-                                path,
-                                "org.freedesktop.systemd1.Unit") as Unit;
+                try {
 
-                u.changed += on_unit_changed;
+                        Properties p = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        "org.freedesktop.systemd1",
+                                        path);
 
-                TreeIter iter;
-                unit_model.append(out iter);
-                update_unit_iter(iter, id, u);
+                        p.properties_changed.connect(on_unit_changed);
+
+                        TreeIter iter;
+                        unit_model.append(out iter);
+
+                        Unit u = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        "org.freedesktop.systemd1",
+                                        path);
+
+                        update_unit_iter(iter, id, u);
+                } catch (IOError e) {
+                        show_error(e.message);
+                }
         }
 
         public void update_job_iter(TreeIter iter, uint32 id, Job j) {
@@ -688,16 +723,29 @@ public class MainWindow : Window {
         }
 
         public void on_job_new(uint32 id, ObjectPath path) {
-                Job j = bus.get_object(
-                                "org.freedesktop.systemd1",
-                                path,
-                                "org.freedesktop.systemd1.Job") as Job;
 
-                j.changed += on_job_changed;
+                try  {
 
-                TreeIter iter;
-                job_model.append(out iter);
-                update_job_iter(iter, id, j);
+                        Properties p = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        "org.freedesktop.systemd1",
+                                        path);
+
+                        p.properties_changed.connect(on_job_changed);
+
+                        TreeIter iter;
+                        job_model.append(out iter);
+
+                        Job j = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        "org.freedesktop.systemd1",
+                                        path);
+
+                        update_job_iter(iter, id, j);
+
+                } catch (IOError e) {
+                        show_error(e.message);
+                }
         }
 
         public void on_unit_removed(string id, ObjectPath path) {
@@ -721,7 +769,7 @@ public class MainWindow : Window {
                 } while (unit_model.iter_next(ref iter));
         }
 
-        public void on_job_removed(uint32 id, ObjectPath path) {
+        public void on_job_removed(uint32 id, ObjectPath path, string res) {
                 TreeIter iter;
                 if (!(job_model.get_iter_first(out iter)))
                         return;
@@ -743,56 +791,77 @@ public class MainWindow : Window {
                 } while (job_model.iter_next(ref iter));
         }
 
-        public void on_unit_changed(Unit u) {
-                TreeIter iter;
-                string id;
+        public void on_unit_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
 
-                if (!(unit_model.get_iter_first(out iter)))
-                        return;
+                try {
+                        TreeIter iter;
+                        string id;
 
-                id = u.id;
+                        Unit u = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        p.get_name(),
+                                        p.get_object_path());
 
-                do {
-                        string name;
+                        if (!(unit_model.get_iter_first(out iter)))
+                                return;
 
-                        unit_model.get(iter, 0, out name);
+                        id = u.id;
 
-                        if (id == name) {
-                                update_unit_iter(iter, id, u);
+                        do {
+                                string name;
 
-                                if (current_unit_id == id)
-                                        show_unit(u);
+                                unit_model.get(iter, 0, out name);
 
-                                break;
-                        }
+                                if (id == name) {
+                                        update_unit_iter(iter, id, u);
 
-                } while (unit_model.iter_next(ref iter));
+                                        if (current_unit_id == id)
+                                                show_unit(u);
+
+                                        break;
+                                }
+
+                        } while (unit_model.iter_next(ref iter));
+
+                } catch (IOError e) {
+                        show_error(e.message);
+                }
         }
 
-        public void on_job_changed(Job j) {
-                TreeIter iter;
-                uint32 id;
+        public void on_job_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
+                try {
+                        TreeIter iter;
+                        uint32 id;
 
-                if (!(job_model.get_iter_first(out iter)))
-                        return;
+                        Job j = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
+                                        p.get_name(),
+                                        p.get_object_path());
 
-                id = j.id;
+                        if (!(job_model.get_iter_first(out iter)))
+                                return;
 
-                do {
-                        uint32 k;
+                        id = j.id;
 
-                        job_model.get(iter, 5, out k);
+                        do {
+                                uint32 k;
 
-                        if (id == k) {
-                                update_job_iter(iter, id, j);
+                                job_model.get(iter, 5, out k);
 
-                                if (current_job_id == id)
-                                        show_job(j);
+                                if (id == k) {
+                                        update_job_iter(iter, id, j);
 
-                                break;
-                        }
+                                        if (current_job_id == id)
+                                                show_job(j);
 
-                } while (job_model.iter_next(ref iter));
+                                        break;
+                                }
+
+                        } while (job_model.iter_next(ref iter));
+
+                } catch (IOError e) {
+                        show_error(e.message);
+                }
         }
 
         public bool unit_filter(TreeModel model, TreeIter iter) {
@@ -837,7 +906,7 @@ public class MainWindow : Window {
         }
 
         public void unit_type_changed() {
-                TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
+                TreeModelFilter model = (TreeModelFilter) ((TreeModelSort) unit_view.get_model()).get_model();
 
                 model.refilter();
         }
@@ -845,7 +914,7 @@ public class MainWindow : Window {
         public void on_server_reload() {
                 try {
                         manager.reload();
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -857,7 +926,7 @@ public class MainWindow : Window {
                         if (unit_type_combo_box.get_active() != 0)
                                 unit_type_combo_box.set_active(8);
 
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -871,10 +940,10 @@ public class MainWindow : Window {
                 try {
                         var path = manager.load_unit(t);
 
-                        Unit u = bus.get_object(
+                        Unit u = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
                                         "org.freedesktop.systemd1",
-                                        path,
-                                        "org.freedesktop.systemd1.Unit") as Unit;
+                                        path);
 
                         var m = new MessageDialog(this,
                                                   DialogFlags.DESTROY_WITH_PARENT,
@@ -886,7 +955,7 @@ public class MainWindow : Window {
                         m.destroy();
 
                         show_unit(u);
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
         }
@@ -900,13 +969,13 @@ public class MainWindow : Window {
                 try {
                         string path = manager.get_unit(uri);
 
-                        Unit u = bus.get_object(
+                        Unit u = Bus.get_proxy_sync(
+                                        user ? BusType.SESSION : BusType.SYSTEM,
                                         "org.freedesktop.systemd1",
-                                        path,
-                                        "org.freedesktop.systemd1.Unit") as Unit;
+                                        path);
 
                         show_unit(u);
-                } catch (DBus.Error e) {
+                } catch (IOError e) {
                         show_error(e.message);
                 }
 
@@ -926,8 +995,8 @@ public class MainWindow : Window {
 }
 
 static const OptionEntry entries[] = {
-        { "session", 0,   0,                   OptionArg.NONE,   out session, "Connect to session bus", null },
-        { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE,   out session, "Connect to system bus", null },
+        { "user",    0,   0,                   OptionArg.NONE, out user, "Connect to user service manager", null },
+        { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE, out user, "Connect to system manager",       null },
         { null }
 };
 
@@ -937,7 +1006,7 @@ void show_error(string e) {
         m.destroy();
 }
 
-int main (string[] args) {
+int main(string[] args) {
 
         try {
                 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
@@ -946,10 +1015,10 @@ int main (string[] args) {
                 window.show_all();
 
                 Gtk.main();
-        } catch (DBus.Error e) {
+        } catch (IOError e) {
                 show_error(e.message);
         } catch (GLib.Error e) {
-                show_error(e.message);
+                stderr.printf("%s\n", e.message);
         }
 
         return 0;