[grilo-plugins] tracker: add support for set_metadata()



commit 39604ebb8d834a187e54de726303728420f73a8b
Author: Lionel Landwerlin <lionel g landwerlin linux intel com>
Date:   Wed Mar 30 11:36:21 2011 +0100

    tracker: add support for set_metadata()
    
    At the moment only 3 keys are supported :
     * GRL_METADATA_KEY_PLAY_COUNT
     * GRL_METADATA_KEY_LAST_PLAYED
     * GRL_METADATA_KEY_LAST_POSITION
    
    Signed-off-by: Lionel Landwerlin <lionel g landwerlin linux intel com>

 src/media/tracker/grl-tracker-media-api.c |   82 +++++++++++++++-
 src/media/tracker/grl-tracker-media-api.h |    5 +
 src/media/tracker/grl-tracker-media.c     |    6 +-
 src/media/tracker/grl-tracker-utils.c     |  152 +++++++++++++++++++++++++++++
 src/media/tracker/grl-tracker-utils.h     |    8 ++
 5 files changed, 250 insertions(+), 3 deletions(-)
---
diff --git a/src/media/tracker/grl-tracker-media-api.c b/src/media/tracker/grl-tracker-media-api.c
index 0e11f69..694d166 100644
--- a/src/media/tracker/grl-tracker-media-api.c
+++ b/src/media/tracker/grl-tracker-media-api.c
@@ -133,6 +133,10 @@ GRL_LOG_DOMAIN_STATIC(tracker_media_result_log_domain);
   "FILTER (tracker:id(?urn) = %s) "             \
   "}"
 
