[gnome-games] generic: Add GenericUriGameSource
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] generic: Add GenericUriGameSource
- Date: Thu, 4 May 2017 19:19:39 +0000 (UTC)
commit 4efeb39d2e62e5c259645d4d02eea2c9294dbbaf
Author: Adrien Plazas <kekun plazas laposte net>
Date: Wed May 3 20:09:20 2017 +0200
generic: Add GenericUriGameSource
This will be used in the next commit as a temporary transition helper
between the game source model and the uri source and game factory model.
https://bugzilla.gnome.org/show_bug.cgi?id=781334
src/Makefile.am | 1 +
src/generic/generic-uri-game-source.vala | 74 ++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 58cc040..dbb0477 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -90,6 +90,7 @@ gnome_games_SOURCES = \
generic/generic-title.vala \
generic/generic-uid.vala \
generic/generic-uri-game-factory.vala \
+ generic/generic-uri-game-source.vala \
\
grilo/grilo-cover.vala \
grilo/grilo-media.vala \
diff --git a/src/generic/generic-uri-game-source.vala b/src/generic/generic-uri-game-source.vala
new file mode 100644
index 0000000..03306c2
--- /dev/null
+++ b/src/generic/generic-uri-game-source.vala
@@ -0,0 +1,74 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public class Games.GenericUriGameSource : Object, GameSource {
+ private UriSource[] sources;
+ private UriGameFactory[] factories;
+
+ private HashTable<string, Array<UriGameFactory>> factories_for_mime_type;
+
+ construct {
+ factories_for_mime_type = new HashTable<string, Array<UriGameFactory>> (str_hash, str_equal);
+ }
+
+ public void add_source (UriSource source) {
+ sources += source;
+ }
+
+ public void add_factory (UriGameFactory factory) {
+ factories += factory;
+
+ foreach (var mime_type in factory.get_mime_types ()) {
+ if (!factories_for_mime_type.contains (mime_type))
+ factories_for_mime_type[mime_type] = new Array<UriGameFactory> ();
+ factories_for_mime_type[mime_type].append_val (factory);
+ }
+ }
+
+ public async void each_game (GameCallback callback) {
+ foreach (var source in sources)
+ foreach (var uri in source)
+ yield add_uri (uri);
+
+ foreach (var factory in factories)
+ yield factory.foreach_game (callback);
+ }
+
+ private async void add_uri (string uri) {
+ Idle.add (add_uri.callback);
+ yield;
+
+ if (uri.has_prefix ("file:")) {
+ try {
+ yield add_file (uri);
+ }
+ catch (Error e) {
+ debug (e.message);
+ }
+
+ return;
+ }
+ // TODO Add support for URN and other schemes.
+ }
+
+ private async void add_file (string uri) throws Error {
+ var file = File.new_for_uri (uri);
+ if (!file.query_exists ())
+ return;
+
+ var file_info = file.query_info (FileAttribute.STANDARD_CONTENT_TYPE,
FileQueryInfoFlags.NONE);
+ var mime_type = file_info.get_content_type ();
+ if (!factories_for_mime_type.contains (mime_type))
+ return;
+
+ foreach (var factory in factories_for_mime_type[mime_type].data)
+ yield add_uri_to_factory (factory, uri);
+ }
+
+ private async void add_uri_to_factory (UriGameFactory factory, string uri) {
+ if (factory.is_uri_valid (uri))
+ factory.add_uri (uri);
+
+ Idle.add (add_uri_to_factory.callback);
+ yield;
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]