[devhelp] KeywordModel: fix the handling of book_id only



commit a196b9181701693a7af5103c40fcb231863e7801
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jan 19 11:42:06 2018 +0100

    KeywordModel: fix the handling of book_id only

 src/dh-keyword-model.c  |   75 ++++++++++++++++++++++++++++++++--------------
 src/dh-search-context.c |   24 ++++++++++++--
 src/dh-search-context.h |    5 +++
 3 files changed, 77 insertions(+), 27 deletions(-)
---
diff --git a/src/dh-keyword-model.c b/src/dh-keyword-model.c
index 8c106e8..2963445 100644
--- a/src/dh-keyword-model.c
+++ b/src/dh-keyword-model.c
@@ -436,28 +436,9 @@ search_books (DhKeywordModel  *model,
                 GQueue *book_result;
 
                 /* Filtering by book? */
-                if (settings->book_id != NULL) {
-                        if (g_strcmp0 (settings->book_id, dh_book_get_id (book)) != 0) {
-                                continue;
-                        }
-
-                        /* Looking only for some specific book, without page or
-                         * keywords? Return only the match of the first book page.
-                         */
-                        if (settings->page_id == NULL && settings->keywords == NULL) {
-                                GNode *node;
-
-                                node = dh_book_get_tree (book);
-                                if (node != NULL) {
-                                        if (exact_link != NULL)
-                                                *exact_link = node->data;
-
-                                        g_queue_clear (ret);
-                                        g_queue_push_tail (ret, node->data);
-                                        /* FIXME: must stop the search after this. */
-                                        return ret;
-                                }
-                        }
+                if (settings->book_id != NULL &&
+                    g_strcmp0 (settings->book_id, dh_book_get_id (book)) != 0) {
+                        continue;
                 }
 
                 /* Skipping a given book? */
@@ -479,6 +460,48 @@ search_books (DhKeywordModel  *model,
 }
 
 static GQueue *
+handle_book_id_only (DhSearchContext  *search_context,
+                     DhLink          **exact_link)
+{
+        DhBookManager *book_manager;
+        GList *books;
+        GList *l;
+        GQueue *ret;
+
+        if (_dh_search_context_get_book_id (search_context) == NULL ||
+            _dh_search_context_get_page_id (search_context) != NULL ||
+            _dh_search_context_get_keywords (search_context) != NULL) {
+                return NULL;
+        }
+
+        ret = g_queue_new ();
+
+        book_manager = dh_book_manager_get_singleton ();
+        books = dh_book_manager_get_books (book_manager);
+
+        for (l = books; l != NULL; l = l->next) {
+                DhBook *book = DH_BOOK (l->data);
+                GNode *node;
+
+                if (!_dh_search_context_match_book (search_context, book))
+                        continue;
+
+                /* Return only the top-level book page. */
+                node = dh_book_get_tree (book);
+                if (node != NULL) {
+                        if (exact_link != NULL)
+                                *exact_link = node->data;
+
+                        g_queue_push_tail (ret, node->data);
+                }
+
+                break;
+        }
+
+        return ret;
+}
+
+static GQueue *
 keyword_model_search (DhKeywordModel   *model,
                       const gchar      *search_string,
                       DhSearchContext  *search_context,
@@ -491,7 +514,13 @@ keyword_model_search (DhKeywordModel   *model,
         GQueue *other_books = NULL;
         DhLink *in_book_exact_link = NULL;
         DhLink *other_books_exact_link = NULL;
-        GQueue *out = g_queue_new ();
+        GQueue *out;
+
+        out = handle_book_id_only (search_context, exact_link);
+        if (out != NULL)
+                return out;
+
+        out = g_queue_new ();
 
         settings.search_context = search_context;
         settings.keywords = _dh_search_context_get_keywords (search_context);
diff --git a/src/dh-search-context.c b/src/dh-search-context.c
index 1bb097a..b4f112f 100644
--- a/src/dh-search-context.c
+++ b/src/dh-search-context.c
@@ -323,8 +323,24 @@ _dh_search_context_get_case_sensitive (DhSearchContext *search)
         return search->case_sensitive;
 }
 
-/* This function assumes that checking that the DhBook (book_id) matches has
- * already been done (to not check the book_id for each DhLink).
+gboolean
+_dh_search_context_match_book (DhSearchContext *search,
+                               DhBook          *book)
+{
+        g_return_val_if_fail (search != NULL, FALSE);
+        g_return_val_if_fail (DH_IS_BOOK (book), FALSE);
+
+        if (!dh_book_get_enabled (book))
+                return FALSE;
+
+        if (search->book_id == NULL)
+                return TRUE;
+
+        return g_strcmp0 (search->book_id, dh_book_get_id (book)) == 0;
+}
+
+/* This function assumes that _dh_search_context_match_book() returns TRUE for
+ * the DhBook containing @link (to avoid checking the book_id for each DhLink).
  */
 gboolean
 _dh_search_context_match_link (DhSearchContext *search,
@@ -368,8 +384,8 @@ _dh_search_context_match_link (DhSearchContext *search,
 }
 
 /* This function assumes:
- * - That checking that the DhBook (book_id) matches has already been done (to
- *   not check the book_id for each DhLink).
+ * - That _dh_search_context_match_book() returns TRUE for the DhBook containing
+ *   @link (to avoid checking the book_id for each DhLink).
  * - That _dh_search_context_match_link(prefix=TRUE) returns TRUE for @link.
  */
 gboolean
diff --git a/src/dh-search-context.h b/src/dh-search-context.h
index 1fcc175..a10fce4 100644
--- a/src/dh-search-context.h
+++ b/src/dh-search-context.h
@@ -20,6 +20,7 @@
 #define DH_SEARCH_CONTEXT_H
 
 #include <glib.h>
+#include "dh-book.h"
 #include "dh-link.h"
 
 G_BEGIN_DECLS
@@ -45,6 +46,10 @@ G_GNUC_INTERNAL
 gboolean                _dh_search_context_get_case_sensitive   (DhSearchContext *search);
 
 G_GNUC_INTERNAL
+gboolean                _dh_search_context_match_book           (DhSearchContext *search,
+                                                                 DhBook          *book);
+
+G_GNUC_INTERNAL
 gboolean                _dh_search_context_match_link           (DhSearchContext *search,
                                                                  DhLink          *link,
                                                                  gboolean         prefix);


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