[grilo-plugins/wip/carlosg/tracker3: 21/30] tracker3: Simplify rdf:type to GrlTypeFilter conversion




commit d5f74034bd7c09aca6547cde37b2a64f0e71b87c
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon May 25 12:44:30 2020 +0200

    tracker3: Simplify rdf:type to GrlTypeFilter conversion
    
    Do things straight from the query, so we get a proper media type
    for the given row.

 src/tracker3/grl-tracker-source-api.c        | 20 ++++-----
 src/tracker3/grl-tracker-source-notif.c      |  3 +-
 src/tracker3/grl-tracker-source-statements.c | 11 +++--
 src/tracker3/grl-tracker-utils.c             | 63 +++-------------------------
 src/tracker3/grl-tracker-utils.h             | 19 +--------
 5 files changed, 22 insertions(+), 94 deletions(-)
---
diff --git a/src/tracker3/grl-tracker-source-api.c b/src/tracker3/grl-tracker-source-api.c
index ad1e9642..f24ec293 100644
--- a/src/tracker3/grl-tracker-source-api.c
+++ b/src/tracker3/grl-tracker-source-api.c
@@ -220,8 +220,7 @@ grl_tracker_op_free (GrlTrackerOp *os)
                               GrlTrackerOp *os)                         \
   {                                                                     \
     TrackerSparqlCursor *cursor = TRACKER_SPARQL_CURSOR (source_object);\
-    gint         col;                                                   \
-    const gchar *sparql_type;                                           \
+    gint         col, type;                                             \
     GError      *tracker_error = NULL, *error = NULL;                   \
     GrlMedia    *media;                                                 \
     spec_type   *spec =                                                 \
@@ -263,14 +262,12 @@ grl_tracker_op_free (GrlTrackerOp *os)
       return;                                                           \
     }                                                                   \
                                                                         \
-    sparql_type = tracker_sparql_cursor_get_string (cursor,             \
-                                                    0,                  \
-                                                    NULL);              \
+    type = tracker_sparql_cursor_get_integer (cursor, 0);               \
                                                                         \
-    GRL_ODEBUG ("\tParsing line of type %s",                            \
-                sparql_type);                                           \
+    GRL_ODEBUG ("\tParsing line of type %x",                            \
+                type);                                                  \
                                                                         \
