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