[gnome-photos/wip/foo: 6/8] Split the PhotosSearchType into "where" and "filter" properties



commit 7b8ba6a70f57af28a298620dea69d4fdd0d8756b
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Apr 9 00:38:38 2013 +0200

    Split the PhotosSearchType into "where" and "filter" properties
    
    This would help us leverage the PhotosSearchTypeManager much more
    while building queries. Currently some SPARQL snippets are duplicated
    in multiple places. eg., ?urn nao:hasTag nao:predefined-tag-favorite.
    Ideally these should be encapsulated in a PhotosSearchType.
    
    A get_where method was added to PhotosFilterable, which is implemented
    by PhotosSearchType, and a new builtin PhotosSearchType was added for
    favorites.

 src/photos-filterable.c          |   10 +++-
 src/photos-filterable.h          |    5 +-
 src/photos-query-builder.c       |  146 +++++++++++++++++++++++++++-----------
 src/photos-search-type-manager.c |   70 ++++++++++++++++---
 src/photos-search-type-manager.h |    4 +-
 src/photos-search-type.c         |   41 ++++++++++-
 src/photos-search-type.h         |    4 +-
 7 files changed, 222 insertions(+), 58 deletions(-)
---
diff --git a/src/photos-filterable.c b/src/photos-filterable.c
index 198e0e3..57ef1c6 100644
--- a/src/photos-filterable.c
+++ b/src/photos-filterable.c
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012 Red Hat, Inc.
+ * Copyright © 2012, 2013 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -45,3 +45,11 @@ photos_filterable_get_filter (PhotosFilterable *iface)
   g_return_val_if_fail (PHOTOS_IS_FILTERABLE (iface), NULL);
   return PHOTOS_FILTERABLE_GET_INTERFACE (iface)->get_filter (iface);
 }
+
+
+gchar *
+photos_filterable_get_where (PhotosFilterable *iface)
+{
+  g_return_val_if_fail (PHOTOS_IS_FILTERABLE (iface), NULL);
+  return PHOTOS_FILTERABLE_GET_INTERFACE (iface)->get_where (iface);
+}
diff --git a/src/photos-filterable.h b/src/photos-filterable.h
index 0909036..3669151 100644
--- a/src/photos-filterable.h
+++ b/src/photos-filterable.h
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012 Red Hat, Inc.
+ * Copyright © 2012, 2013 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -51,12 +51,15 @@ struct _PhotosFilterableInterface
   GTypeInterface parent_iface;
 
   gchar *(*get_filter) (PhotosFilterable *self);
+  gchar *(*get_where) (PhotosFilterable *self);
 };
 
 GType               photos_filterable_get_type           (void) G_GNUC_CONST;
 
 gchar              *photos_filterable_get_filter         (PhotosFilterable *iface);
 
+gchar              *photos_filterable_get_where          (PhotosFilterable *iface);
+
 G_END_DECLS
 
 #endif /* PHOTOS_FILTERABLE_H */
diff --git a/src/photos-query-builder.c b/src/photos-query-builder.c
index 51487b4..0c5650d 100644
--- a/src/photos-query-builder.c
+++ b/src/photos-query-builder.c
@@ -54,6 +54,24 @@ photos_query_builder_convert_path_to_uri (const gchar *path)
 }
 
 
+static void
+photos_query_builder_set_search_type (gint flags)
+{
+  PhotosBaseManager *srch_typ_mngr;
+
+  srch_typ_mngr = photos_search_type_manager_new ();
+
+  if (flags & PHOTOS_QUERY_FLAGS_COLLECTIONS)
+    photos_base_manager_set_active_object_by_id (srch_typ_mngr, PHOTOS_SEARCH_TYPE_STOCK_COLLECTIONS);
+  else if (flags & PHOTOS_QUERY_FLAGS_FAVORITES)
+    photos_base_manager_set_active_object_by_id (srch_typ_mngr, PHOTOS_SEARCH_TYPE_STOCK_FAVORITES);
+  else
+    photos_base_manager_set_active_object_by_id (srch_typ_mngr, PHOTOS_SEARCH_TYPE_STOCK_ALL);
+
+  g_object_unref (srch_typ_mngr);
+}
+
+
 static gchar *
 photos_query_builder_filter (gint flags)
 {
@@ -67,15 +85,12 @@ photos_query_builder_filter (gint flags)
   src_mngr_filter = photos_base_manager_get_filter (src_mngr);
 
   srch_typ_mngr = photos_search_type_manager_new ();
-
-  if (flags & PHOTOS_QUERY_FLAGS_COLLECTIONS)
-    photos_base_manager_set_active_object_by_id (srch_typ_mngr, PHOTOS_SEARCH_TYPE_STOCK_COLLECTIONS);
-
+  photos_query_builder_set_search_type (flags);
   srch_typ_mngr_filter = photos_base_manager_get_filter (srch_typ_mngr);
 
   sparql = g_strdup_printf ("FILTER (%s && %s)", src_mngr_filter, srch_typ_mngr_filter);
 
-  photos_base_manager_set_active_object_by_id (srch_typ_mngr, PHOTOS_SEARCH_TYPE_STOCK_ALL);
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_NONE);
   g_free (srch_typ_mngr_filter);
   g_object_unref (srch_typ_mngr);
 
