[gnome-usage] processes-tagging: discriminate between users; all processes displayed (GTop.KERN_PROC_ALL)



commit f876ed3ef8a60a746a10b259c86012bcb7b3ec0c
Author: Lukasz Kolodziejczyk <lukasz m kolodziejczyk gmail com>
Date:   Thu Nov 9 16:08:00 2017 +0000

    processes-tagging: discriminate between users; all processes displayed (GTop.KERN_PROC_ALL)

 data/interface/adwaita.css | 18 +++++++++++
 data/ui/process-row.ui     | 21 +++++++++++++
 src/process-row.vala       | 77 ++++++++++++++++++++++++++++++++++++++++++++++
 src/process.vala           |  4 ++-
 src/system-monitor.vala    | 20 ++++++++----
 5 files changed, 133 insertions(+), 7 deletions(-)
---
diff --git a/data/interface/adwaita.css b/data/interface/adwaita.css
index d508e0a..0d9128a 100644
--- a/data/interface/adwaita.css
+++ b/data/interface/adwaita.css
@@ -154,3 +154,21 @@ box.storage {
     background-color: @theme_bg_color;
     margin: 5px;
 }
+
+.tag {
+    border-style: none;
+    border-radius: 3px;
+    padding: 3px 6px;
+    color: @theme_base_color;
+}
+
+.tag.tag-root {
+    background-color: red;
+}
+
+.tag.tag-system {
+    background-color: orange;
+}
+
+.tag.tag-user {
+    background: alpha(@theme_fg_color, 0.4);
diff --git a/data/ui/process-row.ui b/data/ui/process-row.ui
index 0fc0444..d14d553 100644
--- a/data/ui/process-row.ui
+++ b/data/ui/process-row.ui
@@ -56,6 +56,27 @@
                 <property name="position">3</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkBox" id="user_tag_box">
+                <property name="visible">False</property>
+                <property name="can_focus">False</property>
+                <property name="margin_right">10</property>
+                <style>
+                  <class name="tag"/>
+                </style>
+                <child>
+                  <object class="GtkLabel" id="user_tag_label">
+                    <property name="visible">True</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="pack_type">end</property>
+                <property name="position">3</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">False</property>
diff --git a/src/process-row.vala b/src/process-row.vala
index 0db4925..3bc47b3 100644
--- a/src/process-row.vala
+++ b/src/process-row.vala
@@ -32,8 +32,16 @@ namespace Usage
         private Gtk.Label title_label;
 
         [GtkChild]
+        private Gtk.Box user_tag_box;
+
+        [GtkChild]
+        private Gtk.Label user_tag_label;
+
+        [GtkChild]
         private Gtk.Label load_label;
 
+        private Act.User user;
+
         public Process process { get; private set; }
         public bool max_usage { get; private set; }
         public bool group {
@@ -45,13 +53,22 @@ namespace Usage
         private const int MAX_CPU_USAGE_LIMIT = 90;
         private const int MAX_MEMORY_USAGE_LIMIT = 90;
 
+        private const string CSS_TAG_USER = "tag-user";
+        private const string CSS_TAG_ROOT = "tag-root";
+        private const string CSS_TAG_SYSTEM = "tag-system";
+
         public ProcessRow(Process process, ProcessListBoxType type, bool opened = false)
         {
             this.type = type;
             this.process = process;
+            this.user = Act.UserManager.get_default().get_user_by_id(process.uid);
 
             load_icon(process.display_name);
             update();
+
+            this.user.notify["is-loaded"].connect(() => {
+                update_user_tag();
+            });
         }
 
         private void load_icon(string display_name)
@@ -68,6 +85,7 @@ namespace Usage
         private void update()
         {
             update_load_label();
+            update_user_tag();
             check_max_usage();
             set_styles();
 
@@ -94,6 +112,65 @@ namespace Usage
             }
         }
 
+        private void update_user_tag()
+        {
+            remove_user_tag();
+            if(user.is_loaded)
+            {
+                create_user_tag();
+            }
+        }
+
+        private void remove_user_tag()
+        {
+            user_tag_box.visible = false;
+            user_tag_box.get_style_context().remove_class(CSS_TAG_USER);
+            user_tag_box.get_style_context().remove_class(CSS_TAG_ROOT);
+            user_tag_box.get_style_context().remove_class(CSS_TAG_SYSTEM);
+        }
+
+        private void create_user_tag()
+        {
+            user_tag_box.visible = true;
+            user_tag_label.label = user.real_name;
+
+            string class_name = "";
+            if(is_regular_user())
+            {
+                class_name = CSS_TAG_USER;
+            }
+            else if(is_root_user())
+            {
+                class_name = CSS_TAG_ROOT;
+            }
+            else if(is_system_user())
+            {
+                class_name = CSS_TAG_SYSTEM;
+            }
+            user_tag_box.get_style_context().add_class(class_name);
+
+            if(is_logged_in())
+            {
+                user_tag_box.visible = false;
+            }
+        }
+
+        private bool is_regular_user(){
+            return user.is_local_account();
+        }
+
+        private bool is_root_user(){
+            return user.uid == 0;
+        }
+
+        private bool is_system_user(){
+            return !is_regular_user() && !is_root_user();
+        }
+
+        private bool is_logged_in(){
+            return user.user_name == GLib.Environment.get_user_name();
+        }
+
         private void check_max_usage()
         {
             switch(type)
diff --git a/src/process.vala b/src/process.vala
index 9dcc6fe..478ad33 100644
--- a/src/process.vala
+++ b/src/process.vala
@@ -26,6 +26,7 @@ namespace Usage
         public string cmdline { get; private set; }
         public string cmdline_parameter { get; private set; } //Isn't parameters as "-p" etc, but parameter 
for running app, for ex. "--writer' with libreoffice, or "privacy" with gnome-control-center
         public string display_name { get; private set; }
+        public uint uid { get; private set; }
 
         public double cpu_load { get; set; default = 0; }
         public double x_cpu_load { get; set; default = 0; }
@@ -40,12 +41,13 @@ namespace Usage
         public bool alive { get; set; default = true; }
         public ProcessStatus status { get; set; default = ProcessStatus.SLEEPING; }
 
-        public Process(Pid pid, string cmdline, string cmdline_parameter, string display_name)
+        public Process(Pid pid, string cmdline, string cmdline_parameter, string display_name, uint uid)
         {
             this.pid = pid;
             this.cmdline = cmdline;
             this.cmdline_parameter = cmdline_parameter;
             this.display_name = display_name;
+            this.uid = uid;
         }
 
         public void update_from_process(Process process)
diff --git a/src/system-monitor.vala b/src/system-monitor.vala
index ebfb3d9..280d02e 100644
--- a/src/system-monitor.vala
+++ b/src/system-monitor.vala
@@ -37,7 +37,7 @@ namespace Usage
         private HashTable<string, Process> cpu_process_table;
         private HashTable<string, Process> ram_process_table;
 
-        private int process_mode = GTop.EXCLUDE_SYSTEM;
+        private int process_mode = GTop.KERN_PROC_ALL;
         private GLib.List<AppInfo> apps_info;
 
         private static SystemMonitor system_monitor;
@@ -147,7 +147,8 @@ namespace Usage
                     string cmdline_parameter;
                     string cmdline = get_full_process_cmd(pids[i], out cmdline_parameter);
                     string display_name = get_display_name(cmdline, cmdline_parameter);
-                    var process = new Process(pids[i], cmdline, cmdline_parameter, display_name);
+                    uint uid = get_uid(pids[i]);
+                    var process = new Process(pids[i], cmdline, cmdline_parameter, display_name, uid);
                     cpu_monitor.update_process(ref process);
                     process_table.insert (pids[i], (owned) process);
                 }
@@ -189,6 +190,13 @@ namespace Usage
             return true;
         }
 
