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