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