2005-03-24 Daniel Reed <n@ml.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       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   char *text;
382
383   text = gtk_combo_box_get_active_text (combo);
384
385   if (text)
386     {
387       tree_window_set_service (w, text);
388       g_free (text);
389     }
390 }
391
392 static void
393 window_closed_callback (GtkWidget  *window,
394                         TreeWindow *w)
395 {
396   g_assert (window == w->window);
397   w->window = NULL;
398   gtk_main_quit ();
399 }
400
401 static TreeWindow*
402 tree_window_new (DBusGConnection *connection,
403                  GtkTreeModel    *names_model)
404 {
405   TreeWindow *w;
406   GtkWidget *sw;
407   GtkWidget *vbox;
408   GtkWidget *hbox;
409   GtkWidget *combo;
410
411   /* Should use glade, blah */
412   
413   w = g_new0 (TreeWindow, 1);
414   w->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
415
416   gtk_window_set_title (GTK_WINDOW (w->window), "D-BUS Viewer");
417   gtk_window_set_default_size (GTK_WINDOW (w->window), 400, 500);
418
419   g_signal_connect (w->window, "destroy", G_CALLBACK (window_closed_callback),
420                     w);
421   gtk_container_set_border_width (GTK_CONTAINER (w->window), 6);
422
423   vbox = gtk_vbox_new (FALSE, 6);
424   gtk_container_add (GTK_CONTAINER (w->window), vbox);
425
426   /* Create names option menu */
427   if (connection)
428     {
429       GtkCellRenderer *cell;
430
431       w->connection = connection;
432       
433       w->names_model = names_model;
434
435       combo = gtk_combo_box_new_with_model (w->names_model);
436
437       cell = gtk_cell_renderer_text_new ();
438       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
439       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
440                                       "text", 0,
441                                       NULL);
442       
443       gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
444
445       g_signal_connect (combo, "changed",
446                         G_CALLBACK (name_combo_changed_callback),
447                         w);
448     }
449   
450   /* Create tree view */
451   hbox = gtk_hbox_new (FALSE, 6);
452   gtk_container_add (GTK_CONTAINER (vbox), hbox);
453   
454   sw = gtk_scrolled_window_new (NULL, NULL);
455   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
456                                   GTK_POLICY_AUTOMATIC,
457                                   GTK_POLICY_AUTOMATIC);
458
459   gtk_box_pack_start (GTK_BOX (hbox), sw, TRUE, TRUE, 0);
460
461   w->treeview = dbus_tree_view_new ();
462
463   gtk_container_add (GTK_CONTAINER (sw), w->treeview);
464
465   /* Show everything */
466   gtk_widget_show_all (w->window);
467
468   return w;
469 }
470
471 static void
472 usage (int ecode)
473 {
474   fprintf (stderr, "dbus-viewer [--version] [--help]\n");
475   exit (ecode);
476 }
477
478 static void
479 version (void)
480 {
481   printf ("D-BUS Message Bus Viewer %s\n"
482           "Copyright (C) 2003 Red Hat, Inc.\n"
483           "This is free software; see the source for copying conditions.\n"
484           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
485           VERSION);
486   exit (0);
487 }
488
489 int
490 main (int argc, char **argv)
491 {
492   const char *prev_arg;
493   int i;
494   GSList *files;
495   gboolean end_of_args;
496   GSList *tmp;
497   gboolean services;
498   DBusGConnection *connection;
499   GError *error;
500   GtkTreeModel *names_model;
501
502   g_thread_init (NULL);
503   dbus_g_thread_init ();
504   
505   bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
506   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
507   textdomain (GETTEXT_PACKAGE);
508   
509   gtk_init (&argc, &argv);
510
511   services = FALSE;
512   end_of_args = FALSE;
513   files = NULL;
514   prev_arg = NULL;
515   i = 1;
516   while (i < argc)
517     {
518       const char *arg = argv[i];
519
520       if (!end_of_args)
521         {
522           if (strcmp (arg, "--help") == 0 ||
523               strcmp (arg, "-h") == 0 ||
524               strcmp (arg, "-?") == 0)
525             usage (0);
526           else if (strcmp (arg, "--version") == 0)
527             version ();
528           else if (strcmp (arg, "--services") == 0)
529             services = TRUE;
530           else if (arg[0] == '-' &&
531                    arg[1] == '-' &&
532                    arg[2] == '\0')
533             end_of_args = TRUE;
534           else if (arg[0] == '-')
535             {
536               usage (1);
537             }
538           else
539             {
540               files = g_slist_prepend (files, (char*) arg);
541             }
542         }
543       else
544         files = g_slist_prepend (files, (char*) arg);
545       
546       prev_arg = arg;
547       
548       ++i;
549     }
550
551   if (services || files == NULL)
552     {
553       error = NULL;
554       connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
555       if (connection == NULL)
556         {
557           g_printerr ("Could not open bus connection: %s\n",
558                       error->message);
559           g_error_free (error);
560           exit (1);
561         }
562
563       g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
564
565       names_model = names_model_new (connection);
566     }
567   else
568     {
569       connection = NULL;
570       names_model = NULL;
571     }
572
573   if (files == NULL)
574     {
575       TreeWindow *w;
576
577       w = tree_window_new (connection, names_model);
578     }
579   
580   files = g_slist_reverse (files);
581
582   tmp = files;
583   while (tmp != NULL)
584     {
585       const char *filename;
586       TreeWindow *w;
587
588       filename = tmp->data;
589       
590       if (services)
591         {
592           w = tree_window_new (connection, names_model);
593           tree_window_set_service (w, filename);
594         }
595       else
596         {
597           NodeInfo *node;
598           
599           error = NULL;
600           node = description_load_from_file (filename,
601                                              &error);
602           
603           if (node == NULL)
604             {
605               g_assert (error != NULL);
606               show_error_dialog (NULL, NULL,
607                                  _("Unable to load \"%s\": %s\n"),
608                                  filename, error->message);
609               g_error_free (error);
610             }
611           else
612             {
613               w = tree_window_new (connection, names_model);
614               tree_window_set_node (w, node);
615               node_info_unref (node);
616             }
617         }
618       
619       tmp = tmp->next;
620     }
621
622   gtk_main ();
623   
624   return 0;
625 }
626