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