[gnome-games/wip/aplazas/781334-refactor-game-source: 15/35] desktop: Provide MIME types, URI sources and URI game factories



commit f3045e1ac05fbce7dbcb62667ebcd64b3c8b7cf5
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed May 3 22:12:43 2017 +0200

    desktop: Provide MIME types, URI sources and URI game factories
    
    Also stop providing a GameSource.
    
    This helps splitting the conerns of looking for game resources and
    building games from them.

 plugins/desktop/src/Makefile.am                    |    2 +-
 plugins/desktop/src/desktop-plugin.vala            |  131 +++++++++++++++-
 plugins/desktop/src/desktop-tracker-query.vala     |  167 --------------------
 plugins/desktop/src/desktop-tracker-uri-query.vala |    7 +
 4 files changed, 135 insertions(+), 172 deletions(-)
---
diff --git a/plugins/desktop/src/Makefile.am b/plugins/desktop/src/Makefile.am
index 16203a8..2671f35 100644
--- a/plugins/desktop/src/Makefile.am
+++ b/plugins/desktop/src/Makefile.am
@@ -33,7 +33,7 @@ libgames_desktop_plugin_la_SOURCES = \
        desktop-icon.vala \
        desktop-plugin.vala \
        desktop-title.vala \
-       desktop-tracker-query.vala \
+       desktop-tracker-uri-query.vala \
        $(BUILT_SOURCES) \
        $(NULL)
 
diff --git a/plugins/desktop/src/desktop-plugin.vala b/plugins/desktop/src/desktop-plugin.vala
index d472415..f405dbd 100644
--- a/plugins/desktop/src/desktop-plugin.vala
+++ b/plugins/desktop/src/desktop-plugin.vala
@@ -1,12 +1,135 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private class Games.DesktopPlugin : Object, Plugin {
-       public GameSource? get_game_source () throws Error {
+       private const string MIME_TYPE = "application/x-desktop";
+
+       public string[] get_mime_types () {
+               return { MIME_TYPE };
+       }
+
+       public UriSource[] get_uri_sources () {
+               var query = new DesktopTrackerUriQuery ();
                var connection = Tracker.Sparql.Connection.@get ();
-               var source = new TrackerGameSource (connection);
-               source.add_query (new DesktopTrackerQuery ());
+               var uri_source = new TrackerUriSource (connection);
+               uri_source.add_query (query);
+
+               return { uri_source };
+       }
+
+       public UriGameFactory[] get_uri_game_factories () {
+               var game_uri_adapter = new GenericSyncGameUriAdapter (game_for_uri);
+               var factory = new GenericUriGameFactory (game_uri_adapter);
+               factory.add_mime_type (MIME_TYPE);
+
+               return { factory };
+       }
+
+       private static Game game_for_uri (string uri) throws Error {
+               check_uri (uri);
+
+               var file = File.new_for_uri (uri);
+               var path = file.get_path ();
+
+               var app_info = new DesktopAppInfo.from_filename (path);
+               var title = new DesktopTitle (app_info);
+               var icon = new DesktopIcon (app_info);
+               var cover = new DummyCover ();
+
+               string[] args;
+               var command = app_info.get_commandline ();
+               if (!Shell.parse_argv (command, out args))
+                       throw new CommandError.INVALID_COMMAND (_("Invalid command “%s”."), command);
+               var runner = new CommandRunner (args, true);
+
+               return new GenericGame (title, icon, cover, runner);
+       }
+
+       private static void check_uri (string uri) throws Error {
+               var file = File.new_for_uri (uri);
+
+               if (!file.query_exists ())
+                       throw new IOError.NOT_FOUND (_("Tracker listed file not found: “%s”."), uri);
+
+               var path = file.get_path ();
+               var app_info = new DesktopAppInfo.from_filename (path);
+
+               if (app_info == null)
+                       throw new DesktopError.INVALID_APPINFO (_("Couldn’t parse desktop entry “%s”."), 
path);
+
+               check_displayability (app_info);
+               check_categories (app_info);
+               check_executable (app_info);
+               check_base_name (file);
+       }
+
+       private static void check_displayability (DesktopAppInfo app_info) throws Error {
+               if (app_info.get_nodisplay ())
+                       throw new DesktopError.BLACKLISTED_GAME (_("“%s” shouldn’t be displayed."), 
app_info.filename);
+
+               if (app_info.get_is_hidden ())
+                       throw new DesktopError.BLACKLISTED_GAME (_("“%s” is hidden."), app_info.filename);
+       }
+
+       private static void check_categories (DesktopAppInfo app_info) throws Error {
+               var categories_string = app_info.get_categories ();
+               var categories = categories_string.split (";");
+
+               foreach (var category in get_categories_black_list ())
+                       if (category in categories)
+                               throw new DesktopError.BLACKLISTED_GAME (_("“%s” has blacklisted category 
“%s”."), app_info.filename, category);
+       }
+
+       private static void check_executable (DesktopAppInfo app_info) throws Error {
+               var app_executable = app_info.get_executable ();
+
+               foreach (var executable in get_executable_black_list ())
+                       if (app_executable == executable ||
+                           app_executable.has_suffix ("/" + executable))
+                               throw new DesktopError.BLACKLISTED_GAME (_("“%s” has blacklisted executable 
“%s”."), app_info.filename, executable);
+       }
+
+       private static void check_base_name (File file) throws Error {
+               var base_name = file.get_basename ();
+
+               if (base_name in get_base_name_black_list ())
+                       throw new DesktopError.BLACKLISTED_GAME (_("“%s” is blacklisted."), file.get_path ());
+       }
+
+       private static string[] categories_black_list;
+       private static string[] get_categories_black_list () throws Error {
+               if (categories_black_list == null)
+                       categories_black_list = get_lines_from_resource 
("plugins/desktop/blacklists/desktop-categories.blacklist");
+
+               return categories_black_list;
+       }
+
+       private static string[] executable_black_list;
+       private static string[] get_executable_black_list () throws Error {
+               if (executable_black_list == null)
+                       executable_black_list = get_lines_from_resource 
("plugins/desktop/blacklists/desktop-executable.blacklist");
+
+               return executable_black_list;
+       }
+
+       private static string[] base_name_black_list;
+       private static string[] get_base_name_black_list () throws Error {
+               if (base_name_black_list == null)
+                       base_name_black_list = get_lines_from_resource 
("plugins/desktop/blacklists/desktop-base-name.blacklist");
+
+               return base_name_black_list;
+       }
+
+       private static string[] get_lines_from_resource (string resource) throws Error {
+               var bytes = resources_lookup_data ("/org/gnome/Games/" + resource, ResourceLookupFlags.NONE);
+               var text = (string) bytes.get_data ();
+
+               string[] lines = {};
+
+               foreach (var line in text.split ("\n"))
+                       if (line != "")
+                               lines += line;
 
-               return source;
+               return lines;
        }
 }
 
diff --git a/plugins/desktop/src/desktop-tracker-uri-query.vala 
b/plugins/desktop/src/desktop-tracker-uri-query.vala
new file mode 100644
index 0000000..fee975b
--- /dev/null
+++ b/plugins/desktop/src/desktop-tracker-uri-query.vala
@@ -0,0 +1,7 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DesktopTrackerUriQuery : Object, TrackerUriQuery {
+       public string get_query () {
+               return "SELECT ?soft WHERE { ?soft nie:isLogicalPartOf 'urn:software-category:Game' . }";
+       }
+}


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