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