[gnome-games/wip/exalm/desktop-in-flatpak: 59/64] tracker: Add TrackerMetadata



commit b463ba18e521d9f842c31297e1a9061d13869259
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Thu Aug 9 18:15:04 2018 +0500

    tracker: Add TrackerMetadata

 src/meson.build                   |  1 +
 src/tracker/tracker-metadata.vala | 69 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)
---
diff --git a/src/meson.build b/src/meson.build
index 6c556d47..73d83ab1 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -119,6 +119,7 @@ vala_sources = [
 
   'tracker/mime-type-tracker-uri-query.vala',
   'tracker/tracker-error.vala',
+  'tracker/tracker-metadata.vala',
   'tracker/tracker-uri-iterator.vala',
   'tracker/tracker-uri-query.vala',
   'tracker/tracker-uri-source.vala',
diff --git a/src/tracker/tracker-metadata.vala b/src/tracker/tracker-metadata.vala
new file mode 100644
index 00000000..f7b4ccae
--- /dev/null
+++ b/src/tracker/tracker-metadata.vala
@@ -0,0 +1,69 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private struct Games.TrackerMetadataPair {
+       public string predicate;
+       public string object;
+}
+
+public class Games.TrackerMetadata : Object {
+       private Tracker.Sparql.Connection connection;
+       private Uri uri;
+       private Tracker.Sparql.Cursor cursor;
+       private TrackerMetadataPair[] pairs;
+
+       public TrackerMetadata (Tracker.Sparql.Connection connection, Uri uri) {
+               this.connection = connection;
+               this.uri = uri;
+               cursor = null;
+       }
+
+       private void perform_query () throws Error {
+               var uri_string = uri.to_string ();
+               var query = @"SELECT ?predicate ?object WHERE { <$uri_string> ?predicate ?object }";
+
+               cursor = connection.query (query);
+
+               pairs = {};
+               while (cursor.next ()) {
+                       var predicate = cursor.get_string (0);
+                       var object = cursor.get_string (1);
+
+                       var pair = TrackerMetadataPair ();
+                       pair.predicate = predicate;
+                       pair.object = object;
+                       pairs += pair;
+               }
+       }
+
+       private void ensure_is_loaded () {
+               if (pairs == null) {
+                       try {
+                               perform_query ();
+                       }
+                       catch (Error e) {
+                               critical (e.message);
+                       }
+               }
+       }
+
+       public string get_object (string predicate) {
+               ensure_is_loaded ();
+
+               foreach (var pair in pairs)
+                       if (pair.predicate == predicate)
+                               return pair.object;
+
+               return "";
+       }
+
+       public string[] get_all_objects (string predicate) {
+               ensure_is_loaded ();
+
+               string[] result = {};
+               foreach (var pair in pairs)
+                       if (pair.predicate == predicate)
+                               result += pair.object;
+
+               return result;
+       }
+}


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