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



commit cbbb070449d7f1b74a88ca1bd44893f70d843cf4
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 a2c1720..ece73fb 100644
--- a/src/dh-book-tree.c
+++ b/src/dh-book-tree.c
@@ -868,26 +868,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]