[devhelp: 30/36] search: Enable language-based search (GNOME bug 353108)



commit a8fcc3407aff6d8e3e2c4360d98878f394a190dd
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Wed Dec 15 21:40:47 2010 +0100

    search: Enable language-based search (GNOME bug 353108)

 src/dh-keyword-model.c |   21 ++++---
 src/dh-keyword-model.h |    3 +-
 src/dh-search.c        |  172 ++++++++++++++++++++++++++++++++++++------------
 3 files changed, 143 insertions(+), 53 deletions(-)
---
diff --git a/src/dh-keyword-model.c b/src/dh-keyword-model.c
index 94b74a2..2d0d47b 100644
--- a/src/dh-keyword-model.c
+++ b/src/dh-keyword-model.c
@@ -341,6 +341,7 @@ keyword_model_search_books (DhKeywordModel  *model,
                             const gchar     *string,
                             gchar          **stringv,
                             const gchar     *book_id,
+                            const gchar     *language,
                             gboolean         case_sensitive,
                             gboolean         prefix,
                             guint            max_hits,
@@ -378,6 +379,11 @@ keyword_model_search_books (DhKeywordModel  *model,
                         continue;
                 }
 
+                if (language &&
+                    g_strcmp0 (language, dh_book_get_language (book)) != 0) {
+                        continue;
+                }
+
                 for (l = dh_book_get_keywords (book);
                      l && hits < max_hits;
                      l = g_list_next (l)) {
@@ -473,26 +479,20 @@ keyword_model_search (DhKeywordModel  *model,
                       const gchar     *string,
                       gchar          **stringv,
                       const gchar     *book_id,
+                      const gchar     *language,
                       gboolean         case_sensitive,
                       DhLink         **exact_link)
 {
         guint max_hits = MAX_HITS;
         guint n_hits;
         GList *list;
-        gint i;
-
-        g_debug ("-------------------");
-        g_debug ("string: %s", string);
-        g_debug ("book_id: %s", book_id);
-        for (i = 0; stringv[i]; i++) {
-                g_debug ("stringv[%d]: '%s'", i, stringv[i]);
-        }
 
         /* First, look for prefixed items */
         list = keyword_model_search_books (model,
                                            string,
                                            stringv,
                                            book_id,
+                                           language,
                                            case_sensitive,
                                            TRUE,
                                            max_hits,
@@ -507,6 +507,7 @@ keyword_model_search (DhKeywordModel  *model,
                                                                 string,
                                                                 stringv,
                                                                 book_id,
+                                                                language,
                                                                 case_sensitive,
                                                                 FALSE,
                                                                 max_hits - n_hits,
@@ -521,7 +522,8 @@ keyword_model_search (DhKeywordModel  *model,
 DhLink *
 dh_keyword_model_filter (DhKeywordModel *model,
                          const gchar    *string,
-                         const gchar    *book_id)
+                         const gchar    *book_id,
+                         const gchar    *language)
 {
         DhKeywordModelPriv  *priv;
         GList               *new_list = NULL;
@@ -577,6 +579,7 @@ dh_keyword_model_filter (DhKeywordModel *model,
                                                  processed_string,
                                                  stringv,
                                                  book_id,
+                                                 language,
                                                  case_sensitive,
                                                  &exact_link);
                 hits = g_list_length (new_list);
diff --git a/src/dh-keyword-model.h b/src/dh-keyword-model.h
index d2ba70d..bfbc1f5 100644
--- a/src/dh-keyword-model.h
+++ b/src/dh-keyword-model.h
@@ -62,7 +62,8 @@ void            dh_keyword_model_set_words (DhKeywordModel *model,
                                             DhBookManager  *book_manager);
 DhLink *        dh_keyword_model_filter    (DhKeywordModel *model,
                                             const gchar    *string,
-                                            const gchar    *book_id);
+                                            const gchar    *book_id,
+                                            const gchar    *language);
 
 G_END_DECLS
 
diff --git a/src/dh-search.c b/src/dh-search.c
index 2cfe456..ea6bd22 100644
--- a/src/dh-search.c
+++ b/src/dh-search.c
@@ -283,8 +283,8 @@ search_entry_key_press_event_cb (GtkEntry    *entry,
 }
 
 static void
-search_combo_set_active_id (DhSearch    *search,
-                            const gchar *book_id)
+search_combo_set_active_book_id (DhSearch    *search,
+                                 const gchar *book_id)
 {
         DhSearchPriv *priv = GET_PRIVATE (search);
         GtkTreeIter   iter;
@@ -301,12 +301,17 @@ search_combo_set_active_id (DhSearch    *search,
                 has_next = gtk_tree_model_get_iter_first (model, &iter);
                 while (has_next) {
                         gchar *id;
+                        gint row_type;
 
-                        gtk_tree_model_get (model, &iter,
-                                            COL_BOOK_ID, &id,
+                        gtk_tree_model_get (model,
+                                            &iter,
+                                            COL_BOOK_ID,  &id,
+                                            COL_ROW_TYPE, &row_type,
                                             -1);
 
-                        if (id && strcmp (book_id, id) == 0) {
+                        if (row_type == ROW_TYPE_BOOK &&
+                            id &&
+                            strcmp (book_id, id) == 0) {
                                 g_free (id);
 
                                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->book_combo),
@@ -327,36 +332,52 @@ search_combo_set_active_id (DhSearch    *search,
                                            search);
 }
 
-static gchar *
-search_combo_get_active_id (DhSearch *search)
+static void
+search_combo_get_active (DhSearch  *search,
+                         gchar    **book_id,
+                         gchar    **language)
 {
         DhSearchPriv *priv = GET_PRIVATE (search);
-        GtkTreeIter   iter;
         GtkTreeModel *model;
-        gchar        *id;
+        GtkTreeIter   iter;
+        gint          row_type;
 
         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->book_combo),
                                             &iter)) {
-                return NULL;
+                return;
         }
 
         model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->book_combo));
-
-        gtk_tree_model_get (model, &iter,
-                            COL_BOOK_ID, &id,
+        gtk_tree_model_get (model,
+                            &iter,
+                            COL_ROW_TYPE, &row_type,
                             -1);
-
-        return id;
+        switch (row_type) {
+        case ROW_TYPE_LANGUAGE:
+                gtk_tree_model_get (model,
+                                    &iter,
+                                    COL_TITLE, language,
+                                    -1);
+                *book_id = NULL;
+                break;
+        case ROW_TYPE_BOOK:
+                gtk_tree_model_get (model,
+                                    &iter,
+                                    COL_BOOK_ID, book_id,
+                                    -1);
+                *language = NULL;
+                break;
+        default:
+                *book_id = NULL;
+                *language = NULL;
+                break;
+        }
 }
 
 static void
-search_combo_changed_cb (GtkComboBox *combo,
-                         DhSearch    *search)
+search_run_idle (DhSearch *search)
 {
         DhSearchPriv *priv = GET_PRIVATE (search);
-        gchar        *id;
-
-        id = search_combo_get_active_id (search);
 
         if (!priv->idle_filter) {
                 priv->idle_filter =
@@ -365,15 +386,17 @@ search_combo_changed_cb (GtkComboBox *combo,
 }
 
 static void
+search_combo_changed_cb (GtkComboBox *combo,
+                         DhSearch    *search)
+{
+        search_run_idle (search);
+}
+
+static void
 search_entry_changed_cb (GtkEntry *entry,
                          DhSearch *search)
 {
-        DhSearchPriv *priv = GET_PRIVATE (search);
-
-        if (!priv->idle_filter) {
-                priv->idle_filter =
-                        g_idle_add ((GSourceFunc) search_filter_idle, search);
-        }
+        search_run_idle (search);
 }
 
 static void
@@ -381,14 +404,17 @@ search_entry_activated_cb (GtkEntry *entry,
                            DhSearch *search)
 {
         DhSearchPriv *priv = GET_PRIVATE (search);
-        gchar        *id;
+        gchar        *book_id;
+        gchar        *language;
         const gchar  *str;
         DhLink       *link;
 
-        id = search_combo_get_active_id (search);
+        /* Always sets book_id and language */
+        search_combo_get_active (search, &book_id, &language);
         str = gtk_entry_get_text (GTK_ENTRY (priv->entry));
-        link = dh_keyword_model_filter (priv->model, str, id);
-        g_free (id);
+        link = dh_keyword_model_filter (priv->model, str, book_id, language);
+        g_free (book_id);
+        g_free (language);
 }
 
 static void
@@ -439,13 +465,16 @@ search_filter_idle (DhSearch *search)
 {
         DhSearchPriv *priv = GET_PRIVATE (search);
         const gchar  *str;
-        gchar        *id;
+        gchar        *book_id;
+        gchar        *language;
         DhLink       *link;
 
+        /* Always sets book_id and language */
+        search_combo_get_active (search, &book_id, &language);
         str = gtk_entry_get_text (GTK_ENTRY (priv->entry));
-        id = search_combo_get_active_id (search);
-        link = dh_keyword_model_filter (priv->model, str, id);
-        g_free (id);
+        link = dh_keyword_model_filter (priv->model, str, book_id, language);
+        g_free (book_id);
+        g_free (language);
 
         priv->idle_filter = 0;
 
@@ -619,10 +648,9 @@ search_combo_find_language (DhSearch    *search,
                         return;
                 }
 
-                g_debug ("loop... %s", in_list_language_name);
-
                 /* Exact found? */
-                if (g_strcmp0 (in_list_language_name, language_name) == 0) {
+                if (exact_iter &&
+                    g_strcmp0 (in_list_language_name, language_name) == 0) {
                         *exact_iter = loop_iter;
                         *exact_found = TRUE;
                         if (prev_found)
@@ -719,6 +747,7 @@ search_combo_delete_language (DhSearch    *search,
         GtkTreeIter   prev_iter;
         GtkTreeIter   exact_iter;
         GtkTreeIter   next_iter;
+        GtkTreeIter   active_iter;
         gboolean      prev_iter_found = FALSE;
         gboolean      exact_iter_found = FALSE;
         gboolean      next_iter_found = FALSE;
@@ -726,7 +755,33 @@ search_combo_delete_language (DhSearch    *search,
 
         store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (priv->book_combo)));
 
-        /* Look for the item */
+        /* Deleting active iter? */
+        if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->book_combo),
+                                           &active_iter)) {
+                gchar *active_title;
+                gint   row_type;
+
+                gtk_tree_model_get (GTK_TREE_MODEL (store),
+                                    &active_iter,
+                                    COL_TITLE,    &active_title,
+                                    COL_ROW_TYPE, &row_type,
+                                    -1);
+                if (row_type == ROW_TYPE_LANGUAGE &&
+                    active_title &&
+                    strcmp (active_title, language_name) == 0) {
+                        /* Reset active item */
+                        g_signal_handlers_block_by_func (priv->book_combo,
+                                                         search_combo_changed_cb,
+                                                         search);
+                        gtk_combo_box_set_active (GTK_COMBO_BOX (priv->book_combo), 0);
+                        g_signal_handlers_unblock_by_func (priv->book_combo,
+                                                           search_combo_changed_cb,
+                                                           search);
+                }
+                g_free (active_title);
+        }
+
+        /* Look for the item, keeping next and prev items as well */
         search_combo_find_language (search,
                                     language_name,
                                     &exact_iter,
@@ -896,6 +951,33 @@ search_combo_delete_book (DhSearch *search,
 
         store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (priv->book_combo)));
 
+        /* Deleting active iter? */
+        if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->book_combo),
+                                           &iter)) {
+                DhBook *active_book = NULL;
+
+                gtk_tree_model_get (GTK_TREE_MODEL (store),
+                                    &iter,
+                                    COL_BOOK, &active_book,
+                                    -1);
+                if (active_book == book) {
+                        /* Reset active item */
+                        g_signal_handlers_block_by_func (priv->book_combo,
+                                                         search_combo_changed_cb,
+                                                         search);
+                        gtk_combo_box_set_active (GTK_COMBO_BOX (priv->book_combo), 0);
+                        g_signal_handlers_unblock_by_func (priv->book_combo,
+                                                           search_combo_changed_cb,
+                                                           search);
+                        /* And remove it right here */
+                        gtk_list_store_remove (store, &iter);
+                        g_object_unref (active_book);
+                        return;
+                }
+                if (active_book)
+                        g_object_unref (active_book);
+        }
+
         /* Look for the item */
         search_combo_find_book (search,
                                 book,
@@ -903,10 +985,10 @@ search_combo_delete_book (DhSearch *search,
                                 &found,
                                 NULL,
                                 NULL);
-        /* If found, delete item from the store */
-        if (found) {
-                gtk_list_store_remove (store, &iter);
-        }
+
+        g_assert (found);
+
+        gtk_list_store_remove (store, &iter);
 }
 
 static void
@@ -984,6 +1066,8 @@ search_book_created_or_enabled_cb (DhBookManager *book_manager,
 
         search_completion_add_book (search, book);
         search_combo_add_book (search, book);
+        /* Update current search if any */
+        search_run_idle (search);
 }
 
 static void
@@ -995,6 +1079,8 @@ search_book_deleted_or_disabled_cb (DhBookManager *book_manager,
 
         search_completion_delete_book (search, book);
         search_combo_delete_book (search, book);
+        /* Update current search if any */
+        search_run_idle (search);
 }
 
 static void
@@ -1182,7 +1268,7 @@ dh_search_set_search_string (DhSearch    *search,
                                            search_entry_changed_cb,
                                            search);
 
-        search_combo_set_active_id (search, book_id);
+        search_combo_set_active_book_id (search, book_id);
 
         if (!priv->idle_filter) {
                 priv->idle_filter =



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