[nautilus/wip/gbsneto/search-popover: 3/3] query: improve code



commit 9ea819e732bb1b1fc853bc2e799cc0327529929a
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Oct 5 15:41:08 2015 -0300

    query: improve code
    
    NautilusQuery currently is designed much like an
    structure of data, making no use of all the object
    system that GLib provides.
    
    In order to prepare ground for the new search, make
    NautilusQuery work more like a real object than like
    a plain structure.

 libnautilus-private/nautilus-query.c               |  272 +++++++++++++++++++-
 libnautilus-private/nautilus-query.h               |   19 ++-
 libnautilus-private/nautilus-search-directory.c    |   22 +-
 .../nautilus-search-engine-simple.c                |    5 +-
 .../nautilus-search-engine-tracker.c               |    5 +-
 libnautilus-private/nautilus-search-hit.c          |    5 +-
 6 files changed, 299 insertions(+), 29 deletions(-)
---
diff --git a/libnautilus-private/nautilus-query.c b/libnautilus-private/nautilus-query.c
index 285bbfe..d282799 100644
--- a/libnautilus-private/nautilus-query.c
+++ b/libnautilus-private/nautilus-query.c
@@ -28,12 +28,17 @@
 
 #include "nautilus-file-utilities.h"
 #include "nautilus-query.h"
+#include "nautilus-private-enum-types.h"
 
 struct _NautilusQuery {
+        GObject parent;
+
        char *text;
-       char *location_uri;
+       GFile *location;
        GList *mime_types;
        gboolean show_hidden;
+        GDateTime *datetime;
+        NautilusQuerySearchType search_type;
 
        char **prepared_words;
 };
@@ -43,6 +48,17 @@ static void  nautilus_query_init             (NautilusQuery      *query);
 
 G_DEFINE_TYPE (NautilusQuery, nautilus_query, G_TYPE_OBJECT);
 
+enum {
+        PROP_0,
+        PROP_DATE,
+        PROP_LOCATION,
+        PROP_MIMETYPES,
+        PROP_SEARCH_TYPE,
+        PROP_SHOW_HIDDEN,
+        PROP_TEXT,
+        LAST_PROP
+};
+
 static void
 finalize (GObject *object)
 {
@@ -52,25 +68,188 @@ finalize (GObject *object)
 
         g_free (query->text);
         g_strfreev (query->prepared_words);
-        g_free (query->location_uri);
+        g_clear_object (&query->location);
+        g_clear_pointer (&query->datetime, g_date_time_unref);
 
        G_OBJECT_CLASS (nautilus_query_parent_class)->finalize (object);
 }
 
 static void
