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