@@ -95,52 +110,64 @@ photos_query_builder_optional (void)
 
 
 static gchar *
-photos_query_builder_query (gboolean global, gint flags)
+photos_query_builder_where (gboolean global, gint flags)
 {
   PhotosBaseManager *col_mngr;
-  gchar *filter;
-  gchar *optional;
+  PhotosBaseManager *srch_typ_mngr;
+  gchar *col_mngr_where = NULL;
+  gchar *filter = NULL;
+  gchar *optional = NULL;
   gchar *sparql;
-  gchar *tail_sparql = NULL;
-  gchar *tmp;
-  gchar *where_sparql;
+  gchar *srch_typ_mngr_where = NULL;
 
   col_mngr = photos_collection_manager_new ();
 
+  srch_typ_mngr = photos_search_type_manager_new ();
+  photos_query_builder_set_search_type (flags);
+  srch_typ_mngr_where = photos_search_type_manager_get_where (PHOTOS_SEARCH_TYPE_MANAGER (srch_typ_mngr));
+
   optional = photos_query_builder_optional ();
-  if (flags & PHOTOS_QUERY_FLAGS_COLLECTIONS)
-    where_sparql = g_strconcat ("WHERE { ?urn a nfo:DataContainer ; a nie:DataObject ", optional, NULL);
-  else if (flags & PHOTOS_QUERY_FLAGS_FAVORITES)
-    where_sparql = g_strconcat ("WHERE { ?urn nao:hasTag nao:predefined-tag-favorite ", optional, NULL);
-  else
-    where_sparql = g_strconcat ("WHERE { ?urn a rdfs:Resource ", optional, NULL);
-  g_free (optional);
 
   if (!(flags & PHOTOS_QUERY_FLAGS_UNFILTERED))
     {
       if (global)
         {
-          gchar *where;
-
           /* TODO: SearchCategoryManager */
 
-          where = photos_collection_manager_get_where (PHOTOS_COLLECTION_MANAGER (col_mngr));
-          tmp = where_sparql;
-          where_sparql = g_strconcat (where_sparql, where, NULL);
-          g_free (tmp);
-          g_free (where);
+          col_mngr_where = photos_collection_manager_get_where (PHOTOS_COLLECTION_MANAGER (col_mngr));
         }
 
       filter = photos_query_builder_filter (flags);
-      tmp = where_sparql;
-      where_sparql = g_strconcat (where_sparql, filter, NULL);
-      g_free (tmp);
-      g_free (filter);
     }
 
-  tmp = where_sparql;
-  where_sparql = g_strconcat (where_sparql, " }", NULL);
-  g_free (tmp);
+  sparql = g_strdup_printf ("WHERE { %s %s %s %s }",
+                            srch_typ_mngr_where,
+                            optional,
+                            (col_mngr_where != NULL) ? col_mngr_where : "",
+                            (filter != NULL) ? filter : "");
+
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_NONE);
+  g_free (col_mngr_where);
+  g_free (filter);
+  g_free (srch_typ_mngr_where);
+  g_object_unref (col_mngr);
+  g_object_unref (srch_typ_mngr);
+
+  return sparql;
+}
+
+
+static gchar *
+photos_query_builder_query (gboolean global, gint flags)
+{
+  gchar *filter;
+  gchar *optional;
+  gchar *sparql;
+  gchar *tail_sparql = NULL;
+  gchar *tmp;
+  gchar *where_sparql;
+
+  where_sparql = photos_query_builder_where (global, flags);
 
   if (global)
     {
@@ -181,8 +208,6 @@ photos_query_builder_query (gboolean global, gint flags)
   g_free (where_sparql);
   g_free (tail_sparql);
 
-  g_object_unref (col_mngr);
-
   return sparql;
 }
 
@@ -231,19 +256,31 @@ photos_query_builder_collection_icon_query (const gchar *resource)
 PhotosQuery *
 photos_query_builder_count_collections_query (void)
 {
+  PhotosBaseManager *srch_typ_mngr;
   gchar *filter;
   gchar *optional;
   gchar *sparql;
+  gchar *where;
 
   filter = photos_query_builder_filter (PHOTOS_QUERY_FLAGS_COLLECTIONS);
   optional = photos_query_builder_optional ();
-  sparql = g_strconcat ("SELECT DISTINCT COUNT(?urn) WHERE { ?urn a nfo:DataContainer ; a nie:DataObject ",
-                        optional,
+
+  srch_typ_mngr = photos_search_type_manager_new ();
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_COLLECTIONS);
+  where = photos_search_type_manager_get_where (PHOTOS_SEARCH_TYPE_MANAGER (srch_typ_mngr));
+
+  sparql = g_strconcat ("SELECT DISTINCT COUNT(?urn) WHERE { ",
+                        where, " ",
+                        optional, " ",
                         filter,
-                        " }", NULL);
+                        " }",
+                        NULL);
 
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_NONE);
+  g_free (where);
   g_free (optional);
   g_free (filter);
