[devhelp/wip/swilmet/various-code-improvements: 2/3] book-tree: fix get_selected_book() when books are grouped by language



commit 1de6b9e226e632a15d7f24967e39cd768deb3b95
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Nov 11 20:25:37 2015 +0100

    book-tree: fix get_selected_book() when books are grouped by language
    
    dh_book_tree_get_selected_book() is used by the search hitlist to mark
    in bold the results that are part of the current book. But it didn't
    work when books are grouped by language.

 src/dh-book-tree.c |   33 +++++++++++++++++++--------------
 1 files changed, 19 insertions(+), 14 deletions(-)
---
diff --git a/src/dh-book-tree.c b/src/dh-book-tree.c
index 89be7d8..514669b 100644
--- a/src/dh-book-tree.c
+++ b/src/dh-book-tree.c
@@ -869,26 +869,31 @@ dh_book_tree_get_selected_book (DhBookTree *tree)
         GtkTreeSelection *selection;
         GtkTreeModel     *model;
         GtkTreeIter       iter;
-        GtkTreePath      *path;
-        DhLink           *link;
 
         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
-        if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
+        if (!gtk_tree_selection_get_selected (selection, &model, &iter))
                 return NULL;
-        }
 
-        path = gtk_tree_model_get_path (model, &iter);
+        /* Depending on whether books are grouped by language, the book link can
+         * be at a different depth. And it's safer to check that the returned
+         * link has the good type. So walk up the tree to find the book.
+         */
+        while (TRUE) {
+                DhLink *link;
+                GtkTreeIter parent;
 
-        /* Get the book node for this link. */
-        while (gtk_tree_path_get_depth (path) > 1)
-                gtk_tree_path_up (path);
+                gtk_tree_model_get (model, &iter,
+                                    COL_LINK, &link,
+                                    -1);
 
-        gtk_tree_model_get_iter (model, &iter, path);
-        gtk_tree_path_free (path);
+                if (dh_link_get_link_type (link) == DH_LINK_TYPE_BOOK)
+                        return link;
 
-        gtk_tree_model_get (model, &iter,
-                            COL_LINK, &link,
-                            -1);
+                if (!gtk_tree_model_iter_parent (model, &parent, &iter))
+                        break;
+
+                iter = parent;
+        }
 
-        return link;
+        g_return_val_if_reached (NULL);
 }


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