chiark / gitweb /
systemadm: use color for dependency links
[elogind.git] / src / systemadm.vala
index d45ec64ca45048de4beaa3b8a8d1e85bdbb2620d..eed46b57e9e9ebd64d3342b7836ad153dc0ad655 100644 (file)
@@ -23,6 +23,20 @@ using Pango;
 
 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) {
                 if (text != null)
@@ -32,12 +46,11 @@ public class LeftLabel : Label {
         }
 }
 
-public class RightLabel : Label {
+public class RightLabel : WrapLabel {
+
         public RightLabel(string? text = null) {
-                set_text_or_na(text);
-                set_alignment(0, 0);
-                set_ellipsize(EllipsizeMode.START);
                 set_selectable(true);
+                set_text_or_na(text);
         }
 
         public void set_text_or_na(string? text = null) {
@@ -66,6 +79,8 @@ public class MainWindow : Window {
         private ListStore unit_model;
         private ListStore job_model;
 
+        private Gee.HashMap<string, Unit> unit_map;
+
         private Button start_button;
         private Button stop_button;
         private Button restart_button;
@@ -99,6 +114,7 @@ public class MainWindow : Window {
         private RightLabel job_type_label;
 
         private ComboBox unit_type_combo_box;
+        private CheckButton inactive_checkbox;
 
         public MainWindow() throws IOError {
                 title = user ? "systemd User Service Manager" : "systemd System Manager";
@@ -123,18 +139,24 @@ public class MainWindow : Window {
                 type_hbox.pack_start(unit_type_combo_box, false, false, 0);
                 unit_vbox.pack_start(type_hbox, false, false, 0);
 
-                unit_type_combo_box.append_text("Show All Units");
-                unit_type_combo_box.append_text("Show Only Live Units");
+                unit_type_combo_box.append_text("All unit types");
+                unit_type_combo_box.append_text("Targets");
                 unit_type_combo_box.append_text("Services");
-                unit_type_combo_box.append_text("Sockets");
                 unit_type_combo_box.append_text("Devices");
                 unit_type_combo_box.append_text("Mounts");
                 unit_type_combo_box.append_text("Automounts");
-                unit_type_combo_box.append_text("Targets");
+                unit_type_combo_box.append_text("Swaps");
+                unit_type_combo_box.append_text("Sockets");
+                unit_type_combo_box.append_text("Paths");
+                unit_type_combo_box.append_text("Timers");
                 unit_type_combo_box.append_text("Snapshots");
-                unit_type_combo_box.set_active(1);
+                unit_type_combo_box.set_active(0); // Show All
                 unit_type_combo_box.changed.connect(unit_type_changed);
 
+                inactive_checkbox = new CheckButton.with_label("inactive too");
+                inactive_checkbox.toggled.connect(unit_type_changed);
+                type_hbox.pack_start(inactive_checkbox, false, false, 0);
+
                 unit_load_entry = new Entry();
                 unit_load_button = new Button.with_mnemonic("_Load");
                 unit_load_button.set_sensitive(false);
@@ -160,26 +182,30 @@ public class MainWindow : Window {
                 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
                 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
 
+                unit_map = new Gee.HashMap<string, Unit>();
+
                 TreeModelFilter unit_model_filter;
                 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.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);
@@ -333,6 +359,8 @@ public class MainWindow : Window {
                                         "org.freedesktop.systemd1",
                                         i.unit_path);
 
+                        unit_map[i.id] = u;
+
                         unit_model.append(out iter);
                         unit_model.set(iter,
                                        0, i.id,
@@ -393,6 +421,10 @@ public class MainWindow : Window {
                 return u;
         }
 
+        public Unit? get_unit(string id) {
+                return this.unit_map[id];
+        }
+
         public void unit_changed() {
                 Unit u = get_current_unit();
 
@@ -425,7 +457,30 @@ public class MainWindow : Window {
                 unit_cgroup_label.set_text_or_na();
         }
 
+        public string format_unit_link(string i) {
+                Unit? u = get_unit(i);
+                if(u == null)
+                        return "<span color='grey'>" + i + "</span";
+
+                string color;
+                switch (u.sub_state) {
+                case "active": color = "blue"; break;
+                case "dead": color = "red"; break;
+                case "running": color = "green"; break;
+                default: color = "black"; break;
+                }
+                string span = "<span underline='none' color='" + color + "'>"
+                              + i + "(" +
+                              u.sub_state + ")" + "</span>";
+                return  " <a href='" + i + "'>" + span + "</a>";
+        }
+
+
         public string make_dependency_string(string? prefix, string word, string[] dependencies) {
+                Gee.Collection<unowned string> sorted = new Gee.TreeSet<string>();
+                foreach (string i in dependencies)
+                        sorted.add(i);
+
                 bool first = true;
                 string r;
 
@@ -434,7 +489,7 @@ public class MainWindow : Window {
                 else
                         r = prefix;
 
-                foreach (string i in dependencies) {
+                foreach (string i in sorted) {
                         if (r != "")
                                 r += first ? "\n" : ",";
 
@@ -443,7 +498,7 @@ public class MainWindow : Window {
                                 first = false;
                         }
 
-                        r += " <a href=\"" + i + "\">" + i + "</a>";
+                        r += format_unit_link(i);
                 }
 
                 return r;
@@ -515,19 +570,10 @@ public class MainWindow : Window {
                 else
                         unit_fragment_path_label.set_text_or_na();
 
-                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();
+                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);
@@ -699,6 +745,8 @@ public class MainWindow : Window {
                                         "org.freedesktop.systemd1",
                                         path);
 
+                        unit_map[id] = u;
+
                         update_unit_iter(iter, id, u);
                 } catch (IOError e) {
                         show_error(e.message);
@@ -760,6 +808,8 @@ public class MainWindow : Window {
                         }
 
                 } while (unit_model.iter_next(ref iter));
+
+                unit_map.unset(id);
         }
 
         public void on_job_removed(uint32 id, ObjectPath path, string res) {
@@ -865,41 +915,41 @@ public class MainWindow : Window {
                 if (id == null)
                         return false;
 
-                switch (unit_type_combo_box.get_active()) {
-
-                        case 0:
-                                return true;
-
-                        case 1:
-                                return active_state != "inactive" || job != "";
-
-                        case 2:
-                                return id.has_suffix(".service");
-
-                        case 3:
-                                return id.has_suffix(".socket");
-
-                        case 4:
-                                return id.has_suffix(".device");
-
-                        case 5:
-                                return id.has_suffix(".mount");
-
-                        case 6:
-                                return id.has_suffix(".automount");
-
-                        case 7:
-                                return id.has_suffix(".target");
+                if (!inactive_checkbox.get_active()
+                    && active_state == "inactive" && job == "")
+                        return false;
 
-                        case 8:
-                                return id.has_suffix(".snapshot");
+                switch (unit_type_combo_box.get_active()) {
+                case 0:
+                        return true;
+                case 1:
+                        return id.has_suffix(".target");
+                case 2:
+                        return id.has_suffix(".service");
+                case 3:
+                        return id.has_suffix(".device");
+                case 4:
+                        return id.has_suffix(".mount");
+                case 5:
+                        return id.has_suffix(".automount");
+                case 6:
+                        return id.has_suffix(".swap");
+                case 7:
+                        return id.has_suffix(".socket");
+                case 8:
+                        return id.has_suffix(".path");
+                case 9:
+                        return id.has_suffix(".timer");
+                case 10:
+                        return id.has_suffix(".snapshot");
+                default:
+                        assert(false);
+                        return false;
                 }
-
-                return false;
         }
 
         public void unit_type_changed() {
-                TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
+                TreeModelFilter model = (TreeModelFilter) ((TreeModelSort) unit_view.get_model()).get_model();
 
                 model.refilter();
         }