[gnome-games/wip/aplazas/781334-refactor-game-source: 3/37] tracker: Add TrackerUriSource



commit c22dd86b20dccc433d6d5bec1fb4d13da0362e3c
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed May 3 19:42:12 2017 +0200

    tracker: Add TrackerUriSource
    
    This will be used in some next commit to split the concern of
    discovering potential game resources and turning them into usable game
    objects.

 src/Makefile.am                              |    4 ++
 src/core/uri-iterator.vala                   |    4 +-
 src/core/uri-source.vala                     |    2 +-
 src/tracker/mime-type-tracker-uri-query.vala |   13 ++++++
 src/tracker/tracker-uri-iterator.vala        |   61 ++++++++++++++++++++++++++
 src/tracker/tracker-uri-query.vala           |    5 ++
 src/tracker/tracker-uri-source.vala          |   22 +++++++++
 7 files changed, 108 insertions(+), 3 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9805a0e..58cc040 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -102,8 +102,12 @@ gnome_games_SOURCES = \
        retro/retro-runner.vala \
        \
        tracker/mime-type-tracker-query.vala \
+       tracker/mime-type-tracker-uri-query.vala \
        tracker/tracker-game-source.vala \
        tracker/tracker-query.vala \
+       tracker/tracker-uri-iterator.vala \
+       tracker/tracker-uri-query.vala \
+       tracker/tracker-uri-source.vala \
        \
        ui/application.vala \
        ui/application-window.vala \
diff --git a/src/core/uri-iterator.vala b/src/core/uri-iterator.vala
index 613e1ed..964a22c 100644
--- a/src/core/uri-iterator.vala
+++ b/src/core/uri-iterator.vala
@@ -1,6 +1,6 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 public interface Games.UriIterator : Object {
-       public new string? get ();
-       public bool next ();
+       public abstract new string? get ();
+       public abstract bool next ();
 }
diff --git a/src/core/uri-source.vala b/src/core/uri-source.vala
index 69de450..20ba099 100644
--- a/src/core/uri-source.vala
+++ b/src/core/uri-source.vala
@@ -1,5 +1,5 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 public interface Games.UriSource : Object {
-       public UriIterator iterator ();
+       public abstract UriIterator iterator ();
 }
diff --git a/src/tracker/mime-type-tracker-uri-query.vala b/src/tracker/mime-type-tracker-uri-query.vala
new file mode 100644
index 0000000..d536899
--- /dev/null
+++ b/src/tracker/mime-type-tracker-uri-query.vala
@@ -0,0 +1,13 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public class Games.MimeTypeTrackerUriQuery : Object, TrackerUriQuery {
+       private string mime_type;
+
+       public MimeTypeTrackerUriQuery (string mime_type) {
+               this.mime_type = mime_type;
+       }
+
+       public string get_query () {
+               return @"SELECT DISTINCT nie:url(?urn) WHERE { ?urn nie:mimeType \"$mime_type\" . }";
+       }
+}
diff --git a/src/tracker/tracker-uri-iterator.vala b/src/tracker/tracker-uri-iterator.vala
new file mode 100644
index 0000000..db3381a
--- /dev/null
+++ b/src/tracker/tracker-uri-iterator.vala
@@ -0,0 +1,61 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.TrackerUriIterator : Object, UriIterator {
+       private Tracker.Sparql.Connection connection;
+       private TrackerUriQuery[] queries;
+       private int query_index;
+       private string? uri;
+       private Tracker.Sparql.Cursor cursor;
+
+       internal TrackerUriIterator (Tracker.Sparql.Connection connection, TrackerUriQuery[] queries) {
+               this.connection = connection;
+               this.queries = queries;
+               query_index = 0;
+               uri = null;
+               cursor = null;
+       }
+
+       public new string? get () {
+               return uri;
+       }
+
+       public bool next () {
+               while (query_index < queries.length) {
+                       if (try_next_for_query (queries[query_index]))
+                               return true;
+
+                       query_index++;
+               }
+
+               return false;
+       }
+
+       private bool try_next_for_query (TrackerUriQuery query) {
+               try {
+                       if (next_for_query (query))
+                               return true;
+               }
+               catch (Error e) {
+                       debug (e.message);
+               }
+
+               uri = null;
+               cursor = null;
+
+               return false;
+       }
+
+       private bool next_for_query (TrackerUriQuery query) throws Error {
+               if (cursor == null) {
+                       var sparql = query.get_query ();
+                       cursor = connection.query (sparql);
+               }
+
+               if (!cursor.next ())
+                       return false;
+
+               uri = cursor.get_string (0);
+
+               return true;
+       }
+}
diff --git a/src/tracker/tracker-uri-query.vala b/src/tracker/tracker-uri-query.vala
new file mode 100644
index 0000000..43b37ce
--- /dev/null
+++ b/src/tracker/tracker-uri-query.vala
@@ -0,0 +1,5 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public interface Games.TrackerUriQuery : Object {
+       public abstract string get_query ();
+}
diff --git a/src/tracker/tracker-uri-source.vala b/src/tracker/tracker-uri-source.vala
new file mode 100644
index 0000000..75bb677
--- /dev/null
+++ b/src/tracker/tracker-uri-source.vala
@@ -0,0 +1,22 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public class Games.TrackerUriSource : Object, UriSource {
+       private Tracker.Sparql.Connection connection { private set; get; }
+       private TrackerUriQuery[] queries;
+
+       public TrackerUriSource (Tracker.Sparql.Connection connection) {
+               this.connection = connection;
+       }
+
+       construct {
+               queries = {};
+       }
+
+       public void add_query (TrackerUriQuery query) {
+               queries += query;
+       }
+
+       public UriIterator iterator () {
+               return new TrackerUriIterator (connection, queries);
+       }
+}


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