[gnome-games] generic: Add GenericUriGameFactory
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] generic: Add GenericUriGameFactory
- Date: Fri, 19 Aug 2016 07:35:18 +0000 (UTC)
commit 719dceb4ad8a89aee3efbfb04b609d26936dccdb
Author: Ricard Gascons <gascons1995 gmail com>
Date: Thu Aug 18 11:45:09 2016 +0200
generic: Add GenericUriGameFactory
This class abstracts the process of creating a game from a uri, so
MimeTypeTrackerQuery does not have to deal with it.
This is part of a series of commits to add multi-source support.
src/Makefile.am | 1 +
src/generic/generic-uri-game-factory.vala | 55 +++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index b3428c1..a501927 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -73,6 +73,7 @@ gnome_games_SOURCES = \
generic/generic-sync-game-uri-adapter.vala \
generic/generic-title.vala \
generic/generic-uid.vala \
+ generic/generic-uri-game-factory.vala \
\
grilo/grilo-cover.vala \
grilo/grilo-media.vala \
diff --git a/src/generic/generic-uri-game-factory.vala b/src/generic/generic-uri-game-factory.vala
new file mode 100644
index 0000000..577f805
--- /dev/null
+++ b/src/generic/generic-uri-game-factory.vala
@@ -0,0 +1,55 @@
+// This file is part of GNOME Games. License: GPLv3
+
+public class Games.GenericUriGameFactory : Object, UriGameFactory {
+ public delegate bool UriTest (string uri);
+ private const uint HANDLED_URIS_PER_CYCLE = 5;
+
+ private GameUriAdapter game_uri_adapter;
+ private UriTest? uri_validity_test;
+ private string[] uris;
+
+ public GenericUriGameFactory (GameUriAdapter game_uri_adapter, owned UriTest? uri_validity_test =
null) {
+ this.game_uri_adapter = game_uri_adapter;
+ this.uri_validity_test = (owned) uri_validity_test;
+ this.uris = {};
+ }
+
+ public bool is_uri_valid (string uri) {
+ return uri_validity_test == null ? true : uri_validity_test (uri);
+ }
+
+ public void add_uri (string uri) {
+ uris += uri;
+ }
+
+ public async void foreach_game (GameCallback game_callback) {
+ uint handled_uris = 0;
+ foreach (var uri in uris) {
+ var file = File.new_for_uri (uri);
+ if (!file.query_exists ())
+ continue;
+
+ try {
+ Game game = yield game_uri_adapter.game_for_uri (uri);
+ game_callback (game);
+ }
+ catch (Error e) {
+ debug (e.message);
+
+ continue;
+ }
+
+ handled_uris++;
+
+ // Free the execution only once every HANDLED_URIS_PER_CYCLE
+ // games to speed up the execution by avoiding too many context
+ // switching.
+ if (handled_uris >= HANDLED_URIS_PER_CYCLE) {
+ handled_uris = 0;
+
+ Idle.add (this.foreach_game.callback);
+ yield;
+ }
+ }
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]