[PATCH 4/4] tracker: add metadata source support



From: Lionel Landwerlin <lionel g landwerlin linux intel com>

Signed-off-by: Lionel Landwerlin <lionel g landwerlin linux intel com>
---
 src/media/tracker/Makefile.am              |    6 +
 src/media/tracker/grl-tracker-media-api.c  |   19 +-
 src/media/tracker/grl-tracker-media-api.h  |    2 -
 src/media/tracker/grl-tracker-media-priv.h |   10 +-
 src/media/tracker/grl-tracker-media.c      |  282 +++++++++-------------
 src/media/tracker/grl-tracker-media.h      |    4 +-
 src/media/tracker/grl-tracker-metadata.c   |  360 ++++++++++++++++++++++++++++
 src/media/tracker/grl-tracker-metadata.h   |   98 ++++++++
 src/media/tracker/grl-tracker-utils.c      |   16 +-
 src/media/tracker/grl-tracker-utils.h      |    7 +-
 src/media/tracker/grl-tracker.c            |  124 ++++++++++
 src/media/tracker/grl-tracker.h            |   32 +++
 12 files changed, 763 insertions(+), 197 deletions(-)
 create mode 100644 src/media/tracker/grl-tracker-metadata.c
 create mode 100644 src/media/tracker/grl-tracker-metadata.h
 create mode 100644 src/media/tracker/grl-tracker.c
 create mode 100644 src/media/tracker/grl-tracker.h

diff --git a/src/media/tracker/Makefile.am b/src/media/tracker/Makefile.am
index accacfe..f944fed 100644
--- a/src/media/tracker/Makefile.am
+++ b/src/media/tracker/Makefile.am
@@ -20,6 +20,9 @@ libgrltracker_la_LDFLAGS = \
 	-avoid-version
 
 libgrltracker_la_SOURCES = 		\
+	grl-tracker.c			\
+	grl-tracker.h			\
+	\
 	grl-tracker-media.c		\
 	grl-tracker-media.h		\
 	grl-tracker-media-api.c		\
@@ -29,6 +32,9 @@ libgrltracker_la_SOURCES = 		\
 	grl-tracker-media-notif.c	\
 	grl-tracker-media-notif.h	\
 	\
+	grl-tracker-metadata.c		\
+	grl-tracker-metadata.h		\
+	\
 	grl-tracker-utils.c		\
 	grl-tracker-utils.h
 