+nautilus_query_get_property (GObject    *object,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+        NautilusQuery *self = NAUTILUS_QUERY (object);
+
+        switch (prop_id) {
+        case PROP_DATE:
+                g_value_set_boxed (value, self->datetime);
+                break;
+
+        case PROP_LOCATION:
+                g_value_set_object (value, self->location);
+                break;
+
+        case PROP_MIMETYPES:
+                g_value_set_pointer (value, self->mime_types);
+                break;
+
+        case PROP_SEARCH_TYPE:
+                g_value_set_enum (value, self->search_type);
+                break;
+
+        case PROP_SHOW_HIDDEN:
+                g_value_set_boolean (value, self->show_hidden);
+                break;
+
+        case PROP_TEXT:
+                g_value_set_string (value, self->text);
+                break;
+
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        }
+}
+
+static void
+nautilus_query_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+        NautilusQuery *self = NAUTILUS_QUERY (object);
+
+        switch (prop_id) {
+        case PROP_DATE:
+                nautilus_query_set_date (self, g_value_get_boxed (value));
+                break;
+
+        case PROP_LOCATION:
+                nautilus_query_set_location (self, g_value_get_object (value));
+                break;
+
+        case PROP_MIMETYPES:
+                nautilus_query_set_mime_types (self, g_value_get_pointer (value));
+                break;
+
+        case PROP_SEARCH_TYPE:
+                nautilus_query_set_search_type (self, g_value_get_enum (value));
+                break;
+
+        case PROP_SHOW_HIDDEN:
+                nautilus_query_set_show_hidden_files (self, g_value_get_boolean (value));
+                break;
+
+        case PROP_TEXT:
+                nautilus_query_set_text (self, g_value_get_string (value));
+                break;
+
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        }
+}
+
+static void
 nautilus_query_class_init (NautilusQueryClass *class)
 {
        GObjectClass *gobject_class;
 
        gobject_class = G_OBJECT_CLASS (class);
        gobject_class->finalize = finalize;
+        gobject_class->get_property = nautilus_query_get_property;
+        gobject_class->set_property = nautilus_query_set_property;
+
+        /**
+         * NautilusQuery::date:
+         *
+         * The initial date of the query.
+         *
+         */
+        g_object_class_install_property (gobject_class,
+                                         PROP_DATE,
+                                         g_param_spec_boxed ("date",
+                                                             "Date of the query",
+                                                             "The initial date of the query",
+                                                             G_TYPE_DATE_TIME,
+                                                             G_PARAM_READWRITE));
+
+        /**
+         * NautilusQuery::location:
+         *
+         * The location of the query.
+         *
+         */
+        g_object_class_install_property (gobject_class,
+                                         PROP_LOCATION,
+                                         g_param_spec_object ("location",
+                                                              "Location of the query",
+                                                              "The location of the query",
+                                                              G_TYPE_FILE,
+                                                              G_PARAM_READWRITE));
+
+        /**
+         * NautilusQuery::mimetypes:
+         *
+         * MIME types the query holds.
+         *
+         */
+        g_object_class_install_property (gobject_class,
+                                         PROP_MIMETYPES,
+                                         g_param_spec_pointer ("mimetypes",
+                                                               "MIME types of the query",
+                                                               "The MIME types of the query",
+                                                               G_PARAM_READWRITE));
+
+        /**
+         * NautilusQuery::search-type:
+         *
+         * The search type of the query.
+         *
+         */
+        g_object_class_install_property (gobject_class,
+                                         PROP_SEARCH_TYPE,
+                                         g_param_spec_enum ("search-type",
+                                                            "Type of the query",
+                                                            "The type of the query",
+                                                            NAUTILUS_TYPE_QUERY_SEARCH_TYPE,
+                                                            NAUTILUS_QUERY_SEARCH_TYPE_SIMPLE,
+                                                            G_PARAM_READWRITE));
+
+        /**
+         * NautilusQuery::show-hidden:
+         *
+         * Whether the search should include hidden files.
+         *
+         */
+        g_object_class_install_property (gobject_class,
+                                         PROP_SHOW_HIDDEN,
+                                         g_param_spec_boolean ("show-hidden",
+                                                               "Show hidden files",
+                                                               "Whether the search should show hidden files",
+                                                               FALSE,
+                                                               G_PARAM_READWRITE));
+
+        /**
+         * NautilusQuery::text:
+         *
+         * The search string.
+         *
+         */
+        g_object_class_install_property (gobject_class,
+                                         PROP_TEXT,
+                                         g_param_spec_string ("text",
+                                                              "Text of the search",
+                                                              "The text string of the search",
+                                                              NULL,
+                                                              G_PARAM_READWRITE));
 }
 
 static void
 nautilus_query_init (NautilusQuery *query)
 {
         query->show_hidden = TRUE;
-        query->location_uri = nautilus_get_home_directory_uri ();
+        query->location = g_file_new_for_path (g_get_home_dir ());
 }
 
 static gchar *
@@ -139,61 +318,91 @@ nautilus_query_new (void)
 char *
 nautilus_query_get_text (NautilusQuery *query)
 {
+        g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);
+
         return g_strdup (query->text);
 }
 
 void 
 nautilus_query_set_text (NautilusQuery *query, const char *text)
 {
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
         g_free (query->text);
         query->text = g_strstrip (g_strdup (text));
 
         g_strfreev (query->prepared_words);
         query->prepared_words = NULL;
+
+        g_object_notify (G_OBJECT (query), "text");
 }
 
-char *
+GFile*
 nautilus_query_get_location (NautilusQuery *query)
 {
-        return g_strdup (query->location_uri);
+        g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);
+
+        return g_object_ref (query->location);
 }
-       
+
 void
-nautilus_query_set_location (NautilusQuery *query, const char *uri)
+nautilus_query_set_location (NautilusQuery *query,
+                             GFile         *location)
 {
-        g_free (query->location_uri);
-        query->location_uri = g_strdup (uri);
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
+        if (g_set_object (&query->location, location)) {
+                g_object_notify (G_OBJECT (query), "location");
+        }
+
 }
 
 GList *
 nautilus_query_get_mime_types (NautilusQuery *query)
 {
+        g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);
+
         return g_list_copy_deep (query->mime_types, (GCopyFunc) g_strdup, NULL);
 }
 
 void
 nautilus_query_set_mime_types (NautilusQuery *query, GList *mime_types)
 {
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
         g_list_free_full (query->mime_types, g_free);
         query->mime_types = g_list_copy_deep (mime_types, (GCopyFunc) g_strdup, NULL);
+
+        g_object_notify (G_OBJECT (query), "mimetypes");
 }
 
 void
 nautilus_query_add_mime_type (NautilusQuery *query, const char *mime_type)
 {
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
         query->mime_types = g_list_append (query->mime_types, g_strdup (mime_type));
+
+        g_object_notify (G_OBJECT (query), "mimetypes");
 }
 
 gboolean
 nautilus_query_get_show_hidden_files (NautilusQuery *query)
 {
+        g_return_val_if_fail (NAUTILUS_IS_QUERY (query), FALSE);
+
         return query->show_hidden;
 }
 
 void
 nautilus_query_set_show_hidden_files (NautilusQuery *query, gboolean show_hidden)
 {
-        query->show_hidden = show_hidden;
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
+        if (query->show_hidden != show_hidden) {
+                query->show_hidden = show_hidden;
+                g_object_notify (G_OBJECT (query), "show-hidden");
+        }
 }
 
 char *
