1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-viewer.c Graphical D-BUS frontend utility
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
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>
37 show_error_dialog (GtkWindow *transient_parent,
39 const char *message_format,
47 va_start (args, message_format);
48 message = g_strdup_vprintf (message_format, args);
54 if (weak_ptr == NULL || *weak_ptr == NULL)
57 dialog = gtk_message_dialog_new (transient_parent,
58 GTK_DIALOG_DESTROY_WITH_PARENT,
63 g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
68 g_object_add_weak_pointer (G_OBJECT (dialog), (void**)weak_ptr);
71 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
73 gtk_widget_show_all (dialog);
77 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (*weak_ptr));
79 gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (*weak_ptr)->label), message);
81 gtk_window_present (GTK_WINDOW (*weak_ptr));
87 DBusGConnection *connection;
93 GtkTreeModel *names_model;
95 GtkWidget *error_dialog;
101 tree_window_set_node (TreeWindow *w,
107 name = node_info_get_name (node);
111 g_printerr (_("Assuming root node is at path /, since no absolute path is specified"));
115 path = _dbus_gutils_split_path (name);
117 dbus_tree_view_update (GTK_TREE_VIEW (w->treeview),
126 DBusGConnection *connection;
130 TreeWindow *window; /* Not touched from child thread */
131 } LoadFromServiceData;
134 load_child_nodes (const char *service_name,
139 DBusGConnection *connection;
142 connection = dbus_g_bus_get (DBUS_BUS_SESSION, error);
143 if (connection == NULL)
146 tmp = node_info_get_nodes (parent);
150 DBusGPendingCall *call;
153 NodeInfo *complete_child;
156 complete_child = NULL;
161 save_len = path->len;
164 g_string_append (path, "/");
165 g_string_append (path, base_info_get_name ((BaseInfo*)child));
167 if (*service_name == ':')
169 proxy = dbus_g_proxy_new_for_name (connection,
172 DBUS_INTERFACE_INTROSPECTABLE);
173 g_assert (proxy != NULL);
177 proxy = dbus_g_proxy_new_for_name_owner (connection,
180 DBUS_INTERFACE_INTROSPECTABLE,
186 call = dbus_g_proxy_begin_call (proxy, "Introspect",
190 if (!dbus_g_proxy_end_call (proxy, call, error, DBUS_TYPE_STRING, &data,
194 complete_child = description_load_from_string (data, -1, error);
195 if (complete_child == NULL)
197 g_printerr ("%s\n", data);
203 dbus_g_pending_call_unref (call);
204 g_object_unref (proxy);
206 if (complete_child == NULL)
209 /* change complete_child's name to relative */
210 base_info_set_name ((BaseInfo*)complete_child,
211 base_info_get_name ((BaseInfo*)child));
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 */
218 if (!load_child_nodes (service_name, complete_child, path, error))
222 g_string_set_size (path, save_len);
231 load_from_service_complete_idle (void *data)
233 /* Called in main thread */
234 GThread *thread = data;
235 LoadFromServiceData *d;
238 d = g_thread_join (thread);
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);
252 g_assert (d->error == NULL);
254 tree_window_set_node (d->window, node);
255 node_info_unref (node);
258 g_free (d->service_name);
259 dbus_g_connection_unref (d->connection);
266 load_from_service_thread_func (void *thread_data)
268 DBusGProxy *root_proxy;
269 DBusGPendingCall *call;
273 LoadFromServiceData *lfsd;
282 /* this will end up autolaunching the service when we introspect it */
283 root_proxy = dbus_g_proxy_new_for_name (lfsd->connection,
286 DBUS_INTERFACE_INTROSPECTABLE);
287 g_assert (root_proxy != NULL);
289 /* this will be an error if the service doesn't exist */
290 root_proxy = dbus_g_proxy_new_for_name_owner (lfsd->connection,
293 DBUS_INTERFACE_INTROSPECTABLE,
295 if (root_proxy == NULL)
297 g_printerr ("Failed to get owner of '%s'\n", lfsd->service_name);
302 call = dbus_g_proxy_begin_call (root_proxy, "Introspect",
306 if (!dbus_g_proxy_end_call (root_proxy, call, &lfsd->error, DBUS_TYPE_STRING, &data,
309 g_printerr ("Failed to Introspect() %s\n",
310 dbus_g_proxy_get_bus_name (root_proxy));
314 node = description_load_from_string (data, -1, &lfsd->error);
316 /* g_print ("%s\n", data); */
321 base_info_set_name ((BaseInfo*)node, "/");
323 path = g_string_new ("/");
325 if (!load_child_nodes (dbus_g_proxy_get_bus_name (root_proxy),
326 node, path, &lfsd->error))
328 node_info_unref (node);
335 dbus_g_pending_call_unref (call);
337 g_object_unref (root_proxy);
340 g_string_free (path, TRUE);
343 g_assert (lfsd->node || lfsd->error);
344 g_assert (lfsd->node == NULL || lfsd->error == NULL);
346 /* Add idle to main thread that will join us back */
347 g_idle_add (load_from_service_complete_idle, g_thread_self ());
353 start_load_from_service (TreeWindow *w,
354 DBusGConnection *connection,
355 const char *service_name)
357 LoadFromServiceData *d;
359 d = g_new0 (LoadFromServiceData, 1);
361 d->connection = dbus_g_connection_ref (connection);
362 d->service_name = g_strdup (service_name);
367 g_thread_create (load_from_service_thread_func, d, TRUE, NULL);
371 tree_window_set_service (TreeWindow *w,
372 const char *service_name)
374 start_load_from_service (w, w->connection, service_name);
378 name_combo_changed_callback (GtkComboBox *combo,
383 if (gtk_combo_box_get_active_iter (combo, &iter))
388 model = gtk_combo_box_get_model (combo);
389 gtk_tree_model_get (model, &iter, 0, &text, -1);
393 tree_window_set_service (w, text);
400 window_closed_callback (GtkWidget *window,
403 g_assert (window == w->window);
409 tree_window_new (DBusGConnection *connection,
410 GtkTreeModel *names_model)
418 /* Should use glade, blah */
420 w = g_new0 (TreeWindow, 1);
421 w->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
423 gtk_window_set_title (GTK_WINDOW (w->window), "D-BUS Viewer");
424 gtk_window_set_default_size (GTK_WINDOW (w->window), 400, 500);
426 g_signal_connect (w->window, "destroy", G_CALLBACK (window_closed_callback),
428 gtk_container_set_border_width (GTK_CONTAINER (w->window), 6);
430 vbox = gtk_vbox_new (FALSE, 6);
431 gtk_container_add (GTK_CONTAINER (w->window), vbox);
433 /* Create names option menu */
436 GtkCellRenderer *cell;
438 w->connection = connection;
440 w->names_model = names_model;
442 combo = gtk_combo_box_new_with_model (w->names_model);
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,
450 gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
452 g_signal_connect (combo, "changed",
453 G_CALLBACK (name_combo_changed_callback),
457 /* Create tree view */
458 hbox = gtk_hbox_new (FALSE, 6);
459 gtk_container_add (GTK_CONTAINER (vbox), hbox);
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);
466 gtk_box_pack_start (GTK_BOX (hbox), sw, TRUE, TRUE, 0);
468 w->treeview = dbus_tree_view_new ();
470 gtk_container_add (GTK_CONTAINER (sw), w->treeview);
472 /* Show everything */
473 gtk_widget_show_all (w->window);
481 fprintf (stderr, "dbus-viewer [--version] [--help]\n");
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",
497 main (int argc, char **argv)
499 const char *prev_arg;
502 gboolean end_of_args;
505 DBusGConnection *connection;
507 GtkTreeModel *names_model;
509 g_thread_init (NULL);
510 dbus_g_thread_init ();
512 bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
513 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
514 textdomain (GETTEXT_PACKAGE);
516 gtk_init (&argc, &argv);
525 const char *arg = argv[i];
529 if (strcmp (arg, "--help") == 0 ||
530 strcmp (arg, "-h") == 0 ||
531 strcmp (arg, "-?") == 0)
533 else if (strcmp (arg, "--version") == 0)
535 else if (strcmp (arg, "--services") == 0)
537 else if (arg[0] == '-' &&
541 else if (arg[0] == '-')
547 files = g_slist_prepend (files, (char*) arg);
551 files = g_slist_prepend (files, (char*) arg);
558 if (services || files == NULL)
561 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
562 if (connection == NULL)
564 g_printerr ("Could not open bus connection: %s\n",
566 g_error_free (error);
570 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
572 names_model = names_model_new (connection);
584 w = tree_window_new (connection, names_model);
587 files = g_slist_reverse (files);
592 const char *filename;
595 filename = tmp->data;
599 w = tree_window_new (connection, names_model);
600 tree_window_set_service (w, filename);
607 node = description_load_from_file (filename,
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);
620 w = tree_window_new (connection, names_model);
621 tree_window_set_node (w, node);
622 node_info_unref (node);