* dbus/dbus-auth.c, dbus/dbus-connection.c, dbus/dbus-keyring.c,
[platform/upstream/dbus.git] / tools / dbus-tree-view.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-tree-view.c GtkTreeView for a D-BUS interface description
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 #include <string.h>
24 #include <config.h>
25 #include "dbus-tree-view.h"
26 #include <glib/gi18n.h>
27
28 enum
29 {
30   MODEL_COLUMN_INFO,
31
32   MODEL_COLUMN_LAST
33 };
34
35 enum
36 {
37   VIEW_COLUMN_NAME,
38
39   VIEW_COLUMN_LAST
40 };
41
42 /* We stuff the node tree into a GtkTreeStore, rather
43  * than bothering to write a custom model
44  */
45 static GtkTreeModel*
46 model_new (void)
47 {
48   GtkTreeModel *model;
49   GtkTreeStore *store;
50
51   store = gtk_tree_store_new (MODEL_COLUMN_LAST,
52                               BASE_INFO_TYPE);
53
54   model = GTK_TREE_MODEL (store);
55
56   return model;
57 }
58
59 static void set_info (GtkTreeModel *model,
60                       GtkTreeIter  *root,
61                       BaseInfo     *info);
62
63 static void
64 append_child_list (GtkTreeModel *model,
65                    GtkTreeIter  *parent,
66                    GSList       *children)
67 {
68   GSList *tmp;
69   GtkTreeStore *store;
70
71   store = GTK_TREE_STORE (model);
72
73   /* parent may be NULL for root */
74
75   tmp = children;
76   while (tmp != NULL)
77     {
78       GtkTreeIter iter;
79
80       gtk_tree_store_append (store, &iter, parent);
81
82       set_info (model, &iter, tmp->data);
83
84       tmp = tmp->next;
85     }
86 }
87
88 static void
89 set_info (GtkTreeModel *model,
90           GtkTreeIter  *root,
91           BaseInfo     *info)
92 {
93   GtkTreeStore *store;
94   GtkTreeIter child;
95
96   store = GTK_TREE_STORE (model);
97
98   /* Remeber that root is NULL for "/" path */
99
100   /* Clear existing children */
101   while (gtk_tree_model_iter_children (model, &child, root))
102     gtk_tree_store_remove (store, &child);
103
104   /* Set our new value; we simply discard NodeInfo for "/" at the
105    * moment.
106    */
107   if (root != NULL)
108     {
109       gtk_tree_store_set (store, root,
110                           MODEL_COLUMN_INFO, info,
111                           -1);
112     }
113
114   /* Fill in new children */
115   switch (base_info_get_type (info))
116     {
117     case INFO_TYPE_NODE:
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));
122       break;
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));
130       break;
131     case INFO_TYPE_METHOD:
132       append_child_list (model, root,
133                          method_info_get_args ((MethodInfo*)info));
134       break;
135     case INFO_TYPE_SIGNAL:
136       append_child_list (model, root,
137                          signal_info_get_args ((SignalInfo*)info));
138       break;
139     case INFO_TYPE_PROPERTY:
140       /* no children */
141       break;
142     case INFO_TYPE_ARG:
143       /* no children */
144       break;
145     }
146 }
147
148 static void
149 ensure_tree_node (GtkTreeModel  *model,
150                   const char   **path,
151                   GtkTreeIter   *iter)
152 {
153   GtkTreeStore *store;
154   int i;
155   GtkTreeIter child;
156   GtkTreeIter *parent;
157   GtkTreeIter prev;
158
159   store = GTK_TREE_STORE (model);
160
161   /* The path[0] == NULL case for path "/" can't happen since no tree
162    * node is created for that
163    */
164   g_assert (path[0] != NULL);
165
166   parent = NULL;
167
168   i = 0;
169   while (path[i] != NULL)
170     {
171       gboolean found;
172
173       found = FALSE;
174
175       if (gtk_tree_model_iter_children (model, &child, parent))
176         {
177           /* Scan for the right path */
178           do
179             {
180               BaseInfo *info;
181
182               info = NULL;
183               gtk_tree_model_get (model, &child,
184                                   MODEL_COLUMN_INFO, &info,
185                                   -1);
186
187               if (info != NULL &&
188                   base_info_get_type (info) == INFO_TYPE_NODE &&
189                   strcmp (base_info_get_name (info), path[i]) == 0)
190                 {
191                   /* Found it */
192                   found = TRUE;
193                   break;
194                 }
195             }
196           while (gtk_tree_model_iter_next (model, &child));
197         }
198
199       if (!found)
200         {
201           NodeInfo *node;
202
203           node = node_info_new (path[i]);
204
205           gtk_tree_store_append (store, &child, parent);
206           gtk_tree_store_set (store, &child,
207                               MODEL_COLUMN_INFO, node,
208                               -1);
209         }
210
211       prev = child;
212       parent = &prev;
213
214       ++i;
215     }
216
217   g_assert (parent == &prev);
218   *iter = prev;
219 }
220
221 static void
222 model_update (GtkTreeModel  *model,
223               const char   **path,
224               NodeInfo      *node)
225 {
226   if (path[0] == NULL)
227     {
228       /* Setting '/' */
229
230       set_info (model, NULL, (BaseInfo*) node);
231     }
232   else
233     {
234       GtkTreeIter iter;
235       BaseInfo *old;
236
237       /* Be sure we have the parent node */
238       ensure_tree_node (model, path, &iter);
239
240       /* Force the canonical relative path name on the node */
241       old = NULL;
242       gtk_tree_model_get (model, &iter,
243                           MODEL_COLUMN_INFO, &old,
244                           -1);
245       base_info_set_name ((BaseInfo*) node,
246                           base_info_get_name (old));
247
248       /* Fill in the new children */
249       set_info (model, &iter, (BaseInfo*) node);
250     }
251 }
252
253 static void
254 info_set_func_text (GtkTreeViewColumn *tree_column,
255                     GtkCellRenderer   *cell,
256                     GtkTreeModel      *model,
257                     GtkTreeIter       *iter,
258                     gpointer           data)
259 {
260   BaseInfo *info;
261   GString *str;
262
263   info = NULL;
264   gtk_tree_model_get (model, iter,
265                       MODEL_COLUMN_INFO, &info,
266                       -1);
267
268   if (info == NULL)
269     return;
270
271   str = g_string_new (NULL);
272
273   switch (base_info_get_type (info))
274     {
275     case INFO_TYPE_NODE:
276       g_string_append (str, "<i>path</i>");
277       break;
278     case INFO_TYPE_INTERFACE:
279       g_string_append (str, "<i>interface</i>");
280       break;
281     case INFO_TYPE_METHOD:
282       g_string_append (str, "<i>method</i>");
283       break;
284     case INFO_TYPE_SIGNAL:
285       g_string_append (str, "<i>signal</i>");
286       break;
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));
291       break;
292     case INFO_TYPE_ARG:
293       g_string_append_printf (str, "<i>arg</i> %s",
294                               arg_info_get_direction ((ArgInfo*)info) == ARG_IN ?
295                               "in" : "out");
296       g_string_append_printf (str, " <b>%s</b>",
297                               arg_info_get_type ((ArgInfo*)info));
298       break;
299     }
300
301   g_string_append (str, " ");
302   g_string_append (str, base_info_get_name (info));
303
304   g_object_set (GTK_CELL_RENDERER (cell),
305                 "markup", str->str,
306                 NULL);
307
308   g_string_free (str, TRUE);
309
310   /* base_info_unref (info); */
311 }
312
313 GtkWidget*
314 dbus_tree_view_new (void)
315 {
316   GtkWidget *treeview;
317   GtkCellRenderer *cell_renderer;
318   GtkTreeViewColumn *column;
319
320   treeview = gtk_tree_view_new ();
321
322   column = gtk_tree_view_column_new ();
323   gtk_tree_view_column_set_title (column, _("Name"));
324
325   cell_renderer = gtk_cell_renderer_text_new ();
326   gtk_tree_view_column_pack_start (column,
327                                    cell_renderer,
328                                    TRUE);
329   gtk_tree_view_column_set_cell_data_func (column, cell_renderer,
330                                            info_set_func_text, NULL, NULL);
331
332   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview),
333                                column);
334
335   return treeview;
336 }
337
338 void
339 dbus_tree_view_update (GtkTreeView *view,
340                        const char **path,
341                        NodeInfo    *node)
342 {
343   GtkTreeModel *model;
344
345   g_return_if_fail (GTK_IS_TREE_VIEW (view));
346
347   model = gtk_tree_view_get_model (view);
348
349   if (model == NULL)
350     {
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));
355     }
356   else
357     {
358       model_update (model, path, node);
359     }
360 }
361
362 void
363 dbus_tree_view_clear (GtkTreeView  *view)
364 {
365   GtkTreeModel *model;
366
367   g_return_if_fail (GTK_IS_TREE_VIEW (view));
368
369   model = gtk_tree_view_get_model (view);
370
371   if (model != NULL)
372     gtk_tree_store_clear (GTK_TREE_STORE (model));
373 }
374