@@ -205,3 +414,46 @@ nautilus_query_to_readable_string (NautilusQuery *query)
 
         return g_strdup_printf (_("Search for ā€œ%sā€"), query->text);
 }
+
+NautilusQuerySearchType
+nautilus_query_get_search_type (NautilusQuery *query)
+{
+        g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NAUTILUS_QUERY_SEARCH_TYPE_SIMPLE);
+
+        return query->search_type;
+}
+
+void
+nautilus_query_set_search_type (NautilusQuery           *query,
+                                NautilusQuerySearchType  type)
+{
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
+        if (query->search_type != type) {
+                query->search_type = type;
+                g_object_notify (G_OBJECT (query), "search-type");
+        }
+}
+
+GDateTime*
+nautilus_query_get_date (NautilusQuery *query)
+{
+        g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);
+
+        return query->datetime;
+}
+
+void
+nautilus_query_set_date (NautilusQuery *query,
+                         GDateTime     *date)
+{
+        g_return_if_fail (NAUTILUS_IS_QUERY (query));
+
+        if (query->datetime != date && !g_date_time_equal (query->datetime, date)) {
+                /* Assign the new date */
+                g_clear_pointer (&query->datetime, g_date_time_unref);
+                query->datetime = g_date_time_ref (date);
+
+                g_object_notify (G_OBJECT (query), "date");
+        }
+}
diff --git a/libnautilus-private/nautilus-query.h b/libnautilus-private/nautilus-query.h
index df3e473..59a8453 100644
--- a/libnautilus-private/nautilus-query.h
+++ b/libnautilus-private/nautilus-query.h
@@ -24,6 +24,12 @@
 #define NAUTILUS_QUERY_H
 
 #include <glib-object.h>
+#include <gio/gio.h>
+
+typedef enum {
+        NAUTILUS_QUERY_SEARCH_TYPE_SIMPLE,
+        NAUTILUS_QUERY_SEARCH_TYPE_FULL_TEXT
+} NautilusQuerySearchType;
 
 #define NAUTILUS_TYPE_QUERY            (nautilus_query_get_type ())
 
