1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-tree-view.c GtkTreeView for a D-BUS interface description
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-tree-view.h"
26 #include <glib/gi18n.h>
42 /* We stuff the node tree into a GtkTreeStore, rather
43 * than bothering to write a custom model
51 store = gtk_tree_store_new (MODEL_COLUMN_LAST,
54 model = GTK_TREE_MODEL (store);
59 static void set_info (GtkTreeModel *model,
64 append_child_list (GtkTreeModel *model,
71 store = GTK_TREE_STORE (model);
73 /* parent may be NULL for root */
80 gtk_tree_store_append (store, &iter, parent);
82 set_info (model, &iter, tmp->data);
89 set_info (GtkTreeModel *model,
96 store = GTK_TREE_STORE (model);
98 /* Remeber that root is NULL for "/" path */
100 /* Clear existing children */
101 while (gtk_tree_model_iter_children (model, &child, root))
102 gtk_tree_store_remove (store, &child);
104 /* Set our new value; we simply discard NodeInfo for "/" at the
109 gtk_tree_store_set (store, root,
110 MODEL_COLUMN_INFO, info,
114 /* Fill in new children */
115 switch (base_info_get_type (info))
118 append_child_list (model, root,
119 node_info_get_interfaces ((NodeInfo*)info));
120 append_child_list (model, root,
121 node_info_get_nodes ((NodeInfo*)info));
123 case INFO_TYPE_INTERFACE:
124 append_child_list (model, root,
125 interface_info_get_methods ((InterfaceInfo*)info));
126 append_child_list (model, root,
127 interface_info_get_signals ((InterfaceInfo*)info));
128 append_child_list (model, root,
129 interface_info_get_properties ((InterfaceInfo*)info));
131 case INFO_TYPE_METHOD:
132 append_child_list (model, root,
133 method_info_get_args ((MethodInfo*)info));
135 case INFO_TYPE_SIGNAL:
136 append_child_list (model, root,
137 signal_info_get_args ((SignalInfo*)info));
139 case INFO_TYPE_PROPERTY:
149 ensure_tree_node (GtkTreeModel *model,
159 store = GTK_TREE_STORE (model);
161 /* The path[0] == NULL case for path "/" can't happen since no tree
162 * node is created for that
164 g_assert (path[0] != NULL);
169 while (path[i] != NULL)
175 if (gtk_tree_model_iter_children (model, &child, parent))
177 /* Scan for the right path */
183 gtk_tree_model_get (model, &child,
184 MODEL_COLUMN_INFO, &info,
188 base_info_get_type (info) == INFO_TYPE_NODE &&
189 strcmp (base_info_get_name (info), path[i]) == 0)
196 while (gtk_tree_model_iter_next (model, &child));
203 node = node_info_new (path[i]);
205 gtk_tree_store_append (store, &child, parent);
206 gtk_tree_store_set (store, &child,
207 MODEL_COLUMN_INFO, node,
217 g_assert (parent == &prev);
222 model_update (GtkTreeModel *model,
230 set_info (model, NULL, (BaseInfo*) node);
237 /* Be sure we have the parent node */
238 ensure_tree_node (model, path, &iter);
240 /* Force the canonical relative path name on the node */
242 gtk_tree_model_get (model, &iter,
243 MODEL_COLUMN_INFO, &old,
245 base_info_set_name ((BaseInfo*) node,
246 base_info_get_name (old));
248 /* Fill in the new children */
249 set_info (model, &iter, (BaseInfo*) node);
254 info_set_func_text (GtkTreeViewColumn *tree_column,
255 GtkCellRenderer *cell,
264 gtk_tree_model_get (model, iter,
265 MODEL_COLUMN_INFO, &info,
271 str = g_string_new (NULL);
273 switch (base_info_get_type (info))
276 g_string_append (str, "<i>path</i>");
278 case INFO_TYPE_INTERFACE:
279 g_string_append (str, "<i>interface</i>");
281 case INFO_TYPE_METHOD:
282 g_string_append (str, "<i>method</i>");
284 case INFO_TYPE_SIGNAL:
285 g_string_append (str, "<i>signal</i>");
287 case INFO_TYPE_PROPERTY:
288 g_string_append (str, "<i>property</i>");
289 g_string_append_printf (str, " <b>%s</b>",
290 property_info_get_type ((PropertyInfo*)info));
293 g_string_append_printf (str, "<i>arg</i> %s",
294 arg_info_get_direction ((ArgInfo*)info) == ARG_IN ?
296 g_string_append_printf (str, " <b>%s</b>",
297 arg_info_get_type ((ArgInfo*)info));
301 g_string_append (str, " ");
302 g_string_append (str, base_info_get_name (info));
304 g_object_set (GTK_CELL_RENDERER (cell),
308 g_string_free (str, TRUE);
310 /* base_info_unref (info); */
314 dbus_tree_view_new (void)
317 GtkCellRenderer *cell_renderer;
318 GtkTreeViewColumn *column;
320 treeview = gtk_tree_view_new ();
322 column = gtk_tree_view_column_new ();
323 gtk_tree_view_column_set_title (column, _("Name"));
325 cell_renderer = gtk_cell_renderer_text_new ();
326 gtk_tree_view_column_pack_start (column,
329 gtk_tree_view_column_set_cell_data_func (column, cell_renderer,
330 info_set_func_text, NULL, NULL);
332 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview),
339 dbus_tree_view_update (GtkTreeView *view,
345 g_return_if_fail (GTK_IS_TREE_VIEW (view));
347 model = gtk_tree_view_get_model (view);
351 model = model_new ();
352 model_update (model, path, node);
353 gtk_tree_view_set_model (view, model);
354 g_object_unref (G_OBJECT (model));
358 model_update (model, path, node);
363 dbus_tree_view_clear (GtkTreeView *view)
367 g_return_if_fail (GTK_IS_TREE_VIEW (view));
369 model = gtk_tree_view_get_model (view);
372 gtk_tree_store_clear (GTK_TREE_STORE (model));