+  g_object_unref (srch_typ_mngr);
 
   return photos_query_new (sparql);
 }
@@ -252,19 +289,31 @@ photos_query_builder_count_collections_query (void)
 PhotosQuery *
 photos_query_builder_count_favorites_query (void)
 {
+  PhotosBaseManager *srch_typ_mngr;
   gchar *filter;
   gchar *optional;
   gchar *sparql;
+  gchar *where;
 
   filter = photos_query_builder_filter (PHOTOS_QUERY_FLAGS_FAVORITES);
   optional = photos_query_builder_optional ();
-  sparql = g_strconcat ("SELECT DISTINCT COUNT(?urn) WHERE { ?urn nao:hasTag nao:predefined-tag-favorite ",
-                        optional,
+
+  srch_typ_mngr = photos_search_type_manager_new ();
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_FAVORITES);
+  where = photos_search_type_manager_get_where (PHOTOS_SEARCH_TYPE_MANAGER (srch_typ_mngr));
+
+  sparql = g_strconcat ("SELECT DISTINCT COUNT(?urn) WHERE { ",
+                        where, " ",
+                        optional, " ",
                         filter,
-                        " }", NULL);
+                        " }",
+                        NULL);
 
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_NONE);
+  g_free (where);
   g_free (optional);
   g_free (filter);
+  g_object_unref (srch_typ_mngr);
 
   return photos_query_new (sparql);
 }
@@ -273,16 +322,31 @@ photos_query_builder_count_favorites_query (void)
 PhotosQuery *
 photos_query_builder_count_query (void)
 {
+  PhotosBaseManager *srch_typ_mngr;
   gchar *filter;
   gchar *optional;
   gchar *sparql;
+  gchar *where;
 
   filter = photos_query_builder_filter (PHOTOS_QUERY_FLAGS_NONE);
   optional = photos_query_builder_optional ();
-  sparql = g_strconcat ("SELECT DISTINCT COUNT(?urn) WHERE { ?urn a rdfs:Resource ", optional, filter, " }", 
NULL);
 
+  srch_typ_mngr = photos_search_type_manager_new ();
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_NONE);
+  where = photos_search_type_manager_get_where (PHOTOS_SEARCH_TYPE_MANAGER (srch_typ_mngr));
+
+  sparql = g_strconcat ("SELECT DISTINCT COUNT(?urn) WHERE { ",
+                        where, " ",
+                        optional, " ",
+                        filter,
+                        " }",
+                        NULL);
+
+  photos_query_builder_set_search_type (PHOTOS_QUERY_FLAGS_NONE);
+  g_free (where);
   g_free (optional);
   g_free (filter);
+  g_object_unref (srch_typ_mngr);
 
   return photos_query_new (sparql);
 }
diff --git a/src/photos-search-type-manager.c b/src/photos-search-type-manager.c
index 7d96002..5abd97e 100644
--- a/src/photos-search-type-manager.c
+++ b/src/photos-search-type-manager.c
@@ -28,6 +28,7 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
+#include "photos-filterable.h"
 #include "photos-query.h"
 #include "photos-search-type.h"
 #include "photos-search-type-manager.h"