diff --git a/src/media/tracker/grl-tracker-media-api.c b/src/media/tracker/grl-tracker-media-api.c
index 16898d6..17060c7 100644
--- a/src/media/tracker/grl-tracker-media-api.c
+++ b/src/media/tracker/grl-tracker-media-api.c
@@ -429,13 +429,6 @@ tracker_metadata_cb (GObject                    *source_object,
 
 /**/
 
-const GList *
-grl_tracker_media_supported_keys (GrlMetadataSource *source)
-{
-  return
-    grl_plugin_registry_get_metadata_keys (grl_plugin_registry_get_default ());
-}
-
 /**
  * Query is a SPARQL query.
  *
@@ -529,7 +522,7 @@ grl_tracker_media_query (GrlMediaSource *source,
   /* Check if it is a full sparql query */
   if (g_ascii_strncasecmp (qs->query, "select ", 7) != 0) {
     constraint = grl_tracker_media_get_device_constraint (priv);
-    sparql_select = grl_tracker_media_get_select_string (source, qs->keys);
+    sparql_select = grl_tracker_media_get_select_string (qs->keys);
     sparql_final = g_strdup_printf (TRACKER_QUERY_REQUEST,
                                     sparql_select,
                                     qs->query,
@@ -578,7 +571,7 @@ grl_tracker_media_metadata (GrlMediaSource *source,
   if (grl_media_get_id (ms->media) == NULL) {
     if (grl_tracker_per_device_source) {
       constraint = grl_tracker_media_get_device_constraint (priv);
-      sparql_select = grl_tracker_media_get_select_string (source, ms->keys);
+      sparql_select = grl_tracker_media_get_select_string (ms->keys);
       sparql_final = g_strdup_printf (TRACKER_BROWSE_FILESYSTEM_ROOT_REQUEST,
                                       sparql_select, constraint, 0, 1);
     } else {
@@ -586,7 +579,7 @@ grl_tracker_media_metadata (GrlMediaSource *source,
       return;
     }
   } else {
-    sparql_select = grl_tracker_media_get_select_string (source, ms->keys);
+    sparql_select = grl_tracker_media_get_select_string (ms->keys);
     sparql_final = g_strdup_printf (TRACKER_METADATA_REQUEST, sparql_select,
                                     grl_media_get_id (ms->media));
   }
@@ -619,7 +612,7 @@ grl_tracker_media_search (GrlMediaSource *source, GrlMediaSourceSearchSpec *ss)
   GRL_IDEBUG ("%s: id=%u", __FUNCTION__, ss->search_id);
 
   constraint = grl_tracker_media_get_device_constraint (priv);
-  sparql_select = grl_tracker_media_get_select_string (source, ss->keys);
+  sparql_select = grl_tracker_media_get_select_string (ss->keys);
   if (!ss->text || ss->text[0] == '\0') {
     /* Search all */
     sparql_final = g_strdup_printf (TRACKER_SEARCH_ALL_REQUEST, sparql_select,
@@ -701,7 +694,7 @@ grl_tracker_media_browse_category (GrlMediaSource *source,
                                   grl_metadata_key_tracker_category);
 
   constraint = grl_tracker_media_get_device_constraint (priv);
-  sparql_select = grl_tracker_media_get_select_string (bs->source, bs->keys);
+  sparql_select = grl_tracker_media_get_select_string (bs->keys);
   sparql_final = g_strdup_printf (TRACKER_BROWSE_CATEGORY_REQUEST,
                                   sparql_select,
                                   category,
@@ -740,7 +733,7 @@ grl_tracker_media_browse_filesystem (GrlMediaSource *source,
 
   GRL_IDEBUG ("%s: id=%u", __FUNCTION__, bs->browse_id);
 
-  sparql_select = grl_tracker_media_get_select_string (bs->source, bs->keys);
+  sparql_select = grl_tracker_media_get_select_string (bs->keys);
   constraint = grl_tracker_media_get_device_constraint (priv);
 
   if (bs->container == NULL ||
diff --git a/src/media/tracker/grl-tracker-media-api.h b/src/media/tracker/grl-tracker-media-api.h
index 30589e0..5beccf3 100644
--- a/src/media/tracker/grl-tracker-media-api.h
+++ b/src/media/tracker/grl-tracker-media-api.h
@@ -31,8 +31,6 @@
 
 void grl_tracker_media_init_requests (void);
 
-const GList *grl_tracker_media_supported_keys (GrlMetadataSource *source);
-
 void grl_tracker_media_query (GrlMediaSource *source,
                               GrlMediaSourceQuerySpec *qs);
 
diff --git a/src/media/tracker/grl-tracker-media-priv.h b/src/media/tracker/grl-tracker-media-priv.h
index 0446de2..1093ff3 100644
--- a/src/media/tracker/grl-tracker-media-priv.h
+++ b/src/media/tracker/grl-tracker-media-priv.h
@@ -30,13 +30,11 @@
 
 #include <tracker-sparql.h>
 
-/* ---- Plugin information --- */
+/* ---- MediaSource information ---- */
 
-#define GRL_TRACKER_PLUGIN_ID   TRACKER_PLUGIN_ID
-
-#define GRL_TRACKER_MEDIA_ID   "grl-tracker"
-#define GRL_TRACKER_MEDIA_NAME "Tracker"
-#define GRL_TRACKER_MEDIA_DESC                 \
+#define GRL_TRACKER_MEDIA_ID   "grl-tracker-media"
+#define GRL_TRACKER_MEDIA_NAME "TrackerMedia"
+#define GRL_TRACKER_MEDIA_DESC                  \
   "A plugin for searching multimedia "          \
   "content using Tracker"
 
diff --git a/src/media/tracker/grl-tracker-media.c b/src/media/tracker/grl-tracker-media.c
index 1ac906f..6d59d55 100644
--- a/src/media/tracker/grl-tracker-media.c
+++ b/src/media/tracker/grl-tracker-media.c
@@ -56,8 +56,6 @@ enum {
   PROP_TRACKER_CONNECTION,
 };
 
-static GrlTrackerMedia *grl_tracker_media_new (TrackerSparqlConnection *connection);
-
 static void grl_tracker_media_set_property (GObject      *object,
                                              guint         propid,
                                              const GValue *value,
@@ -67,24 +65,118 @@ static void grl_tracker_media_constructed (GObject *object);
 
 static void grl_tracker_media_finalize (GObject *object);
 
-gboolean grl_tracker_plugin_init (GrlPluginRegistry *registry,
-                                  const GrlPluginInfo *plugin,
-                                  GList *configs);
-
 /* ===================== Globals  ================= */
 
-TrackerSparqlConnection *grl_tracker_connection = NULL;
-const GrlPluginInfo *grl_tracker_plugin;
-
 /* shared data across  */
 GrlTrackerCache *grl_tracker_item_cache;
 GHashTable *grl_tracker_modified_sources;
 
-/* tracker plugin config */
-gboolean grl_tracker_per_device_source = FALSE;
-gboolean grl_tracker_browse_filesystem = FALSE;
+/* ================== TrackerMedia GObject ================ */
+
+G_DEFINE_TYPE (GrlTrackerMedia, grl_tracker_media, GRL_TYPE_MEDIA_SOURCE);
+
+static GrlTrackerMedia *
+grl_tracker_media_new (TrackerSparqlConnection *connection)
+{
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  return g_object_new (GRL_TRACKER_MEDIA_TYPE,
+                       "source-id", GRL_TRACKER_MEDIA_ID,
+                       "source-name", GRL_TRACKER_MEDIA_NAME,
+                       "source-desc", GRL_TRACKER_MEDIA_DESC,
+                       "tracker-connection", connection,
+                       NULL);
+}
+
+static void
+grl_tracker_media_class_init (GrlTrackerMediaClass * klass)
+{
+  GrlMediaSourceClass    *source_class   = GRL_MEDIA_SOURCE_CLASS (klass);
+  GrlMetadataSourceClass *metadata_class = GRL_METADATA_SOURCE_CLASS (klass);
+  GObjectClass           *g_class        = G_OBJECT_CLASS (klass);
 
-/* =================== Tracker Plugin  =============== */
+  source_class->query               = grl_tracker_media_query;
+  source_class->metadata            = grl_tracker_media_metadata;
+  source_class->search              = grl_tracker_media_search;
+  source_class->browse              = grl_tracker_media_browse;
+  source_class->cancel              = grl_tracker_media_cancel;
+  source_class->notify_change_start = grl_tracker_media_change_start;
+  source_class->notify_change_stop  = grl_tracker_media_change_stop;
+
+  metadata_class->supported_keys = grl_tracker_supported_keys;
+
+  g_class->finalize     = grl_tracker_media_finalize;
+  g_class->set_property = grl_tracker_media_set_property;
+  g_class->constructed  = grl_tracker_media_constructed;
+
+  g_object_class_install_property (g_class,
+                                   PROP_TRACKER_CONNECTION,
+                                   g_param_spec_object ("tracker-connection",
+                                                        "tracker connection",
+                                                        "A Tracker connection",
+                                                        TRACKER_SPARQL_TYPE_CONNECTION,
+                                                        G_PARAM_WRITABLE
+                                                        | G_PARAM_CONSTRUCT_ONLY
+                                                        | G_PARAM_STATIC_NAME));
+
+  g_type_class_add_private (klass, sizeof (GrlTrackerMediaPriv));
+
+  grl_tracker_setup_key_mappings ();
+}
+
+static void
+grl_tracker_media_init (GrlTrackerMedia *source)
+{
+  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (source);
+
+  source->priv = priv;
+
+  priv->operations = g_hash_table_new (g_direct_hash, g_direct_equal);
+}
+
+static void
+grl_tracker_media_constructed (GObject *object)
+{
+  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (object);
+
+  if (grl_tracker_per_device_source)
+    g_object_get (object, "source-id", &priv->tracker_datasource, NULL);
+}
+
+static void
+grl_tracker_media_finalize (GObject *object)
+{
+  GrlTrackerMedia *self;
+
+  self = GRL_TRACKER_MEDIA (object);
+  if (self->priv->tracker_connection)
+    g_object_unref (self->priv->tracker_connection);
+
+  G_OBJECT_CLASS (grl_tracker_media_parent_class)->finalize (object);
+}
+
+static void
+grl_tracker_media_set_property (GObject      *object,
+                                 guint         propid,
+                                 const GValue *value,
+                                 GParamSpec   *pspec)
+
+{
+  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (object);
+
+  switch (propid) {
+    case PROP_TRACKER_CONNECTION:
+      if (priv->tracker_connection != NULL)
+        g_object_unref (G_OBJECT (priv->tracker_connection));
+      priv->tracker_connection = g_object_ref (g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
+  }
+}
+
+/* =================== TrackerMedia Plugin  =============== */
 
 void
 grl_tracker_add_source (GrlTrackerMedia *source)
@@ -234,16 +326,15 @@ tracker_get_datasources_cb (GObject      *object,
                                     cursor);
 }
 
-static void
-tracker_get_connection_cb (GObject             *object,
-                           GAsyncResult        *res,
-                           const GrlPluginInfo *plugin)
+void
+grl_tracker_media_sources_init (void)
 {
-  /* GrlTrackerMedia *source; */
-
   GRL_DEBUG ("%s", __FUNCTION__);
 
-  grl_tracker_connection = tracker_sparql_connection_get_finish (res, NULL);
+  grl_tracker_item_cache =
+    grl_tracker_media_cache_new (TRACKER_ITEM_CACHE_SIZE);
+  grl_tracker_modified_sources = g_hash_table_new (g_str_hash, g_str_equal);
+
 
   if (grl_tracker_connection != NULL) {
     grl_tracker_media_dbus_start_watch ();
@@ -264,154 +355,3 @@ tracker_get_connection_cb (GObject             *object,
     }
   }
 }
-
-gboolean
-grl_tracker_plugin_init (GrlPluginRegistry *registry,
-                         const GrlPluginInfo *plugin,
-                         GList *configs)
-{
-  GrlConfig *config;
-  gint config_count;
-
-  GRL_DEBUG ("%s", __FUNCTION__);
-
-  GRL_LOG_DOMAIN_INIT (tracker_general_log_domain, "tracker-general");
-  grl_tracker_media_init_notifs ();
-  grl_tracker_media_init_requests ();
-
-  grl_tracker_plugin = plugin;
-  grl_tracker_item_cache =
-    grl_tracker_media_cache_new (TRACKER_ITEM_CACHE_SIZE);
-  grl_tracker_modified_sources = g_hash_table_new (g_str_hash, g_str_equal);
-
-  if (!configs) {
-    GRL_WARNING ("\tConfiguration not provided! Using default configuration.");
-  } else {
-    config_count = g_list_length (configs);
-    if (config_count > 1) {
-      GRL_WARNING ("\tProvided %i configs, but will only use one", config_count);
-    }
-
-    config = GRL_CONFIG (configs->data);
-
-    grl_tracker_per_device_source =
-      grl_config_get_boolean (config, "per-device-source");
-    grl_tracker_browse_filesystem =
-      grl_config_get_boolean (config, "browse-filesystem");
-  }
-
-  tracker_sparql_connection_get_async (NULL,
-                                       (GAsyncReadyCallback) tracker_get_connection_cb,
-                                       (gpointer) plugin);
-  return TRUE;
-}
-
-GRL_PLUGIN_REGISTER (grl_tracker_plugin_init,
-                     NULL,
-                     GRL_TRACKER_PLUGIN_ID);
-
-/* ================== Tracker GObject ================ */
-
-static GrlTrackerMedia *
-grl_tracker_media_new (TrackerSparqlConnection *connection)
-{
-  GRL_DEBUG ("%s", __FUNCTION__);
-
-  return g_object_new (GRL_TRACKER_MEDIA_TYPE,
-                       "source-id", GRL_TRACKER_MEDIA_ID,
-                       "source-name", GRL_TRACKER_MEDIA_NAME,
-                       "source-desc", GRL_TRACKER_MEDIA_DESC,
-                       "tracker-connection", connection,
-                       NULL);
-}
-
-G_DEFINE_TYPE (GrlTrackerMedia, grl_tracker_media, GRL_TYPE_MEDIA_SOURCE);
-
-static void
-grl_tracker_media_class_init (GrlTrackerMediaClass * klass)
-{
-  GrlMediaSourceClass    *source_class   = GRL_MEDIA_SOURCE_CLASS (klass);
-  GrlMetadataSourceClass *metadata_class = GRL_METADATA_SOURCE_CLASS (klass);
-  GObjectClass           *g_class        = G_OBJECT_CLASS (klass);
-
-  source_class->query               = grl_tracker_media_query;
-  source_class->metadata            = grl_tracker_media_metadata;
-  source_class->search              = grl_tracker_media_search;
-  source_class->browse              = grl_tracker_media_browse;
-  source_class->cancel              = grl_tracker_media_cancel;
-  source_class->notify_change_start = grl_tracker_media_change_start;
-  source_class->notify_change_stop  = grl_tracker_media_change_stop;
-
-  metadata_class->supported_keys = grl_tracker_media_supported_keys;
-
-  g_class->finalize     = grl_tracker_media_finalize;
-  g_class->set_property = grl_tracker_media_set_property;
-  g_class->constructed  = grl_tracker_media_constructed;
-
-  g_object_class_install_property (g_class,
-                                   PROP_TRACKER_CONNECTION,
-                                   g_param_spec_object ("tracker-connection",
-                                                        "tracker connection",
-                                                        "A Tracker connection",
-                                                        TRACKER_SPARQL_TYPE_CONNECTION,
-                                                        G_PARAM_WRITABLE
-                                                        | G_PARAM_CONSTRUCT_ONLY
-                                                        | G_PARAM_STATIC_NAME));
-
-  g_type_class_add_private (klass, sizeof (GrlTrackerMediaPriv));
-
-  grl_tracker_setup_key_mappings ();
-}
-
-static void
-grl_tracker_media_init (GrlTrackerMedia *source)
-{
-  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (source);
-
-  source->priv = priv;
-
-  priv->operations = g_hash_table_new (g_direct_hash, g_direct_equal);
-}
-
-static void
-grl_tracker_media_constructed (GObject *object)
-{
-  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (object);
-
-  if (grl_tracker_per_device_source)
-    g_object_get (object, "source-id", &priv->tracker_datasource, NULL);
-}
-
-static void
-grl_tracker_media_finalize (GObject *object)
-{
-  GrlTrackerMedia *self;
-
-  self = GRL_TRACKER_MEDIA (object);
-  if (self->priv->tracker_connection)
-    g_object_unref (self->priv->tracker_connection);
-
-  G_OBJECT_CLASS (grl_tracker_media_parent_class)->finalize (object);
-}
-
-static void
-grl_tracker_media_set_property (GObject      *object,
-                                 guint         propid,
-                                 const GValue *value,
-                                 GParamSpec   *pspec)
-
-{
-  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (object);
-
-  switch (propid) {
-    case PROP_TRACKER_CONNECTION:
-      if (priv->tracker_connection != NULL)
-        g_object_unref (G_OBJECT (priv->tracker_connection));
-      priv->tracker_connection = g_object_ref (g_value_get_object (value));
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
-  }
-}
-
diff --git a/src/media/tracker/grl-tracker-media.h b/src/media/tracker/grl-tracker-media.h
index 6b484c2..d4b0c9b 100644
--- a/src/media/tracker/grl-tracker-media.h
+++ b/src/media/tracker/grl-tracker-media.h
@@ -78,11 +78,13 @@ GType grl_tracker_media_get_type (void);
 gboolean grl_tracker_media_can_notify (GrlTrackerMedia *source);
 
 /**/
+
+void grl_tracker_media_sources_init (void);
+
 void grl_tracker_add_source (GrlTrackerMedia *source);
 
 void grl_tracker_del_source (GrlTrackerMedia *source);
 
 GrlTrackerMedia *grl_tracker_media_find (const gchar *id);
 
-
 #endif /* _GRL_TRACKER_MEDIA_H_ */
diff --git a/src/media/tracker/grl-tracker-metadata.c b/src/media/tracker/grl-tracker-metadata.c
new file mode 100644
index 0000000..d9ff2ee
--- /dev/null
+++ b/src/media/tracker/grl-tracker-metadata.c
@@ -0,0 +1,360 @@
+/*
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * Authors: Lionel Landwerlin <lionel g landwerlin linux intel com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <tracker-sparql.h>
+
+#include "grl-tracker-metadata.h"
+#include "grl-tracker-utils.h"
+
+/* --------- Logging  -------- */
+
+#define GRL_LOG_DOMAIN_DEFAULT tracker_metadata_request_log_domain
+
+GRL_LOG_DOMAIN_STATIC(tracker_metadata_request_log_domain);
+GRL_LOG_DOMAIN_STATIC(tracker_metadata_result_log_domain);
+
+/* Inputs/requests */
+#define GRL_IDEBUG(args...)                     \
+  GRL_LOG (tracker_metadata_request_log_domain, \
+           GRL_LOG_LEVEL_DEBUG, args)
+
+/* Outputs/results */
+#define GRL_ODEBUG(args...)                     \
+  GRL_LOG (tracker_metadata_result_log_domain,  \
+           GRL_LOG_LEVEL_DEBUG, args)
+
+/* ------- Definitions ------- */
+
+#define TRACKER_RESOLVE_REQUEST                 \
+  "SELECT %s "                                  \
+  "WHERE "                                      \
+  "{ "                                          \
+  "?urn a nie:DataObject . "                    \
+  "?urn nie:url \"%s\" " \
+  "}"
+
+/**/
+
+#define GRL_TRACKER_METADATA_GET_PRIVATE(object)		\
+  (G_TYPE_INSTANCE_GET_PRIVATE((object),                        \
+                               GRL_TRACKER_METADATA_TYPE,       \
+                               GrlTrackerMetadataPriv))
+
+enum {
+  PROP_0,
+  PROP_TRACKER_CONNECTION,
+};
+
+struct _GrlTrackerMetadataPriv {
+  TrackerSparqlConnection *tracker_connection;
+};
+
+static void grl_tracker_metadata_set_property (GObject      *object,
+                                               guint         propid,
+                                               const GValue *value,
+                                               GParamSpec   *pspec);
+
+static void grl_tracker_metadata_finalize (GObject *object);
+
+static gboolean grl_tracker_metadata_may_resolve (GrlMetadataSource  *source,
+                                                  GrlMedia           *media,
+                                                  GrlKeyID            key_id,
+                                                  GList             **missing_keys);
+
+static void grl_tracker_metadata_resolve (GrlMetadataSource            *source,
+                                          GrlMetadataSourceResolveSpec *rs);
+
+/* ================== TrackerMetadata GObject ================ */
+
+G_DEFINE_TYPE (GrlTrackerMetadata, grl_tracker_metadata, GRL_TYPE_METADATA_SOURCE);
+
+static GrlTrackerMetadata *
+grl_tracker_metadata_new (TrackerSparqlConnection *connection)
+{
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  return g_object_new (GRL_TRACKER_METADATA_TYPE,
+                       "source-id", GRL_TRACKER_METADATA_ID,
+                       "source-name", GRL_TRACKER_METADATA_NAME,
+                       "source-desc", GRL_TRACKER_METADATA_DESC,
+                       "tracker-connection", connection,
+                       NULL);
+}
+
+static void
+grl_tracker_metadata_class_init (GrlTrackerMetadataClass * klass)
+{
+  GrlMetadataSourceClass *metadata_class = GRL_METADATA_SOURCE_CLASS (klass);
+  GObjectClass           *g_class        = G_OBJECT_CLASS (klass);
+
+  metadata_class->supported_keys = grl_tracker_supported_keys;
+  metadata_class->may_resolve    = grl_tracker_metadata_may_resolve;
+  metadata_class->resolve        = grl_tracker_metadata_resolve;
+
+  g_class->finalize     = grl_tracker_metadata_finalize;
+  g_class->set_property = grl_tracker_metadata_set_property;
+
+  g_object_class_install_property (g_class,
+                                   PROP_TRACKER_CONNECTION,
+                                   g_param_spec_object ("tracker-connection",
+                                                        "tracker connection",
+                                                        "A Tracker connection",
+                                                        TRACKER_SPARQL_TYPE_CONNECTION,
+                                                        G_PARAM_WRITABLE
+                                                        | G_PARAM_CONSTRUCT_ONLY
+                                                        | G_PARAM_STATIC_NAME));
+
+  g_type_class_add_private (klass, sizeof (GrlTrackerMetadataPriv));
+}
+
+static void
+grl_tracker_metadata_init (GrlTrackerMetadata *source)
+{
+  GrlTrackerMetadataPriv *priv = GRL_TRACKER_METADATA_GET_PRIVATE (source);
+
+  source->priv = priv;
+}
+
+static void
+grl_tracker_metadata_finalize (GObject *object)
+{
+  GrlTrackerMetadata *self;
+
+  self = GRL_TRACKER_METADATA (object);
+  if (self->priv->tracker_connection)
+    g_object_unref (self->priv->tracker_connection);
+
+  G_OBJECT_CLASS (grl_tracker_metadata_parent_class)->finalize (object);
+}
+
+static void
+grl_tracker_metadata_set_property (GObject      *object,
+                                   guint         propid,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+
+{
+  GrlTrackerMetadataPriv *priv = GRL_TRACKER_METADATA_GET_PRIVATE (object);
+
+  switch (propid) {
+    case PROP_TRACKER_CONNECTION:
+      if (priv->tracker_connection != NULL)
+        g_object_unref (G_OBJECT (priv->tracker_connection));
+      priv->tracker_connection = g_object_ref (g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
+  }
+}
+
+/**/
+
+static void
+fill_grilo_media_from_sparql (GrlTrackerMetadata  *source,
+                              GrlMedia            *media,
+                              TrackerSparqlCursor *cursor,
+                              gint                 column)
+{
+  const gchar *sparql_key = tracker_sparql_cursor_get_variable_name (cursor,
+                                                                     column);
+  tracker_grl_sparql_t *assoc =
+    grl_tracker_get_mapping_from_sparql (sparql_key);
+  union {
+    gint int_val;
+    gdouble double_val;
+    const gchar *str_val;
+  } val;
+
+  if (assoc == NULL)
+    return;
+
+  GRL_ODEBUG ("\tSetting media prop (col=%i/var=%s/prop=%s) %s",
+              column,
+              sparql_key,
+              g_param_spec_get_name (G_PARAM_SPEC (assoc->grl_key)),
+              tracker_sparql_cursor_get_string (cursor, column, NULL));
+
+  if (tracker_sparql_cursor_is_bound (cursor, column) == FALSE) {
+    GRL_ODEBUG ("\t\tDropping, no data");
+    return;
+  }
+
+  if (grl_data_key_is_known (GRL_DATA (media), assoc->grl_key)) {
+    GRL_ODEBUG ("\t\tDropping, already here");
+    return;
+  }
+
+  switch (G_PARAM_SPEC (assoc->grl_key)->value_type) {
+  case G_TYPE_STRING:
+    /* Cache the source associated to this result. */
+    /* if (assoc->grl_key == GRL_METADATA_KEY_ID) { */
+    /*   grl_tracker_media_cache_add_item (grl_tracker_item_cache, */
+    /*                                     tracker_sparql_cursor_get_integer (cursor, */
+    /*                                                                        column), */
+    /*                                     source); */
+    /* } */
+    val.str_val = tracker_sparql_cursor_get_string (cursor, column, NULL);
+    if (val.str_val != NULL)
+      grl_data_set_string (GRL_DATA (media), assoc->grl_key, val.str_val);
+    break;
+
+  case G_TYPE_INT:
+    val.int_val = tracker_sparql_cursor_get_integer (cursor, column);
+    grl_data_set_int (GRL_DATA (media), assoc->grl_key, val.int_val);
+    break;
+
+  case G_TYPE_FLOAT:
+    val.double_val = tracker_sparql_cursor_get_double (cursor, column);
+    grl_data_set_float (GRL_DATA (media), assoc->grl_key, (gfloat) val.double_val);
+    break;
+
+  default:
+    GRL_ODEBUG ("\t\tUnexpected data type");
+    break;
+  }
+}
+
+static void
+tracker_resolve_cb (GObject                      *source_object,
+                    GAsyncResult                 *result,
+                    GrlMetadataSourceResolveSpec *rs)
+{
+  GrlTrackerMetadataPriv *priv = GRL_TRACKER_METADATA_GET_PRIVATE (rs->source);
+  gint                  col;
+  GError               *tracker_error = NULL, *error = NULL;
+  TrackerSparqlCursor  *cursor;
+
+  GRL_ODEBUG ("%s", __FUNCTION__);
+
+  cursor = tracker_sparql_connection_query_finish (priv->tracker_connection,
+                                                   result, &tracker_error);
+
+  if (tracker_error) {
+    GRL_WARNING ("Could not execute sparql resolve query : %s",
+                 tracker_error->message);
+
+    error = g_error_new (GRL_CORE_ERROR,
+			 GRL_CORE_ERROR_BROWSE_FAILED,
+			 "Failed to start resolve action : %s",
+                         tracker_error->message);
+
+    rs->callback (rs->source, NULL, rs->user_data, error);
+
+    g_error_free (tracker_error);
+    g_error_free (error);
+
+    goto end_operation;
+  }
+
+
+  tracker_sparql_cursor_next (cursor, NULL, NULL);
+
+  /* Translate Sparql result into Grilo result */
+  for (col = 0 ; col < tracker_sparql_cursor_get_n_columns (cursor) ; col++) {
+    fill_grilo_media_from_sparql (GRL_TRACKER_METADATA (rs->source),
+                                  rs->media, cursor, col);
+  }
+
+  rs->callback (rs->source, rs->media, rs->user_data, NULL);
+
+ end_operation:
+  if (cursor)
+    g_object_unref (G_OBJECT (cursor));
+}
+
+
+/**/
+
+static gboolean
+grl_tracker_metadata_may_resolve (GrlMetadataSource  *source,
+                                  GrlMedia           *media,
+                                  GrlKeyID            key_id,
+                                  GList             **missing_keys)
+{
+  const gchar *url;
+
+  if (media) {
+    url = grl_media_get_url (media);
+
+    if (url && grl_tracker_key_is_supported (key_id)) {
+      return TRUE;
+    }
+  } else {
+    if (grl_tracker_key_is_supported (key_id))
+      return TRUE;
+  }
+
+  return FALSE;
+}
+
+
+static void
+grl_tracker_metadata_resolve (GrlMetadataSource            *source,
+                              GrlMetadataSourceResolveSpec *rs)
+{
+  GrlTrackerMetadataPriv *priv = GRL_TRACKER_METADATA_GET_PRIVATE (source);
+  const gchar *url = grl_media_get_url (rs->media);
+  gchar *sparql_select, *sparql_final;
+
+  GRL_IDEBUG ("%s", __FUNCTION__);
+
+  g_return_if_fail (url != NULL);
+
+  sparql_select = grl_tracker_media_get_select_string (rs->keys);
+  sparql_final = g_strdup_printf (TRACKER_RESOLVE_REQUEST, sparql_select, url);
+
+  tracker_sparql_connection_query_async (priv->tracker_connection,
+                                         sparql_final,
+                                         NULL,
+                                         (GAsyncReadyCallback) tracker_resolve_cb,
+                                         rs);
+
+  GRL_IDEBUG ("request: '%s'", sparql_final);
+
+  g_free (sparql_select);
+  g_free (sparql_final);
+}
+
+/* =================== TrackerMedia Plugin  =============== */
+
+void
+grl_tracker_metadata_source_init (void)
+{
+  GrlTrackerMetadata *source = grl_tracker_metadata_new (grl_tracker_connection);
+
+  grl_plugin_registry_register_source (grl_plugin_registry_get_default (),
+                                       grl_tracker_plugin,
+                                       GRL_MEDIA_PLUGIN (source),
+                                       NULL);
+}
+
+void
+grl_tracker_metadata_init_requests (void)
+{
+  GRL_LOG_DOMAIN_INIT (tracker_metadata_request_log_domain,
+                       "tracker-metadata-request");
+  GRL_LOG_DOMAIN_INIT (tracker_metadata_result_log_domain,
+                       "tracker-metadata-result");
+}
diff --git a/src/media/tracker/grl-tracker-metadata.h b/src/media/tracker/grl-tracker-metadata.h
new file mode 100644
index 0000000..fc67fa4
--- /dev/null
+++ b/src/media/tracker/grl-tracker-metadata.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * Authors: Lionel Landwerlin <lionel g landwerlin linux intel com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef _GRL_TRACKER_METADATA_H_
+#define _GRL_TRACKER_METADATA_H_
+
+#include <grilo.h>
+
+#define GRL_TRACKER_METADATA_TYPE               \
+  (grl_tracker_metadata_get_type ())
+
+#define GRL_TRACKER_METADATA(obj)                               \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj),                           \
+                               GRL_TRACKER_METADATA_TYPE,       \
+                               GrlTrackerMetadata))
+
+#define GRL_IS_TRACKER_METADATA(obj)                       \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj),                      \
+                               GRL_TRACKER_METADATA_TYPE))
+
+#define GRL_TRACKER_METADATA_CLASS(klass)               \
+  (G_TYPE_CHECK_CLASS_CAST((klass),                     \
+                           GRL_TRACKER_METADATA_TYPE,   \
+                           GrlTrackerMetadataClass))
+
+#define GRL_IS_TRACKER_METADATA_CLASS(klass)            \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),                     \
+                           GRL_TRACKER_METADATA_TYPE))
+
+#define GRL_TRACKER_METADATA_GET_CLASS(obj)                     \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj),                            \
+                              GRL_TRACKER_METADATA_TYPE,        \
+                              GrlTrackerMetadataClass))
+
+/* ---- MetadataSource information ---- */
+
+#define GRL_TRACKER_METADATA_ID   "grl-tracker-media"
+#define GRL_TRACKER_METADATA_NAME "TrackerMetadata"
+#define GRL_TRACKER_METADATA_DESC               \
+  "A plugin for searching metadata"             \
+  "using Tracker"
+
+#define GRL_TRACKER_AUTHOR  "Igalia S.L."
+#define GRL_TRACKER_LICENSE "LGPL"
+#define GRL_TRACKER_SITE    "http://www.igalia.com";
+
+/**/
+
+typedef struct _GrlTrackerMetadata GrlTrackerMetadata;
+typedef struct _GrlTrackerMetadataPriv GrlTrackerMetadataPriv;
+
+struct _GrlTrackerMetadata {
+
+  GrlMetadataSource parent;
+
+  /*< private >*/
+  GrlTrackerMetadataPriv *priv;
+
+};
+
+typedef struct _GrlTrackerMetadataClass GrlTrackerMetadataClass;
+
+struct _GrlTrackerMetadataClass {
+
+  GrlMetadataSourceClass parent_class;
+
+};
+
+GType grl_tracker_metadata_get_type (void);
+
+/**/
+
+void grl_tracker_metadata_init_requests (void);
+
+void grl_tracker_metadata_source_init (void);
+
+#endif /* _GRL_TRACKER_METADATA_H_ */
diff --git a/src/media/tracker/grl-tracker-utils.c b/src/media/tracker/grl-tracker-utils.c
index ce0072d..e95db84 100644
--- a/src/media/tracker/grl-tracker-utils.c
+++ b/src/media/tracker/grl-tracker-utils.c
@@ -161,6 +161,12 @@ get_mapping_from_grl (const GrlKeyID key)
   return (GList *) g_hash_table_lookup (grl_to_sparql_mapping, key);
 }
 
+gboolean
+grl_tracker_key_is_supported (const GrlKeyID key)
+{
+  return g_hash_table_lookup (grl_to_sparql_mapping, key) != NULL;
+}
+
 /**/
 
 gchar *
@@ -174,8 +180,7 @@ grl_tracker_media_get_device_constraint (GrlTrackerMediaPriv *priv)
 }
 
 gchar *
-grl_tracker_media_get_select_string (GrlMediaSource *source,
-                                     const GList *keys)
+grl_tracker_media_get_select_string (const GList *keys)
 {
   const GList *key = keys;
   GString *gstr = g_string_new ("");
@@ -327,3 +332,10 @@ grl_tracker_get_media_name (const gchar *rdf_type,
 
   return source_name;
 }
+
+const GList *
+grl_tracker_supported_keys (GrlMetadataSource *source)
+{
+  return
+    grl_plugin_registry_get_metadata_keys (grl_plugin_registry_get_default ());
+}
diff --git a/src/media/tracker/grl-tracker-utils.h b/src/media/tracker/grl-tracker-utils.h
index 881828e..2c3e835 100644
--- a/src/media/tracker/grl-tracker-utils.h
+++ b/src/media/tracker/grl-tracker-utils.h
@@ -51,6 +51,10 @@ typedef struct {
   const gchar *sparql_key_flavor;
 } tracker_grl_sparql_t;
 
+const GList *grl_tracker_supported_keys (GrlMetadataSource *source);
+
+gboolean grl_tracker_key_is_supported (const GrlKeyID key);
+
 void grl_tracker_setup_key_mappings (void);
 
 tracker_grl_sparql_t *grl_tracker_get_mapping_from_sparql (const gchar *key);
@@ -59,8 +63,7 @@ GrlMedia *grl_tracker_build_grilo_media (const gchar *rdf_type);
 
 gchar *grl_tracker_media_get_device_constraint (GrlTrackerMediaPriv *priv);
 
-gchar *grl_tracker_media_get_select_string (GrlMediaSource *source,
-                                            const GList *keys);
+gchar *grl_tracker_media_get_select_string (const GList *keys);
 
 gchar *grl_tracker_get_media_name (const gchar *rdf_type,
                                    const gchar *uri,
diff --git a/src/media/tracker/grl-tracker.c b/src/media/tracker/grl-tracker.c
new file mode 100644
index 0000000..adf214a
--- /dev/null
+++ b/src/media/tracker/grl-tracker.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2011 Igalia S.L.
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * Authors: Juan A. Suarez Romero <jasuarez igalia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <grilo.h>
+#include <string.h>
+#include <tracker-sparql.h>
+
+#include "grl-tracker.h"
+#include "grl-tracker-media.h"
+#include "grl-tracker-media-api.h"
+#include "grl-tracker-media-notif.h"
+#include "grl-tracker-metadata.h"
+
+/* --------- Logging  -------- */
+
+#define GRL_LOG_DOMAIN_DEFAULT tracker_general_log_domain
+GRL_LOG_DOMAIN_STATIC(tracker_general_log_domain);
+
+/* ------- Definitions ------- */
+
+/* --- Other --- */
+
+gboolean grl_tracker_plugin_init (GrlPluginRegistry *registry,
+                                  const GrlPluginInfo *plugin,
+                                  GList *configs);
+
+/* ===================== Globals  ================= */
+
+TrackerSparqlConnection *grl_tracker_connection = NULL;
+const GrlPluginInfo *grl_tracker_plugin;
+
+/* tracker plugin config */
+gboolean grl_tracker_per_device_source = FALSE;
+gboolean grl_tracker_browse_filesystem = FALSE;
+
+/* =================== Tracker Plugin  =============== */
+
+static void
+tracker_get_connection_cb (GObject             *object,
+                           GAsyncResult        *res,
+                           const GrlPluginInfo *plugin)
+{
+  /* GrlTrackerMedia *source; */
+
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  grl_tracker_connection = tracker_sparql_connection_get_finish (res, NULL);
+
+  if (grl_tracker_connection != NULL) {
+    grl_tracker_media_dbus_start_watch ();
+
+    grl_tracker_metadata_source_init ();
+    grl_tracker_media_sources_init ();
+  }
+}
+
+gboolean
+grl_tracker_plugin_init (GrlPluginRegistry *registry,
+                         const GrlPluginInfo *plugin,
+                         GList *configs)
+{
+  GrlConfig *config;
+  gint config_count;
+
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  GRL_LOG_DOMAIN_INIT (tracker_general_log_domain, "tracker-general");
+  grl_tracker_media_init_notifs ();
+  grl_tracker_media_init_requests ();
+  grl_tracker_metadata_init_requests ();
+
+  grl_tracker_plugin = plugin;
+
+  if (!configs) {
+    GRL_WARNING ("\tConfiguration not provided! Using default configuration.");
+  } else {
+    config_count = g_list_length (configs);
+    if (config_count > 1) {
+      GRL_WARNING ("\tProvided %i configs, but will only use one", config_count);
+    }
+
+    config = GRL_CONFIG (configs->data);
+
+    grl_tracker_per_device_source =
+      grl_config_get_boolean (config, "per-device-source");
+    grl_tracker_browse_filesystem =
+      grl_config_get_boolean (config, "browse-filesystem");
+  }
+
+  tracker_sparql_connection_get_async (NULL,
+                                       (GAsyncReadyCallback) tracker_get_connection_cb,
+                                       (gpointer) plugin);
+  return TRUE;
+}
+
+GRL_PLUGIN_REGISTER (grl_tracker_plugin_init,
+                     NULL,
+                     GRL_TRACKER_PLUGIN_ID);
diff --git a/src/media/tracker/grl-tracker.h b/src/media/tracker/grl-tracker.h
new file mode 100644
index 0000000..cc1dd28
--- /dev/null
+++ b/src/media/tracker/grl-tracker.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * Authors: Lionel Landwerlin <lionel g landwerlin linux intel com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef _GRL_TRACKER_H_
+#define _GRL_TRACKER_H_
+
+/* ---- Plugin information --- */
+
+#define GRL_TRACKER_PLUGIN_ID   TRACKER_PLUGIN_ID
+
+#endif /* _GRL_TRACKER_H_ */
-- 
1.7.4.1



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