]> git.0d.be Git - empathy.git/blob - src/empathy-debug-dialog.c
Only set Enabled=false in dispose if there we have a proxy.
[empathy.git] / src / empathy-debug-dialog.c
1 /*
2 *  Copyright (C) 2009 Collabora Ltd.
3 *
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.
8 *
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.
13 *
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
17 *
18 *  Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
19 */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
27 #include <libempathy/empathy-debug.h>
28 #include <libempathy/empathy-utils.h>
29
30 #include <libempathy-gtk/empathy-account-chooser.h>
31
32 #include <telepathy-glib/dbus.h>
33 #include <telepathy-glib/util.h>
34
35 #include "extensions/extensions.h"
36
37 #include "empathy-debug-dialog.h"
38
39 G_DEFINE_TYPE (EmpathyDebugDialog, empathy_debug_dialog,
40     GTK_TYPE_DIALOG)
41
42 enum
43 {
44   PROP_0,
45   PROP_PARENT
46 };
47
48 enum
49 {
50   COL_TIMESTAMP = 0,
51   COL_DOMAIN,
52   COL_CATEGORY,
53   COL_LEVEL,
54   COL_MESSAGE,
55   NUM_COLS
56 };
57
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDebugDialog)
59 typedef struct
60 {
61   GtkWidget *filter;
62   GtkWindow *parent;
63   GtkWidget *view;
64   GtkWidget *cm_chooser;
65   GtkListStore *store;
66   TpProxy *proxy;
67   TpProxySignalConnection *signal_connection;
68   gboolean paused;
69   gboolean dispose_run;
70 } EmpathyDebugDialogPriv;
71
72 static const gchar *
73 log_level_to_string (guint level)
74 {
75   switch (level)
76     {
77     case EMP_DEBUG_LEVEL_ERROR:
78       return _("Error");
79       break;
80     case EMP_DEBUG_LEVEL_CRITICAL:
81       return _("Critical");
82       break;
83     case EMP_DEBUG_LEVEL_WARNING:
84       return _("Warning");
85       break;
86     case EMP_DEBUG_LEVEL_MESSAGE:
87       return _("Message");
88       break;
89     case EMP_DEBUG_LEVEL_INFO:
90       return _("Info");
91       break;
92     case EMP_DEBUG_LEVEL_DEBUG:
93       return _("Debug");
94       break;
95     default:
96       g_assert_not_reached ();
97       break;
98     }
99 }
100
101 static void
102 debug_dialog_add_message (EmpathyDebugDialog *debug_dialog,
103                           gdouble timestamp,
104                           const gchar *domain_category,
105                           guint level,
106                           const gchar *message)
107 {
108   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
109   gchar *domain, *category;
110   GtkTreeIter iter;
111
112   if (g_strrstr (domain_category, "/"))
113     {
114       gchar **parts = g_strsplit (domain_category, "/", 2);
115       domain = g_strdup (parts[0]);
116       category = g_strdup (parts[1]);
117       g_strfreev (parts);
118     }
119   else
120     {
121       domain = g_strdup (domain_category);
122       category = "";
123     }
124
125   gtk_list_store_append (priv->store, &iter);
126   gtk_list_store_set (priv->store, &iter,
127                       COL_TIMESTAMP, timestamp,
128                       COL_DOMAIN, domain,
129                       COL_CATEGORY, category,
130                       COL_LEVEL, log_level_to_string (level),
131                       COL_MESSAGE, message,
132                       -1);
133
134   g_free (domain);
135   g_free (category);
136 }
137
138 static void
139 debug_dialog_new_debug_message_cb (TpProxy *proxy,
140                                    gdouble timestamp,
141                                    const gchar *domain,
142                                    guint level,
143                                    const gchar *message,
144                                    gpointer user_data,
145                                    GObject *weak_object)
146 {
147   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
148
149   debug_dialog_add_message (debug_dialog, timestamp, domain, level,
150       message);
151 }
152
153 static void
154 debug_dialog_set_enabled (EmpathyDebugDialog *debug_dialog,
155                           gboolean enabled)
156 {
157   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
158   GValue *val;
159
160   val = tp_g_value_slice_new_boolean (enabled);
161
162   tp_cli_dbus_properties_call_set (priv->proxy, -1, EMP_IFACE_DEBUG,
163       "Enabled", val, NULL, NULL, NULL, NULL);
164
165   tp_g_value_slice_free (val);
166 }
167
168 static void
169 debug_dialog_get_messages_cb (TpProxy *proxy,
170                               const GPtrArray *messages,
171                               const GError *error,
172                               gpointer user_data,
173                               GObject *weak_object)
174 {
175   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
176   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
177   gint i;
178
179   if (error != NULL)
180     {
181       DEBUG ("GetMessages failed: %s", error->message);
182       return;
183     }
184
185   for (i = 0; i < messages->len; i++)
186     {
187       GValueArray *values = g_ptr_array_index (messages, i);
188
189       debug_dialog_add_message (debug_dialog,
190           g_value_get_double (g_value_array_get_nth (values, 0)),
191           g_value_get_string (g_value_array_get_nth (values, 1)),
192           g_value_get_uint (g_value_array_get_nth (values, 2)),
193           g_value_get_string (g_value_array_get_nth (values, 3)));
194     }
195
196   /* Connect to NewDebugMessage */
197   priv->signal_connection = emp_cli_debug_connect_to_new_debug_message (
198       proxy, debug_dialog_new_debug_message_cb, debug_dialog,
199       NULL, NULL, NULL);
200
201   /* Set Enabled as appropriate */
202   debug_dialog_set_enabled (debug_dialog, !priv->paused);
203 }
204
205 static void
206 debug_dialog_cm_chooser_changed_cb (GtkComboBox *cm_chooser,
207                                     EmpathyDebugDialog *debug_dialog)
208 {
209   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
210   gchar *cm;
211   MissionControl *mc;
212   TpDBusDaemon *dbus;
213   GError *error = NULL;
214   gchar *bus_name;
215   TpConnection *connection;
216
217   mc = empathy_mission_control_dup_singleton ();
218   cm = gtk_combo_box_get_active_text (cm_chooser);
219
220   if (cm == NULL)
221     {
222       DEBUG ("No CM is selected");
223       g_object_unref (mc);
224       return;
225     }
226
227   dbus = tp_dbus_daemon_dup (&error);
228
229   if (error != NULL)
230     {
231       DEBUG ("Failed at duping the dbus daemon: %s", error->message);
232       g_free (cm);
233       g_object_unref (mc);
234     }
235
236   /* TODO: Fix this. */
237   bus_name = g_strdup_printf ("org.freedesktop.Telepathy.ConnectionManager.%s", cm);
238   g_free (cm);
239
240   connection = tp_connection_new (dbus, bus_name,
241       "/org/freedesktop/Telepathy/debug", &error);
242   g_free (bus_name);
243
244   if (error != NULL)
245     {
246       DEBUG ("Getting a new TpConnection failed: %s", error->message);
247       g_error_free (error);
248       g_object_unref (dbus);
249       g_object_unref (mc);
250       return;
251     }
252
253   /* Disable debug signalling */
254   if (priv->proxy != NULL)
255     debug_dialog_set_enabled (debug_dialog, FALSE);
256
257   /* Disconnect from previous NewDebugMessage signal */
258   if (priv->signal_connection)
259     {
260       tp_proxy_signal_connection_disconnect (priv->signal_connection);
261       priv->signal_connection = NULL;
262     }
263
264   if (priv->proxy)
265     g_object_unref (priv->proxy);
266
267   priv->proxy = TP_PROXY (connection);
268
269   emp_cli_debug_call_get_messages (priv->proxy, -1,
270       debug_dialog_get_messages_cb, debug_dialog, NULL, NULL);
271
272   g_object_unref (dbus);
273   g_object_unref (mc);
274 }
275
276 static void
277 debug_dialog_list_connection_names_cb (const gchar * const *names,
278                                        gsize n,
279                                        const gchar * const *cms,
280                                        const gchar * const *protocols,
281                                        const GError *error,
282                                        gpointer user_data,
283                                        GObject *weak_object)
284 {
285   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
286   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
287   int i;
288
289   if (error != NULL)
290     {
291       DEBUG ("list_connection_names failed: %s", error->message);
292       return;
293     }
294
295   for (i = 0; cms[i] != NULL; i++)
296     gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cm_chooser), cms[i]);
297
298   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cm_chooser), 0);
299
300   /* Fill treeview */
301   debug_dialog_cm_chooser_changed_cb (GTK_COMBO_BOX (priv->cm_chooser), debug_dialog);
302 }
303
304 static void
305 debug_dialog_fill_cm_chooser (EmpathyDebugDialog *debug_dialog)
306 {
307   TpDBusDaemon *dbus;
308   GError *error = NULL;
309
310   dbus = tp_dbus_daemon_dup (&error);
311
312   if (error != NULL)
313     {
314       DEBUG ("Failed to dup dbus daemon: %s", error->message);
315       g_error_free (error);
316       return;
317     }
318
319   tp_list_connection_names (dbus, debug_dialog_list_connection_names_cb,
320       debug_dialog, NULL, NULL);
321
322   g_object_unref (dbus);
323 }
324
325 static void
326 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
327                                EmpathyDebugDialog *debug_dialog)
328 {
329   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
330
331   priv->paused = gtk_toggle_tool_button_get_active (pause);
332
333   debug_dialog_set_enabled (debug_dialog, !priv->paused);
334 }
335
336 static GObject *
337 debug_dialog_constructor (GType type,
338                           guint n_construct_params,
339                           GObjectConstructParam *construct_params)
340 {
341   GObject *object;
342   EmpathyDebugDialogPriv *priv;
343   GtkWidget *vbox;
344   GtkWidget *toolbar;
345   GtkWidget *image;
346   GtkWidget *label;
347   GtkToolItem *item;
348   GtkCellRenderer *renderer;
349   GtkWidget *scrolled_win;
350
351   object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
352     (type, n_construct_params, construct_params);
353   priv = GET_PRIV (object);
354
355   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
356   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
357   gtk_window_set_transient_for (GTK_WINDOW (object), priv->parent);
358
359   vbox = GTK_DIALOG (object)->vbox;
360
361   toolbar = gtk_toolbar_new ();
362   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
363   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
364   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_SMALL_TOOLBAR);
365   gtk_widget_show (toolbar);
366
367   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
368
369   /* CM */
370   priv->cm_chooser = gtk_combo_box_new_text ();
371   gtk_widget_show (priv->cm_chooser);
372
373   item = gtk_tool_item_new ();
374   gtk_widget_show (GTK_WIDGET (item));
375   gtk_container_add (GTK_CONTAINER (item), priv->cm_chooser);
376   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
377   debug_dialog_fill_cm_chooser (EMPATHY_DEBUG_DIALOG (object));
378   g_signal_connect (priv->cm_chooser, "changed",
379       G_CALLBACK (debug_dialog_cm_chooser_changed_cb), object);
380   gtk_widget_show (GTK_WIDGET (priv->cm_chooser));
381
382   item = gtk_separator_tool_item_new ();
383   gtk_widget_show (GTK_WIDGET (item));
384   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
385
386   /* Save */
387   item = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
388   gtk_widget_show (GTK_WIDGET (item));
389   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
390   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
391
392   /* Clear */
393   item = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
394   gtk_widget_show (GTK_WIDGET (item));
395   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
396   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
397
398   item = gtk_separator_tool_item_new ();
399   gtk_widget_show (GTK_WIDGET (item));
400   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
401
402   /* Pause */
403   priv->paused = FALSE;
404   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE, GTK_ICON_SIZE_MENU);
405   gtk_widget_show (image);
406   item = gtk_toggle_tool_button_new ();
407   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (item), priv->paused);
408   g_signal_connect (item, "toggled", G_CALLBACK (debug_dialog_pause_toggled_cb),
409       object);
410   gtk_widget_show (GTK_WIDGET (item));
411   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
412   gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), _("Pause"));
413   gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
414   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
415
416   item = gtk_separator_tool_item_new ();
417   gtk_widget_show (GTK_WIDGET (item));
418   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
419
420   /* Level */
421   item = gtk_tool_item_new ();
422   gtk_widget_show (GTK_WIDGET (item));
423   label = gtk_label_new (_("Level "));
424   gtk_widget_show (label);
425   gtk_container_add (GTK_CONTAINER (item), label);
426   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
427
428   priv->filter = gtk_combo_box_new_text ();
429   gtk_widget_show (priv->filter);
430
431   item = gtk_tool_item_new ();
432   gtk_widget_show (GTK_WIDGET (item));
433   gtk_container_add (GTK_CONTAINER (item), priv->filter);
434   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
435
436   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("All"));
437   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Debug"));
438   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Info"));
439   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Message"));
440   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Warning"));
441   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Critical"));
442   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Error"));
443
444   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->filter), 0);
445   gtk_widget_show (GTK_WIDGET (priv->filter));
446
447   /* Debug treeview */
448   priv->view = gtk_tree_view_new ();
449
450   renderer = gtk_cell_renderer_text_new ();
451
452   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
453       -1, _("Time"), renderer, "text", COL_TIMESTAMP, NULL);
454   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
455       -1, _("Domain"), renderer, "text", COL_DOMAIN, NULL);
456   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
457       -1, _("Category"), renderer, "text", COL_CATEGORY, NULL);
458   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
459       -1, _("Level"), renderer, "text", COL_LEVEL, NULL);
460   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
461       -1, _("Message"), renderer, "text", COL_MESSAGE, NULL);
462
463   priv->store = gtk_list_store_new (NUM_COLS, G_TYPE_DOUBLE,
464       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
465   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view),
466       GTK_TREE_MODEL (priv->store));
467
468   /* Scrolled window */
469   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
470   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
471       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
472
473   gtk_widget_show (priv->view);
474   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->view);
475
476   gtk_widget_show (scrolled_win);
477   gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
478
479   gtk_widget_show (GTK_WIDGET (object));
480
481   return object;
482 }
483
484 static void
485 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
486 {
487   EmpathyDebugDialogPriv *priv =
488       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
489       EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
490
491   empathy_debug_dialog->priv = priv;
492
493   priv->dispose_run = FALSE;
494 }
495
496 static void
497 debug_dialog_set_property (GObject *object,
498                            guint prop_id,
499                            const GValue *value,
500                            GParamSpec *pspec)
501 {
502   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
503
504   switch (prop_id)
505     {
506       case PROP_PARENT:
507         priv->parent = GTK_WINDOW (g_value_dup_object (value));
508         break;
509       default:
510         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
511         break;
512     }
513 }
514
515 static void
516 debug_dialog_get_property (GObject *object,
517                            guint prop_id,
518                            GValue *value,
519                            GParamSpec *pspec)
520 {
521   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
522
523   switch (prop_id)
524     {
525       case PROP_PARENT:
526         g_value_set_object (value, priv->parent);
527         break;
528       default:
529         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530         break;
531     }
532 }
533
534 static void
535 debug_dialog_dispose (GObject *object)
536 {
537   EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
538   EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
539
540   if (priv->dispose_run)
541     return;
542
543   priv->dispose_run = TRUE;
544
545   if (priv->parent)
546     g_object_unref (priv->parent);
547
548   if (priv->store)
549     g_object_unref (priv->store);
550
551   if (priv->proxy)
552     {
553       debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
554       g_object_unref (priv->proxy);
555     }
556
557   if (priv->signal_connection)
558     tp_proxy_signal_connection_disconnect (priv->signal_connection);
559
560   (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
561 }
562
563 static void
564 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
565 {
566   GObjectClass *object_class = G_OBJECT_CLASS (klass);
567   object_class->constructor = debug_dialog_constructor;
568   object_class->dispose = debug_dialog_dispose;
569   object_class->set_property = debug_dialog_set_property;
570   object_class->get_property = debug_dialog_get_property;
571   g_type_class_add_private (klass, sizeof (EmpathyDebugDialogPriv));
572
573   g_object_class_install_property (object_class, PROP_PARENT,
574       g_param_spec_object ("parent", "parent", "parent",
575       GTK_TYPE_WINDOW, G_PARAM_CONSTRUCT_ONLY |
576       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
577 }
578
579 /* public methods */
580
581 GtkWidget *
582 empathy_debug_dialog_new (GtkWindow *parent)
583 {
584   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
585       "parent", parent, NULL));
586 }