[libgnome-games-support] context: Make usable when created via g_object_new()



commit bef0bb5380ea265eda3c9658f50bd485cd9898a4
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Sun Feb 4 22:14:36 2018 -0600

    context: Make usable when created via g_object_new()
    
    This is tricky. First, we need to add properties for all the constructor
    parameters. But that doesn't work for the CategoryRequestFunc: the best
    we can do there would be a void* property, which would be
    non-introspectable, defeating the point of the whole exercise. Instead,
    we can add a bool object to disable loading scores when the object is
    created. Then a category request function can be set separately when
    loading scores manually.
    
    I'm not actually sure if that's introspectable either, TBH. Maybe it
    doesn't matter, because we don't currently support introspection. But
    hey, it's a step in the right direction.
    
    Nobody will ever figure out how to do use this, because there's no
    documentation. Would be good to add some gtkdoc....

 games/scores/context.vala |   63 ++++++++++++++++++++++++++++----------------
 1 files changed, 40 insertions(+), 23 deletions(-)
---
diff --git a/games/scores/context.vala b/games/scores/context.vala
index 64c3e98..37f2bf8 100644
--- a/games/scores/context.vala
+++ b/games/scores/context.vala
@@ -32,17 +32,20 @@ public enum Style
 
 public class Context : Object
 {
-    /* All these variables are needed by dialog and as parameters to Dialog constructor. */
+    public string app_name { get; construct; }
+    public string category_type { get; construct; }
+    public Gtk.Window? game_window { get; construct; }
+    public Style style { get; construct; }
+    public Importer? importer { get; construct; }
+    public bool load_on_creation { get; construct; default = true; }
+
     private Category? current_category = null;
-    private Style style;
-    private string category_type;
-    private Gtk.Window? game_window;
-    private string app_name;
 
     /* A priority queue enables us to easily fetch the top 10 scores */
     private Gee.HashMap<Category?, Gee.PriorityQueue<Score> > scores_per_category = new 
Gee.HashMap<Category?, Gee.PriorityQueue<Score> > ((owned) category_hash, (owned) category_equal);
 
     private string user_score_dir;
+    private bool scores_loaded = false;
 
     /* Comparison and hash functions for Map and Priority Queue.*/
     private CompareDataFunc<Score?> scorecmp;
@@ -61,9 +64,7 @@ public class Context : Object
      * know in advance which categories may be in use.
      */
     public delegate Category? CategoryRequestFunc (string category_key);
-    private CategoryRequestFunc category_request;
-
-    private Importer? importer;
+    private CategoryRequestFunc? category_request = null;
 
     class construct
     {
@@ -87,13 +88,17 @@ public class Context : Object
                                   Style style,
                                   Importer? importer)
     {
-        this.app_name = app_name;
-        this.category_type = category_type;
-        this.game_window = game_window;
         this.category_request = (key) => { return category_request (key); };
-        this.style = style;
-        this.importer = importer;
 
+        Object (app_name: app_name,
+                category_type: category_type,
+                game_window: game_window,
+                style: style,
+                importer: importer);
+    }
+
+    public override void constructed ()
+    {
         if (style == Style.POINTS_GREATER_IS_BETTER || style == Style.TIME_GREATER_IS_BETTER)
         {
             scorecmp = (a,b) => {
@@ -112,13 +117,16 @@ public class Context : Object
         if (importer != null)
             importer.run (this, user_score_dir);
 
-        try
+        if (load_on_creation)
         {
-            load_scores_from_files ();
-        }
-        catch (Error e)
-        {
-            warning ("Failed to load scores: %s", e.message);
+            try
+            {
+                load_scores_from_files ();
+            }
+            catch (Error e)
+            {
+                warning ("Failed to load scores: %s", e.message);
+            }
         }
     }
 
@@ -311,14 +319,16 @@ public class Context : Object
     }
 
     private void load_scores_from_files () throws Error
+        requires (!scores_loaded)
     {
+        scores_loaded = true;
+
         if (game_window != null && game_window.visible)
         {
             error ("The application window associated with the GamesScoresContext " +
-                   "was set visible before the Context was constructed. The Context " +
-                   "performs synchronous I/O in the default main context to load " +
-                   "scores when it is constructed, so you should create the Context " +
-                   "before showing your main window.");
+                   "was set visible before loading scores. The Context performs " +
+                   "synchronous I/O in the default main context to load scores, so " +
+                   "so you should do this before showing your main window.");
         }
 
         var directory = File.new_for_path (user_score_dir);
@@ -333,6 +343,13 @@ public class Context : Object
         }
     }
 
+    public void load_scores (CategoryRequestFunc category_request) throws Error
+        requires (!load_on_creation && this.category_request == null)
+    {
+        this.category_request = (key) => { return category_request (key); };
+        load_scores_from_files ();
+    }
+
     internal void run_dialog_internal (Score? new_high_score)
         requires (game_window != null)
     {


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