@@ -60,27 +61,45 @@ static void
 photos_search_type_manager_init (PhotosSearchTypeManager *self)
 {
   PhotosSearchType *search_type;
-  gchar *filter;
+  const gchar *item_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')",
+                                PHOTOS_QUERY_COLLECTIONS_IDENTIFIER);
+  fav_filter = g_strdup_printf ("(%s || %s)", col_filter, item_filter);
 
   search_type = photos_search_type_new (PHOTOS_SEARCH_TYPE_STOCK_ALL, _("All"));
   photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (search_type));
   g_object_unref (search_type);
 
-  filter = g_strdup_printf ("(fn:contains (rdf:type (?urn), 'nfo#DataContainer')"
-                            " && fn:starts-with (nao:identifier (?urn), '%s'))",
-                            PHOTOS_QUERY_COLLECTIONS_IDENTIFIER);
-  search_type = photos_search_type_new_with_filter (PHOTOS_SEARCH_TYPE_STOCK_COLLECTIONS, _("Albums"), 
filter);
+  search_type = photos_search_type_new_full (PHOTOS_SEARCH_TYPE_STOCK_COLLECTIONS,
+                                             _("Albums"),
+                                             "?urn a nfo:DataContainer ; a nie:DataObject",
+                                             col_filter);
   photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (search_type));
   g_object_unref (search_type);
-  g_free (filter);
 
-  search_type = photos_search_type_new_with_filter (PHOTOS_SEARCH_TYPE_STOCK_PHOTOS,
-                                                    _("Photos"),
-                                                    "fn:contains (rdf:type (?urn), 'nmm#Photo')");
+  search_type = photos_search_type_new_full (PHOTOS_SEARCH_TYPE_STOCK_FAVORITES,
+                                             _("Favorites"),
+                                             "?urn nao:hasTag nao:predefined-tag-favorite",
+                                             fav_filter);
+  photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (search_type));
+  g_object_unref (search_type);
+
+  /* This is only meant to be used with ALL. */
+  search_type = photos_search_type_new_full (PHOTOS_SEARCH_TYPE_STOCK_PHOTOS,
+                                             _("Photos"),
+                                             "",
+                                             item_filter);
   photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (search_type));
   g_object_unref (search_type);
 
   photos_base_manager_set_active_object_by_id (PHOTOS_BASE_MANAGER (self), PHOTOS_SEARCH_TYPE_STOCK_ALL);
+
+  g_free (col_filter);
+  g_free (fav_filter);
 }
 
 
@@ -98,3 +117,36 @@ photos_search_type_manager_new (void)
 {
   return g_object_new (PHOTOS_TYPE_SEARCH_TYPE_MANAGER, NULL);
 }
+
+
+gchar *
+photos_search_type_manager_get_where (PhotosSearchTypeManager *self)
+{
+  GObject *search_type;
+  gboolean is_all;
+  const gchar *all_where = "?urn a rdfs:Resource";
+  gchar *id;
+  gchar *ret_val;
+
+  search_type = photos_base_manager_get_active_object (PHOTOS_BASE_MANAGER (self));
+  if (search_type == NULL)
+    {
+      ret_val = g_strdup (all_where);
+      goto out;
+    }
+
+  g_object_get (search_type, "id", &id, NULL);
+  is_all = (g_strcmp0 (id, PHOTOS_SEARCH_TYPE_STOCK_ALL) == 0);
+  g_free (id);
+
+  if (is_all)
+    {
+      ret_val = g_strdup (all_where);
+      goto out;
+    }
+
+  ret_val = photos_filterable_get_where (PHOTOS_FILTERABLE (search_type));
+
+ out:
+  return ret_val;
+}
diff --git a/src/photos-search-type-manager.h b/src/photos-search-type-manager.h
index 39e2c01..4b839e2 100644
--- a/src/photos-search-type-manager.h
+++ b/src/photos-search-type-manager.h
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012 Red Hat, Inc.
+ * Copyright © 2012, 2013 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -70,6 +70,8 @@ GType                     photos_search_type_manager_get_type           (void) G
 
 PhotosBaseManager        *photos_search_type_manager_new                (void);
 
+gchar                    *photos_search_type_manager_get_where          (PhotosSearchTypeManager *self);
+
 G_END_DECLS
 
 #endif /* PHOTOS_SEARCH_TYPE_MANAGER_H */
