[gnome-usage] system-monitor: Do SystemMonitor class singleton



commit a73b525d82c372357422db3d76780f7cd3c188a7
Author: Petr Štětka <pstetka redhat com>
Date:   Fri May 26 12:55:02 2017 +0200

    system-monitor: Do SystemMonitor class singleton
    
    We have only one instance of SystemMonitor, so we can use
    singleton pattern for SystemMonitor class.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781542

 src/application.vala         |    7 -------
 src/cpu-graph-table.vala     |    4 ++--
 src/cpu-sub-view.vala        |    2 +-
 src/memory-graph-table.vala  |    2 +-
 src/memory-sub-view.vala     |    2 +-
 src/process-dialog.vala      |    8 ++++----
 src/process-list-box.vala    |    7 ++++---
 src/process-row.vala         |    6 +++---
 src/sub-process-sub-row.vala |   12 ++++++------
 src/system-monitor.vala      |   16 +++++++++++++---
 src/view.vala                |    2 --
 11 files changed, 35 insertions(+), 33 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 1719091..b71d6b5 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -26,7 +26,6 @@ namespace Usage
     {
         public Settings settings;
         private Window window;
-        private SystemMonitor monitor;
         private StorageAnalyzer storage_analyzer;
 
         private const GLib.ActionEntry app_entries[] =
@@ -40,15 +39,9 @@ namespace Usage
         {
             application_id = "org.gnome.Usage";
             settings = new Settings();
-            monitor = new SystemMonitor();
             storage_analyzer = new StorageAnalyzer();
         }
 
-        public SystemMonitor get_system_monitor()
-        {
-            return monitor;
-        }
-
         public StorageAnalyzer get_storage_analyzer()
         {
             return storage_analyzer;
diff --git a/src/cpu-graph-table.vala b/src/cpu-graph-table.vala
index f0c4241..198d325 100644
--- a/src/cpu-graph-table.vala
+++ b/src/cpu-graph-table.vala
@@ -58,7 +58,7 @@ namespace Usage {
             Rg.TableIter iter;
             push (out iter, get_monotonic_time ());
 
-            SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
+            SystemMonitor monitor = SystemMonitor.get_default();
 
             for (int i = 0; i < get_num_processors(); i++)
             {
@@ -112,7 +112,7 @@ namespace Usage {
             Rg.TableIter iter;
             push (out iter, get_monotonic_time ());
 
-            SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
+            SystemMonitor monitor = SystemMonitor.get_default();
             double most_used_core = monitor.x_cpu_load[0];
 
             for (int i = 1; i < get_num_processors(); i++)
diff --git a/src/cpu-sub-view.vala b/src/cpu-sub-view.vala
index 3ff50a6..b7c2462 100644
--- a/src/cpu-sub-view.vala
+++ b/src/cpu-sub-view.vala
@@ -58,7 +58,7 @@ namespace Usage
             no_process_label.set_use_markup(true);
             no_process_label.get_style_context().add_class("dim-label");
 
-            (GLib.Application.get_default() as 
Application).get_system_monitor().cpu_processes_ready.connect(() =>
+            SystemMonitor.get_default().cpu_processes_ready.connect(() =>
             {
                 cpu_box.pack_start(process_list_box, false, false, 0);
                 cpu_box.pack_start(no_process_label, true, true, 0);
diff --git a/src/memory-graph-table.vala b/src/memory-graph-table.vala
index af304fe..e508e32 100644
--- a/src/memory-graph-table.vala
+++ b/src/memory-graph-table.vala
@@ -50,7 +50,7 @@ namespace Usage {
             Rg.TableIter iter;
             push (out iter, get_monotonic_time ());
 
-            SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
+            SystemMonitor monitor = SystemMonitor.get_default();
             double ram_usage = 0;
             if(monitor.ram_total != 0)
                 ram_usage = (((double) monitor.ram_usage / monitor.ram_total) * 100);
diff --git a/src/memory-sub-view.vala b/src/memory-sub-view.vala
index e1e5b8f..64ad3fe 100644
--- a/src/memory-sub-view.vala
+++ b/src/memory-sub-view.vala
@@ -58,7 +58,7 @@ namespace Usage
             no_process_label.set_use_markup(true);
             no_process_label.get_style_context().add_class("dim-label");
 
-            (GLib.Application.get_default() as 
Application).get_system_monitor().cpu_processes_ready.connect(() =>
+            SystemMonitor.get_default().cpu_processes_ready.connect(() =>
             {
                 memory_box.pack_start(process_list_box, false, false, 0);
                 memory_box.pack_start(no_process_label, true, true, 0);
diff --git a/src/process-dialog.vala b/src/process-dialog.vala
index 9ee6cf8..f75e107 100644
--- a/src/process-dialog.vala
+++ b/src/process-dialog.vala
@@ -61,11 +61,11 @@ namespace Usage
 
             Timeout.add((GLib.Application.get_default() as Application).settings.list_update_pie_charts_UI, 
update);
             update();
-       }
+        }
 
-       private bool update()
-       {
-           SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
+        private bool update()
+        {
+            SystemMonitor monitor = SystemMonitor.get_default();
             unowned Process data = monitor.get_process_by_pid(pid);
 
             ProcessStatus process_status = ProcessStatus.DEAD;
diff --git a/src/process-list-box.vala b/src/process-list-box.vala
index d247134..c3eff8a 100644
--- a/src/process-list-box.vala
+++ b/src/process-list-box.vala
@@ -94,24 +94,25 @@ namespace Usage
             bind_model(null, null);
             model.remove_all();
 
+            SystemMonitor system_monitor = SystemMonitor.get_default();
             if(search_text == "")
             {
                 switch(type)
                 {
                     default:
                     case ProcessListBoxType.PROCESSOR:
-                        foreach(unowned Process process in (GLib.Application.get_default() as 
Application).get_system_monitor().get_cpu_processes())
+                        foreach(unowned Process process in system_monitor.get_cpu_processes())
                             model.insert_sorted(process, processcmp);
                         break;
                     case ProcessListBoxType.MEMORY:
-                        foreach(unowned Process process in (GLib.Application.get_default() as 
Application).get_system_monitor().get_ram_processes())
+                        foreach(unowned Process process in system_monitor.get_ram_processes())
                             model.insert_sorted(process, processcmp);
                         break;
                 }
             }
             else
             {
-                foreach(unowned Process process in (GLib.Application.get_default() as 
Application).get_system_monitor().get_ram_processes()) //because ram contains all processes
+                foreach(unowned Process process in system_monitor.get_ram_processes()) //because ram 
contains all processes
                 {
                     if(process.get_display_name().down().contains(search_text.down()) || 
process.get_cmdline().down().contains(search_text.down()))
                         model.insert_sorted(process, processcmp);
diff --git a/src/process-row.vala b/src/process-row.vala
index c114e63..abb8c09 100644
--- a/src/process-row.vala
+++ b/src/process-row.vala
@@ -83,8 +83,8 @@ namespace Usage
         private Gtk.Image load_icon(string display_name)
         {
             Gtk.Image icon = null;
-               foreach (AppInfo app_info in (GLib.Application.get_default() as 
Application).get_system_monitor().get_apps_info())
-               {
+            foreach (AppInfo app_info in SystemMonitor.get_default().get_apps_info())
+            {
                 if(app_info.get_display_name() == display_name)
                 {
                     if(app_info.get_icon() != null)
@@ -148,7 +148,7 @@ namespace Usage
                         max_usage = false;
                     break;
                 case ProcessListBoxType.MEMORY:
-                    SystemMonitor monitor = (GLib.Application.get_default() as 
Application).get_system_monitor();
+                    SystemMonitor monitor = SystemMonitor.get_default();
 
                     if(group)
                     {
diff --git a/src/sub-process-sub-row.vala b/src/sub-process-sub-row.vala
index 8495086..e0f88ea 100644
--- a/src/sub-process-sub-row.vala
+++ b/src/sub-process-sub-row.vala
@@ -32,11 +32,11 @@ namespace Usage
             this.type = type;
             this.process = process;
 
-                       var row_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
-                       row_box.margin = 10;
-               load_label = new Gtk.Label(null);
-               load_label.ellipsize = Pango.EllipsizeMode.END;
-               load_label.max_width_chars = 30;
+            var row_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
+            row_box.margin = 10;
+            load_label = new Gtk.Label(null);
+            load_label.ellipsize = Pango.EllipsizeMode.END;
+            load_label.max_width_chars = 30;
 
             var icon = new Gtk.Image.from_icon_name("system-run-symbolic", Gtk.IconSize.BUTTON);
             icon.width_request = 24;
@@ -69,7 +69,7 @@ namespace Usage
                         max_usage = false;
                     break;
                 case ProcessListBoxType.MEMORY:
-                    SystemMonitor monitor = (GLib.Application.get_default() as 
Application).get_system_monitor();
+                    SystemMonitor monitor = SystemMonitor.get_default();
                     load_label.set_label(Utils.format_size_values(process.get_mem_usage()));
 
                     if((((double) process.get_mem_usage() / monitor.ram_total) * 100) >= 90)
diff --git a/src/system-monitor.vala b/src/system-monitor.vala
index 6a6d363..7828e52 100644
--- a/src/system-monitor.vala
+++ b/src/system-monitor.vala
@@ -37,10 +37,20 @@ namespace Usage
         private HashTable<string, Process> cpu_process_table;
         private HashTable<string, Process> ram_process_table;
 
-               private int process_mode = GTop.EXCLUDE_SYSTEM;
-               private GLib.List<AppInfo> apps_info;
+        private int process_mode = GTop.EXCLUDE_SYSTEM;
+        private GLib.List<AppInfo> apps_info;
 
-               public List<unowned Process> get_processes()
+        private static SystemMonitor system_monitor;
+
+        public static SystemMonitor get_default()
+        {
+            if (system_monitor == null)
+                system_monitor = new SystemMonitor ();
+
+            return system_monitor;
+        }
+
+        public List<unowned Process> get_processes()
         {
             return process_table.get_values();
         }
diff --git a/src/view.vala b/src/view.vala
index d9bd346..ed9bf6e 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -22,12 +22,10 @@ namespace Usage
 {
     public abstract class View : Gtk.Bin
     {
-        protected SystemMonitor monitor;
         public string title;
 
         public View ()
         {
-            monitor = (GLib.Application.get_default() as Application).get_system_monitor();
             visible = true;
         }
     }


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