2 * Copyright (C) 2009 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
23 #include <glib/gi18n.h>
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
27 #include <libempathy/empathy-debug.h>
28 #include <libempathy/empathy-utils.h>
30 #include <libempathy-gtk/empathy-account-chooser.h>
32 #include <telepathy-glib/dbus.h>
33 #include <telepathy-glib/util.h>
34 #include <telepathy-glib/proxy-subclass.h>
36 #include "extensions/extensions.h"
38 #include "empathy-debug-dialog.h"
40 G_DEFINE_TYPE (EmpathyDebugDialog, empathy_debug_dialog,
66 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDebugDialog)
72 GtkWidget *cm_chooser;
75 TpProxySignalConnection *signal_connection;
79 } EmpathyDebugDialogPriv;
82 log_level_to_string (guint level)
86 case EMP_DEBUG_LEVEL_ERROR:
89 case EMP_DEBUG_LEVEL_CRITICAL:
92 case EMP_DEBUG_LEVEL_WARNING:
95 case EMP_DEBUG_LEVEL_MESSAGE:
98 case EMP_DEBUG_LEVEL_INFO:
101 case EMP_DEBUG_LEVEL_DEBUG:
105 g_assert_not_reached ();
111 debug_dialog_add_message (EmpathyDebugDialog *debug_dialog,
113 const gchar *domain_category,
115 const gchar *message)
117 EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
118 gchar *domain, *category;
122 if (g_strrstr (domain_category, "/"))
124 gchar **parts = g_strsplit (domain_category, "/", 2);
125 domain = g_strdup (parts[0]);
126 category = g_strdup (parts[1]);
131 domain = g_strdup (domain_category);
132 category = g_strdup ("");
135 if (g_str_has_suffix (message, "\n"))
136 string = g_strchomp (g_strdup (message));
138 string = g_strdup (message);
141 gtk_list_store_append (priv->store, &iter);
142 gtk_list_store_set (priv->store, &iter,
143 COL_TIMESTAMP, timestamp,
145 COL_CATEGORY, category,
146 COL_LEVEL, log_level_to_string (level),
156 debug_dialog_new_debug_message_cb (TpProxy *proxy,
160 const gchar *message,
162 GObject *weak_object)
164 EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
166 debug_dialog_add_message (debug_dialog, timestamp, domain, level,
171 debug_dialog_set_enabled (EmpathyDebugDialog *debug_dialog,
174 EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
177 val = tp_g_value_slice_new_boolean (enabled);
179 tp_cli_dbus_properties_call_set (priv->proxy, -1, EMP_IFACE_DEBUG,
180 "Enabled", val, NULL, NULL, NULL, NULL);
182 tp_g_value_slice_free (val);
186 debug_dialog_get_messages_cb (TpProxy *proxy,
187 const GPtrArray *messages,
190 GObject *weak_object)
192 EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
193 EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
198 DEBUG ("GetMessages failed: %s", error->message);
202 for (i = 0; i < messages->len; i++)
204 GValueArray *values = g_ptr_array_index (messages, i);
206 debug_dialog_add_message (debug_dialog,
207 g_value_get_double (g_value_array_get_nth (values, 0)),
208 g_value_get_string (g_value_array_get_nth (values, 1)),
209 g_value_get_uint (g_value_array_get_nth (values, 2)),
210 g_value_get_string (g_value_array_get_nth (values, 3)));
213 /* Connect to NewDebugMessage */
214 priv->signal_connection = emp_cli_debug_connect_to_new_debug_message (
215 proxy, debug_dialog_new_debug_message_cb, debug_dialog,
218 /* Set Enabled as appropriate */
219 debug_dialog_set_enabled (debug_dialog, !priv->paused);
223 debug_dialog_cm_chooser_changed_cb (GtkComboBox *cm_chooser,
224 EmpathyDebugDialog *debug_dialog)
226 EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
229 GError *error = NULL;
231 TpConnection *connection;
234 if (!gtk_combo_box_get_active_iter (cm_chooser, &iter))
236 DEBUG ("No CM is selected");
240 mc = empathy_mission_control_dup_singleton ();
241 dbus = tp_dbus_daemon_dup (&error);
245 DEBUG ("Failed at duping the dbus daemon: %s", error->message);
249 gtk_tree_model_get (GTK_TREE_MODEL (priv->cms), &iter,
250 COL_CM_BUS, &bus_name, -1);
251 connection = tp_connection_new (dbus, bus_name, DEBUG_OBJECT_PATH, &error);
256 DEBUG ("Getting a new TpConnection failed: %s", error->message);
257 g_error_free (error);
258 g_object_unref (dbus);
263 /* Disable debug signalling */
264 if (priv->proxy != NULL)
265 debug_dialog_set_enabled (debug_dialog, FALSE);
267 /* Disconnect from previous NewDebugMessage signal */
268 if (priv->signal_connection)
270 tp_proxy_signal_connection_disconnect (priv->signal_connection);
271 priv->signal_connection = NULL;
275 g_object_unref (priv->proxy);
277 priv->proxy = TP_PROXY (connection);
279 tp_proxy_add_interface_by_id (priv->proxy, emp_iface_quark_debug ());
281 emp_cli_debug_call_get_messages (priv->proxy, -1,
282 debug_dialog_get_messages_cb, debug_dialog, NULL, NULL);
284 g_object_unref (dbus);
289 debug_dialog_list_connection_names_cb (const gchar * const *names,
291 const gchar * const *cms,
292 const gchar * const *protocols,
295 GObject *weak_object)
297 EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
298 EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
303 DEBUG ("list_connection_names failed: %s", error->message);
307 for (i = 0; cms[i] != NULL; i++)
310 gtk_list_store_append (priv->cms, &iter);
311 gtk_list_store_set (priv->cms, &iter,
313 COL_CM_BUS, names[i],
317 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cm_chooser), 0);
320 debug_dialog_cm_chooser_changed_cb (GTK_COMBO_BOX (priv->cm_chooser), debug_dialog);
324 debug_dialog_fill_cm_chooser (EmpathyDebugDialog *debug_dialog)
327 GError *error = NULL;
329 dbus = tp_dbus_daemon_dup (&error);
333 DEBUG ("Failed to dup dbus daemon: %s", error->message);
334 g_error_free (error);
338 tp_list_connection_names (dbus, debug_dialog_list_connection_names_cb,
339 debug_dialog, NULL, NULL);
341 g_object_unref (dbus);
345 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
346 EmpathyDebugDialog *debug_dialog)
348 EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
350 priv->paused = gtk_toggle_tool_button_get_active (pause);
352 debug_dialog_set_enabled (debug_dialog, !priv->paused);
356 debug_dialog_constructor (GType type,
357 guint n_construct_params,
358 GObjectConstructParam *construct_params)
361 EmpathyDebugDialogPriv *priv;
367 GtkCellRenderer *renderer;
368 GtkWidget *scrolled_win;
370 object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
371 (type, n_construct_params, construct_params);
372 priv = GET_PRIV (object);
374 gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
375 gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
376 gtk_window_set_transient_for (GTK_WINDOW (object), priv->parent);
378 vbox = GTK_DIALOG (object)->vbox;
380 toolbar = gtk_toolbar_new ();
381 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
382 gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
383 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_SMALL_TOOLBAR);
384 gtk_widget_show (toolbar);
386 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
389 priv->cm_chooser = gtk_combo_box_new_text ();
390 priv->cms = gtk_list_store_new (NUM_COLS_CM, G_TYPE_STRING, G_TYPE_STRING);
391 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->cm_chooser),
392 GTK_TREE_MODEL (priv->cms));
393 gtk_widget_show (priv->cm_chooser);
395 item = gtk_tool_item_new ();
396 gtk_widget_show (GTK_WIDGET (item));
397 gtk_container_add (GTK_CONTAINER (item), priv->cm_chooser);
398 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
399 debug_dialog_fill_cm_chooser (EMPATHY_DEBUG_DIALOG (object));
400 g_signal_connect (priv->cm_chooser, "changed",
401 G_CALLBACK (debug_dialog_cm_chooser_changed_cb), object);
402 gtk_widget_show (GTK_WIDGET (priv->cm_chooser));
404 item = gtk_separator_tool_item_new ();
405 gtk_widget_show (GTK_WIDGET (item));
406 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
409 item = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
410 gtk_widget_show (GTK_WIDGET (item));
411 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
412 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
415 item = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
416 gtk_widget_show (GTK_WIDGET (item));
417 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
418 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
420 item = gtk_separator_tool_item_new ();
421 gtk_widget_show (GTK_WIDGET (item));
422 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
425 priv->paused = FALSE;
426 image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE, GTK_ICON_SIZE_MENU);
427 gtk_widget_show (image);
428 item = gtk_toggle_tool_button_new ();
429 gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (item), priv->paused);
430 g_signal_connect (item, "toggled", G_CALLBACK (debug_dialog_pause_toggled_cb),
432 gtk_widget_show (GTK_WIDGET (item));
433 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
434 gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), _("Pause"));
435 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
436 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
438 item = gtk_separator_tool_item_new ();
439 gtk_widget_show (GTK_WIDGET (item));
440 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
443 item = gtk_tool_item_new ();
444 gtk_widget_show (GTK_WIDGET (item));
445 label = gtk_label_new (_("Level "));
446 gtk_widget_show (label);
447 gtk_container_add (GTK_CONTAINER (item), label);
448 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
450 priv->filter = gtk_combo_box_new_text ();
451 gtk_widget_show (priv->filter);
453 item = gtk_tool_item_new ();
454 gtk_widget_show (GTK_WIDGET (item));
455 gtk_container_add (GTK_CONTAINER (item), priv->filter);
456 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
458 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("All"));
459 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Debug"));
460 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Info"));
461 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Message"));
462 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Warning"));
463 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Critical"));
464 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Error"));
466 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->filter), 0);
467 gtk_widget_show (GTK_WIDGET (priv->filter));
470 priv->view = gtk_tree_view_new ();
471 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
473 renderer = gtk_cell_renderer_text_new ();
475 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
476 -1, _("Time"), renderer, "text", COL_TIMESTAMP, NULL);
477 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
478 -1, _("Domain"), renderer, "text", COL_DOMAIN, NULL);
479 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
480 -1, _("Category"), renderer, "text", COL_CATEGORY, NULL);
481 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
482 -1, _("Level"), renderer, "text", COL_LEVEL, NULL);
483 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
484 -1, _("Message"), renderer, "text", COL_MESSAGE, NULL);
486 priv->store = gtk_list_store_new (NUM_COLS, G_TYPE_DOUBLE,
487 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
488 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view),
489 GTK_TREE_MODEL (priv->store));
491 /* Scrolled window */
492 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
493 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
494 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
496 gtk_widget_show (priv->view);
497 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->view);
499 gtk_widget_show (scrolled_win);
500 gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
502 gtk_widget_show (GTK_WIDGET (object));
508 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
510 EmpathyDebugDialogPriv *priv =
511 G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
512 EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
514 empathy_debug_dialog->priv = priv;
516 priv->dispose_run = FALSE;
520 debug_dialog_set_property (GObject *object,
525 EmpathyDebugDialogPriv *priv = GET_PRIV (object);
530 priv->parent = GTK_WINDOW (g_value_dup_object (value));
533 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
539 debug_dialog_get_property (GObject *object,
544 EmpathyDebugDialogPriv *priv = GET_PRIV (object);
549 g_value_set_object (value, priv->parent);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 debug_dialog_dispose (GObject *object)
560 EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
561 EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
563 if (priv->dispose_run)
566 priv->dispose_run = TRUE;
569 g_object_unref (priv->parent);
572 g_object_unref (priv->store);
576 debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
577 g_object_unref (priv->proxy);
580 if (priv->signal_connection)
581 tp_proxy_signal_connection_disconnect (priv->signal_connection);
584 g_object_unref (priv->cms);
586 (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
590 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
592 GObjectClass *object_class = G_OBJECT_CLASS (klass);
593 object_class->constructor = debug_dialog_constructor;
594 object_class->dispose = debug_dialog_dispose;
595 object_class->set_property = debug_dialog_set_property;
596 object_class->get_property = debug_dialog_get_property;
597 g_type_class_add_private (klass, sizeof (EmpathyDebugDialogPriv));
599 g_object_class_install_property (object_class, PROP_PARENT,
600 g_param_spec_object ("parent", "parent", "parent",
601 GTK_TYPE_WINDOW, G_PARAM_CONSTRUCT_ONLY |
602 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
608 empathy_debug_dialog_new (GtkWindow *parent)
610 g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
612 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
613 "parent", parent, NULL));