[PATCH 13/14] tracker: refactorize browsing code



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

Signed-off-by: Lionel Landwerlin <lionel g landwerlin linux intel com>
---
 src/tracker/grl-tracker.c |  242 ++++++++++++++++++++++++++++-----------------
 1 files changed, 151 insertions(+), 91 deletions(-)

diff --git a/src/tracker/grl-tracker.c b/src/tracker/grl-tracker.c
index 1526c1e..1691dee 100644
--- a/src/tracker/grl-tracker.c
+++ b/src/tracker/grl-tracker.c
@@ -605,8 +605,26 @@ get_select_string (GrlMediaSource *source, const GList *keys)
   return g_string_free (gstr, FALSE);
 }
 
-/* Builds an appropriate GrlMedia based on ontology type returned by tracker, or
-   NULL if unknown */
+static struct OperationSpec *
+operation_new_browse (GrlTrackerSourcePriv     *priv,
+                      GrlMediaSourceBrowseSpec *bs)
+{
+  struct OperationSpec *os = g_slice_new0 (struct OperationSpec);
+
+  os->source       = bs->source;
+  os->priv         = priv;
+  os->operation_id = bs->browse_id;
+  os->keys         = bs->keys;
+  os->skip         = bs->skip;
+  os->count        = bs->count;
+  os->callback     = bs->callback;
+  os->user_data    = bs->user_data;
+
+  return os;
+}
+
+/* Builds an appropriate GrlMedia based on ontology type returned by
+   tracker. */
 static GrlMedia *
 build_grilo_media (const gchar *rdf_type)
 {
@@ -649,9 +667,9 @@ build_grilo_media (const gchar *rdf_type)
 }
 
 static void
