2005-03-30 Havoc Pennington <hp@redhat.com>
[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       const 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                                       DBUS_TYPE_INVALID);
188       
189       data = NULL;
190       if (!dbus_g_proxy_end_call (proxy, call, error, DBUS_TYPE_STRING, &data,
191                                   DBUS_TYPE_INVALID))
192         goto done;
193       
194       complete_child = description_load_from_string (data, -1, error);
195       if (complete_child == NULL)
196         {
197           g_printerr ("%s\n", data);
198           goto done;
199         }
200       
201     done:
202       if (call)
203         dbus_g_pending_call_unref (call);
204       g_object_unref (proxy);
205
206       if (complete_child == NULL)
207         return FALSE;
208
209       /* change complete_child's name to relative */
210       base_info_set_name ((BaseInfo*)complete_child,
211                           base_info_get_name ((BaseInfo*)child));
212       
213       /* Stitch in complete_child rather than child */
214       node_info_replace_node (parent, child, complete_child);
215       node_info_unref (complete_child); /* ref still held by parent */
216       
217       /* Now recurse */
218       if (!load_child_nodes (service_name, complete_child, path, error))
219         return FALSE;
220
221       /* restore path */
222       g_string_set_size (path, save_len);
223       
224       tmp = tmp->next;
225     }
226
227   return TRUE;
228 }
229
230 static gboolean
231 load_from_service_complete_idle (void *data)
232 {
233   /* Called in main thread */
234   GThread *thread = data;
235   LoadFromServiceData *d;
236   NodeInfo *node;
237
238   d = g_thread_join (thread);
239
240   node = d->node;
241   
242   if (d->error)
243     {
244       g_assert (d->node == NULL);
245       show_error_dialog (GTK_WINDOW (d->window->window), &d->window->error_dialog,
246                          _("Unable to load \"%s\": %s\n"),
247                          d->service_name, d->error->message);
248       g_error_free (d->error);
249     }
250   else
251     {
252       g_assert (d->error == NULL);
253
254       tree_window_set_node (d->window, node);
255       node_info_unref (node);
256     }
257
258   g_free (d->service_name);
259   dbus_g_connection_unref (d->connection);
260   g_free (d);
261
262   return FALSE;
263 }
264
265 static void*
266 load_from_service_thread_func (void *thread_data)
267 {  
268   DBusGProxy *root_proxy;
269   DBusGPendingCall *call;
270   const char *data;
271   NodeInfo *node;
272   GString *path;
273   LoadFromServiceData *lfsd;
274
275   lfsd = thread_data;
276
277   node = NULL;
278   call = NULL;
279   path = NULL;
280  
281 #if 1
282   /* this will end up autolaunching the service when we introspect it */
283   root_proxy = dbus_g_proxy_new_for_name (lfsd->connection,
284                                           lfsd->service_name,
285                                           "/",
286                                           DBUS_INTERFACE_INTROSPECTABLE);
287   g_assert (root_proxy != NULL);
288 #else
289   /* this will be an error if the service doesn't exist */
290   root_proxy = dbus_g_proxy_new_for_name_owner (lfsd->connection,
291                                                 lfsd->service_name,
292                                                 "/",
293                                                 DBUS_INTERFACE_INTROSPECTABLE,
294                                                 &lfsd->error);
295   if (root_proxy == NULL)
296     {
297       g_printerr ("Failed to get owner of '%s'\n", lfsd->service_name);
298       return lfsd->data;
299     }
300 #endif
301   
302   call = dbus_g_proxy_begin_call (root_proxy, "Introspect",
303                                   DBUS_TYPE_INVALID);
304
305   data = NULL;
306   if (!dbus_g_proxy_end_call (root_proxy, call, &lfsd->error, DBUS_TYPE_STRING, &data,
307                               DBUS_TYPE_INVALID))
308     {
309       g_printerr ("Failed to Introspect() %s\n",
310                   dbus_g_proxy_get_bus_name (root_proxy));
311       goto out;
312     }
313
314   node = description_load_from_string (data, -1, &lfsd->error);
315
316   /* g_print ("%s\n", data); */
317   
318   if (node == NULL)
319     goto out;
320
321   base_info_set_name ((BaseInfo*)node, "/");
322
323   path = g_string_new ("/");
324   
325   if (!load_child_nodes (dbus_g_proxy_get_bus_name (root_proxy),
326                          node, path, &lfsd->error))
327     {
328       node_info_unref (node);
329       node = NULL;
330       goto out;
331     }
332   
333  out:
334   if (call)
335     dbus_g_pending_call_unref (call);
336     
337   g_object_unref (root_proxy);
338
339   if (path)
340     g_string_free (path, TRUE);
341
342   lfsd->node = node;
343   g_assert (lfsd->node || lfsd->error);
344   g_assert (lfsd->node == NULL || lfsd->error == NULL);
345
346   /* Add idle to main thread that will join us back */
347   g_idle_add (load_from_service_complete_idle, g_thread_self ());
348   
349   return lfsd;
350 }
351
352 static void
353 start_load_from_service (TreeWindow      *w,
354                          DBusGConnection *connection,
355                          const char      *service_name)
356 {
357   LoadFromServiceData *d;
358
359   d = g_new0 (LoadFromServiceData, 1);
360   
361   d->connection = dbus_g_connection_ref (connection);
362   d->service_name = g_strdup (service_name);
363   d->error = NULL;
364   d->node = NULL;
365   d->window = w;
366   
367   g_thread_create (load_from_service_thread_func, d, TRUE, NULL);
368 }
369
370 static void
371 tree_window_set_service (TreeWindow *w,
372                          const char *service_name)
373 {
374   start_load_from_service (w, w->connection, service_name);
375 }
376
377 static void
378 name_combo_changed_callback (GtkComboBox *combo,
379                              TreeWindow  *w)
380 {
381   GtkTreeIter iter;
382
383   if (gtk_combo_box_get_active_iter (combo, &iter))
384     {
385       GtkTreeModel *model;
386       char *text;
387
388       model = gtk_combo_box_get_model (combo);
389       gtk_tree_model_get (model, &iter, 0, &text, -1);
390
391       if (text)
392         {
393           tree_window_set_service (w, text);
394           g_free (text);
395         }
396     }
397 }
398
399 static void
400 window_closed_callback (GtkWidget  *window,
401                         TreeWindow *w)
402 {
403   g_assert (window == w->window);
404   w->window = NULL;
405   gtk_main_quit ();
406 }
407
408 static TreeWindow*
409 tree_window_new (DBusGConnection *connection,
410                  GtkTreeModel    *names_model)
411 {
412   TreeWindow *w;
413   GtkWidget *sw;
414   GtkWidget *vbox;
415   GtkWidget *hbox;
416   GtkWidget *combo;
417
418   /* Should use glade, blah */
419   
420   w = g_new0 (TreeWindow, 1);
421   w->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
422
423   gtk_window_set_title (GTK_WINDOW (w->window), "D-BUS Viewer");
424   gtk_window_set_default_size (GTK_WINDOW (w->window), 400, 500);
425
426   g_signal_connect (w->window, "destroy", G_CALLBACK (window_closed_callback),
427                     w);
428   gtk_container_set_border_width (GTK_CONTAINER (w->window), 6);
429
430   vbox = gtk_vbox_new (FALSE, 6);
431   gtk_container_add (GTK_CONTAINER (w->window), vbox);
432
433   /* Create names option menu */
434   if (connection)
435     {
436       GtkCellRenderer *cell;
437
438       w->connection = connection;
439       
440       w->names_model = names_model;
441
442       combo = gtk_combo_box_new_with_model (w->names_model);
443
444       cell = gtk_cell_renderer_text_new ();
445       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
446       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
447                                       "text", 0,
448                                       NULL);
449       
450       gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
451
452       g_signal_connect (combo, "changed",
453                         G_CALLBACK (name_combo_changed_callback),
454                         w);
455     }
456   
457   /* Create tree view */
458   hbox = gtk_hbox_new (FALSE, 6);
459   gtk_container_add (GTK_CONTAINER (vbox), hbox);
460   
461   sw = gtk_scrolled_window_new (NULL, NULL);
462   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
463                                   GTK_POLICY_AUTOMATIC,
464                                   GTK_POLICY_AUTOMATIC);
465
466   gtk_box_pack_start (GTK_BOX (hbox), sw, TRUE, TRUE, 0);
467
468   w->treeview = dbus_tree_view_new ();
469
470   gtk_container_add (GTK_CONTAINER (sw), w->treeview);
471
472   /* Show everything */
473   gtk_widget_show_all (w->window);
474
475   return w;
476 }
477
478 static void
479 usage (int ecode)
480 {
481   fprintf (stderr, "dbus-viewer [--version] [--help]\n");
482   exit (ecode);
483 }
484
485 static void
486 version (void)
487 {
488   printf ("D-BUS Message Bus Viewer %s\n"
489           "Copyright (C) 2003 Red Hat, Inc.\n"
490           "This is free software; see the source for copying conditions.\n"
491           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
492           VERSION);
493   exit (0);
494 }
495
496 int
497 main (int argc, char **argv)
498 {
499   const char *prev_arg;
500   int i;
501   GSList *files;
502   gboolean end_of_args;
503   GSList *tmp;
504   gboolean services;
505   DBusGConnection *connection;
506   GError *error;
507   GtkTreeModel *names_model;
508
509   g_thread_init (NULL);
510   dbus_g_thread_init ();
511   
512   bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
513   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
514   textdomain (GETTEXT_PACKAGE);
515   
516   gtk_init (&argc, &argv);
517
518   services = FALSE;
519   end_of_args = FALSE;
520   files = NULL;
521   prev_arg = NULL;
522   i = 1;
523   while (i < argc)
524     {
525       const char *arg = argv[i];
526
527       if (!end_of_args)
528         {
529           if (strcmp (arg, "--help") == 0 ||
530               strcmp (arg, "-h") == 0 ||
531               strcmp (arg, "-?") == 0)
532             usage (0);
533           else if (strcmp (arg, "--version") == 0)
534             version ();
535           else if (strcmp (arg, "--services") == 0)
536             services = TRUE;
537           else if (arg[0] == '-' &&
538                    arg[1] == '-' &&
539                    arg[2] == '\0')
540             end_of_args = TRUE;
541           else if (arg[0] == '-')
542             {
543               usage (1);
544             }
545           else
546             {
547               files = g_slist_prepend (files, (char*) arg);
548             }
549         }
550       else
551         files = g_slist_prepend (files, (char*) arg);
552       
553       prev_arg = arg;
554       
555       ++i;
556     }
557
558   if (services || files == NULL)
559     {
560       error = NULL;
561       connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
562       if (connection == NULL)
563         {
564           g_printerr ("Could not open bus connection: %s\n",
565                       error->message);
566           g_error_free (error);
567           exit (1);
568         }
569
570       g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
571
572       names_model = names_model_new (connection);
573     }
574   else
575     {
576       connection = NULL;
577       names_model = NULL;
578     }
579
580   if (files == NULL)
581     {
582       TreeWindow *w;
583
584       w = tree_window_new (connection, names_model);
585     }
586   
587   files = g_slist_reverse (files);
588
589   tmp = files;
590   while (tmp != NULL)
591     {
592       const char *filename;
593       TreeWindow *w;
594
595       filename = tmp->data;
596       
597       if (services)
598         {
599           w = tree_window_new (connection, names_model);
600           tree_window_set_service (w, filename);
601         }
602       else
603         {
604           NodeInfo *node;
605           
606           error = NULL;
607           node = description_load_from_file (filename,
608                                              &error);
609           
610           if (node == NULL)
611             {
612               g_assert (error != NULL);
613               show_error_dialog (NULL, NULL,
614                                  _("Unable to load \"%s\": %s\n"),
615                                  filename, error->message);
616               g_error_free (error);
617             }
618           else
619             {
620               w = tree_window_new (connection, names_model);
621               tree_window_set_node (w, node);
622               node_info_unref (node);
623             }
624         }
625       
626       tmp = tmp->next;
627     }
628
629   gtk_main ();
630   
631   return 0;
632 }
633