]> git.0d.be Git - empathy.git/blob - src/empathy-debug-dialog.c
Merge branch 'debug'
[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 #include <gio/gio.h>
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
28 #include <libempathy/empathy-debug.h>
29 #include <libempathy/empathy-utils.h>
30
31 #include <libempathy-gtk/empathy-account-chooser.h>
32
33 #include <telepathy-glib/dbus.h>
34 #include <telepathy-glib/util.h>
35 #include <telepathy-glib/proxy-subclass.h>
36
37 #include "extensions/extensions.h"
38
39 #include "empathy-debug-dialog.h"
40
41 G_DEFINE_TYPE (EmpathyDebugDialog, empathy_debug_dialog,
42     GTK_TYPE_DIALOG)
43
44 enum
45 {
46   COL_DEBUG_TIMESTAMP = 0,
47   COL_DEBUG_DOMAIN,
48   COL_DEBUG_CATEGORY,
49   COL_DEBUG_LEVEL_STRING,
50   COL_DEBUG_MESSAGE,
51   COL_DEBUG_LEVEL_VALUE,
52   NUM_DEBUG_COLS
53 };
54
55 enum
56 {
57   COL_CM_NAME = 0,
58   COL_CM_UNIQUE_NAME,
59   NUM_COLS_CM
60 };
61
62 enum
63 {
64   COL_LEVEL_NAME,
65   COL_LEVEL_VALUE,
66   NUM_COLS_LEVEL
67 };
68
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDebugDialog)
70 typedef struct
71 {
72   /* Toolbar items */
73   GtkWidget *cm_chooser;
74   GtkToolItem *save_button;
75   GtkToolItem *copy_button;
76   GtkToolItem *clear_button;
77   GtkToolItem *pause_button;
78   GtkToolItem *level_label;
79   GtkWidget *level_filter;
80
81   /* TreeView */
82   GtkListStore *store;
83   GtkTreeModel *store_filter;
84   GtkWidget *view;
85   GtkWidget *scrolled_win;
86   GtkWidget *not_supported_label;
87   gboolean view_visible;
88
89   /* Connection */
90   TpDBusDaemon *dbus;
91   TpProxy *proxy;
92   TpProxySignalConnection *new_debug_message_signal;
93   TpProxySignalConnection *name_owner_changed_signal;
94
95   /* Whether NewDebugMessage will be fired */
96   gboolean paused;
97
98   /* CM chooser store */
99   GtkListStore *cms;
100
101   /* Misc. */
102   gboolean dispose_run;
103 } EmpathyDebugDialogPriv;
104
105 static const gchar *
106 log_level_to_string (guint level)
107 {
108   switch (level)
109     {
110     case EMP_DEBUG_LEVEL_ERROR:
111       return _("Error");
112       break;
113     case EMP_DEBUG_LEVEL_CRITICAL:
114       return _("Critical");
115       break;
116     case EMP_DEBUG_LEVEL_WARNING:
117       return _("Warning");
118       break;
119     case EMP_DEBUG_LEVEL_MESSAGE:
120       return _("Message");
121       break;
122     case EMP_DEBUG_LEVEL_INFO:
123       return _("Info");
124       break;
125     case EMP_DEBUG_LEVEL_DEBUG:
126       return _("Debug");
127       break;
128     default:
129       g_assert_not_reached ();
130       break;
131     }
132 }
133
134 static void
135 debug_dialog_add_message (EmpathyDebugDialog *debug_dialog,
136     gdouble timestamp,
137     const gchar *domain_category,
138     guint level,
139     const gchar *message)
140 {
141   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
142   gchar *domain, *category;
143   GtkTreeIter iter;
144   gchar *string;
145
146   if (g_strrstr (domain_category, "/"))
147     {
148       gchar **parts = g_strsplit (domain_category, "/", 2);
149       domain = g_strdup (parts[0]);
150       category = g_strdup (parts[1]);
151       g_strfreev (parts);
152     }
153   else
154     {
155       domain = g_strdup (domain_category);
156       category = g_strdup ("");
157     }
158
159   if (g_str_has_suffix (message, "\n"))
160     string = g_strchomp (g_strdup (message));
161   else
162     string = g_strdup (message);
163
164
165   gtk_list_store_append (priv->store, &iter);
166   gtk_list_store_set (priv->store, &iter,
167       COL_DEBUG_TIMESTAMP, timestamp,
168       COL_DEBUG_DOMAIN, domain,
169       COL_DEBUG_CATEGORY, category,
170       COL_DEBUG_LEVEL_STRING, log_level_to_string (level),
171       COL_DEBUG_MESSAGE, string,
172       COL_DEBUG_LEVEL_VALUE, level,
173       -1);
174
175   g_free (string);
176   g_free (domain);
177   g_free (category);
178 }
179
180 static void
181 debug_dialog_new_debug_message_cb (TpProxy *proxy,
182     gdouble timestamp,
183     const gchar *domain,
184     guint level,
185     const gchar *message,
186     gpointer user_data,
187     GObject *weak_object)
188 {
189   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
190
191   debug_dialog_add_message (debug_dialog, timestamp, domain, level,
192       message);
193 }
194
195 static void
196 debug_dialog_set_enabled (EmpathyDebugDialog *debug_dialog,
197     gboolean enabled)
198 {
199   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
200   GValue *val;
201
202   val = tp_g_value_slice_new_boolean (enabled);
203
204   tp_cli_dbus_properties_call_set (priv->proxy, -1, EMP_IFACE_DEBUG,
205       "Enabled", val, NULL, NULL, NULL, NULL);
206
207   tp_g_value_slice_free (val);
208 }
209
210 static void
211 debug_dialog_set_toolbar_sensitivity (EmpathyDebugDialog *debug_dialog,
212     gboolean sensitive)
213 {
214   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
215   GtkWidget *vbox = GTK_DIALOG (debug_dialog)->vbox;
216
217   gtk_widget_set_sensitive (GTK_WIDGET (priv->save_button), sensitive);
218   gtk_widget_set_sensitive (GTK_WIDGET (priv->copy_button), sensitive);
219   gtk_widget_set_sensitive (GTK_WIDGET (priv->clear_button), sensitive);
220   gtk_widget_set_sensitive (GTK_WIDGET (priv->pause_button), sensitive);
221   gtk_widget_set_sensitive (GTK_WIDGET (priv->level_label), sensitive);
222   gtk_widget_set_sensitive (GTK_WIDGET (priv->level_filter), sensitive);
223   gtk_widget_set_sensitive (GTK_WIDGET (priv->view), sensitive);
224
225   if (sensitive && !priv->view_visible)
226     {
227       /* Add view and remove label */
228       gtk_container_remove (GTK_CONTAINER (vbox), priv->not_supported_label);
229       gtk_box_pack_start (GTK_BOX (vbox), priv->scrolled_win, TRUE, TRUE, 0);
230       priv->view_visible = TRUE;
231     }
232   else if (!sensitive && priv->view_visible)
233     {
234       /* Add label and remove view */
235       gtk_container_remove (GTK_CONTAINER (vbox), priv->scrolled_win);
236       gtk_box_pack_start (GTK_BOX (vbox), priv->not_supported_label,
237           TRUE, TRUE, 0);
238       priv->view_visible = FALSE;
239     }
240 }
241
242 static void
243 debug_dialog_get_messages_cb (TpProxy *proxy,
244     const GPtrArray *messages,
245     const GError *error,
246     gpointer user_data,
247     GObject *weak_object)
248 {
249   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
250   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
251   gint i;
252
253   if (error != NULL)
254     {
255       DEBUG ("GetMessages failed: %s", error->message);
256       debug_dialog_set_toolbar_sensitivity (debug_dialog, FALSE);
257       return;
258     }
259
260   debug_dialog_set_toolbar_sensitivity (debug_dialog, TRUE);
261
262   for (i = 0; i < messages->len; i++)
263     {
264       GValueArray *values = g_ptr_array_index (messages, i);
265
266       debug_dialog_add_message (debug_dialog,
267           g_value_get_double (g_value_array_get_nth (values, 0)),
268           g_value_get_string (g_value_array_get_nth (values, 1)),
269           g_value_get_uint (g_value_array_get_nth (values, 2)),
270           g_value_get_string (g_value_array_get_nth (values, 3)));
271     }
272
273   /* Connect to NewDebugMessage */
274   priv->new_debug_message_signal = emp_cli_debug_connect_to_new_debug_message (
275       proxy, debug_dialog_new_debug_message_cb, debug_dialog,
276       NULL, NULL, NULL);
277
278   /* Set Enabled as appropriate */
279   debug_dialog_set_enabled (debug_dialog, !priv->paused);
280 }
281
282 static void
283 debug_dialog_cm_chooser_changed_cb (GtkComboBox *cm_chooser,
284     EmpathyDebugDialog *debug_dialog)
285 {
286   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
287   MissionControl *mc;
288   TpDBusDaemon *dbus;
289   GError *error = NULL;
290   gchar *bus_name;
291   TpProxy *proxy;
292   GtkTreeIter iter;
293
294   if (!gtk_combo_box_get_active_iter (cm_chooser, &iter))
295     {
296       DEBUG ("No CM is selected");
297       if (gtk_tree_model_iter_n_children (
298           GTK_TREE_MODEL (priv->cms), NULL) > 0)
299         {
300           gtk_combo_box_set_active (cm_chooser, 0);
301         }
302       return;
303     }
304
305   mc = empathy_mission_control_dup_singleton ();
306   dbus = tp_dbus_daemon_dup (&error);
307
308   if (error != NULL)
309     {
310       DEBUG ("Failed at duping the dbus daemon: %s", error->message);
311       g_object_unref (mc);
312     }
313
314   gtk_tree_model_get (GTK_TREE_MODEL (priv->cms), &iter,
315       COL_CM_UNIQUE_NAME, &bus_name, -1);
316   proxy = g_object_new (TP_TYPE_PROXY,
317       "bus-name", bus_name,
318       "dbus-daemon", dbus,
319       "object-path", DEBUG_OBJECT_PATH,
320       NULL);
321   g_free (bus_name);
322
323   gtk_list_store_clear (priv->store);
324
325   /* Disable debug signalling */
326   if (priv->proxy != NULL)
327     debug_dialog_set_enabled (debug_dialog, FALSE);
328
329   /* Disconnect from previous NewDebugMessage signal */
330   if (priv->new_debug_message_signal != NULL)
331     {
332       tp_proxy_signal_connection_disconnect (priv->new_debug_message_signal);
333       priv->new_debug_message_signal = NULL;
334     }
335
336   if (priv->proxy != NULL)
337     g_object_unref (priv->proxy);
338
339   priv->proxy = proxy;
340
341   tp_proxy_add_interface_by_id (priv->proxy, emp_iface_quark_debug ());
342
343   emp_cli_debug_call_get_messages (priv->proxy, -1,
344       debug_dialog_get_messages_cb, debug_dialog, NULL, NULL);
345
346   g_object_unref (dbus);
347   g_object_unref (mc);
348 }
349
350 typedef struct
351 {
352   const gchar *unique_name;
353   gboolean found;
354 } CmInModelForeachData;
355
356 static gboolean
357 debug_dialog_cms_foreach (GtkTreeModel *model,
358     GtkTreePath *path,
359     GtkTreeIter *iter,
360     gpointer user_data)
361 {
362   CmInModelForeachData *data = (CmInModelForeachData *) user_data;
363   gchar *unique_name;
364
365   gtk_tree_model_get (model, iter,
366       COL_CM_UNIQUE_NAME, &unique_name,
367       -1);
368
369   if (!tp_strdiff (unique_name, data->unique_name))
370     data->found = TRUE;
371
372   g_free (unique_name);
373
374   return data->found;
375 }
376
377 static gboolean
378 debug_dialog_cm_is_in_model (EmpathyDebugDialog *debug_dialog,
379     const gchar *unique_name)
380 {
381   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
382   CmInModelForeachData *data;
383   gboolean found;
384
385   data = g_slice_new0 (CmInModelForeachData);
386   data->unique_name = unique_name;
387   data->found = FALSE;
388
389   gtk_tree_model_foreach (GTK_TREE_MODEL (priv->cms),
390       debug_dialog_cms_foreach, data);
391
392   found = data->found;
393
394   g_slice_free (CmInModelForeachData, data);
395
396   return found;
397 }
398
399 typedef struct
400 {
401   EmpathyDebugDialog *debug_dialog;
402   gchar *cm_name;
403 } FillCmChooserData;
404
405 static void
406 debug_dialog_get_name_owner_cb (TpDBusDaemon *proxy,
407     const gchar *out,
408     const GError *error,
409     gpointer user_data,
410     GObject *weak_object)
411 {
412   FillCmChooserData *data = (FillCmChooserData *) user_data;
413   EmpathyDebugDialogPriv *priv = GET_PRIV (data->debug_dialog);
414
415   if (error != NULL)
416     {
417       DEBUG ("GetNameOwner failed: %s", error->message);
418       goto OUT;
419     }
420
421   if (!debug_dialog_cm_is_in_model (data->debug_dialog, out))
422     {
423       GtkTreeIter iter;
424
425       DEBUG ("Adding CM to list: %s at unique name: %s",
426           data->cm_name, out);
427
428       gtk_list_store_append (priv->cms, &iter);
429       gtk_list_store_set (priv->cms, &iter,
430           COL_CM_NAME, data->cm_name,
431           COL_CM_UNIQUE_NAME, out,
432           -1);
433
434       gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cm_chooser), 0);
435     }
436
437 OUT:
438   g_free (data->cm_name);
439   g_slice_free (FillCmChooserData, data);
440 }
441
442 static void
443 debug_dialog_list_connection_names_cb (const gchar * const *names,
444     gsize n,
445     const gchar * const *cms,
446     const gchar * const *protocols,
447     const GError *error,
448     gpointer user_data,
449     GObject *weak_object)
450 {
451   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
452   guint i;
453   TpDBusDaemon *dbus;
454   GError *error2 = NULL;
455
456   if (error != NULL)
457     {
458       DEBUG ("list_connection_names failed: %s", error->message);
459       return;
460     }
461
462   dbus = tp_dbus_daemon_dup (&error2);
463
464   if (error2 != NULL)
465     {
466       DEBUG ("Failed to dup TpDBusDaemon.");
467       return;
468     }
469
470   for (i = 0; cms[i] != NULL; i++)
471     {
472       FillCmChooserData *data;
473
474       data = g_slice_new0 (FillCmChooserData);
475       data->debug_dialog = debug_dialog;
476       data->cm_name = g_strdup (cms[i]);
477
478       tp_cli_dbus_daemon_call_get_name_owner (dbus, -1,
479           names[i], debug_dialog_get_name_owner_cb,
480           data, NULL, NULL);
481     }
482
483   g_object_unref (dbus);
484 }
485
486 static gboolean
487 debug_dialog_remove_cm_foreach (GtkTreeModel *model,
488     GtkTreePath *path,
489     GtkTreeIter *iter,
490     gpointer user_data)
491 {
492   const gchar *unique_name_to_remove = (const gchar *) user_data;
493   gchar *name;
494   gboolean found = FALSE;
495
496   gtk_tree_model_get (model, iter, COL_CM_UNIQUE_NAME, &name, -1);
497
498   if (!tp_strdiff (name, unique_name_to_remove))
499     {
500       found = TRUE;
501       gtk_list_store_remove (GTK_LIST_STORE (model), iter);
502     }
503
504   g_free (name);
505
506   return found;
507 }
508
509 #define CM_WELL_KNOWN_NAME_PREFIX \
510     "org.freedesktop.Telepathy.ConnectionManager."
511
512 static void
513 debug_dialog_name_owner_changed_cb (TpDBusDaemon *proxy,
514     const gchar *arg0,
515     const gchar *arg1,
516     const gchar *arg2,
517     gpointer user_data,
518     GObject *weak_object)
519 {
520   EmpathyDebugDialogPriv *priv = GET_PRIV (user_data);
521
522   /* Wow, I hate all of this code... */
523   if (!g_str_has_prefix (arg0, CM_WELL_KNOWN_NAME_PREFIX))
524     return;
525
526   if (EMP_STR_EMPTY (arg1) && !EMP_STR_EMPTY (arg2))
527     {
528       /* A connection manager joined -- because it's guaranteed
529        * that the CM just joined (because o.fd.Tp.CM.foo
530        * just joined), we don't need to check whether the unique
531        * name is in the CM model. Hooray.
532        */
533       GtkTreeIter iter;
534       const gchar *name = arg0 + strlen (CM_WELL_KNOWN_NAME_PREFIX);
535
536       DEBUG ("Adding new CM '%s' at %s.", name, arg2);
537
538       gtk_list_store_append (priv->cms, &iter);
539       gtk_list_store_set (priv->cms, &iter,
540           COL_CM_NAME, name,
541           COL_CM_UNIQUE_NAME, arg2,
542           -1);
543     }
544   else if (!EMP_STR_EMPTY (arg1) && EMP_STR_EMPTY (arg2))
545     {
546       /* A connection manager died -- because it's guaranteed
547        * that the CM itself just died (because o.fd.Tp.CM.foo
548        * just died), we don't need to check that it was already
549        * in the model.
550        */
551       gchar *to_remove;
552
553       /* Can't pass a const into a GtkTreeModelForeachFunc. */
554       to_remove = g_strdup (arg1);
555
556       DEBUG ("Removing CM from %s.", to_remove);
557
558       gtk_tree_model_foreach (GTK_TREE_MODEL (priv->cms),
559           debug_dialog_remove_cm_foreach, to_remove);
560
561       g_free (to_remove);
562     }
563 }
564
565 static void
566 debug_dialog_fill_cm_chooser (EmpathyDebugDialog *debug_dialog)
567 {
568   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
569   GError *error = NULL;
570
571   priv->dbus = tp_dbus_daemon_dup (&error);
572
573   if (error != NULL)
574     {
575       DEBUG ("Failed to dup dbus daemon: %s", error->message);
576       g_error_free (error);
577       return;
578     }
579
580   tp_list_connection_names (priv->dbus, debug_dialog_list_connection_names_cb,
581       debug_dialog, NULL, NULL);
582
583   priv->name_owner_changed_signal =
584       tp_cli_dbus_daemon_connect_to_name_owner_changed (priv->dbus,
585       debug_dialog_name_owner_changed_cb, debug_dialog, NULL, NULL, NULL);
586 }
587
588 static void
589 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
590     EmpathyDebugDialog *debug_dialog)
591 {
592   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
593
594   priv->paused = gtk_toggle_tool_button_get_active (pause);
595
596   debug_dialog_set_enabled (debug_dialog, !priv->paused);
597 }
598
599 static gboolean
600 debug_dialog_visible_func (GtkTreeModel *model,
601     GtkTreeIter *iter,
602     gpointer user_data)
603 {
604   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
605   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
606   guint filter_value, level;
607   GtkTreeModel *filter_model;
608   GtkTreeIter filter_iter;
609
610   filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->level_filter));
611   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->level_filter),
612       &filter_iter);
613
614   gtk_tree_model_get (model, iter, COL_DEBUG_LEVEL_VALUE, &level, -1);
615   gtk_tree_model_get (filter_model, &filter_iter,
616       COL_LEVEL_VALUE, &filter_value, -1);
617
618   if (level <= filter_value)
619     return TRUE;
620
621   return FALSE;
622 }
623
624 static void
625 debug_dialog_filter_changed_cb (GtkComboBox *filter,
626     EmpathyDebugDialog *debug_dialog)
627 {
628   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
629
630   gtk_tree_model_filter_refilter (
631       GTK_TREE_MODEL_FILTER (priv->store_filter));
632 }
633
634 static void
635 debug_dialog_clear_clicked_cb (GtkToolButton *clear_button,
636     EmpathyDebugDialog *debug_dialog)
637 {
638   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
639
640   gtk_list_store_clear (priv->store);
641 }
642
643 static void
644 debug_dialog_menu_copy_activate_cb (GtkMenuItem *menu_item,
645     EmpathyDebugDialog *debug_dialog)
646 {
647   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
648   GtkTreePath *path;
649   GtkTreeViewColumn *focus_column;
650   GtkTreeIter iter;
651   gchar *message;
652   GtkClipboard *clipboard;
653
654   gtk_tree_view_get_cursor (GTK_TREE_VIEW (priv->view),
655       &path, &focus_column);
656
657   if (path == NULL)
658     {
659       DEBUG ("No row is in focus");
660       return;
661     }
662
663   gtk_tree_model_get_iter (priv->store_filter, &iter, path);
664
665   gtk_tree_model_get (priv->store_filter, &iter,
666       COL_DEBUG_MESSAGE, &message,
667       -1);
668
669   if (EMP_STR_EMPTY (message))
670     {
671       DEBUG ("Log message is empty");
672       return;
673     }
674
675   clipboard = gtk_clipboard_get_for_display (
676       gtk_widget_get_display (GTK_WIDGET (menu_item)),
677       GDK_SELECTION_CLIPBOARD);
678
679   gtk_clipboard_set_text (clipboard, message, -1);
680
681   g_free (message);
682 }
683
684 typedef struct
685 {
686   EmpathyDebugDialog *debug_dialog;
687   guint button;
688   guint32 time;
689 } MenuPopupData;
690
691 static gboolean
692 debug_dialog_show_menu (gpointer user_data)
693 {
694   MenuPopupData *data = (MenuPopupData *) user_data;
695   GtkWidget *menu, *item;
696   GtkMenuShell *shell;
697
698   menu = gtk_menu_new ();
699   shell = GTK_MENU_SHELL (menu);
700
701   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
702
703   g_signal_connect (item, "activate",
704       G_CALLBACK (debug_dialog_menu_copy_activate_cb), data->debug_dialog);
705
706   gtk_menu_shell_append (shell, item);
707   gtk_widget_show (item);
708
709   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
710      data->button, data->time);
711
712   g_slice_free (MenuPopupData, user_data);
713
714   return FALSE;
715 }
716
717 static gboolean
718 debug_dialog_button_press_event_cb (GtkTreeView *view,
719     GdkEventButton *event,
720     gpointer user_data)
721 {
722   /* A mouse button was pressed on the tree view. */
723
724   if (event->button == 3)
725     {
726       /* The tree view was right-clicked. (3 == third mouse button) */
727       MenuPopupData *data;
728       data = g_slice_new0 (MenuPopupData);
729       data->debug_dialog = user_data;
730       data->button = event->button;
731       data->time = event->time;
732       g_idle_add (debug_dialog_show_menu, data);
733     }
734
735   return FALSE;
736 }
737
738 static gboolean
739 debug_dialog_store_filter_foreach (GtkTreeModel *model,
740     GtkTreePath *path,
741     GtkTreeIter *iter,
742     gpointer user_data)
743 {
744   GFileOutputStream *output_stream = (GFileOutputStream *) user_data;
745   gchar *domain, *category, *message, *level_str, *level_upper;
746   gdouble timestamp;
747   gchar *line;
748   GError *error = NULL;
749   gboolean out = FALSE;
750
751   gtk_tree_model_get (model, iter,
752       COL_DEBUG_TIMESTAMP, &timestamp,
753       COL_DEBUG_DOMAIN, &domain,
754       COL_DEBUG_CATEGORY, &category,
755       COL_DEBUG_LEVEL_STRING, &level_str,
756       COL_DEBUG_MESSAGE, &message,
757       -1);
758
759   level_upper = g_ascii_strup (level_str, -1);
760
761   line = g_strdup_printf ("%s%s%s-%s: %e: %s\n",
762       domain, EMP_STR_EMPTY (category) ? "" : "/",
763       category, level_upper, timestamp, message);
764
765   g_output_stream_write (G_OUTPUT_STREAM (output_stream), line,
766       strlen (line), NULL, &error);
767
768   if (error != NULL)
769     {
770       DEBUG ("Failed to write to file: %s", error->message);
771       g_error_free (error);
772       out = TRUE;
773     }
774
775   g_free (line);
776   g_free (level_upper);
777   g_free (level_str);
778   g_free (domain);
779   g_free (category);
780   g_free (message);
781
782   return out;
783 }
784
785 static void
786 debug_dialog_save_file_chooser_response_cb (GtkDialog *dialog,
787     gint response_id,
788     EmpathyDebugDialog *debug_dialog)
789 {
790   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
791   gchar *filename = NULL;
792   GFile *gfile = NULL;
793   GFileOutputStream *output_stream = NULL;
794   GError *error = NULL;
795
796   if (response_id != GTK_RESPONSE_ACCEPT)
797     goto OUT;
798
799   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
800
801   DEBUG ("Saving log as %s", filename);
802
803   gfile = g_file_new_for_path (filename);
804   output_stream = g_file_replace (gfile, NULL, FALSE,
805       G_FILE_CREATE_NONE, NULL, &error);
806
807   if (error != NULL)
808     {
809       DEBUG ("Failed to open file for writing: %s", error->message);
810       g_error_free (error);
811       goto OUT;
812     }
813
814   gtk_tree_model_foreach (priv->store_filter,
815       debug_dialog_store_filter_foreach, output_stream);
816
817 OUT:
818   if (gfile != NULL)
819     g_object_unref (gfile);
820
821   if (output_stream != NULL)
822     g_object_unref (output_stream);
823
824   if (filename != NULL)
825     g_free (filename);
826
827   gtk_widget_destroy (GTK_WIDGET (dialog));
828 }
829
830 static void
831 debug_dialog_save_clicked_cb (GtkToolButton *tool_button,
832     EmpathyDebugDialog *debug_dialog)
833 {
834   GtkWidget *file_chooser;
835
836   file_chooser = gtk_file_chooser_dialog_new (_("Save"),
837       GTK_WINDOW (debug_dialog), GTK_FILE_CHOOSER_ACTION_SAVE,
838       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
839       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
840       NULL);
841
842   gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
843   gtk_file_chooser_set_do_overwrite_confirmation (
844       GTK_FILE_CHOOSER (file_chooser), TRUE);
845
846   g_signal_connect (file_chooser, "response",
847       G_CALLBACK (debug_dialog_save_file_chooser_response_cb),
848       debug_dialog);
849
850   gtk_widget_show (file_chooser);
851 }
852
853 static gboolean
854 debug_dialog_copy_model_foreach (GtkTreeModel *model,
855     GtkTreePath *path,
856     GtkTreeIter *iter,
857     gpointer user_data)
858 {
859   gchar **text = (gchar **) user_data;
860   gchar *tmp;
861   gchar *domain, *category, *message, *level_str, *level_upper;
862   gdouble timestamp;
863   gchar *line;
864
865   gtk_tree_model_get (model, iter,
866       COL_DEBUG_TIMESTAMP, &timestamp,
867       COL_DEBUG_DOMAIN, &domain,
868       COL_DEBUG_CATEGORY, &category,
869       COL_DEBUG_LEVEL_STRING, &level_str,
870       COL_DEBUG_MESSAGE, &message,
871       -1);
872
873   level_upper = g_ascii_strup (level_str, -1);
874
875   line = g_strdup_printf ("%s%s%s-%s: %e: %s\n",
876       domain, EMP_STR_EMPTY (category) ? "" : "/",
877       category, level_upper, timestamp, message);
878
879   tmp = g_strconcat (*text, line, NULL);
880
881   g_free (*text);
882   g_free (line);
883   g_free (level_upper);
884   g_free (level_str);
885   g_free (domain);
886   g_free (category);
887   g_free (message);
888
889   *text = tmp;
890
891   return FALSE;
892 }
893
894 static void
895 debug_dialog_copy_clicked_cb (GtkToolButton *tool_button,
896     EmpathyDebugDialog *debug_dialog)
897 {
898   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
899   GtkClipboard *clipboard;
900   gchar *text;
901
902   text = g_strdup ("");
903
904   gtk_tree_model_foreach (priv->store_filter,
905       debug_dialog_copy_model_foreach, &text);
906
907   clipboard = gtk_clipboard_get_for_display (
908       gtk_widget_get_display (GTK_WIDGET (tool_button)),
909       GDK_SELECTION_CLIPBOARD);
910
911   DEBUG ("Copying text to clipboard (length: %" G_GSIZE_FORMAT ")",
912       strlen (text));
913
914   gtk_clipboard_set_text (clipboard, text, -1);
915
916   g_free (text);
917 }
918
919 static GObject *
920 debug_dialog_constructor (GType type,
921     guint n_construct_params,
922     GObjectConstructParam *construct_params)
923 {
924   GObject *object;
925   EmpathyDebugDialogPriv *priv;
926   GtkWidget *vbox;
927   GtkWidget *toolbar;
928   GtkWidget *image;
929   GtkWidget *label;
930   GtkToolItem *item;
931   GtkCellRenderer *renderer;
932   GtkListStore *level_store;
933   GtkTreeIter iter;
934
935   object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
936     (type, n_construct_params, construct_params);
937   priv = GET_PRIV (object);
938
939   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
940   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
941
942   vbox = GTK_DIALOG (object)->vbox;
943
944   toolbar = gtk_toolbar_new ();
945   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
946   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
947   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar),
948       GTK_ICON_SIZE_SMALL_TOOLBAR);
949   gtk_widget_show (toolbar);
950
951   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
952
953   /* CM */
954   priv->cm_chooser = gtk_combo_box_new_text ();
955   priv->cms = gtk_list_store_new (NUM_COLS_CM, G_TYPE_STRING, G_TYPE_STRING);
956   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->cm_chooser),
957       GTK_TREE_MODEL (priv->cms));
958   gtk_widget_show (priv->cm_chooser);
959
960   item = gtk_tool_item_new ();
961   gtk_widget_show (GTK_WIDGET (item));
962   gtk_container_add (GTK_CONTAINER (item), priv->cm_chooser);
963   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
964   debug_dialog_fill_cm_chooser (EMPATHY_DEBUG_DIALOG (object));
965   g_signal_connect (priv->cm_chooser, "changed",
966       G_CALLBACK (debug_dialog_cm_chooser_changed_cb), object);
967   gtk_widget_show (GTK_WIDGET (priv->cm_chooser));
968
969   item = gtk_separator_tool_item_new ();
970   gtk_widget_show (GTK_WIDGET (item));
971   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
972
973   /* Save */
974   priv->save_button = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
975   g_signal_connect (priv->save_button, "clicked",
976       G_CALLBACK (debug_dialog_save_clicked_cb), object);
977   gtk_widget_show (GTK_WIDGET (priv->save_button));
978   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->save_button), TRUE);
979   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->save_button, -1);
980
981   /* Copy */
982   priv->copy_button = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
983   g_signal_connect (priv->copy_button, "clicked",
984       G_CALLBACK (debug_dialog_copy_clicked_cb), object);
985   gtk_widget_show (GTK_WIDGET (priv->copy_button));
986   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->copy_button), TRUE);
987   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->copy_button, -1);
988
989   /* Clear */
990   priv->clear_button = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
991   g_signal_connect (priv->clear_button, "clicked",
992       G_CALLBACK (debug_dialog_clear_clicked_cb), object);
993   gtk_widget_show (GTK_WIDGET (priv->clear_button));
994   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->clear_button), TRUE);
995   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->clear_button, -1);
996
997   item = gtk_separator_tool_item_new ();
998   gtk_widget_show (GTK_WIDGET (item));
999   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1000
1001   /* Pause */
1002   priv->paused = FALSE;
1003   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE,
1004       GTK_ICON_SIZE_MENU);
1005   gtk_widget_show (image);
1006   priv->pause_button = gtk_toggle_tool_button_new ();
1007   gtk_toggle_tool_button_set_active (
1008       GTK_TOGGLE_TOOL_BUTTON (priv->pause_button), priv->paused);
1009   g_signal_connect (priv->pause_button, "toggled",
1010       G_CALLBACK (debug_dialog_pause_toggled_cb), object);
1011   gtk_widget_show (GTK_WIDGET (priv->pause_button));
1012   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->pause_button), TRUE);
1013   gtk_tool_button_set_label (GTK_TOOL_BUTTON (priv->pause_button), _("Pause"));
1014   gtk_tool_button_set_icon_widget (
1015       GTK_TOOL_BUTTON (priv->pause_button), image);
1016   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->pause_button, -1);
1017
1018   item = gtk_separator_tool_item_new ();
1019   gtk_widget_show (GTK_WIDGET (item));
1020   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1021
1022   /* Level */
1023   priv->level_label = gtk_tool_item_new ();
1024   gtk_widget_show (GTK_WIDGET (priv->level_label));
1025   label = gtk_label_new (_("Level "));
1026   gtk_widget_show (label);
1027   gtk_container_add (GTK_CONTAINER (priv->level_label), label);
1028   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->level_label, -1);
1029
1030   priv->level_filter = gtk_combo_box_new_text ();
1031   gtk_widget_show (priv->level_filter);
1032
1033   item = gtk_tool_item_new ();
1034   gtk_widget_show (GTK_WIDGET (item));
1035   gtk_container_add (GTK_CONTAINER (item), priv->level_filter);
1036   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1037
1038   level_store = gtk_list_store_new (NUM_COLS_LEVEL,
1039       G_TYPE_STRING, G_TYPE_UINT);
1040   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->level_filter),
1041       GTK_TREE_MODEL (level_store));
1042
1043   gtk_list_store_append (level_store, &iter);
1044   gtk_list_store_set (level_store, &iter,
1045       COL_LEVEL_NAME, _("Debug"),
1046       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_DEBUG,
1047       -1);
1048
1049   gtk_list_store_append (level_store, &iter);
1050   gtk_list_store_set (level_store, &iter,
1051       COL_LEVEL_NAME, _("Info"),
1052       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_INFO,
1053       -1);
1054
1055   gtk_list_store_append (level_store, &iter);
1056   gtk_list_store_set (level_store, &iter,
1057       COL_LEVEL_NAME, _("Message"),
1058       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_MESSAGE,
1059       -1);
1060
1061   gtk_list_store_append (level_store, &iter);
1062   gtk_list_store_set (level_store, &iter,
1063       COL_LEVEL_NAME, _("Warning"),
1064       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_WARNING,
1065       -1);
1066
1067   gtk_list_store_append (level_store, &iter);
1068   gtk_list_store_set (level_store, &iter,
1069       COL_LEVEL_NAME, _("Critical"),
1070       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_CRITICAL,
1071       -1);
1072
1073   gtk_list_store_append (level_store, &iter);
1074   gtk_list_store_set (level_store, &iter,
1075       COL_LEVEL_NAME, _("Error"),
1076       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_ERROR,
1077       -1);
1078
1079   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->level_filter), 0);
1080   g_signal_connect (priv->level_filter, "changed",
1081       G_CALLBACK (debug_dialog_filter_changed_cb), object);
1082
1083   /* Debug treeview */
1084   priv->view = gtk_tree_view_new ();
1085   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
1086
1087   g_signal_connect (priv->view, "button-press-event",
1088       G_CALLBACK (debug_dialog_button_press_event_cb), object);
1089
1090   renderer = gtk_cell_renderer_text_new ();
1091   g_object_set (renderer, "yalign", 0, NULL);
1092
1093   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1094       -1, _("Time"), renderer, "text", COL_DEBUG_TIMESTAMP, NULL);
1095   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1096       -1, _("Domain"), renderer, "text", COL_DEBUG_DOMAIN, NULL);
1097   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1098       -1, _("Category"), renderer, "text", COL_DEBUG_CATEGORY, NULL);
1099   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1100       -1, _("Level"), renderer, "text", COL_DEBUG_LEVEL_STRING, NULL);
1101
1102   renderer = gtk_cell_renderer_text_new ();
1103   g_object_set (renderer, "family", "Monospace", NULL);
1104   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1105       -1, _("Message"), renderer, "text", COL_DEBUG_MESSAGE, NULL);
1106
1107   priv->store = gtk_list_store_new (NUM_DEBUG_COLS, G_TYPE_DOUBLE,
1108       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
1109       G_TYPE_UINT);
1110
1111   priv->store_filter = gtk_tree_model_filter_new (
1112       GTK_TREE_MODEL (priv->store), NULL);
1113
1114   gtk_tree_model_filter_set_visible_func (
1115       GTK_TREE_MODEL_FILTER (priv->store_filter),
1116       debug_dialog_visible_func, object, NULL);
1117
1118   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view), priv->store_filter);
1119
1120   /* Scrolled window */
1121   priv->scrolled_win = g_object_ref (gtk_scrolled_window_new (NULL, NULL));
1122   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_win),
1123       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1124
1125   gtk_widget_show (priv->view);
1126   gtk_container_add (GTK_CONTAINER (priv->scrolled_win), priv->view);
1127
1128   gtk_widget_show (priv->scrolled_win);
1129   gtk_box_pack_start (GTK_BOX (vbox), priv->scrolled_win, TRUE, TRUE, 0);
1130
1131   /* Not supported label */
1132   priv->not_supported_label = g_object_ref (gtk_label_new (
1133           _("The selected connection manager does not support the remote "
1134               "debugging extension.")));
1135   gtk_widget_show (priv->not_supported_label);
1136
1137   priv->view_visible = TRUE;
1138   gtk_widget_show (GTK_WIDGET (object));
1139
1140   return object;
1141 }
1142
1143 static void
1144 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
1145 {
1146   EmpathyDebugDialogPriv *priv =
1147       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
1148       EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
1149
1150   empathy_debug_dialog->priv = priv;
1151
1152   priv->dispose_run = FALSE;
1153 }
1154
1155 static void
1156 debug_dialog_set_property (GObject *object,
1157     guint prop_id,
1158     const GValue *value,
1159     GParamSpec *pspec)
1160 {
1161   switch (prop_id)
1162     {
1163       default:
1164         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1165         break;
1166     }
1167 }
1168
1169 static void
1170 debug_dialog_get_property (GObject *object,
1171     guint prop_id,
1172     GValue *value,
1173     GParamSpec *pspec)
1174 {
1175   switch (prop_id)
1176     {
1177       default:
1178         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1179         break;
1180     }
1181 }
1182
1183 static void
1184 debug_dialog_dispose (GObject *object)
1185 {
1186   EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
1187   EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
1188
1189   if (priv->dispose_run)
1190     return;
1191
1192   priv->dispose_run = TRUE;
1193
1194   if (priv->store != NULL)
1195     g_object_unref (priv->store);
1196
1197   if (priv->scrolled_win != NULL)
1198     g_object_unref (priv->scrolled_win);
1199
1200   if (priv->view != NULL)
1201     g_object_unref (priv->view);
1202
1203   if (priv->name_owner_changed_signal != NULL)
1204     tp_proxy_signal_connection_disconnect (priv->name_owner_changed_signal);
1205
1206   if (priv->proxy != NULL)
1207     {
1208       debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
1209       g_object_unref (priv->proxy);
1210     }
1211
1212   if (priv->new_debug_message_signal != NULL)
1213     tp_proxy_signal_connection_disconnect (priv->new_debug_message_signal);
1214
1215   if (priv->cms != NULL)
1216     g_object_unref (priv->cms);
1217
1218   if (priv->dbus != NULL)
1219     g_object_unref (priv->dbus);
1220
1221   (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
1222 }
1223
1224 static void
1225 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
1226 {
1227   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1228   object_class->constructor = debug_dialog_constructor;
1229   object_class->dispose = debug_dialog_dispose;
1230   object_class->set_property = debug_dialog_set_property;
1231   object_class->get_property = debug_dialog_get_property;
1232   g_type_class_add_private (klass, sizeof (EmpathyDebugDialogPriv));
1233 }
1234
1235 /* public methods */
1236
1237 GtkWidget *
1238 empathy_debug_dialog_new (GtkWindow *parent)
1239 {
1240   g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
1241
1242   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
1243       "transient-for", parent, NULL));
1244 }