[gnome-usage] settings: Make Settings class singleton



commit 1b70dc7738292d053545c26eb407153e12b4f59e
Author: Petr Štětka <pstetka redhat com>
Date:   Thu Aug 24 12:56:47 2017 +0200

    settings: Make Settings class singleton
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781542

 src/application.vala        |    2 --
 src/cpu-graph-table.vala    |    4 ++--
 src/memory-graph-table.vala |    2 +-
 src/process-dialog.vala     |    3 ++-
 src/process-list-box.vala   |    2 +-
 src/settings.vala           |   10 ++++++++++
 src/system-monitor.vala     |    2 +-
 7 files changed, 17 insertions(+), 8 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index d34aea8..404bbdd 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -24,7 +24,6 @@ namespace Usage
 {
     public class Application : Gtk.Application
     {
-        public Settings settings;
         private Window window;
 
         private const GLib.ActionEntry app_entries[] =
@@ -37,7 +36,6 @@ namespace Usage
         public Application ()
         {
             application_id = "org.gnome.Usage";
-            settings = new Settings();
         }
 
         public Window? get_window()
diff --git a/src/cpu-graph-table.vala b/src/cpu-graph-table.vala
index 198d325..f63ed88 100644
--- a/src/cpu-graph-table.vala
+++ b/src/cpu-graph-table.vala
@@ -31,7 +31,7 @@ namespace Usage {
 
         public CpuGraphTableComplex ()
         {
-            var settings = (GLib.Application.get_default() as Application).settings;
+            var settings = Settings.get_default();
             set_timespan (settings.graph_timespan * 1000);
             set_max_samples (settings.graph_max_samples);
 
@@ -97,7 +97,7 @@ namespace Usage {
 
         public CpuGraphTableMostUsedCore ()
         {
-            var settings = (GLib.Application.get_default() as Application).settings;
+            var settings = Settings.get_default();
             set_timespan (settings.graph_timespan * 1000);
             set_max_samples (settings.graph_max_samples);
 
diff --git a/src/memory-graph-table.vala b/src/memory-graph-table.vala
index e508e32..22a28f6 100644
--- a/src/memory-graph-table.vala
+++ b/src/memory-graph-table.vala
@@ -33,7 +33,7 @@ namespace Usage {
 
         public MemoryGraphTable ()
         {
-            var settings = (GLib.Application.get_default() as Application).settings;
+            var settings = Settings.get_default();
             set_timespan (settings.graph_timespan * 1000);
             set_max_samples (settings.graph_max_samples);
 
diff --git a/src/process-dialog.vala b/src/process-dialog.vala
index f75e107..8d2b56a 100644
--- a/src/process-dialog.vala
+++ b/src/process-dialog.vala
@@ -59,7 +59,8 @@ namespace Usage
             headerbar = new ProcessDialogHeaderBar(stop_button, pid, this.title, process);
             set_titlebar(headerbar);
 
-            Timeout.add((GLib.Application.get_default() as Application).settings.list_update_pie_charts_UI, 
update);
+            var settings = Settings.get_default();
+            Timeout.add(settings.list_update_pie_charts_UI, update);
             update();
         }
 
diff --git a/src/process-list-box.vala b/src/process-list-box.vala
index c3eff8a..db0e6c1 100644
--- a/src/process-list-box.vala
+++ b/src/process-list-box.vala
@@ -71,7 +71,7 @@ namespace Usage
             model = new ListStore(typeof(Process));
             bind_model(model, on_row_created);
 
-            var settings = (GLib.Application.get_default() as Application).settings;
+            var settings = Settings.get_default();
             Timeout.add(settings.list_update_interval_UI, update);
         }
 
diff --git a/src/settings.vala b/src/settings.vala
index ab73a04..b2783b6 100644
--- a/src/settings.vala
+++ b/src/settings.vala
@@ -30,5 +30,15 @@ namespace Usage {
         public uint list_update_interval_UI { get; set; default = 5000; }
         public uint list_update_pie_charts_UI { get; set; default = 1000; }
         public uint data_update_interval { get; set; default = 1000; }
+
+        private static Settings settings;
+
+        public static Settings get_default()
+        {
+            if (settings == null)
+                settings = new Settings ();
+
+            return settings;
+        }
     }
 }
diff --git a/src/system-monitor.vala b/src/system-monitor.vala
index 7828e52..6d7a47c 100644
--- a/src/system-monitor.vala
+++ b/src/system-monitor.vala
@@ -96,7 +96,7 @@ namespace Usage
             cpu_process_table = new HashTable<string, Process>(str_hash, str_equal);
             ram_process_table = new HashTable<string, Process>(str_hash, str_equal);
 
-            var settings = (GLib.Application.get_default() as Application).settings;
+            var settings = Settings.get_default();
             apps_info = AppInfo.get_all();
 
             update_data();


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