diff --git a/src/photos-search-type.c b/src/photos-search-type.c
index 9c04931..f7cb027 100644
--- a/src/photos-search-type.c
+++ b/src/photos-search-type.c
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012 Red Hat, Inc.
+ * Copyright © 2012, 2013 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -34,6 +34,7 @@ struct _PhotosSearchTypePrivate
   gchar *filter;
   gchar *id;
   gchar *name;
+  gchar *where;
 };
 
 enum
@@ -41,7 +42,8 @@ enum
   PROP_0,
   PROP_FILTER,
   PROP_ID,
-  PROP_NAME
+  PROP_NAME,
+  PROP_WHERE,
 };
 
 static void photos_filterable_interface_init (PhotosFilterableInterface *iface);
@@ -64,6 +66,18 @@ photos_search_type_get_filter (PhotosFilterable *iface)
 }
 
 
+static gchar *
+photos_search_type_get_where (PhotosFilterable *iface)
+{
+  PhotosSearchType *self = PHOTOS_SEARCH_TYPE (iface);
+  PhotosSearchTypePrivate *priv = self->priv;
+  const gchar *where;
+
+  where = (priv->where != NULL && priv->where[0] != '\0') ? priv->where : "";
+  return g_strdup (where);
+}
+
+
 static void
 photos_search_type_finalize (GObject *object)
 {
@@ -73,6 +87,7 @@ photos_search_type_finalize (GObject *object)
   g_free (priv->filter);
   g_free (priv->id);
   g_free (priv->name);
+  g_free (priv->where);
 
   G_OBJECT_CLASS (photos_search_type_parent_class)->finalize (object);
 }
@@ -116,6 +131,10 @@ photos_search_type_set_property (GObject *object, guint prop_id, const GValue *v
       priv->name = g_value_dup_string (value);
       break;
 
+    case PROP_WHERE:
+      priv->where = g_value_dup_string (value);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -163,6 +182,14 @@ photos_search_type_class_init (PhotosSearchTypeClass *class)
                                                         NULL,
                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
 
+  g_object_class_install_property (object_class,
+                                   PROP_WHERE,
+                                   g_param_spec_string ("where",
+                                                        "",
+                                                        "",
+                                                        NULL,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+
   g_type_class_add_private (class, sizeof (PhotosSearchTypePrivate));
 }
 
@@ -171,6 +198,7 @@ static void
 photos_filterable_interface_init (PhotosFilterableInterface *iface)
 {
   iface->get_filter = photos_search_type_get_filter;
+  iface->get_where = photos_search_type_get_where;
 }
 
 
@@ -182,7 +210,12 @@ photos_search_type_new (const gchar *id, const gchar *name)
 
 
 PhotosSearchType *
-photos_search_type_new_with_filter (const gchar *id, const gchar *name, const gchar *filter)
+photos_search_type_new_full (const gchar *id, const gchar *name, const gchar *where, const gchar *filter)
 {
-  return g_object_new (PHOTOS_TYPE_SEARCH_TYPE, "id", id, "name", name, "filter", filter, NULL);
+  return g_object_new (PHOTOS_TYPE_SEARCH_TYPE,
+                       "id", id,
+                       "name", name,
+                       "filter", filter,
+                       "where", where,
+                       NULL);
 }
diff --git a/src/photos-search-type.h b/src/photos-search-type.h
index 24c2102..41756e6 100644
--- a/src/photos-search-type.h
+++ b/src/photos-search-type.h
@@ -53,6 +53,7 @@ G_BEGIN_DECLS
 
 #define PHOTOS_SEARCH_TYPE_STOCK_ALL    "all"
 #define PHOTOS_SEARCH_TYPE_STOCK_COLLECTIONS "collections"
+#define PHOTOS_SEARCH_TYPE_STOCK_FAVORITES "favorites"
 #define PHOTOS_SEARCH_TYPE_STOCK_PHOTOS "photos"
 
 typedef struct _PhotosSearchType        PhotosSearchType;
@@ -74,8 +75,9 @@ GType                photos_search_type_get_type           (void) G_GNUC_CONST;
 
 PhotosSearchType    *photos_search_type_new                (const gchar *id, const gchar *name);
 
-PhotosSearchType    *photos_search_type_new_with_filter    (const gchar *id,
+PhotosSearchType    *photos_search_type_new_full           (const gchar *id,
                                                             const gchar *name,
+                                                            const gchar *where,
                                                             const gchar *filter);
 
 G_END_DECLS


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