[gnome-photos] Skip empty collections



commit 288a52a8cb7193b7f711ed143bad4d048b3912b3
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Mar 4 16:35:57 2014 +0100

    Skip empty collections
    
    Skipping empty collections in the collection view is easy. Adding
    '?item a nie:InformationElement; nie:isPartOf ?urn.' to the WHERE
    clause of the SearchType is enough for that.
    
    However, it is not so easy when we want to have both non-empty
    collections and items. eg., in the search view. In that case we need
    to use a 'OPTIONAL {?item a nie:InformationElement; nie:isPartOf ?urn}'
    and 'COUNT (?item)', and nested SELECTs to filter out empty
    collections while still catching nmm:Photo instances. We use the
    same FILTER as the favorites view instead of the generic catch all one
    to avoid bloating the query.
    
    Fixes: https://bugzilla.gnome.org/725589

 src/photos-query-builder.c       |   45 ++++++++++++++++++++++++++++---------
 src/photos-search-type-manager.c |   27 +++++++++++-----------
 2 files changed, 48 insertions(+), 24 deletions(-)
---
diff --git a/src/photos-query-builder.c b/src/photos-query-builder.c
index eac8331..ac0d72d 100644
--- a/src/photos-query-builder.c
+++ b/src/photos-query-builder.c
@@ -86,16 +86,13 @@ photos_query_builder_optional (void)
 
 
 static gchar *
