2005-06-17 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / tools / dbus-viewer.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-viewer.c Graphical D-BUS frontend utility
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 <config.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <gtk/gtk.h>
29 #include "dbus-tree-view.h"
30 #include "dbus-names-model.h"
31 #include <glib/dbus-gparser.h>
32 #include <glib/dbus-gutils.h>
33 #include <dbus/dbus-glib.h>
34 #include <glib/gi18n.h>
35
36 static void
37 show_error_dialog (GtkWindow *transient_parent,
38                    GtkWidget **weak_ptr,
39                    const char *message_format,
40                    ...)
41 {
42   char *message;
43   va_list args;
44
45   if (message_format)
46     {
47       va_start (args, message_format);
48       message = g_strdup_vprintf (message_format, args);
49       va_end (args);
50     }
51   else
52     message = NULL;
53
54   if (weak_ptr == NULL || *weak_ptr == NULL)
55     {
56       GtkWidget *dialog;
57       dialog = gtk_message_dialog_new (transient_parent,
58                                        GTK_DIALOG_DESTROY_WITH_PARENT,
59                                        GTK_MESSAGE_ERROR,
60                                        GTK_BUTTONS_CLOSE,
61                                        message);
62
63       g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
64
65       if (weak_ptr != NULL)
66         {
67           *weak_ptr = dialog;
68           g_object_add_weak_pointer (G_OBJECT (dialog), (void**)weak_ptr);
69         }
70
71       gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
72       
73       gtk_widget_show_all (dialog);
74     }
75   else 
76     {
77       g_return_if_fail (GTK_IS_MESSAGE_DIALOG (*weak_ptr));
78
79       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (*weak_ptr)->label), message);
80
81       gtk_window_present (GTK_WINDOW (*weak_ptr));
82     }
83 }
84
85 typedef struct
86 {
87   DBusGConnection *connection;
88   
89   GtkWidget *window;
90   GtkWidget *treeview;
91   GtkWidget *name_menu;
92
93   GtkTreeModel *names_model;
94
95   GtkWidget *error_dialog;
96   
97 } TreeWindow;
98
99
100 static void
101 tree_window_set_node (TreeWindow *w,
102                       NodeInfo   *node)
103 {
104   char **path;
105   const char *name;
106
107   name = node_info_get_name (node);
108   if (name == NULL ||
109       name[0] != '/')
110     {
111       g_printerr (_("Assuming root node is at path /, since no absolute path is specified"));
112       name = "/";
113     }
114
115   path = _dbus_gutils_split_path (name);
116   
117   dbus_tree_view_update (GTK_TREE_VIEW (w->treeview),
118                          (const char**) path,
119                          node);
120
121   g_strfreev (path);
122 }
123
124 typedef struct
125 {
126   DBusGConnection *connection;
127   char *service_name;
128   GError *error;
129   NodeInfo *node;
130   TreeWindow *window; /* Not touched from child thread */
131 } LoadFromServiceData;
132
133 static gboolean
134 load_child_nodes (const char *service_name,
135                   NodeInfo   *parent,
136                   GString    *path,
137                   GError    **error)
138 {
139   DBusGConnection *connection;
140   GSList *tmp;
141   
142   connection = dbus_g_bus_get (DBUS_BUS_SESSION, error);
143   if (connection == NULL)
144     return FALSE;
145   
146   tmp = node_info_get_nodes (parent);
147   while (tmp != NULL)
148     {
149       DBusGProxy *proxy;
150       DBusGPendingCall *call;
151       char *data;
152       NodeInfo *child;
153       NodeInfo *complete_child;
154       int save_len;
155
156       complete_child = NULL;
157       call = NULL;
158       
159       child = tmp->data;
160
161       save_len = path->len;
162
163       if (save_len > 1)
164         g_string_append (path, "/");
165       g_string_append (path, base_info_get_name ((BaseInfo*)child));
166
167       if (*service_name == ':')
168         {
169           proxy = dbus_g_proxy_new_for_name (connection,
170                                              service_name,
171                                              path->str,
172                                              DBUS_INTERFACE_INTROSPECTABLE);
173           g_assert (proxy != NULL);
174         }
175       else
176         {
177           proxy = dbus_g_proxy_new_for_name_owner (connection,
178                                                    service_name,
179                                                    path->str,
180                                                    DBUS_INTERFACE_INTROSPECTABLE,
181                                                    error);
182           if (proxy == NULL)
183             goto done;
184         }
185   
186       call = dbus_g_proxy_begin_call (proxy, "Introspect",
187                                       G_TYPE_INVALID);
188       
189       data = NULL;
190       if (!dbus_g_proxy_end_call (proxy, call, error, G_TYPE_STRING, &data,
191                                   G_TYPE_INVALID))
192         {
193           call = NULL;
194           goto done;
195         }
196       call = NULL;
197       
198       complete_child = description_load_from_string (data, -1, error);
199       g_free (data);
200       if (complete_child == NULL)
201         {
202           g_printerr ("%s\n", data);
203           goto done;
204         }
205       
206     done:
207       if (call)
208         dbus_g_pending_call_unref (call);
209       g_object_unref (proxy);
210
211       if (complete_child == NULL)
212         return FALSE;
213
214       /* change complete_child's name to relative */
215       base_info_set_name ((BaseInfo*)complete_child,
216                           base_info_get_name ((BaseInfo*)child));
217       
218       /* Stitch in complete_child rather than child */
219       node_info_replace_node (parent, child, complete_child);
220       node_info_unref (complete_child); /* ref still held by parent */
221       
222       /* Now recurse */
223       if (!load_child_nodes (service_name, complete_child, path, error))
224         return FALSE;
225
226       /* restore path */
227       g_string_set_size (path, save_len);
228       
229       tmp = tmp->next;
230     }
231
232   return TRUE;
233 }
234
235 static gboolean
236 load_from_service_complete_idle (void *data)
237 {
238   /* Called in main thread */
239   GThread *thread = data;
240   LoadFromServiceData *d;
241   NodeInfo *node;
242
243   d = g_thread_join (thread);
244
245   node = d->node;
246   
247   if (d->error)
248     {
249       g_assert (d->node == NULL);
250       show_error_dialog (GTK_WINDOW (d->window->window), &d->window->error_dialog,
251                          _("Unable to load \"%s\": %s\n"),
252                          d->service_name, d->error->message);
253       g_error_free (d->error);
254     }
255   else
256     {
257       g_assert (d->error == NULL);
258
259       tree_window_set_node (d->window, node);
260       node_info_unref (node);
261     }
262
263   g_free (d->service_name);
264   dbus_g_connection_unref (d->connection);
265   g_free (d);
266
267   return FALSE;
268 }
269
270 static void*
271 load_from_service_thread_func (void *thread_data)
272 {  
273   DBusGProxy *root_proxy;
274   DBusGPendingCall *call;
275   const char *data;
276   NodeInfo *node;
277   GString *path;
278   LoadFromServiceData *lfsd;
279
280   lfsd = thread_data;
281
282   node = NULL;
283   call = NULL;
284   path = NULL;
285  
286 #if 1
287   /* this will end up autolaunching the service when we introspect it */
288   root_proxy = dbus_g_proxy_new_for_name (lfsd->connection,
289                                           lfsd->service_name,
290                                           "/",
291                                           DBUS_INTERFACE_INTROSPECTABLE);
292   g_assert (root_proxy != NULL);
293 #else
294   /* this will be an error if the service doesn't exist */
295   root_proxy = dbus_g_proxy_new_for_name_owner (lfsd->connection,
296                                                 lfsd->service_name,
297                                                 "/",
298                                                 DBUS_INTERFACE_INTROSPECTABLE,
299                                                 &lfsd->error);
300   if (root_proxy == NULL)
301     {
302       g_printerr ("Failed to get owner of '%s'\n", lfsd->service_name);
303       return lfsd->data;
304     }
305 #endif
306   
307   call = dbus_g_proxy_begin_call (root_proxy, "Introspect",
308                                   G_TYPE_INVALID);
309
310   data = NULL;
311   if (!dbus_g_proxy_end_call (root_proxy, call, &lfsd->error, G_TYPE_STRING, &data,
312                               G_TYPE_INVALID))
313     {
314       call = NULL;
315       g_printerr ("Failed to Introspect() %s\n",
316                   dbus_g_proxy_get_bus_name (root_proxy));
317       goto out;
318     }
319   call = NULL;
320
321   node = description_load_from_string (data, -1, &lfsd->error);
322
323   /* g_print ("%s\n", data); */
324   
325   if (node == NULL)
326     goto out;
327
328   base_info_set_name ((BaseInfo*)node, "/");
329
330   path = g_string_new ("/");
331   
332   if (!load_child_nodes (dbus_g_proxy_get_bus_name (root_proxy),
333                          node, path, &lfsd->error))
334     {
335       node_info_unref (node);
336       node = NULL;
337       goto out;
338     }
339   
340  out:
341   if (call)
342     dbus_g_pending_call_unref (call);
343     
344   g_object_unref (root_proxy);
345
346   if (path)
347     g_string_free (path, TRUE);
348
349   lfsd->node = node;
350   g_assert (lfsd->node || lfsd->error);
351   g_assert (lfsd->node == NULL || lfsd->error == NULL);
352
353   /* Add idle to main thread that will join us back */
354   g_idle_add (load_from_service_complete_idle, g_thread_self ());
355   
356   return lfsd;
357 }
358
359 static void
360 start_load_from_service (TreeWindow      *w,
361                          DBusGConnection *connection,
362                          const char      *service_name)
363 {
364   LoadFromServiceData *d;
365
366   d = g_new0 (LoadFromServiceData, 1);
367   
368   d->connection = dbus_g_connection_ref (connection);
369   d->service_name = g_strdup (service_name);
370   d->error = NULL;
371   d->node = NULL;
372   d->window = w;
373   
374   g_thread_create (load_from_service_thread_func, d, TRUE, NULL);
375 }
376
377 static void
378 tree_window_set_service (TreeWindow *w,
379                          const char *service_name)
380 {
381   start_load_from_service (w, w->connection, service_name);
382 }
383
384 static void
385 name_combo_changed_callback (GtkComboBox *combo,
386                              TreeWindow  *w)
387 {
388   GtkTreeIter iter;
389
390   if (gtk_combo_box_get_active_iter (combo, &iter))
391     {
392       GtkTreeModel *model;
393       char *text;
394
395       model = gtk_combo_box_get_model (combo);
396       gtk_tree_model_get (model, &iter, 0, &text, -1);
397
398       if (text)
399         {
400           tree_window_set_service (w, text);
401           g_free (text);
402         }
403     }
404 }
405
406 static void
407 window_closed_callback (GtkWidget  *window,
408                         TreeWindow *w)
409 {
410   g_assert (window == w->window);
411   w->window = NULL;
412   gtk_main_quit ();
413 }
414
415 static TreeWindow*
416 tree_window_new (DBusGConnection *connection,
417                  GtkTreeModel    *names_model)
418 {
419   TreeWindow *w;
420   GtkWidget *sw;
421   GtkWidget *vbox;
422   GtkWidget *hbox;
423   GtkWidget *combo;
424
425   /* Should use glade, blah */
426   
427   w = g_new0 (TreeWindow, 1);
428   w->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
429
430   gtk_window_set_title (GTK_WINDOW (w->window), "D-BUS Viewer");
431   gtk_window_set_default_size (GTK_WINDOW (w->window), 400, 500);
432
433   g_signal_connect (w->window, "destroy", G_CALLBACK (window_closed_callback),
434                     w);
435   gtk_container_set_border_width (GTK_CONTAINER (w->window), 6);
436
437   vbox = gtk_vbox_new (FALSE, 6);
438   gtk_container_add (GTK_CONTAINER (w->window), vbox);
439
440   /* Create names option menu */
441   if (connection)
442     {
443       GtkCellRenderer *cell;
444
445       w->connection = connection;
446       
447       w->names_model = names_model;
448
449       combo = gtk_combo_box_new_with_model (w->names_model);
450
451       cell = gtk_cell_renderer_text_new ();
452       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
453       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
454                                       "text", 0,
455                                       NULL);
456       
457       gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
458
459       g_signal_connect (combo, "changed",
460                         G_CALLBACK (name_combo_changed_callback),
461                         w);
462     }
463   
464   /* Create tree view */
465   hbox = gtk_hbox_new (FALSE, 6);
466   gtk_container_add (GTK_CONTAINER (vbox), hbox);
467   
468   sw = gtk_scrolled_window_new (NULL, NULL);
469   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
470                                   GTK_POLICY_AUTOMATIC,
471                                   GTK_POLICY_AUTOMATIC);
472
473   gtk_box_pack_start (GTK_BOX (hbox), sw, TRUE, TRUE, 0);
474
475   w->treeview = dbus_tree_view_new ();
476
477   gtk_container_add (GTK_CONTAINER (sw), w->treeview);
478
479   /* Show everything */
480   gtk_widget_show_all (w->window);
481
482   return w;
483 }
484
485 static void
486 usage (int ecode)
487 {
488   fprintf (stderr, "dbus-viewer [--version] [--help]\n");
489   exit (ecode);
490 }
491
492 static void
493 version (void)
494 {
495   printf ("D-BUS Message Bus Viewer %s\n"
496           "Copyright (C) 2003 Red Hat, Inc.\n"
497           "This is free software; see the source for copying conditions.\n"
498           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
499           VERSION);
500   exit (0);
501 }
502
503 int
504 main (int argc, char **argv)
505 {
506   const char *prev_arg;
507   int i;
508   GSList *files;
509   gboolean end_of_args;
510   GSList *tmp;
511   gboolean services;
512   DBusGConnection *connection;
513   GError *error;
514   GtkTreeModel *names_model;
515
516   g_thread_init (NULL);
517   dbus_g_thread_init ();
518   
519   bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
520   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
521   textdomain (GETTEXT_PACKAGE);
522   
523   gtk_init (&argc, &argv);
524
525   services = FALSE;
526   end_of_args = FALSE;
527   files = NULL;
528   prev_arg = NULL;
529   i = 1;
530   while (i < argc)
531     {
532       const char *arg = argv[i];
533
534       if (!end_of_args)
535         {
536           if (strcmp (arg, "--help") == 0 ||
537               strcmp (arg, "-h") == 0 ||
538               strcmp (arg, "-?") == 0)
539             usage (0);
540           else if (strcmp (arg, "--version") == 0)
541             version ();
542           else if (strcmp (arg, "--services") == 0)
543             services = TRUE;
544           else if (arg[0] == '-' &&
545                    arg[1] == '-' &&
546                    arg[2] == '\0')
547             end_of_args = TRUE;
548           else if (arg[0] == '-')
549             {
550               usage (1);
551             }
552           else
553             {
554               files = g_slist_prepend (files, (char*) arg);
555             }
556         }
557       else
558         files = g_slist_prepend (files, (char*) arg);
559       
560       prev_arg = arg;
561       
562       ++i;
563     }
564
565   if (services || files == NULL)
566     {
567       error = NULL;
568       connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
569       if (connection == NULL)
570         {
571           g_printerr ("Could not open bus connection: %s\n",
572                       error->message);
573           g_error_free (error);
574           exit (1);
575         }
576
577       g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
578
579       names_model = names_model_new (connection);
580     }
581   else
582     {
583       connection = NULL;
584       names_model = NULL;
585     }
586
587   if (files == NULL)
588     {
589       TreeWindow *w;
590
591       w = tree_window_new (connection, names_model);
592     }
593   
594   files = g_slist_reverse (files);
595
596   tmp = files;
597   while (tmp != NULL)
598     {
599       const char *filename;
600       TreeWindow *w;
601
602       filename = tmp->data;
603       
604       if (services)
605         {
606           w = tree_window_new (connection, names_model);
607           tree_window_set_service (w, filename);
608         }
609       else
610         {
611           NodeInfo *node;
612           
613           error = NULL;
614           node = description_load_from_file (filename,
615                                              &error);
616           
617           if (node == NULL)
618             {
619               g_assert (error != NULL);
620               show_error_dialog (NULL, NULL,
621                                  _("Unable to load \"%s\": %s\n"),
622                                  filename, error->message);
623               g_error_free (error);
624             }
625           else
626             {
627               w = tree_window_new (connection, names_model);
628               tree_window_set_node (w, node);
629               node_info_unref (node);
630             }
631         }
632       
633       tmp = tmp->next;
634     }
635
636   gtk_main ();
637   
638   return 0;
639 }
640