+        private uint get_uid(Pid pid)
+        {
+            GTop.ProcUid procUid;
+            GTop.get_proc_uid(out procUid, pid);
+            return procUid.uid;
+        }
+
                private string get_display_name(string cmdline, string cmdline_parameter)
                {
             AppInfo app_info = null;
@@ -342,7 +350,7 @@ namespace Usage
                         }
                         else //add subrow
                         {
-                            var process = new Process(process_it.pid, process_it.cmdline, 
process_it.cmdline_parameter, process_it.display_name);
+                            var process = new Process(process_it.pid, process_it.cmdline, 
process_it.cmdline_parameter, process_it.display_name, process_it.uid);
                             process.update_from_process(process_it);
                             to_table[process.cmdline].sub_processes.insert(process.pid, (owned) process);
                         }
@@ -359,11 +367,11 @@ namespace Usage
                             to_table[process_it.cmdline].sub_processes = new HashTable<Pid?, 
Process>(int_hash, int_equal);
                             unowned Process process = to_table[process_it.cmdline];
 
-                            var sub_process_one = new Process(process.pid, process.cmdline, 
process.cmdline_parameter, process.display_name);
+                            var sub_process_one = new Process(process.pid, process.cmdline, 
process.cmdline_parameter, process.display_name, process.uid);
                             sub_process_one.update_from_process(process);
                             to_table[process_it.cmdline].sub_processes.insert(sub_process_one.pid, (owned) 
sub_process_one);
 
-                            var sub_process = new Process(process_it.pid, process_it.cmdline, 
process_it.cmdline_parameter, process_it.display_name);
+                            var sub_process = new Process(process_it.pid, process_it.cmdline, 
process_it.cmdline_parameter, process_it.display_name, process_it.uid);
                             sub_process.update_from_process(process_it);
                             to_table[process_it.cmdline].sub_processes.insert(process_it.pid, (owned) 
sub_process);
                         }
@@ -371,7 +379,7 @@ namespace Usage
                 }
                 else //add process
                 {
-                     var process = new Process(process_it.pid, process_it.cmdline, 
process_it.cmdline_parameter, process_it.display_name);
+                     var process = new Process(process_it.pid, process_it.cmdline, 
process_it.cmdline_parameter, process_it.display_name, process_it.uid);
                      process.update_from_process(process_it);
                      to_table.insert(process.cmdline, (owned) process);
                 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]