+#define TRACKER_SAVE_REQUEST                            \
+  "DELETE { <%s> %s } WHERE { <%s> a nfo:Media . %s } " \
+  "INSERT { <%s> a nfo:Media ; %s . }"
+
 /**/
 
 struct OperationSpec {
@@ -431,8 +435,51 @@ tracker_metadata_cb (GObject                    *source_object,
     g_object_unref (G_OBJECT (cursor));
 }
 
+static void
+tracker_set_metadata_cb (GObject                          *source_object,
+                         GAsyncResult                     *result,
+                         GrlMetadataSourceSetMetadataSpec *sms)
+{
+  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (sms->source);
+  GError *tracker_error = NULL, *error = NULL;
+
+  tracker_sparql_connection_update_finish (priv->tracker_connection,
+                                           result,
+                                           &tracker_error);
+
+  if (tracker_error) {
+    GRL_WARNING ("Could not execute sparql update : %s",
+                 tracker_error->message);
+
+    error = g_error_new (GRL_CORE_ERROR,
+			 GRL_CORE_ERROR_SET_METADATA_FAILED,
+			 "Failed to set metadata : %s",
+                         tracker_error->message);
+
+    sms->callback (sms->source, sms->media, NULL, sms->user_data, error);
+
+    g_error_free (tracker_error);
+    g_error_free (error);
+  } else {
+    sms->callback (sms->source, sms->media, NULL, sms->user_data, error);
+  }
+}
+
 /**/
 
+const GList *
+grl_tracker_media_writable_keys (GrlMetadataSource *source)
+{
+  static GList *keys = NULL;
+  if (!keys) {
+    keys = grl_metadata_key_list_new (GRL_METADATA_KEY_PLAY_COUNT,
+                                      GRL_METADATA_KEY_LAST_PLAYED,
+                                      GRL_METADATA_KEY_LAST_POSITION,
+                                      NULL);
+  }
+  return keys;
+}
+
 /**
  * Query is a SPARQL query.
  *
@@ -605,6 +652,40 @@ grl_tracker_media_metadata (GrlMediaSource *source,
 }
 
 void
+grl_tracker_media_set_metadata (GrlMetadataSource *source,
+                                GrlMetadataSourceSetMetadataSpec *sms)
+{
+  GrlTrackerMediaPriv *priv = GRL_TRACKER_MEDIA_GET_PRIVATE (source);
+  gchar *sparql_delete, *sparql_cdelete, *sparql_insert, *sparql_final;
+  const gchar *urn = grl_data_get_string (GRL_DATA (sms->media),
+                                          grl_metadata_key_tracker_urn);
+
+  GRL_IDEBUG ("%s: urn=%s", G_STRFUNC, urn);
+
+  sparql_delete = grl_tracker_get_delete_string (sms->keys);
+  sparql_cdelete = grl_tracker_get_delete_conditional_string (urn, sms->keys);
+  sparql_insert = grl_tracker_tracker_get_insert_string (sms->media, sms->keys);
+  sparql_final = g_strdup_printf (TRACKER_SAVE_REQUEST,
+                                  urn, sparql_delete,
+                                  urn, sparql_cdelete,
+                                  urn, sparql_insert);
+
+  GRL_IDEBUG ("\trequest: '%s'", sparql_final);
+
+  tracker_sparql_connection_update_async (priv->tracker_connection,
+                                          sparql_final,
+                                          G_PRIORITY_DEFAULT,
+                                          NULL,
+                                          (GAsyncReadyCallback) tracker_set_metadata_cb,
+                                          sms);
+
+  g_free (sparql_delete);
+  g_free (sparql_cdelete);
+  g_free (sparql_insert);
+  g_free (sparql_final);
+}
+
+void
 grl_tracker_media_search (GrlMediaSource *source, GrlMediaSourceSearchSpec *ss)
 {
   GrlTrackerMediaPriv *priv  = GRL_TRACKER_MEDIA_GET_PRIVATE (source);
@@ -832,7 +913,6 @@ grl_tracker_media_init_requests (void)
                                                                     G_PARAM_READWRITE),
                                                NULL);
 
-
   GRL_LOG_DOMAIN_INIT (tracker_media_request_log_domain,
                        "tracker-media-request");
   GRL_LOG_DOMAIN_INIT (tracker_media_result_log_domain,
diff --git a/src/media/tracker/grl-tracker-media-api.h b/src/media/tracker/grl-tracker-media-api.h
index 5beccf3..cd2f5ad 100644
--- a/src/media/tracker/grl-tracker-media-api.h
+++ b/src/media/tracker/grl-tracker-media-api.h
@@ -31,12 +31,17 @@
 
 void grl_tracker_media_init_requests (void);
 
+const GList *grl_tracker_media_writable_keys (GrlMetadataSource *source);
+
 void grl_tracker_media_query (GrlMediaSource *source,
                               GrlMediaSourceQuerySpec *qs);
 
 void grl_tracker_media_metadata (GrlMediaSource *source,
                                  GrlMediaSourceMetadataSpec *ms);
 
+void grl_tracker_media_set_metadata (GrlMetadataSource *source,
+                                     GrlMetadataSourceSetMetadataSpec *sms);
+
 void grl_tracker_media_search (GrlMediaSource *source,
                                GrlMediaSourceSearchSpec *ss);
 
diff --git a/src/media/tracker/grl-tracker-media.c b/src/media/tracker/grl-tracker-media.c
index 0a3e09e..e927c88 100644
--- a/src/media/tracker/grl-tracker-media.c
+++ b/src/media/tracker/grl-tracker-media.c
@@ -105,6 +105,8 @@ grl_tracker_media_class_init (GrlTrackerMediaClass * klass)
   source_class->notify_change_stop  = grl_tracker_media_change_stop;
 
   metadata_class->supported_keys = grl_tracker_supported_keys;
+  metadata_class->writable_keys  = grl_tracker_media_writable_keys;
+  metadata_class->set_metadata   = grl_tracker_media_set_metadata;
 
   g_class->finalize     = grl_tracker_media_finalize;
   g_class->set_property = grl_tracker_media_set_property;
@@ -337,8 +339,8 @@ tracker_get_datasource_cb (GObject             *object,
   if ((source == NULL) && source_available) {
     gchar *source_name = grl_tracker_get_media_name (type, uri, datasource,
                                                      datasource_name);
-    GRL_DEBUG ("\tnew datasource: urn=%s name=%s uri=%s\n",
-	       datasource, datasource_name, uri);
+    GRL_DEBUG ("\tnew datasource: urn=%s name=%s uri=%s => name=%s\n",
+	       datasource, datasource_name, uri, source_name);
     source = g_object_new (GRL_TRACKER_MEDIA_TYPE,
                            "source-id", GRL_TRACKER_MEDIA_ID,
                            "source-name", source_name,
diff --git a/src/media/tracker/grl-tracker-utils.c b/src/media/tracker/grl-tracker-utils.c
index 4685731..346e29f 100644
--- a/src/media/tracker/grl-tracker-utils.c
+++ b/src/media/tracker/grl-tracker-utils.c
@@ -264,6 +264,21 @@ grl_tracker_setup_key_mappings (void)
                                   "nfo:orientation(?urn)",
                                   "image",
                                   set_orientation);
+
+  insert_key_mapping (GRL_METADATA_KEY_PLAY_COUNT,
+                      "nie:usageCounter",
+                      "nie:usageCounter(?urn)",
+                      "media");
+
+  insert_key_mapping (GRL_METADATA_KEY_LAST_PLAYED,
+                      "nie:contentAccessed",
+                      "nie:contentAccessed(?urn)",
+                      "media");
+
+  insert_key_mapping (GRL_METADATA_KEY_LAST_POSITION,
+                      "nfo:lastPlayedPosition",
+                      "nfo:lastPlayedPosition(?urn)",
+                      "media");
 }
 
 tracker_grl_sparql_t *
@@ -329,6 +344,143 @@ grl_tracker_media_get_select_string (const GList *keys)
   return g_string_free (gstr, FALSE);
 }
 
+static void
+gen_prop_insert_string (GString *gstr,
+                        tracker_grl_sparql_t *assoc,
+                        GrlData *data)
+{
+  switch (G_PARAM_SPEC (assoc->grl_key)->value_type) {
+  case G_TYPE_STRING:
+    g_string_append_printf (gstr, "%s %s",
+                            assoc->sparql_key_attr,
+                            grl_data_get_string (data, assoc->grl_key));
+    break;
+
+  case G_TYPE_INT:
+    g_string_append_printf (gstr, "%s %i",
+                            assoc->sparql_key_attr,
+                            grl_data_get_int (data, assoc->grl_key));
+    break;
+
+  case G_TYPE_FLOAT:
+    g_string_append_printf (gstr, "%s %f",
+                            assoc->sparql_key_attr,
+                            grl_data_get_float (data, assoc->grl_key));
+    break;
+
+  default:
+    break;
+  }
+}
+
+gchar *
+grl_tracker_tracker_get_insert_string (GrlMedia *media, const GList *keys)
+{
+  gboolean first = TRUE;
+  const GList *key = keys, *assoc_list;
+  tracker_grl_sparql_t *assoc;
+  GString *gstr = g_string_new ("");
+  gchar *ret;
+
+  while (key != NULL) {
+    assoc_list = get_mapping_from_grl ((GrlKeyID) key->data);
+    while (assoc_list != NULL) {
+      assoc = (tracker_grl_sparql_t *) assoc_list->data;
+      if (assoc != NULL) {
+        if (grl_data_key_is_known (GRL_DATA (media), key->data)) {
+          if (first) {
+            gen_prop_insert_string (gstr, assoc, GRL_DATA (media));
+            first = FALSE;
+          } else {
+            g_string_append (gstr, " ; ");
+            gen_prop_insert_string (gstr, assoc, GRL_DATA (media));
+          }
+        }
+      }
+      assoc_list = assoc_list->next;
+    }
+    key = key->next;
+  }
+
+  ret = gstr->str;
+  g_string_free (gstr, FALSE);
+
+  return ret;
+}
+
+gchar *
+grl_tracker_get_delete_string (const GList *keys)
+{
+  gboolean first = TRUE;
+  const GList *key = keys, *assoc_list;
+  tracker_grl_sparql_t *assoc;
+  GString *gstr = g_string_new ("");
+  gchar *ret;
+  gint var_n = 0;
+
+  while (key != NULL) {
+    assoc_list = get_mapping_from_grl ((GrlKeyID) key->data);
+    while (assoc_list != NULL) {
+      assoc = (tracker_grl_sparql_t *) assoc_list->data;
+      if (assoc != NULL) {
+        if (first) {
+          g_string_append_printf (gstr, "%s ?v%i",
+                                  assoc->sparql_key_attr, var_n);
+          first = FALSE;
+        } else {
+          g_string_append_printf (gstr, " ; %s ?v%i",
+                                  assoc->sparql_key_attr, var_n);
+        }
+        var_n++;
+      }
+      assoc_list = assoc_list->next;
+    }
+    key = key->next;
+  }
+
+  ret = gstr->str;
+  g_string_free (gstr, FALSE);
+
+  return ret;
+}
+
+gchar *
+grl_tracker_get_delete_conditional_string (const gchar *urn,
+                                           const GList *keys)
+{
+  gboolean first = TRUE;
+  const GList *key = keys, *assoc_list;
+  tracker_grl_sparql_t *assoc;
+  GString *gstr = g_string_new ("");
+  gchar *ret;
+  gint var_n = 0;
+
+  while (key != NULL) {
+    assoc_list = get_mapping_from_grl ((GrlKeyID) key->data);
+    while (assoc_list != NULL) {
+      assoc = (tracker_grl_sparql_t *) assoc_list->data;
+      if (assoc != NULL) {
+        if (first) {
+          g_string_append_printf (gstr, "OPTIONAL { <%s>  %s ?v%i }",
+                                  urn,  assoc->sparql_key_attr, var_n);
+          first = FALSE;
+        } else {
+          g_string_append_printf (gstr, " . OPTIONAL { <%s> %s ?v%i }",
+                                  urn, assoc->sparql_key_attr, var_n);
+        }
+        var_n++;
+      }
+      assoc_list = assoc_list->next;
+    }
+    key = key->next;
+  }
+
+  ret = gstr->str;
+  g_string_free (gstr, FALSE);
+
+  return ret;
+}
+
 /**/
 
 /* Builds an appropriate GrlMedia based on ontology type returned by
diff --git a/src/media/tracker/grl-tracker-utils.h b/src/media/tracker/grl-tracker-utils.h
index c2213f8..b55ff8c 100644
--- a/src/media/tracker/grl-tracker-utils.h
+++ b/src/media/tracker/grl-tracker-utils.h
@@ -75,6 +75,14 @@ gchar *grl_tracker_media_get_device_constraint (GrlTrackerMediaPriv *priv);
 
 gchar *grl_tracker_media_get_select_string (const GList *keys);
 
+gchar *grl_tracker_tracker_get_insert_string (GrlMedia *media,
+                                              const GList *keys);
+
+gchar *grl_tracker_get_delete_string (const GList *keys);
+
+gchar *grl_tracker_get_delete_conditional_string (const gchar *urn,
+                                                  const GList *keys);
+
 gchar *grl_tracker_get_media_name (const gchar *rdf_type,
                                    const gchar *uri,
                                    const gchar *datasource,



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