[gnome-usage] storage-analyzer: Make StorageAnalyzer class singleton



commit 84a88578c9f041f32215cfb03e14c61211d97f37
Author: Petr Štětka <pstetka redhat com>
Date:   Fri Aug 18 11:21:20 2017 +0200

    storage-analyzer: Make StorageAnalyzer class singleton
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781542

 src/application.vala       |    7 -------
 src/header-bar.vala        |    2 +-
 src/storage-actionbar.vala |   13 ++++++-------
 src/storage-analyzer.vala  |   11 ++++++++++-
 src/storage-graph.vala     |    2 +-
 src/storage-list-box.vala  |    2 +-
 src/storage-row.vala       |   16 ++++++++--------
 src/window.vala            |    2 +-
 8 files changed, 28 insertions(+), 27 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index b71d6b5..aec27a6 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -26,7 +26,6 @@ namespace Usage
     {
         public Settings settings;
         private Window window;
-        private StorageAnalyzer storage_analyzer;
 
         private const GLib.ActionEntry app_entries[] =
         {
@@ -39,12 +38,6 @@ namespace Usage
         {
             application_id = "org.gnome.Usage";
             settings = new Settings();
-            storage_analyzer = new StorageAnalyzer();
-        }
-
-        public StorageAnalyzer get_storage_analyzer()
-        {
-            return storage_analyzer;
         }
 
         public Window? get_window()
diff --git a/src/header-bar.vala b/src/header-bar.vala
index d6a2549..e36b199 100644
--- a/src/header-bar.vala
+++ b/src/header-bar.vala
@@ -133,7 +133,7 @@ namespace Usage
             storage_rescan_button.hide ();
 
             storage_back_button.hide ();
-            (GLib.Application.get_default() as Application).get_storage_analyzer().create_cache.begin(true);
+            StorageAnalyzer.get_default().create_cache.begin(true);
             ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().reload();
         }
 
diff --git a/src/storage-actionbar.vala b/src/storage-actionbar.vala
index 4b69e87..d15effc 100644
--- a/src/storage-actionbar.vala
+++ b/src/storage-actionbar.vala
@@ -128,11 +128,11 @@ namespace Usage
             if(chooser.run() == Gtk.ResponseType.ACCEPT)
             {
                Timeout.add(0, () => {
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                     {
                         StorageRow actual_storage_row = (StorageRow) row;
                         string destination = chooser.get_file().get_parse_name() + "/" + 
Path.get_basename(actual_storage_row.get_item_path());
-                        var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                         
storage_analyzer.move_file.begin(File.new_for_path(actual_storage_row.get_item_path()), 
File.new_for_path(destination), () => {
                             ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                         });
@@ -163,10 +163,10 @@ namespace Usage
             if(dialog.run() == Gtk.ResponseType.OK)
             {
                Timeout.add(0, () => {
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                     {
                         StorageRow storage_row = (StorageRow) row;
-                        var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                         storage_analyzer.delete_file.begin(storage_row.get_item_path(), () => {
                             ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                         });
@@ -186,8 +186,8 @@ namespace Usage
             Timeout.add(0, () => {
                 foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                 {
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     StorageRow storage_row = (StorageRow) row;
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                     storage_analyzer.trash_file.begin(storage_row.get_item_path(), () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
@@ -202,6 +202,7 @@ namespace Usage
         {
             Timeout.add(0, () => {
                 string folders = "";
+                var storage_analyzer = StorageAnalyzer.get_default();
                 foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                 {
                     StorageRow storage_row = (StorageRow) row;
@@ -213,7 +214,6 @@ namespace Usage
 
                         if(dialog.run() == Gtk.ResponseType.OK)
                         {
-                            var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                             storage_analyzer.wipe_trash.begin(() => {
                                 ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                             });
@@ -239,7 +239,6 @@ namespace Usage
                         foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                         {
                             StorageRow storage_row = (StorageRow) row;
-                            var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                             storage_analyzer.wipe_folder.begin(storage_row.get_item_path(), () => {
                                 ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                             });
@@ -257,10 +256,10 @@ namespace Usage
         private void restore_clicked()
         {
             Timeout.add(0, () => {
+                var storage_analyzer = StorageAnalyzer.get_default();
                 foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                 {
                     StorageRow storage_row = (StorageRow) row;
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                     storage_analyzer.restore_trash_file.begin(storage_row.get_item_path(), () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
@@ -274,10 +273,10 @@ namespace Usage
         private void delete_from_trash_clicked()
         {
             Timeout.add(0, () => {
+                var storage_analyzer = StorageAnalyzer.get_default();
                 foreach (Gtk.ListBoxRow row in ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().get_selected_rows())
                 {
                     StorageRow storage_row = (StorageRow) row;
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
                     storage_analyzer.delete_trash_file.begin(storage_row.get_item_path(), () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
diff --git a/src/storage-analyzer.vala b/src/storage-analyzer.vala
index 7291fa3..3d741f0 100644
--- a/src/storage-analyzer.vala
+++ b/src/storage-analyzer.vala
@@ -36,6 +36,15 @@ namespace Usage
         private bool can_scan = true;
         private const string TRASH_PATH = "trash:///";
         private const string file_attributes = FileAttribute.STANDARD_SIZE + "," + 
FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE;
+        private static StorageAnalyzer storage_analyzer;
+
+        public static StorageAnalyzer get_default ()
+        {
+            if (storage_analyzer == null)
+                storage_analyzer = new StorageAnalyzer ();
+
+            return storage_analyzer;
+        }
 
         private enum Operation
         {
@@ -812,7 +821,7 @@ namespace Usage
             }
         }
 
-        public StorageAnalyzer()
+        private StorageAnalyzer()
         {
             cache = false;
             cancellable = new Cancellable();
diff --git a/src/storage-graph.vala b/src/storage-graph.vala
index 012b27e..08ad80e 100644
--- a/src/storage-graph.vala
+++ b/src/storage-graph.vala
@@ -208,7 +208,7 @@ namespace Usage
             var model = storage_list_box.get_model();
 
             var two_graphs = false;
-            if(storage_list_box.get_root() && (GLib.Application.get_default() as 
Application).get_storage_analyzer().get_separate_home())
+            if(storage_list_box.get_root() && StorageAnalyzer.get_default().get_separate_home())
                 two_graphs = true;
 
             double x = 0;
diff --git a/src/storage-list-box.vala b/src/storage-list-box.vala
index 7a2ced1..593ca50 100644
--- a/src/storage-list-box.vala
+++ b/src/storage-list-box.vala
@@ -54,7 +54,7 @@ namespace Usage
             actual_path = null;
             actual_name = null;
             actual_parent_type = null;
-            storage_analyzer = (GLib.Application.get_default() as Application).get_storage_analyzer();
+            storage_analyzer = StorageAnalyzer.get_default();
 
             get_style_context().add_class("folders");
             color = get_style_context().get_color(get_style_context().get_state());
diff --git a/src/storage-row.vala b/src/storage-row.vala
index 6c336b9..3aa4a89 100644
--- a/src/storage-row.vala
+++ b/src/storage-row.vala
@@ -295,7 +295,7 @@ namespace Usage
         private void action_trash_restore()
         {
             Timeout.add(0, () => {
-                var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                var storage_analyzer = StorageAnalyzer.get_default();
                 storage_analyzer.restore_trash_file.begin(item_path, () => {
                     ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                 });
@@ -307,7 +307,7 @@ namespace Usage
         private void action_trash_delete()
         {
             Timeout.add(0, () => {
-                var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                var storage_analyzer = StorageAnalyzer.get_default();
                 storage_analyzer.delete_trash_file.begin(item_path, () => {
                     ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                 });
@@ -325,7 +325,7 @@ namespace Usage
             if(dialog.run() == Gtk.ResponseType.OK)
             {
                Timeout.add(0, () => {
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     storage_analyzer.wipe_folder.begin(item_path, () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
@@ -345,7 +345,7 @@ namespace Usage
             if(dialog.run() == Gtk.ResponseType.OK)
             {
                Timeout.add(0, () => {
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     storage_analyzer.wipe_trash.begin(() => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
@@ -369,7 +369,7 @@ namespace Usage
             button.clicked.connect (() => {
                Timeout.add(0, () => {
                     string destination = Path.get_dirname(item_path) + "/" + entry.get_text();
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     storage_analyzer.move_file.begin(File.new_for_path(item_path), 
File.new_for_path(destination), () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
@@ -410,7 +410,7 @@ namespace Usage
             {
                Timeout.add(0, () => {
                    string destination = chooser.get_file().get_parse_name() + "/" + 
Path.get_basename(item_path);
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     storage_analyzer.move_file.begin(File.new_for_path(item_path), 
File.new_for_path(destination), () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
@@ -424,7 +424,7 @@ namespace Usage
         private void action_move_to_trash()
         {
             Timeout.add(0, () => {
-                var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                var storage_analyzer = StorageAnalyzer.get_default();
                 storage_analyzer.trash_file.begin(item_path, () => {
                     ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                 });
@@ -442,7 +442,7 @@ namespace Usage
             if(dialog.run() == Gtk.ResponseType.OK)
             {
                Timeout.add(0, () => {
-                    var storage_analyzer = (GLib.Application.get_default() as 
Application).get_storage_analyzer();
+                    var storage_analyzer = StorageAnalyzer.get_default();
                     storage_analyzer.delete_file.begin(item_path, () => {
                         ((StorageView) (GLib.Application.get_default() as 
Application).get_window().get_views()[2]).get_storage_list_box().refresh();
                     });
diff --git a/src/window.vala b/src/window.vala
index c7f460b..5e1304a 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -67,7 +67,7 @@ namespace Usage
                 else if(stack.visible_child_name == views[2].name)
                 {
                     header_bar.set_mode(HeaderBarMode.STORAGE);
-                    (GLib.Application.get_default() as 
Application).get_storage_analyzer().create_cache.begin();
+                    StorageAnalyzer.get_default().create_cache.begin();
                 }
                 else if(stack.visible_child_name == views[3].name)
                 {


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