[gnome-games/wip/exalm/search-provider: 885/886] application: Add --uid command line option



commit 6c006bb7cd1c9b3ff80350131d45de312785aca2
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Feb 12 14:10:34 2020 +0500

    application: Add --uid command line option
    
    This will allow search provider to run games.

 src/core/game-collection.vala | 81 +++++++++++++++++++++++++++++++++++++++----
 src/ui/application.vala       | 23 +++++++++++-
 2 files changed, 96 insertions(+), 8 deletions(-)
---
diff --git a/src/core/game-collection.vala b/src/core/game-collection.vala
index 57ad191c..f3e63b6a 100644
--- a/src/core/game-collection.vala
+++ b/src/core/game-collection.vala
@@ -5,8 +5,10 @@ private class Games.GameCollection : Object {
        public signal void game_replaced (Game game, Game prev_game);
        public signal void game_removed (Game game);
 
+       private signal void loading_done ();
+
+       private HashTable<string, Game> games;
        private HashTable<string, Game> cached_games;
-       private GenericSet<Game> games;
        private UriSource[] sources;
        private UriGameFactory[] factories;
        private RunnerFactory[] runner_factories;
@@ -17,12 +19,13 @@ private class Games.GameCollection : Object {
        private HashTable<Platform, Array<RunnerFactory>> runner_factories_for_platforms;
 
        private SourceFunc search_games_cb;
+       private bool is_loading_done;
 
        public GameCollection (Database database) {
                this.database = database;
 
+               games = new HashTable<string, Game> (str_hash, str_equal);
                cached_games = new HashTable<string, Game> (str_hash, str_equal);
-               games = new GenericSet<Game> (Game.hash, Game.equal);
                factories_for_mime_type = new HashTable<string, Array<UriGameFactory>> (str_hash, str_equal);
                factories_for_scheme = new HashTable<string, Array<UriGameFactory>> (str_hash, str_equal);
                runner_factories_for_platforms = new HashTable<Platform, Array<RunnerFactory>> 
(Platform.hash, Platform.equal);
@@ -95,10 +98,18 @@ private class Games.GameCollection : Object {
                        try {
                                database.list_cached_games ((game) => {
                                        cached_games[game.get_uri ().to_string ()] = game;
-                                       if (games.contains (game))
+
+                                       string? uid = null;
+                                       try {
+                                               uid = game.get_uid ().get_uid ();
+                                       }
+                                       catch (Error e) {}
+
+                                       if (games.contains (uid))
                                                return;
 
-                                       games.add (game);
+                                       games[uid] = game;
+
                                        Idle.add (() => {
                                                game_added (game);
                                                return Source.REMOVE;
@@ -122,7 +133,13 @@ private class Games.GameCollection : Object {
                                        warning ("Couldn't remove game: %s", e.message);
                                }
 
-                               games.remove (game);
+                               string? uid = null;
+                               try {
+                                       uid = game.get_uid ().get_uid ();
+                               }
+                               catch (Error e) {}
+
+                               games.remove (uid);
                                if (removed)
                                        Idle.add (() => {
                                                game_removed (game);
@@ -140,6 +157,9 @@ private class Games.GameCollection : Object {
 
                yield;
 
+               is_loading_done = true;
+               loading_done ();
+
                search_games_cb = null;
        }
 
@@ -238,10 +258,17 @@ private class Games.GameCollection : Object {
                        warning ("Couldn't cache game: %s", e.message);
                }
 
-               if (games.contains (game) && prev_game == null)
+               string? uid = null;
+               try {
+                       uid = game.get_uid ().get_uid ();
+               }
+               catch (Error e) {}
+
+               if (games.contains (uid) && prev_game == null)
                        return;
 
-               games.add (game);
+               games[uid] = game;
+
                Idle.add (() => {
                        if (prev_game != null)
                                game_replaced (game, prev_game);
@@ -250,4 +277,44 @@ private class Games.GameCollection : Object {
                        return Source.REMOVE;
                });
        }
+
+       public async Game? query_game_for_uid (string uid) {
+               if (uid in games)
+                       return games[uid];
+
+               if (is_loading_done)
+                       return null;
+
+               Game? result = null;
+               ulong game_added_id = 0;
+               ulong loading_done_id = 0;
+
+               game_added_id = game_added.connect ((game) => {
+                       string? game_uid = null;
+                       try {
+                               game_uid = game.get_uid ().get_uid ();
+                       }
+                       catch (Error e) {
+                               return;
+                       }
+
+                       if (game_uid != uid)
+                               return;
+
+                       result = game;
+                       disconnect (game_added_id);
+                       disconnect (loading_done_id);
+                       query_game_for_uid.callback ();
+               });
+
+               loading_done_id = loading_done.connect (() => {
+                       disconnect (game_added_id);
+                       disconnect (loading_done_id);
+                       query_game_for_uid.callback ();
+               });
+
+               yield;
+
+               return result;
+       }
 }
diff --git a/src/ui/application.vala b/src/ui/application.vala
index 56555f1c..26e42703 100644
--- a/src/ui/application.vala
+++ b/src/ui/application.vala
@@ -26,7 +26,8 @@ public class Games.Application : Gtk.Application {
        };
 
        private const OptionEntry[] option_entries = {
-               { "search", 0, 0, OptionArg.STRING_ARRAY,   null, N_("Search term") },
+               { "search", 0, 0, OptionArg.STRING_ARRAY,   null, N_("Search term")       },
+               { "uid",    0, 0, OptionArg.STRING,         null, N_("Run a game by uid") },
                { "",       0, 0, OptionArg.FILENAME_ARRAY },
                { null },
        };
@@ -219,11 +220,31 @@ public class Games.Application : Gtk.Application {
                cover_loader = new CoverLoader ();
        }
 
+       private async void run_by_uid (string uid) {
+               var game = yield game_collection.query_game_for_uid (uid);
+
+               if (game == null) {
+                       window.show_error (_("Cannot find game with UID ā€œ%sā€.").printf (uid));
+
+                       return;
+               }
+
+               window.run_game (game);
+       }
+
        protected override int command_line (ApplicationCommandLine command_line) {
                var options = command_line.get_options_dict ();
 
                activate ();
 
+               if ("uid" in options) {
+                       var uid = options.lookup_value ("uid", VariantType.STRING);
+                       if (uid != null)
+                               run_by_uid.begin (uid.get_string ());
+
+                       return 0;
+               }
+
                if ("search" in options) {
                        var terms_variant = options.lookup_value ("search", VariantType.STRING_ARRAY);
                        if (terms_variant != null) {


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