1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2001-2003 Mikael Hallendal <micke@imendio.com>
4  * Copyright (C) 2003      CodeFactory AB
5  * Copyright (C) 2008      Imendio AB
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
23 #include "config.h"
24 #include <string.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gtk/gtk.h>
27 
28 #include "dh-marshal.h"
29 #include "dh-book-tree.h"
30 #include "dh-book.h"
31 
32 typedef struct {
33         const gchar *uri;
34         gboolean     found;
35         GtkTreeIter  iter;
36         GtkTreePath *path;
37 } FindURIData;
38 
39 typedef struct {
40         GtkTreeStore  *store;
41         DhBookManager *book_manager;
42         DhLink        *selected_link;
43 } DhBookTreePriv;
44 
45 static void dh_book_tree_class_init        (DhBookTreeClass  *klass);
46 static void dh_book_tree_init              (DhBookTree       *tree);
47 static void book_tree_add_columns          (DhBookTree       *tree);
48 static void book_tree_setup_selection      (DhBookTree       *tree);
49 static void book_tree_populate_tree        (DhBookTree       *tree);
50 static void book_tree_insert_node          (DhBookTree       *tree,
51                                             GNode            *node,
52                                             GtkTreeIter      *parent_iter);
53 static void book_tree_selection_changed_cb (GtkTreeSelection *selection,
54                                             DhBookTree       *tree);
55 
56 enum {
57         LINK_SELECTED,
58         LAST_SIGNAL
59 };
60 
61 enum {
62 	COL_TITLE,
63 	COL_LINK,
64 	COL_WEIGHT,
65 	N_COLUMNS
66 };
67 
68 G_DEFINE_TYPE (DhBookTree, dh_book_tree, GTK_TYPE_TREE_VIEW);
69 
70 #define GET_PRIVATE(instance) G_TYPE_INSTANCE_GET_PRIVATE \
71   (instance, DH_TYPE_BOOK_TREE, DhBookTreePriv);
72 
73 static gint signals[LAST_SIGNAL] = { 0 };
74 
75 static void
book_tree_finalize(GObject * object)76 book_tree_finalize (GObject *object)
77 {
78 	DhBookTreePriv *priv = GET_PRIVATE (object);
79 
80 	g_object_unref (priv->store);
81         g_object_unref (priv->book_manager);
82 
83         G_OBJECT_CLASS (dh_book_tree_parent_class)->finalize (object);
84 }
85 
86 static void
dh_book_tree_class_init(DhBookTreeClass * klass)87 dh_book_tree_class_init (DhBookTreeClass *klass)
88 {
89         GObjectClass *object_class = G_OBJECT_CLASS (klass);
90 
91 	object_class->finalize = book_tree_finalize;
92 
93         signals[LINK_SELECTED] =
94                 g_signal_new ("link-selected",
95 			      G_TYPE_FROM_CLASS (klass),
96 			      G_SIGNAL_RUN_LAST,
97 			      0,
98 			      NULL, NULL,
99 			      _dh_marshal_VOID__POINTER,
100 			      G_TYPE_NONE,
101 			      1, G_TYPE_POINTER);
102 
103 	g_type_class_add_private (klass, sizeof (DhBookTreePriv));
104 }
105 
106 static void
dh_book_tree_init(DhBookTree * tree)107 dh_book_tree_init (DhBookTree *tree)
108 {
109         DhBookTreePriv *priv;
110 
111         priv = GET_PRIVATE (tree);
112 
113 	priv->store = gtk_tree_store_new (N_COLUMNS,
114 					  G_TYPE_STRING,
115 					  G_TYPE_POINTER,
116                                           PANGO_TYPE_WEIGHT);
117 	priv->selected_link = NULL;
118 	gtk_tree_view_set_model (GTK_TREE_VIEW (tree),
119 				 GTK_TREE_MODEL (priv->store));
120 
121 	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree), FALSE);
122 
123 	book_tree_add_columns (tree);
124 
125 	book_tree_setup_selection (tree);
126 }
127 
128 static void
book_tree_add_columns(DhBookTree * tree)129 book_tree_add_columns (DhBookTree *tree)
130 {
131 	GtkCellRenderer   *cell;
132 	GtkTreeViewColumn *column;
133 
134 	column = gtk_tree_view_column_new ();
135 
136 	cell = gtk_cell_renderer_text_new ();
137 	g_object_set (cell,
138 		      "ellipsize", PANGO_ELLIPSIZE_END,
139 		      NULL);
140 	gtk_tree_view_column_pack_start (column, cell, TRUE);
141 	gtk_tree_view_column_set_attributes (column, cell,
142 					     "text", COL_TITLE,
143                                              "weight", COL_WEIGHT,
144 					     NULL);
145 
146 	gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
147 }
148 
149 static void
book_tree_setup_selection(DhBookTree * tree)150 book_tree_setup_selection (DhBookTree *tree)
151 {
152 	GtkTreeSelection *selection;
153 
154 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
155 
156 	gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
157 
158 	g_signal_connect (selection, "changed",
159 			  G_CALLBACK (book_tree_selection_changed_cb),
160 			  tree);
161 }
162 
163 static void
book_tree_populate_tree(DhBookTree * tree)164 book_tree_populate_tree (DhBookTree *tree)
165 {
166         DhBookTreePriv *priv = GET_PRIVATE (tree);
167         GList          *l;
168 
169         gtk_tree_store_clear (priv->store);
170 
171         for (l = dh_book_manager_get_books (priv->book_manager);
172              l;
173              l = g_list_next (l)) {
174                 DhBook *book = DH_BOOK (l->data);
175                 GNode  *node;
176 
177                 node = dh_book_get_tree (book);
178                 while(node) {
179                         book_tree_insert_node (tree, node, NULL);
180                         node = g_node_next_sibling (node);
181                 }
182         }
183 }
184 
185 static void
book_manager_disabled_book_list_changed_cb(DhBookManager * book_manager,gpointer user_data)186 book_manager_disabled_book_list_changed_cb (DhBookManager *book_manager,
187                                             gpointer user_data)
188 {
189         DhBookTree *tree = user_data;
190         book_tree_populate_tree (tree);
191 }
192 
193 static void
book_tree_insert_node(DhBookTree * tree,GNode * node,GtkTreeIter * parent_iter)194 book_tree_insert_node (DhBookTree  *tree,
195 		       GNode       *node,
196 		       GtkTreeIter *parent_iter)
197 
198 {
199         DhBookTreePriv *priv = GET_PRIVATE (tree);
200 	DhLink         *link;
201 	GtkTreeIter     iter;
202         PangoWeight     weight;
203 	GNode          *child;
204 
205 	link = node->data;
206 
207 	gtk_tree_store_append (priv->store, &iter, parent_iter);
208 
209 	if (dh_link_get_link_type (link) == DH_LINK_TYPE_BOOK) {
210                 weight = PANGO_WEIGHT_BOLD;
211 	} else {
212                 weight = PANGO_WEIGHT_NORMAL;
213         }
214 
215         gtk_tree_store_set (priv->store, &iter,
216                             COL_TITLE, dh_link_get_name (link),
217                             COL_LINK, link,
218                             COL_WEIGHT, weight,
219                             -1);
220 
221 	for (child = g_node_first_child (node);
222 	     child;
223 	     child = g_node_next_sibling (child)) {
224 		book_tree_insert_node (tree, child, &iter);
225 	}
226 }
227 
228 static void
book_tree_selection_changed_cb(GtkTreeSelection * selection,DhBookTree * tree)229 book_tree_selection_changed_cb (GtkTreeSelection *selection,
230 				DhBookTree       *tree)
231 {
232         DhBookTreePriv *priv = GET_PRIVATE (tree);
233         GtkTreeIter     iter;
234 
235 	if (gtk_tree_selection_get_selected (selection, NULL, &iter)) {
236                 DhLink *link;
237 
238 		gtk_tree_model_get (GTK_TREE_MODEL (priv->store),
239 				    &iter,
240                                     COL_LINK, &link,
241                                     -1);
242 		if (link != priv->selected_link) {
243 			g_signal_emit (tree, signals[LINK_SELECTED], 0, link);
244 		}
245 		priv->selected_link = link;
246 	}
247 }
248 
249 GtkWidget *
dh_book_tree_new(DhBookManager * book_manager)250 dh_book_tree_new (DhBookManager *book_manager)
251 {
252         DhBookTree     *tree;
253         DhBookTreePriv *priv;
254 	GtkTreeSelection *selection;
255 	GtkTreeIter     iter;
256 	DhLink *link;
257 
258 	tree = g_object_new (DH_TYPE_BOOK_TREE, NULL);
259         priv = GET_PRIVATE (tree);
260 
261         priv->book_manager = g_object_ref (book_manager);
262         g_signal_connect (priv->book_manager,
263                           "disabled-book-list-updated",
264                           G_CALLBACK (book_manager_disabled_book_list_changed_cb),
265                           tree);
266 
267         book_tree_populate_tree (tree);
268 
269 	/* Mark the first item as selected, or it would get automatically
270 	 * selected when the treeview will get focus; but that's not even
271 	 * enough as a selection changed would still be emitted when there
272 	 * is no change, hence the manual tracking of selection in
273 	 * selected_link.
274 	 *   https://bugzilla.gnome.org/show_bug.cgi?id=492206
275 	 */
276 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
277 	g_signal_handlers_block_by_func	(selection,
278 					 book_tree_selection_changed_cb,
279 					 tree);
280 	gtk_tree_model_get_iter_first ( GTK_TREE_MODEL (priv->store), &iter);
281 	gtk_tree_model_get (GTK_TREE_MODEL (priv->store),
282 			&iter, COL_LINK, &link, -1);
283 	priv->selected_link = link;
284 	gtk_tree_selection_select_iter (selection, &iter);
285 	g_signal_handlers_unblock_by_func (selection,
286 					   book_tree_selection_changed_cb,
287 					   tree);
288 
289         return GTK_WIDGET (tree);
290 }
291 
292 static gboolean
book_tree_find_uri_foreach(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,FindURIData * data)293 book_tree_find_uri_foreach (GtkTreeModel *model,
294 			    GtkTreePath  *path,
295 			    GtkTreeIter  *iter,
296 			    FindURIData  *data)
297 {
298 	DhLink      *link;
299         gchar       *link_uri;
300 
301 	gtk_tree_model_get (model, iter,
302 			    COL_LINK, &link,
303 			    -1);
304 
305         link_uri = dh_link_get_uri (link);
306 	if (g_str_has_prefix (data->uri, link_uri)) {
307 		data->found = TRUE;
308 		data->iter = *iter;
309 		data->path = gtk_tree_path_copy (path);
310 	}
311         g_free (link_uri);
312 
313 	return data->found;
314 }
315 
316 void
dh_book_tree_select_uri(DhBookTree * tree,const gchar * uri)317 dh_book_tree_select_uri (DhBookTree  *tree,
318 			 const gchar *uri)
319 {
320         DhBookTreePriv   *priv = GET_PRIVATE (tree);
321 	GtkTreeSelection *selection;
322 	FindURIData       data;
323 
324 	data.found = FALSE;
325 	data.uri = uri;
326 
327 	gtk_tree_model_foreach (GTK_TREE_MODEL (priv->store),
328 				(GtkTreeModelForeachFunc) book_tree_find_uri_foreach,
329 				&data);
330 
331 	if (!data.found) {
332 		return;
333 	}
334 
335 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
336 
337 	g_signal_handlers_block_by_func	(selection,
338 					 book_tree_selection_changed_cb,
339 					 tree);
340 
341 	gtk_tree_view_expand_to_path (GTK_TREE_VIEW (tree), data.path);
342 	gtk_tree_selection_select_iter (selection, &data.iter);
343 	gtk_tree_view_set_cursor (GTK_TREE_VIEW (tree), data.path, NULL, 0);
344 
345 	g_signal_handlers_unblock_by_func (selection,
346 					   book_tree_selection_changed_cb,
347 					   tree);
348 
349 	gtk_tree_path_free (data.path);
350 }
351 
352 const gchar *
dh_book_tree_get_selected_book_title(DhBookTree * tree)353 dh_book_tree_get_selected_book_title (DhBookTree *tree)
354 {
355 	GtkTreeSelection *selection;
356 	GtkTreeModel     *model;
357 	GtkTreeIter       iter;
358 	GtkTreePath      *path;
359 	DhLink           *link;
360 
361 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
362 	if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
363 		return NULL;
364 	}
365 
366 	path = gtk_tree_model_get_path (model, &iter);
367 
368 	/* Get the book node for this link. */
369 	while (1) {
370 		if (gtk_tree_path_get_depth (path) <= 1) {
371 			break;
372 		}
373 
374 		gtk_tree_path_up (path);
375 	}
376 
377 	gtk_tree_model_get_iter (model, &iter, path);
378 	gtk_tree_path_free (path);
379 
380 	gtk_tree_model_get (model, &iter,
381 			    COL_LINK, &link,
382 			    -1);
383 
384 	return dh_link_get_name (link);
385 }
386