-photos_query_builder_where (PhotosSearchContextState *state, gboolean global, gint flags)
+photos_query_builder_inner_where (PhotosSearchContextState *state, gboolean global, gint flags)
 {
   gchar *col_mngr_where = NULL;
-  gchar *filter = NULL;
-  gchar *optional = NULL;
   gchar *sparql;
   gchar *srch_typ_mngr_where = NULL;
 
   srch_typ_mngr_where = photos_base_manager_get_where (state->srch_typ_mngr, flags);
-  optional = photos_query_builder_optional ();
 
   if (!(flags & PHOTOS_QUERY_FLAGS_UNFILTERED))
     {
@@ -105,19 +102,45 @@ photos_query_builder_where (PhotosSearchContextState *state, gboolean global, gi
 
           col_mngr_where = photos_base_manager_get_where (state->col_mngr, flags);
         }
-
-      filter = photos_query_builder_filter (state, flags);
     }
 
-  sparql = g_strdup_printf ("WHERE { %s %s %s %s }",
+  sparql = g_strdup_printf ("WHERE { %s %s }",
                             srch_typ_mngr_where,
+                            (col_mngr_where != NULL) ? col_mngr_where : "");
+
+  g_free (col_mngr_where);
+  g_free (srch_typ_mngr_where);
+
+  return sparql;
+}
+
+
+static gchar *
+photos_query_builder_where (PhotosSearchContextState *state, gboolean global, gint flags)
+{
+  gchar *filter = NULL;
+  gchar *optional;
+  gchar *sparql;
+  gchar *where_sparql;
+
+  where_sparql = photos_query_builder_inner_where (state, global, flags);
+  optional = photos_query_builder_optional ();
+
+  if (!(flags & PHOTOS_QUERY_FLAGS_UNFILTERED))
+    filter = photos_query_builder_filter (state, flags);
+
+  sparql = g_strdup_printf ("WHERE {{"
+                            "    SELECT ?urn rdf:type (?urn) AS ?type COUNT (?item) AS ?count %s GROUP BY 
(?urn)"
+                            "  }"
+                            "  %s %s"
+                            "}",
+                            where_sparql,
                             optional,
-                            (col_mngr_where != NULL) ? col_mngr_where : "",
                             (filter != NULL) ? filter : "");
 
-  g_free (col_mngr_where);
   g_free (filter);
-  g_free (srch_typ_mngr_where);
+  g_free (optional);
+  g_free (where_sparql);
 
   return sparql;
 }
@@ -149,7 +172,7 @@ photos_query_builder_query (PhotosSearchContextState *state,
       tail_sparql = g_strdup_printf ("ORDER BY DESC (?mtime) LIMIT %d OFFSET %d", step, offset);
     }
 
-  sparql = g_strconcat ("SELECT DISTINCT ?urn "
+  sparql = g_strconcat ("SELECT ?urn "
                         "nie:url (?urn) "
                         "nfo:fileName (?urn) "
                         "nie:mimeType (?urn) "
diff --git a/src/photos-search-type-manager.c b/src/photos-search-type-manager.c
index 28c7734..8364cd9 100644
--- a/src/photos-search-type-manager.c
+++ b/src/photos-search-type-manager.c
@@ -41,7 +41,6 @@ static gchar *
 photos_search_type_manager_get_filter (PhotosBaseManager *mngr, gint flags)
 {
   GObject *search_type;
-  const gchar *id;
   gchar *filter;
 
   if (flags & PHOTOS_QUERY_FLAGS_COLLECTIONS)
@@ -55,12 +54,7 @@ photos_search_type_manager_get_filter (PhotosBaseManager *mngr, gint flags)
   else
     search_type = photos_base_manager_get_object_by_id (mngr, PHOTOS_SEARCH_TYPE_STOCK_ALL);
 
-  id = photos_filterable_get_id (PHOTOS_FILTERABLE (search_type));
-  if (g_strcmp0 (id, PHOTOS_SEARCH_TYPE_STOCK_ALL) == 0)
-    filter = photos_base_manager_get_all_filter (mngr);
-  else
-    filter = photos_filterable_get_filter (PHOTOS_FILTERABLE (search_type));
-
+  filter = photos_filterable_get_filter (PHOTOS_FILTERABLE (search_type));
   return filter;
 }
 
@@ -90,25 +84,31 @@ photos_search_type_manager_init (PhotosSearchTypeManager *self)
 {
   PhotosSearchType *search_type;
   const gchar *item_filter;
+  gchar *all_filter;
   gchar *col_filter;
   gchar *fav_filter;
 
-  item_filter = "fn:contains (rdf:type (?urn), 'nmm#Photo')";
-  col_filter = g_strdup_printf ("(fn:starts-with (nao:identifier (?urn), '%s')"
-                                " || (?urn = nfo:image-category-screenshot))",
+  item_filter = "fn:contains (?type, 'nmm#Photo')";
+  col_filter = g_strdup_printf ("(fn:contains (?type, 'nfo#DataContainer')"
+                                " && ?count > 0"
+                                " && (fn:starts-with (nao:identifier (?urn), '%s')"
+                                "     || (?urn = nfo:image-category-screenshot)))",
                                 PHOTOS_QUERY_COLLECTIONS_IDENTIFIER);
+  all_filter = g_strdup_printf ("(%s || %s)", col_filter, item_filter);
   fav_filter = g_strdup_printf ("(%s || %s)", col_filter, item_filter);
 
   search_type = photos_search_type_new_full (PHOTOS_SEARCH_TYPE_STOCK_ALL,
                                              _("All"),
-                                             "?urn a rdfs:Resource",
-                                             "(false)"); /* unused */
+                                             "?urn a rdfs:Resource. "
+                                             "OPTIONAL {?item a nie:InformationElement; nie:isPartOf ?urn}",
+                                             all_filter);
   photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (search_type));
   g_object_unref (search_type);
 
   search_type = photos_search_type_new_full (PHOTOS_SEARCH_TYPE_STOCK_COLLECTIONS,
                                              _("Albums"),
-                                             "?urn a nfo:DataContainer",
+                                             "?urn a nfo:DataContainer. "
+                                             "?item a nie:InformationElement; nie:isPartOf ?urn.",
                                              col_filter);
   photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (search_type));
   g_object_unref (search_type);
@@ -129,6 +129,7 @@ photos_search_type_manager_init (PhotosSearchTypeManager *self)
 
   photos_base_manager_set_active_object_by_id (PHOTOS_BASE_MANAGER (self), PHOTOS_SEARCH_TYPE_STOCK_PHOTOS);
 
+  g_free (all_filter);
   g_free (col_filter);
   g_free (fav_filter);
 }


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