-fill_grilo_media_from_sparql (GrlMedia *media,
+fill_grilo_media_from_sparql (GrlMedia            *media,
                               TrackerSparqlCursor *cursor,
-                              gint column)
+                              gint                 column)
 {
   const gchar *sparql_key = tracker_sparql_cursor_get_variable_name (cursor, column);
   tracker_grl_sparql_t *assoc = get_mapping_from_sparql (sparql_key);;
@@ -815,8 +833,8 @@ tracker_query_cb (GObject              *source_object,
 }
 
 static void
-tracker_metadata_cb (GObject              *source_object,
-                     GAsyncResult         *result,
+tracker_metadata_cb (GObject                    *source_object,
+                     GAsyncResult               *result,
                      GrlMediaSourceMetadataSpec *ms)
 {
   GrlTrackerSourcePriv *priv = GRL_TRACKER_SOURCE_GET_PRIVATE (ms->source);
@@ -860,6 +878,115 @@ tracker_metadata_cb (GObject              *source_object,
     g_object_unref (G_OBJECT (cursor));
 }
 
+static void
+tracker_source_browse_flat (GrlTrackerSourcePriv     *priv,
+                            GrlMediaSourceBrowseSpec *bs)
+{
+  gchar                *sparql_select;
+  gchar                *sparql_final;
+  struct OperationSpec *os;
+
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  sparql_select = get_select_string (bs->source, bs->keys);
+  sparql_final = g_strdup_printf (TRACKER_BROWSE_FLAT_REQUEST,
+                                  sparql_select, bs->skip, bs->count);
+
+  os = operation_new_browse (priv, bs);
+
+  tracker_sparql_connection_query_async (priv->tracker_connection,
+                                         sparql_final,
+                                         NULL,
+                                         (GAsyncReadyCallback) tracker_query_cb,
+                                         os);
+
+  g_free (sparql_select);
+  g_free (sparql_final);
+}
+
+static void
+tracker_source_browse_directory (GrlTrackerSourcePriv     *priv,
+                                 GrlMediaSourceBrowseSpec *bs)
+{
+  gchar                *sparql_select;
+  gchar                *sparql_final;
+  struct OperationSpec *os;
+
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  sparql_select = get_select_string (bs->source, bs->keys);
+
+  if (bs->container == NULL ||
+      grl_media_get_id (bs->container) == NULL) {
+    sparql_final = g_strdup_printf (TRACKER_BROWSE_DIRECTORY_ROOT_REQUEST,
+                                    sparql_select, bs->skip, bs->count);
+  } else {
+    sparql_final = g_strdup_printf (TRACKER_BROWSE_DIRECTORY_CONT_REQUEST,
+                                    sparql_select,
+                                    grl_media_get_id (bs->container),
+                                    bs->skip, bs->count);
+  }
+
+  os = operation_new_browse (priv, bs);
+
+  tracker_sparql_connection_query_async (priv->tracker_connection,
+                                         sparql_final,
+                                         NULL,
+                                         (GAsyncReadyCallback) tracker_query_cb,
+                                         os);
+
+  g_free (sparql_select);
+  g_free (sparql_final);
+}
+
+static void
+tracker_source_browse_category (GrlTrackerSourcePriv     *priv,
+                                GrlMediaSourceBrowseSpec *bs)
+{
+  gchar                *sparql_select;
+  gchar                *sparql_final;
+  struct OperationSpec *os;
+  GrlMedia             *media;
+
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  if ((bs->container == NULL || grl_media_get_id (bs->container) == NULL)) {
+    /* Hardcoded categories */
+    media = grl_media_box_new ();
+    grl_media_set_title (media, "Music");
+    grl_media_set_id (media, "nmm:MusicPiece");
+    bs->callback (bs->source, bs->browse_id, media, 2, bs->user_data, NULL);
+
+    media = grl_media_box_new ();
+    grl_media_set_title (media, "Photo");
+    grl_media_set_id (media, "nmm:Photo");
+    bs->callback (bs->source, bs->browse_id, media, 1, bs->user_data, NULL);
+
+    media = grl_media_box_new ();
+    grl_media_set_title (media, "Video");
+    grl_media_set_id (media, "nmm:Video");
+    bs->callback (bs->source, bs->browse_id, media, 0, bs->user_data, NULL);
+    return;
+  }
+
+  sparql_select = get_select_string (bs->source, bs->keys);
+  sparql_final = g_strdup_printf (TRACKER_BROWSE_CATEGORY_REQUEST,
+                                  sparql_select,
+                                  grl_media_get_id (bs->container),
+                                  bs->skip, bs->count);
+
+  os = operation_new_browse (priv, bs);
+
+  tracker_sparql_connection_query_async (priv->tracker_connection,
+                                         sparql_final,
+                                         NULL,
+                                         (GAsyncReadyCallback) tracker_query_cb,
+                                         os);
+
+  g_free (sparql_select);
+  g_free (sparql_final);
+}
+
 /* ================== API Implementation ================ */
 
 static GrlSupportedOps
@@ -943,8 +1070,7 @@ grl_tracker_source_query (GrlMediaSource *source,
   GError               *error = NULL;
   struct OperationSpec *os;
 
-
-  GRL_DEBUG ("grl_tracker_source_query");
+  GRL_DEBUG ("%s", __FUNCTION__);
 
   if (!qs->query || qs->query[0] == '\0') {
     error = g_error_new_literal (GRL_CORE_ERROR,
@@ -992,7 +1118,7 @@ grl_tracker_source_search (GrlMediaSource *source, GrlMediaSourceSearchSpec *ss)
   GError               *error = NULL;
   struct OperationSpec *os;
 
-  GRL_DEBUG ("grl_tracker_source_search");
+  GRL_DEBUG ("%s", __FUNCTION__);
 
   if (!ss->text || ss->text[0] == '\0') {
     error = g_error_new_literal (GRL_CORE_ERROR,
@@ -1049,7 +1175,7 @@ grl_tracker_source_metadata (GrlMediaSource *source,
   GrlTrackerSourcePriv *priv = GRL_TRACKER_SOURCE_GET_PRIVATE (source);
   gchar                *sparql_select, *sparql_final;
 
-  GRL_DEBUG ("grl_tracker_source_metadata");
+  GRL_DEBUG ("%s", __FUNCTION__);
 
   sparql_select = get_select_string (source, ms->keys);
   sparql_final = g_strdup_printf (TRACKER_METADATA_REQUEST, sparql_select,
@@ -1075,97 +1201,31 @@ grl_tracker_source_browse (GrlMediaSource           *source,
                            GrlMediaSourceBrowseSpec *bs)
 {
   GrlTrackerSourcePriv *priv  = GRL_TRACKER_SOURCE_GET_PRIVATE (source);
-  gchar                *sparql_select;
-  gchar                *sparql_final;
   GError               *error = NULL;
-  struct OperationSpec *os;
-  GrlMedia             *media;
 
-  GRL_DEBUG ("grl_tracker_source_browse");
+  GRL_DEBUG ("%s", __FUNCTION__);
 
   if (priv->tracker_connection == NULL) {
     error = g_error_new (GRL_CORE_ERROR,
                          GRL_CORE_ERROR_SEARCH_FAILED,
                          "No tracker connection");
-    goto send_error;
-  }
-
-  if ((priv->browsing_mode == TRACKER_BROWSING_MODE_CATEGORY) &&
-      (bs->container == NULL || grl_media_get_id (bs->container) == NULL)) {
-    /* Hardcoded categories */
-    media = grl_media_box_new ();
-    grl_media_set_title (media, "Music");
-    grl_media_set_id (media, "nmm:MusicPiece");
-    bs->callback (bs->source, bs->browse_id, media, 2, bs->user_data, NULL);
-
-    media = grl_media_box_new ();
-    grl_media_set_title (media, "Photo");
-    grl_media_set_id (media, "nmm:Photo");
-    bs->callback (bs->source, bs->browse_id, media, 1, bs->user_data, NULL);
-
-    media = grl_media_box_new ();
-    grl_media_set_title (media, "Video");
-    grl_media_set_id (media, "nmm:Video");
-    bs->callback (bs->source, bs->browse_id, media, 0, bs->user_data, NULL);
+    bs->callback (bs->source, bs->browse_id, NULL, 0, bs->user_data, error);
+    g_error_free (error);
     return;
-  } else {
-    sparql_select = get_select_string (source, bs->keys);
-    switch (priv->browsing_mode) {
-    case TRACKER_BROWSING_MODE_FLAT:
-      sparql_final = g_strdup_printf (TRACKER_BROWSE_FLAT_REQUEST,
-                                      sparql_select, bs->skip, bs->count);
-      break;
-
-    case TRACKER_BROWSING_MODE_INVALID:
-    case TRACKER_BROWSING_MODE_DIRECTORY:
-    default:
-      if (bs->container == NULL ||
-          grl_media_get_id (bs->container) == NULL) {
-        sparql_final = g_strdup_printf (TRACKER_BROWSE_DIRECTORY_ROOT_REQUEST,
-                                        sparql_select, bs->skip, bs->count);
-      } else {
-        sparql_final = g_strdup_printf (TRACKER_BROWSE_DIRECTORY_CONT_REQUEST,
-                                        sparql_select,
-                                        grl_media_get_id (bs->container),
-                                        bs->skip, bs->count);
-      }
-      break;
-
-    case TRACKER_BROWSING_MODE_CATEGORY:
-      sparql_final = g_strdup_printf (TRACKER_BROWSE_CATEGORY_REQUEST,
-                                      sparql_select,
-                                      grl_media_get_id (bs->container),
-                                      bs->skip, bs->count);
-      break;
-    }
   }
 
-  GRL_DEBUG ("select: '%s'", sparql_final);
-
-  os = g_slice_new0 (struct OperationSpec);
-  os->source       = bs->source;
-  os->priv         = priv;
-  os->operation_id = bs->browse_id;
-  os->keys         = grl_tracker_source_supported_keys (GRL_METADATA_SOURCE (source));
-  os->skip         = bs->skip;
-  os->count        = bs->count;
-  os->callback     = bs->callback;
-  os->user_data    = bs->user_data;
-
-  tracker_sparql_connection_query_async (priv->tracker_connection,
-                                         sparql_final,
-                                         NULL,
-                                         (GAsyncReadyCallback) tracker_query_cb,
-                                         os);
-
-  if (sparql_select != NULL)
-    g_free (sparql_select);
-  if (sparql_final != NULL)
-    g_free (sparql_final);
+  switch (priv->browsing_mode) {
+  case TRACKER_BROWSING_MODE_FLAT:
+    tracker_source_browse_flat (priv, bs);
+    break;
 
-  return;
+  case TRACKER_BROWSING_MODE_DIRECTORY:
+    tracker_source_browse_directory (priv, bs);
+    break;
 
- send_error:
-  bs->callback (bs->source, bs->browse_id, NULL, 0, bs->user_data, error);
-  g_error_free (error);
+  default:
+  case TRACKER_BROWSING_MODE_CATEGORY:
+    tracker_source_browse_category (priv, bs);
+    break;
+  }
 }
-- 
1.7.2.3



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