]> git.0d.be Git - empathy.git/blob - src/empathy-debug-dialog.c
Remove trailing whitespace from the debug message.
[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   gchar *string;
121
122   if (g_strrstr (domain_category, "/"))
123     {
124       gchar **parts = g_strsplit (domain_category, "/", 2);
125       domain = g_strdup (parts[0]);
126       category = g_strdup (parts[1]);
127       g_strfreev (parts);
128     }
129   else
130     {
131       domain = g_strdup (domain_category);
132       category = g_strdup ("");
133     }
134
135   if (g_str_has_suffix (message, "\n"))
136     string = g_strchomp (g_strdup (message));
137   else
138     string = g_strdup (message);
139
140
141   gtk_list_store_append (priv->store, &iter);
142   gtk_list_store_set (priv->store, &iter,
143       COL_TIMESTAMP, timestamp,
144       COL_DOMAIN, domain,
145       COL_CATEGORY, category,
146       COL_LEVEL, log_level_to_string (level),
147       COL_MESSAGE, string,
148       -1);
149
150   g_free (string);
151   g_free (domain);
152   g_free (category);
153 }
154
155 static void
156 debug_dialog_new_debug_message_cb (TpProxy *proxy,
157                                    gdouble timestamp,
158                                    const gchar *domain,
159                                    guint level,
160                                    const gchar *message,
161                                    gpointer user_data,
162                                    GObject *weak_object)
163 {
164   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
165
166   debug_dialog_add_message (debug_dialog, timestamp, domain, level,
167       message);
168 }
169
170 static void
171 debug_dialog_set_enabled (EmpathyDebugDialog *debug_dialog,
172                           gboolean enabled)
173 {
174   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
175   GValue *val;
176
177   val = tp_g_value_slice_new_boolean (enabled);
178
179   tp_cli_dbus_properties_call_set (priv->proxy, -1, EMP_IFACE_DEBUG,
180       "Enabled", val, NULL, NULL, NULL, NULL);
181
182   tp_g_value_slice_free (val);
183 }
184
185 static void
186 debug_dialog_get_messages_cb (TpProxy *proxy,
187                               const GPtrArray *messages,
188                               const GError *error,
189                               gpointer user_data,
190                               GObject *weak_object)
191 {
192   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
193   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
194   gint i;
195
196   if (error != NULL)
197     {
198       DEBUG ("GetMessages failed: %s", error->message);
199       return;
200     }
201
202   for (i = 0; i < messages->len; i++)
203     {
204       GValueArray *values = g_ptr_array_index (messages, i);
205
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)));
211     }
212
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,
216       NULL, NULL, NULL);
217
218   /* Set Enabled as appropriate */
219   debug_dialog_set_enabled (debug_dialog, !priv->paused);
220 }
221
222 static void
223 debug_dialog_cm_chooser_changed_cb (GtkComboBox *cm_chooser,
224                                     EmpathyDebugDialog *debug_dialog)
225 {
226   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
227   MissionControl *mc;
228   TpDBusDaemon *dbus;
229   GError *error = NULL;
230   gchar *bus_name;
231   TpConnection *connection;
232   GtkTreeIter iter;
233
234   if (!gtk_combo_box_get_active_iter (cm_chooser, &iter))
235     {
236       DEBUG ("No CM is selected");
237       return;
238     }
239
240   mc = empathy_mission_control_dup_singleton ();
241   dbus = tp_dbus_daemon_dup (&error);
242
243   if (error != NULL)
244     {
245       DEBUG ("Failed at duping the dbus daemon: %s", error->message);
246       g_object_unref (mc);
247     }
248
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);
252   g_free (bus_name);
253
254   if (error != NULL)
255     {
256       DEBUG ("Getting a new TpConnection failed: %s", error->message);
257       g_error_free (error);
258       g_object_unref (dbus);
259       g_object_unref (mc);
260       return;
261     }
262
263   /* Disable debug signalling */
264   if (priv->proxy != NULL)
265     debug_dialog_set_enabled (debug_dialog, FALSE);
266
267   /* Disconnect from previous NewDebugMessage signal */
268   if (priv->signal_connection)
269     {
270       tp_proxy_signal_connection_disconnect (priv->signal_connection);
271       priv->signal_connection = NULL;
272     }
273
274   if (priv->proxy)
275     g_object_unref (priv->proxy);
276
277   priv->proxy = TP_PROXY (connection);
278
279   tp_proxy_add_interface_by_id (priv->proxy, emp_iface_quark_debug ());
280
281   emp_cli_debug_call_get_messages (priv->proxy, -1,
282       debug_dialog_get_messages_cb, debug_dialog, NULL, NULL);
283
284   g_object_unref (dbus);
285   g_object_unref (mc);
286 }
287
288 static void
289 debug_dialog_list_connection_names_cb (const gchar * const *names,
290                                        gsize n,
291                                        const gchar * const *cms,
292                                        const gchar * const *protocols,
293                                        const GError *error,
294                                        gpointer user_data,
295                                        GObject *weak_object)
296 {
297   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
298   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
299   int i;
300
301   if (error != NULL)
302     {
303       DEBUG ("list_connection_names failed: %s", error->message);
304       return;
305     }
306
307   for (i = 0; cms[i] != NULL; i++)
308     {
309       GtkTreeIter iter;
310       gtk_list_store_append (priv->cms, &iter);
311       gtk_list_store_set (priv->cms, &iter,
312                           COL_CM_NAME, cms[i],
313                           COL_CM_BUS, names[i],
314                           -1);
315     }
316
317   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cm_chooser), 0);
318
319   /* Fill treeview */
320   debug_dialog_cm_chooser_changed_cb (GTK_COMBO_BOX (priv->cm_chooser), debug_dialog);
321 }
322
323 static void
324 debug_dialog_fill_cm_chooser (EmpathyDebugDialog *debug_dialog)
325 {
326   TpDBusDaemon *dbus;
327   GError *error = NULL;
328
329   dbus = tp_dbus_daemon_dup (&error);
330
331   if (error != NULL)
332     {
333       DEBUG ("Failed to dup dbus daemon: %s", error->message);
334       g_error_free (error);
335       return;
336     }
337
338   tp_list_connection_names (dbus, debug_dialog_list_connection_names_cb,
339       debug_dialog, NULL, NULL);
340
341   g_object_unref (dbus);
342 }
343
344 static void
345 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
346                                EmpathyDebugDialog *debug_dialog)
347 {
348   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
349
350   priv->paused = gtk_toggle_tool_button_get_active (pause);
351
352   debug_dialog_set_enabled (debug_dialog, !priv->paused);
353 }
354
355 static GObject *
356 debug_dialog_constructor (GType type,
357                           guint n_construct_params,
358                           GObjectConstructParam *construct_params)
359 {
360   GObject *object;
361   EmpathyDebugDialogPriv *priv;
362   GtkWidget *vbox;
363   GtkWidget *toolbar;
364   GtkWidget *image;
365   GtkWidget *label;
366   GtkToolItem *item;
367   GtkCellRenderer *renderer;
368   GtkWidget *scrolled_win;
369
370   object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
371     (type, n_construct_params, construct_params);
372   priv = GET_PRIV (object);
373
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);
377
378   vbox = GTK_DIALOG (object)->vbox;
379
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);
385
386   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
387
388   /* CM */
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);
394
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));
403
404   item = gtk_separator_tool_item_new ();
405   gtk_widget_show (GTK_WIDGET (item));
406   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
407
408   /* Save */
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);
413
414   /* Clear */
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);
419
420   item = gtk_separator_tool_item_new ();
421   gtk_widget_show (GTK_WIDGET (item));
422   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
423
424   /* Pause */
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),
431       object);
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);
437
438   item = gtk_separator_tool_item_new ();
439   gtk_widget_show (GTK_WIDGET (item));
440   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
441
442   /* Level */
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);
449
450   priv->filter = gtk_combo_box_new_text ();
451   gtk_widget_show (priv->filter);
452
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);
457
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"));
465
466   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->filter), 0);
467   gtk_widget_show (GTK_WIDGET (priv->filter));
468
469   /* Debug treeview */
470   priv->view = gtk_tree_view_new ();
471   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
472
473   renderer = gtk_cell_renderer_text_new ();
474
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);
485
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));
490
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);
495
496   gtk_widget_show (priv->view);
497   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->view);
498
499   gtk_widget_show (scrolled_win);
500   gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
501
502   gtk_widget_show (GTK_WIDGET (object));
503
504   return object;
505 }
506
507 static void
508 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
509 {
510   EmpathyDebugDialogPriv *priv =
511       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
512       EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
513
514   empathy_debug_dialog->priv = priv;
515
516   priv->dispose_run = FALSE;
517 }
518
519 static void
520 debug_dialog_set_property (GObject *object,
521                            guint prop_id,
522                            const GValue *value,
523                            GParamSpec *pspec)
524 {
525   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
526
527   switch (prop_id)
528     {
529       case PROP_PARENT:
530         priv->parent = GTK_WINDOW (g_value_dup_object (value));
531         break;
532       default:
533         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534         break;
535     }
536 }
537
538 static void
539 debug_dialog_get_property (GObject *object,
540                            guint prop_id,
541                            GValue *value,
542                            GParamSpec *pspec)
543 {
544   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
545
546   switch (prop_id)
547     {
548       case PROP_PARENT:
549         g_value_set_object (value, priv->parent);
550         break;
551       default:
552         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553         break;
554     }
555 }
556
557 static void
558 debug_dialog_dispose (GObject *object)
559 {
560   EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
561   EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
562
563   if (priv->dispose_run)
564     return;
565
566   priv->dispose_run = TRUE;
567
568   if (priv->parent)
569     g_object_unref (priv->parent);
570
571   if (priv->store)
572     g_object_unref (priv->store);
573
574   if (priv->proxy)
575     {
576       debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
577       g_object_unref (priv->proxy);
578     }
579
580   if (priv->signal_connection)
581     tp_proxy_signal_connection_disconnect (priv->signal_connection);
582
583   if (priv->cms)
584     g_object_unref (priv->cms);
585
586   (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
587 }
588
589 static void
590 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
591 {
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));
598
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));
603 }
604
605 /* public methods */
606
607 GtkWidget *
608 empathy_debug_dialog_new (GtkWindow *parent)
609 {
610   g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
611
612   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
613       "parent", parent, NULL));
614 }