@@ -37,13 +43,22 @@ void           nautilus_query_set_text           (NautilusQuery *query, const ch
 gboolean       nautilus_query_get_show_hidden_files (NautilusQuery *query);
 void           nautilus_query_set_show_hidden_files (NautilusQuery *query, gboolean show_hidden);
 
-char *         nautilus_query_get_location       (NautilusQuery *query);
-void           nautilus_query_set_location       (NautilusQuery *query, const char *uri);
+GFile*         nautilus_query_get_location       (NautilusQuery *query);
+void           nautilus_query_set_location       (NautilusQuery *query,
+                                                  GFile         *location);
 
 GList *        nautilus_query_get_mime_types     (NautilusQuery *query);
 void           nautilus_query_set_mime_types     (NautilusQuery *query, GList *mime_types);
 void           nautilus_query_add_mime_type      (NautilusQuery *query, const char *mime_type);
 
+NautilusQuerySearchType nautilus_query_get_search_type (NautilusQuery *query);
+void                    nautilus_query_set_search_type (NautilusQuery           *query,
+                                                        NautilusQuerySearchType  type);
+
+GDateTime*     nautilus_query_get_date           (NautilusQuery *query);
+void           nautilus_query_set_date           (NautilusQuery *query,
+                                                  GDateTime     *date);
+
 gdouble        nautilus_query_matches_string     (NautilusQuery *query, const gchar *string);
 
 char *         nautilus_query_to_readable_string (NautilusQuery *query);
diff --git a/libnautilus-private/nautilus-search-directory.c b/libnautilus-private/nautilus-search-directory.c
index 5de492f..10b5525 100644
--- a/libnautilus-private/nautilus-search-directory.c
+++ b/libnautilus-private/nautilus-search-directory.c
@@ -871,19 +871,16 @@ nautilus_search_directory_set_base_model (NautilusSearchDirectory *search,
        }
 
        if (search->details->query != NULL) {
-               gchar *uri;
                GFile *query_location, *model_location;
                gboolean is_equal;
 
-               uri = nautilus_query_get_location (search->details->query);
-               query_location = g_file_new_for_uri (uri);
+                query_location = nautilus_query_get_location (search->details->query);
                model_location = nautilus_directory_get_location (base_model);
 
                is_equal = g_file_equal (model_location, query_location);
 
                g_object_unref (model_location);
                g_object_unref (query_location);
-               g_free (uri);
 
                if (!is_equal) {
                        return;
@@ -924,11 +921,20 @@ nautilus_search_directory_set_query (NautilusSearchDirectory *search,
                                     NautilusQuery *query)
 {
        NautilusFile *file;
+        NautilusQuery *old_query;
+
+        old_query = search->details->query;
+
+       if (g_set_object (&search->details->query, query)) {
+                /* Disconnect from the previous query changes */
+                if (old_query) {
+                        g_signal_handlers_disconnect_by_func (old_query, search_force_reload, search);
+                }
+
+                if (query) {
+                        g_signal_connect_swapped (query, "notify", G_CALLBACK (search_force_reload), search);
+                }
 
-       if (search->details->query != query) {
-               g_object_ref (query);
-               g_clear_object (&search->details->query);
-               search->details->query = query;
 
                g_object_notify_by_pspec (G_OBJECT (search), properties[PROP_QUERY]);
        }
diff --git a/libnautilus-private/nautilus-search-engine-simple.c 
b/libnautilus-private/nautilus-search-engine-simple.c
index 33180fb..f3557d6 100644
--- a/libnautilus-private/nautilus-search-engine-simple.c
+++ b/libnautilus-private/nautilus-search-engine-simple.c
@@ -93,7 +93,6 @@ search_thread_data_new (NautilusSearchEngineSimple *engine,
                        NautilusQuery *query)
 {
        SearchThreadData *data;
-       char *uri;
        GFile *location;
        
        data = g_new0 (SearchThreadData, 1);
@@ -103,9 +102,7 @@ search_thread_data_new (NautilusSearchEngineSimple *engine,
        data->visited = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
        data->query = g_object_ref (query);
 
-       uri = nautilus_query_get_location (query);
-       location = g_file_new_for_uri (uri);
-       g_free (uri);
+       location = nautilus_query_get_location (query);
 
        g_queue_push_tail (data->directories, location);
        data->mime_types = nautilus_query_get_mime_types (query);
diff --git a/libnautilus-private/nautilus-search-engine-tracker.c 
b/libnautilus-private/nautilus-search-engine-tracker.c
index 6120448..7b0f801 100644
--- a/libnautilus-private/nautilus-search-engine-tracker.c
+++ b/libnautilus-private/nautilus-search-engine-tracker.c
@@ -250,6 +250,7 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
 {
        NautilusSearchEngineTracker *tracker;
        gchar   *query_text, *search_text, *location_uri, *downcase;
+        GFile *location;
        GString *sparql;
        GList *mimetypes, *l;
        gint mime_count;
@@ -279,7 +280,8 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
        g_free (query_text);
        g_free (downcase);
 
-       location_uri = nautilus_query_get_location (tracker->details->query);
+        location = nautilus_query_get_location (tracker->details->query);
+       location_uri = location ? g_file_get_uri (location) : NULL;
        mimetypes = nautilus_query_get_mime_types (tracker->details->query);
 
        mime_count = g_list_length (mimetypes);
@@ -330,6 +332,7 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
        g_free (search_text);
        g_free (location_uri);
        g_list_free_full (mimetypes, g_free);
+        g_object_unref (location);
 }
 
 static void
diff --git a/libnautilus-private/nautilus-search-hit.c b/libnautilus-private/nautilus-search-hit.c
index a6dc16c..7870437 100644
--- a/libnautilus-private/nautilus-search-hit.c
+++ b/libnautilus-private/nautilus-search-hit.c
@@ -55,7 +55,6 @@ nautilus_search_hit_compute_scores (NautilusSearchHit *hit,
                                    NautilusQuery     *query)
 {
        GDateTime *now;
-       char *query_uri;
        GFile *query_location;
        GFile *hit_location;
        GTimeSpan m_diff = G_MAXINT64;
@@ -65,8 +64,7 @@ nautilus_search_hit_compute_scores (NautilusSearchHit *hit,
        gdouble proximity_bonus = 0.0;
        gdouble match_bonus = 0.0;
 
-       query_uri = nautilus_query_get_location (query);
-       query_location = g_file_new_for_uri (query_uri);
+       query_location = nautilus_query_get_location (query);
        hit_location = g_file_new_for_uri (hit->details->uri);
 
        if (g_file_has_prefix (hit_location, query_location)) {
@@ -122,7 +120,6 @@ nautilus_search_hit_compute_scores (NautilusSearchHit *hit,
               proximity_bonus, recent_bonus, match_bonus);
 
        g_date_time_unref (now);
-       g_free (query_uri);
        g_object_unref (query_location);
 }
 


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