-    media = grl_tracker_build_grilo_media (sparql_type, os->type_filter);\
+    media = grl_tracker_build_grilo_media ((GrlMediaType ) type);       \
                                                                         \
     if (media != NULL) {                                                \
       for (col = 1 ;                                                    \
@@ -403,8 +400,7 @@ tracker_media_from_uri_cb (GObject      *source_object,
   GError                    *tracker_error = NULL, *error = NULL;
   GrlMedia                  *media;
   TrackerSparqlCursor       *cursor;
-  const gchar               *sparql_type;
-  gint                       col;
+  gint                       col, type;
 
   GRL_ODEBUG ("%s", __FUNCTION__);
 
@@ -431,8 +427,8 @@ tracker_media_from_uri_cb (GObject      *source_object,
 
   if (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
     /* Build grilo media */
-    sparql_type = tracker_sparql_cursor_get_string (cursor, 0, NULL);
-    media = grl_tracker_build_grilo_media (sparql_type, GRL_TYPE_FILTER_NONE);
+    type = tracker_sparql_cursor_get_integer (cursor, 0);
+    media = grl_tracker_build_grilo_media ((GrlMediaType) type);
 
     /* Translate Sparql result into Grilo result */
     for (col = 0 ; col < tracker_sparql_cursor_get_n_columns (cursor) ; col++) {
diff --git a/src/tracker3/grl-tracker-source-notif.c b/src/tracker3/grl-tracker-source-notif.c
index c3d7e96e..d77fb41b 100644
--- a/src/tracker3/grl-tracker-source-notif.c
+++ b/src/tracker3/grl-tracker-source-notif.c
@@ -64,8 +64,7 @@ media_for_event (GrlTrackerSourceNotify *self,
 
   id_str = g_strdup_printf ("%" G_GINT64_FORMAT, tracker_notifier_event_get_id (event));
   // FIXME
-  media = grl_tracker_build_grilo_media (NULL,//tracker_notifier_event_get_type (event),
-                                         GRL_TYPE_FILTER_NONE);
+  media = grl_tracker_build_grilo_media (GRL_MEDIA_TYPE_UNKNOWN);
   grl_media_set_id (media, id_str);
   grl_media_set_url (media, tracker_notifier_event_get_urn (event));
 
diff --git a/src/tracker3/grl-tracker-source-statements.c b/src/tracker3/grl-tracker-source-statements.c
index 83cacb21..d85d71b4 100644
--- a/src/tracker3/grl-tracker-source-statements.c
+++ b/src/tracker3/grl-tracker-source-statements.c
@@ -216,7 +216,7 @@ create_query_string (GrlTrackerQueryType  type,
   else
     filter = GRL_TYPE_FILTER_ALL;
 
-  str = g_string_new ("SELECT ?resourceType ?urn ");
+  str = g_string_new ("SELECT ?mediaType ?urn ");
   append_query_variables (str, keys, filter);
   g_string_append (str, "WHERE { ");
 
@@ -231,7 +231,8 @@ create_query_string (GrlTrackerQueryType  type,
   g_string_append (str, "{ ");
 
   if (filter & GRL_TYPE_FILTER_AUDIO) {
-    g_string_append (str, "{ GRAPH tracker:Audio { SELECT (rdf:type(?urn) AS ?resourceType) ?urn ");
+    g_string_append_printf (str, "{ GRAPH tracker:Audio { SELECT (%d AS ?mediaType) ?urn ",
+                            GRL_MEDIA_TYPE_AUDIO);
     append_subselect_bindings (str, merged_list, GRL_TYPE_FILTER_AUDIO);
     g_string_append_printf (str, "{ ?urn a nfo:Audio . %s } } } ",
                             query_bases[type]);
@@ -241,7 +242,8 @@ create_query_string (GrlTrackerQueryType  type,
   }
 
   if (filter & GRL_TYPE_FILTER_VIDEO) {
-    g_string_append (str, "{ GRAPH tracker:Video { SELECT (rdf:type(?urn) AS ?resourceType) ?urn ");
+    g_string_append_printf (str, "{ GRAPH tracker:Video { SELECT (%d AS ?mediaType) ?urn ",
+                            GRL_MEDIA_TYPE_VIDEO);
     append_subselect_bindings (str, merged_list, GRL_TYPE_FILTER_VIDEO);
     g_string_append_printf (str, "{ ?urn a nfo:Video . %s } } } ",
                             query_bases[type]);
@@ -251,7 +253,8 @@ create_query_string (GrlTrackerQueryType  type,
   }
 
   if (filter & GRL_TYPE_FILTER_IMAGE) {
-    g_string_append (str, "{ GRAPH tracker:Pictures { SELECT (rdf:type(?urn) AS ?resourceType) ?urn ");
+    g_string_append_printf (str, "{ GRAPH tracker:Pictures { SELECT (%d AS ?mediaType) ?urn ",
+                            GRL_MEDIA_TYPE_IMAGE);
     append_subselect_bindings (str, merged_list, GRL_TYPE_FILTER_IMAGE);
     g_string_append_printf (str, "{ ?urn a nfo:Image . %s } } } ",
                             query_bases[type]);
diff --git a/src/tracker3/grl-tracker-utils.c b/src/tracker3/grl-tracker-utils.c
index b12baab9..83c04d5f 100644
--- a/src/tracker3/grl-tracker-utils.c
+++ b/src/tracker3/grl-tracker-utils.c
@@ -428,73 +428,20 @@ grl_tracker_key_is_supported (const GrlKeyID key)
 
 /**/
 
-static GrlMedia *
-grl_tracker_build_grilo_media_default (GHashTable *ht)
-{
-  if (g_hash_table_lookup (ht, RDF_TYPE_MUSIC)) {
-    return grl_media_audio_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_VIDEO)) {
-    return grl_media_video_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_IMAGE)) {
-    return grl_media_image_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_ARTIST)) {
-    return grl_media_container_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_ALBUM)) {
-    return grl_media_container_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_CONTAINER)) {
-    return grl_media_container_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_FOLDER)) {
-    return grl_media_container_new ();
-  } else if (g_hash_table_lookup (ht, RDF_TYPE_PLAYLIST)) {
-    return grl_media_container_new ();
-  }
-
-  return NULL;
-}
-
-/**/
-
-/* Builds an appropriate GrlMedia based on ontology type returned by
-   tracker, or NULL if unknown */
+/* Builds an appropriate GrlMedia based on tracker query results */
 GrlMedia *
-grl_tracker_build_grilo_media (const gchar   *rdf_type,
-                               GrlTypeFilter  type_filter)
+grl_tracker_build_grilo_media (GrlMediaType type)
 {
   GrlMedia *media = NULL;
-  gchar **rdf_single_type;
-  int i;
-  GHashTable *ht;
 
-  if (!rdf_type) {
-    return NULL;
-  }
-
-  /* As rdf_type can be formed by several types, split them */
-  rdf_single_type = g_strsplit (rdf_type, ",", -1);
-  i = g_strv_length (rdf_single_type) - 1;
-  ht = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-  for (; i>= 0; i--)
-    g_hash_table_insert (ht, g_path_get_basename (rdf_single_type[i]), GINT_TO_POINTER(TRUE));
-
-  if (type_filter == GRL_TYPE_FILTER_NONE ||
-      type_filter == GRL_TYPE_FILTER_ALL) {
-    media = grl_tracker_build_grilo_media_default (ht);
-  } else if ((type_filter & GRL_TYPE_FILTER_AUDIO) &&
-             g_hash_table_lookup (ht, RDF_TYPE_MUSIC)) {
+  if (type == GRL_MEDIA_TYPE_AUDIO) {
     media = grl_media_audio_new ();
-  } else if ((type_filter & GRL_TYPE_FILTER_VIDEO) &&
-             g_hash_table_lookup (ht, RDF_TYPE_VIDEO)) {
+  } else if (type == GRL_MEDIA_TYPE_VIDEO) {
     media = grl_media_video_new ();
-  } else if ((type_filter & GRL_TYPE_FILTER_IMAGE) &&
-             g_hash_table_lookup (ht, RDF_TYPE_IMAGE)) {
+  } else if (type == GRL_MEDIA_TYPE_IMAGE) {
     media = grl_media_image_new ();
-  } else {
-    media = grl_tracker_build_grilo_media_default (ht);
   }
 
-  g_hash_table_destroy (ht);
-  g_strfreev (rdf_single_type);
-
   if (!media)
     media = grl_media_new ();
 
diff --git a/src/tracker3/grl-tracker-utils.h b/src/tracker3/grl-tracker-utils.h
index 9371832c..a756eb8d 100644
--- a/src/tracker3/grl-tracker-utils.h
+++ b/src/tracker3/grl-tracker-utils.h
@@ -28,22 +28,6 @@
 
 #include "grl-tracker-source-priv.h"
 
-/* ------- Definitions ------- */
-
-#define RDF_TYPE_ALBUM     "nmm#MusicAlbum"
-#define RDF_TYPE_ARTIST    "nmm#Artist"
-#define RDF_TYPE_AUDIO     "nfo#Audio"
-#define RDF_TYPE_MUSIC     "nmm#MusicPiece"
-#define RDF_TYPE_IMAGE     "nmm#Photo"
-#define RDF_TYPE_VIDEO     "nmm#Video"
-#define RDF_TYPE_FOLDER    "nfo#Folder"
-#define RDF_TYPE_DOCUMENT  "nfo#Document"
-#define RDF_TYPE_CONTAINER "grilo#Container"
-#define RDF_TYPE_PLAYLIST  "nmm#Playlist"
-
-#define RDF_TYPE_VOLUME "tracker#Volume"
-#define RDF_TYPE_UPNP   "upnp#ContentDirectory"
-
 /**/
 
 typedef void (*tracker_grl_sparql_setter_cb_t) (TrackerSparqlCursor *cursor,
@@ -76,7 +60,6 @@ TrackerResource * grl_tracker_build_resource_from_media (GrlMedia *media, GList
 
 tracker_grl_sparql_t *grl_tracker_get_mapping_from_sparql (const gchar *key);
 
-GrlMedia *grl_tracker_build_grilo_media (const gchar   *rdf_type,
-                                         GrlTypeFilter  type_filter);
+GrlMedia *grl_tracker_build_grilo_media (GrlMediaType type);
 
 #endif /* _GRL_TRACKER_UTILS_H_ */


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