]> git.0d.be Git - empathy.git/blob - src/empathy-debug-dialog.c
Make sure the toolbar labels are shown.
[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 *account_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_account_chooser_changed_cb (GtkComboBox *account_chooser,
207                                          EmpathyDebugDialog *debug_dialog)
208 {
209   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
210   McAccount *account;
211   TpConnection *connection;
212   MissionControl *mc;
213   TpDBusDaemon *dbus;
214   GError *error = NULL;
215
216   mc = empathy_mission_control_dup_singleton ();
217   account = empathy_account_chooser_get_account (EMPATHY_ACCOUNT_CHOOSER (account_chooser));
218   connection = mission_control_get_tpconnection (mc, account, &error);
219
220   if (error != NULL)
221     {
222       DEBUG ("Getting the account's TpConnection failed: %s", error->message);
223       g_error_free (error);
224       return;
225     }
226
227   dbus = g_object_ref (tp_proxy_get_dbus_daemon (connection));
228
229   /* TODO: Fix this. */
230   connection = tp_connection_new (dbus,
231                                   "org.freedesktop.Telepathy.ConnectionManager.gabble",
232                                   "/org/freedesktop/Telepathy/debug",
233                                   &error);
234
235   if (error != NULL)
236     {
237       DEBUG ("Getting a new TpConnection failed: %s", error->message);
238       g_error_free (error);
239       g_object_unref (dbus);
240       return;
241     }
242
243   /* Disable debug signalling */
244   if (priv->proxy != NULL)
245     debug_dialog_set_enabled (debug_dialog, FALSE);
246
247   /* Disconnect from previous NewDebugMessage signal */
248   if (priv->signal_connection)
249     {
250       tp_proxy_signal_connection_disconnect (priv->signal_connection);
251       priv->signal_connection = NULL;
252     }
253
254   if (priv->proxy)
255     g_object_unref (priv->proxy);
256
257   priv->proxy = TP_PROXY (g_object_ref (connection));
258
259   emp_cli_debug_call_get_messages (priv->proxy, -1,
260       debug_dialog_get_messages_cb, debug_dialog, NULL, NULL);
261
262   g_object_unref (connection);
263   g_object_unref (account);
264   g_object_unref (dbus);
265 }
266
267 static void
268 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
269                                EmpathyDebugDialog *debug_dialog)
270 {
271   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
272
273   priv->paused = gtk_toggle_tool_button_get_active (pause);
274
275   debug_dialog_set_enabled (debug_dialog, !priv->paused);
276 }
277
278 static GObject *
279 debug_dialog_constructor (GType type,
280                           guint n_construct_params,
281                           GObjectConstructParam *construct_params)
282 {
283   GObject *object;
284   EmpathyDebugDialogPriv *priv;
285   GtkWidget *vbox;
286   GtkWidget *toolbar;
287   GtkWidget *image;
288   GtkWidget *label;
289   GtkToolItem *item;
290   GtkCellRenderer *renderer;
291   GtkWidget *scrolled_win;
292
293   object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
294     (type, n_construct_params, construct_params);
295   priv = GET_PRIV (object);
296
297   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
298   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
299   gtk_window_set_transient_for (GTK_WINDOW (object), priv->parent);
300
301   vbox = GTK_DIALOG (object)->vbox;
302
303   toolbar = gtk_toolbar_new ();
304   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
305   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
306   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_SMALL_TOOLBAR);
307   gtk_widget_show (toolbar);
308
309   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
310
311   /* Account */
312   item = gtk_tool_item_new ();
313   gtk_widget_show (GTK_WIDGET (item));
314   label = gtk_label_new (_("Account "));
315   gtk_widget_show (label);
316   gtk_container_add (GTK_CONTAINER (item), label);
317   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
318
319   priv->account_chooser = empathy_account_chooser_new ();
320   empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
321       (EmpathyAccountChooserFilterFunc) mc_account_is_enabled, NULL);
322   g_signal_connect (priv->account_chooser, "changed",
323       G_CALLBACK (debug_dialog_account_chooser_changed_cb), object);
324   gtk_widget_show (priv->account_chooser);
325
326   item = gtk_tool_item_new ();
327   gtk_widget_show (GTK_WIDGET (item));
328   gtk_container_add (GTK_CONTAINER (item), priv->account_chooser);
329   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
330
331   item = gtk_separator_tool_item_new ();
332   gtk_widget_show (GTK_WIDGET (item));
333   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
334
335   /* Save */
336   item = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
337   gtk_widget_show (GTK_WIDGET (item));
338   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
339   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
340
341   /* Clear */
342   item = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
343   gtk_widget_show (GTK_WIDGET (item));
344   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
345   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
346
347   item = gtk_separator_tool_item_new ();
348   gtk_widget_show (GTK_WIDGET (item));
349   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
350
351   /* Pause */
352   priv->paused = FALSE;
353   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE, GTK_ICON_SIZE_MENU);
354   gtk_widget_show (image);
355   item = gtk_toggle_tool_button_new ();
356   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (item), priv->paused);
357   g_signal_connect (item, "toggled", G_CALLBACK (debug_dialog_pause_toggled_cb),
358       object);
359   gtk_widget_show (GTK_WIDGET (item));
360   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
361   gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), _("Pause"));
362   gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
363   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
364
365   item = gtk_separator_tool_item_new ();
366   gtk_widget_show (GTK_WIDGET (item));
367   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
368
369   /* Level */
370   item = gtk_tool_item_new ();
371   gtk_widget_show (GTK_WIDGET (item));
372   label = gtk_label_new (_("Level "));
373   gtk_widget_show (label);
374   gtk_container_add (GTK_CONTAINER (item), label);
375   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
376
377   priv->filter = gtk_combo_box_new_text ();
378   gtk_widget_show (priv->filter);
379
380   item = gtk_tool_item_new ();
381   gtk_widget_show (GTK_WIDGET (item));
382   gtk_container_add (GTK_CONTAINER (item), priv->filter);
383   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
384
385   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("All"));
386   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Debug"));
387   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Info"));
388   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Message"));
389   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Warning"));
390   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Critical"));
391   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->filter), _("Error"));
392
393   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->filter), 0);
394   gtk_widget_show (GTK_WIDGET (priv->filter));
395
396   /* Debug treeview */
397   priv->view = gtk_tree_view_new ();
398
399   renderer = gtk_cell_renderer_text_new ();
400
401   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
402       -1, _("Time"), renderer, "text", COL_TIMESTAMP, NULL);
403   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
404       -1, _("Domain"), renderer, "text", COL_DOMAIN, NULL);
405   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
406       -1, _("Category"), renderer, "text", COL_CATEGORY, NULL);
407   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
408       -1, _("Level"), renderer, "text", COL_LEVEL, NULL);
409   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
410       -1, _("Message"), renderer, "text", COL_MESSAGE, NULL);
411
412   priv->store = gtk_list_store_new (NUM_COLS, G_TYPE_DOUBLE,
413       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
414
415   /* Fill treeview */
416   debug_dialog_account_chooser_changed_cb (GTK_COMBO_BOX (priv->account_chooser),
417       EMPATHY_DEBUG_DIALOG (object));
418
419   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view),
420       GTK_TREE_MODEL (priv->store));
421
422   /* Scrolled window */
423   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
424   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
425       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
426
427   gtk_widget_show (priv->view);
428   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->view);
429
430   gtk_widget_show (scrolled_win);
431   gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
432
433   gtk_widget_show (GTK_WIDGET (object));
434
435   return object;
436 }
437
438 static void
439 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
440 {
441   EmpathyDebugDialogPriv *priv =
442       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
443       EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
444
445   empathy_debug_dialog->priv = priv;
446
447   priv->dispose_run = FALSE;
448 }
449
450 static void
451 debug_dialog_set_property (GObject *object,
452                            guint prop_id,
453                            const GValue *value,
454                            GParamSpec *pspec)
455 {
456   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
457
458   switch (prop_id)
459     {
460       case PROP_PARENT:
461         priv->parent = GTK_WINDOW (g_value_dup_object (value));
462         break;
463       default:
464         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
465         break;
466     }
467 }
468
469 static void
470 debug_dialog_get_property (GObject *object,
471                            guint prop_id,
472                            GValue *value,
473                            GParamSpec *pspec)
474 {
475   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
476
477   switch (prop_id)
478     {
479       case PROP_PARENT:
480         g_value_set_object (value, priv->parent);
481         break;
482       default:
483         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484         break;
485     }
486 }
487
488 static void
489 debug_dialog_dispose (GObject *object)
490 {
491   EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
492   EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
493
494   if (priv->dispose_run)
495     return;
496
497   priv->dispose_run = TRUE;
498
499   if (priv->parent)
500     g_object_unref (priv->parent);
501
502   if (priv->store)
503     g_object_unref (priv->store);
504
505   debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
506
507   if (priv->proxy)
508     g_object_unref (priv->proxy);
509
510   if (priv->signal_connection)
511     tp_proxy_signal_connection_disconnect (priv->signal_connection);
512
513   (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
514 }
515
516 static void
517 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
518 {
519   GObjectClass *object_class = G_OBJECT_CLASS (klass);
520   object_class->constructor = debug_dialog_constructor;
521   object_class->dispose = debug_dialog_dispose;
522   object_class->set_property = debug_dialog_set_property;
523   object_class->get_property = debug_dialog_get_property;
524   g_type_class_add_private (klass, sizeof (EmpathyDebugDialogPriv));
525
526   g_object_class_install_property (object_class, PROP_PARENT,
527       g_param_spec_object ("parent", "parent", "parent",
528       GTK_TYPE_WINDOW, G_PARAM_CONSTRUCT_ONLY |
529       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
530 }
531
532 /* public methods */
533
534 GtkWidget *
535 empathy_debug_dialog_new (GtkWindow *parent)
536 {
537   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
538       "parent", parent, NULL));
539 }