]> git.0d.be Git - empathy.git/blob - src/empathy-debug-window.c
b1234d7ce75d5c32efa6c5fec4bc7d89b3c52977
[empathy.git] / src / empathy-debug-window.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 *           Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20 */
21
22 #include "config.h"
23
24 #include <glib/gi18n.h>
25 #include <gtk/gtk.h>
26 #include <gio/gio.h>
27 #include <gdk/gdkkeysyms.h>
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include <libempathy/empathy-debug.h>
31 #include <libempathy/empathy-utils.h>
32
33 #include <libempathy-gtk/empathy-account-chooser.h>
34 #include <libempathy-gtk/empathy-geometry.h>
35
36 #include <telepathy-glib/dbus.h>
37 #include <telepathy-glib/interfaces.h>
38 #include <telepathy-glib/util.h>
39 #include <telepathy-glib/proxy-subclass.h>
40 #include <telepathy-glib/account-manager.h>
41
42 #include "extensions/extensions.h"
43
44 #include "empathy-debug-window.h"
45
46 G_DEFINE_TYPE (EmpathyDebugWindow, empathy_debug_window,
47     GTK_TYPE_WINDOW)
48
49 typedef enum
50 {
51   SERVICE_TYPE_CM = 0,
52   SERVICE_TYPE_CLIENT,
53 } ServiceType;
54
55 enum
56 {
57   COL_DEBUG_TIMESTAMP = 0,
58   COL_DEBUG_DOMAIN,
59   COL_DEBUG_CATEGORY,
60   COL_DEBUG_LEVEL_STRING,
61   COL_DEBUG_MESSAGE,
62   COL_DEBUG_LEVEL_VALUE,
63   NUM_DEBUG_COLS
64 };
65
66 enum
67 {
68   COL_NAME = 0,
69   COL_UNIQUE_NAME,
70   COL_GONE,
71   NUM_COLS
72 };
73
74 enum
75 {
76   COL_LEVEL_NAME,
77   COL_LEVEL_VALUE,
78   NUM_COLS_LEVEL
79 };
80
81 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDebugWindow)
82 typedef struct
83 {
84   /* Toolbar items */
85   GtkWidget *chooser;
86   GtkToolItem *save_button;
87   GtkToolItem *copy_button;
88   GtkToolItem *clear_button;
89   GtkToolItem *pause_button;
90   GtkToolItem *level_label;
91   GtkWidget *level_filter;
92
93   /* Cache */
94   GHashTable *cache;
95
96   /* TreeView */
97   GtkListStore *store;
98   GtkTreeModel *store_filter;
99   GtkWidget *view;
100   GtkWidget *scrolled_win;
101   GtkWidget *not_supported_label;
102   gboolean view_visible;
103
104   /* Connection */
105   TpDBusDaemon *dbus;
106   TpProxy *proxy;
107   TpProxySignalConnection *new_debug_message_signal;
108   TpProxySignalConnection *name_owner_changed_signal;
109   gulong invalid_signal_id;
110
111   /* Whether NewDebugMessage will be fired */
112   gboolean paused;
113
114   /* Service (CM, Client) chooser store */
115   GtkListStore *service_store;
116
117   /* Misc. */
118   gboolean dispose_run;
119   TpAccountManager *am;
120 } EmpathyDebugWindowPriv;
121
122 static const gchar *
123 log_level_to_string (guint level)
124 {
125   switch (level)
126     {
127     case TP_DEBUG_LEVEL_ERROR:
128       return "Error";
129       break;
130     case TP_DEBUG_LEVEL_CRITICAL:
131       return "Critical";
132       break;
133     case TP_DEBUG_LEVEL_WARNING:
134       return "Warning";
135       break;
136     case TP_DEBUG_LEVEL_MESSAGE:
137       return "Message";
138       break;
139     case TP_DEBUG_LEVEL_INFO:
140       return "Info";
141       break;
142     case TP_DEBUG_LEVEL_DEBUG:
143       return "Debug";
144       break;
145     default:
146       g_assert_not_reached ();
147       break;
148     }
149 }
150
151 typedef struct
152 {
153   gdouble timestamp;
154   gchar *domain;
155   guint level;
156   gchar *message;
157 } DebugMessage;
158
159 static DebugMessage *
160 debug_message_new (gdouble timestamp,
161     const gchar *domain,
162     guint level,
163     const gchar *message)
164 {
165   DebugMessage *retval = g_slice_new0 (DebugMessage);
166
167   retval->timestamp = timestamp;
168   retval->domain = g_strdup (domain);
169   retval->level = level;
170   retval->message = g_strdup (message);
171
172   return retval;
173 }
174
175 static void
176 debug_message_free (DebugMessage *dm)
177 {
178   g_free (dm->domain);
179   g_free (dm->message);
180
181   g_slice_free (DebugMessage, dm);
182 }
183
184 static void
185 debug_message_list_free (gpointer data)
186 {
187   GList *list = data;
188
189   g_list_foreach (list, (GFunc) debug_message_free, NULL);
190   g_list_free (list);
191 }
192
193 static gchar *
194 get_active_service_name (EmpathyDebugWindow *self)
195 {
196   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
197   GtkTreeIter iter;
198   gchar *name;
199
200   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->chooser), &iter))
201     return NULL;
202
203   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
204       COL_NAME, &name, -1);
205
206   return name;
207 }
208
209 static void
210 debug_window_cache_new_message (EmpathyDebugWindow *debug_window,
211     gdouble timestamp,
212     const gchar *domain,
213     guint level,
214     const gchar *message)
215 {
216   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
217   GList *messages;
218   DebugMessage *dm;
219   char *name;
220
221   name = get_active_service_name (debug_window);
222   messages = g_hash_table_lookup (priv->cache, name);
223
224   dm = debug_message_new (timestamp, domain, level, message);
225   messages = g_list_append (messages, dm);
226
227   g_hash_table_insert (priv->cache, name, messages);
228 }
229
230 static void
231 debug_window_add_message (EmpathyDebugWindow *debug_window,
232     gboolean should_cache,
233     gdouble timestamp,
234     const gchar *domain_category,
235     guint level,
236     const gchar *message)
237 {
238   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
239   gchar *domain, *category;
240   GtkTreeIter iter;
241   gchar *string;
242
243   if (should_cache)
244     debug_window_cache_new_message (debug_window, timestamp, domain_category,
245         level, message);
246
247   if (g_strrstr (domain_category, "/"))
248     {
249       gchar **parts = g_strsplit (domain_category, "/", 2);
250       domain = g_strdup (parts[0]);
251       category = g_strdup (parts[1]);
252       g_strfreev (parts);
253     }
254   else
255     {
256       domain = g_strdup (domain_category);
257       category = g_strdup ("");
258     }
259
260   if (g_str_has_suffix (message, "\n"))
261     string = g_strchomp (g_strdup (message));
262   else
263     string = g_strdup (message);
264
265
266   gtk_list_store_append (priv->store, &iter);
267   gtk_list_store_set (priv->store, &iter,
268       COL_DEBUG_TIMESTAMP, timestamp,
269       COL_DEBUG_DOMAIN, domain,
270       COL_DEBUG_CATEGORY, category,
271       COL_DEBUG_LEVEL_STRING, log_level_to_string (level),
272       COL_DEBUG_MESSAGE, string,
273       COL_DEBUG_LEVEL_VALUE, level,
274       -1);
275
276   g_free (string);
277   g_free (domain);
278   g_free (category);
279 }
280
281 static void
282 debug_window_new_debug_message_cb (TpProxy *proxy,
283     gdouble timestamp,
284     const gchar *domain,
285     guint level,
286     const gchar *message,
287     gpointer user_data,
288     GObject *weak_object)
289 {
290   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
291
292   debug_window_add_message (debug_window, TRUE, timestamp, domain, level,
293       message);
294 }
295
296 static void
297 debug_window_set_enabled (EmpathyDebugWindow *debug_window,
298     gboolean enabled)
299 {
300   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
301   GValue *val;
302
303   val = tp_g_value_slice_new_boolean (enabled);
304
305   tp_cli_dbus_properties_call_set (priv->proxy, -1, TP_IFACE_DEBUG,
306       "Enabled", val, NULL, NULL, NULL, NULL);
307
308   tp_g_value_slice_free (val);
309 }
310
311 static void
312 debug_window_set_toolbar_sensitivity (EmpathyDebugWindow *debug_window,
313     gboolean sensitive)
314 {
315   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
316   GtkWidget *vbox = gtk_bin_get_child (GTK_BIN (debug_window));
317
318   gtk_widget_set_sensitive (GTK_WIDGET (priv->save_button), sensitive);
319   gtk_widget_set_sensitive (GTK_WIDGET (priv->copy_button), sensitive);
320   gtk_widget_set_sensitive (GTK_WIDGET (priv->clear_button), sensitive);
321   gtk_widget_set_sensitive (GTK_WIDGET (priv->pause_button), sensitive);
322   gtk_widget_set_sensitive (GTK_WIDGET (priv->level_label), sensitive);
323   gtk_widget_set_sensitive (GTK_WIDGET (priv->level_filter), sensitive);
324   gtk_widget_set_sensitive (GTK_WIDGET (priv->view), sensitive);
325
326   if (sensitive && !priv->view_visible)
327     {
328       /* Add view and remove label */
329       gtk_container_remove (GTK_CONTAINER (vbox), priv->not_supported_label);
330       gtk_box_pack_start (GTK_BOX (vbox), priv->scrolled_win, TRUE, TRUE, 0);
331       priv->view_visible = TRUE;
332     }
333   else if (!sensitive && priv->view_visible)
334     {
335       /* Add label and remove view */
336       gtk_container_remove (GTK_CONTAINER (vbox), priv->scrolled_win);
337       gtk_box_pack_start (GTK_BOX (vbox), priv->not_supported_label,
338           TRUE, TRUE, 0);
339       priv->view_visible = FALSE;
340     }
341 }
342
343 static void
344 debug_window_get_messages_cb (TpProxy *proxy,
345     const GPtrArray *messages,
346     const GError *error,
347     gpointer user_data,
348     GObject *weak_object)
349 {
350   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
351   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
352   gchar *name;
353   GList *old_messages;
354   guint i;
355
356   if (error != NULL)
357     {
358       DEBUG ("GetMessages failed: %s", error->message);
359       debug_window_set_toolbar_sensitivity (debug_window, FALSE);
360       return;
361     }
362
363   debug_window_set_toolbar_sensitivity (debug_window, TRUE);
364
365   name = get_active_service_name (debug_window);
366   old_messages = g_hash_table_lookup (priv->cache, name);
367
368   /* we call get_messages either when a new CM is added or
369    * when a CM that we've already seen re-appears; in both cases
370    * we don't need our old cache anymore.
371    */
372   if (old_messages != NULL)
373     {
374       g_hash_table_remove (priv->cache, name);
375       debug_message_list_free (old_messages);
376     }
377
378   for (i = 0; i < messages->len; i++)
379     {
380       GValueArray *values = g_ptr_array_index (messages, i);
381
382       debug_window_add_message (debug_window, TRUE,
383           g_value_get_double (g_value_array_get_nth (values, 0)),
384           g_value_get_string (g_value_array_get_nth (values, 1)),
385           g_value_get_uint (g_value_array_get_nth (values, 2)),
386           g_value_get_string (g_value_array_get_nth (values, 3)));
387     }
388
389   /* Connect to NewDebugMessage */
390   priv->new_debug_message_signal = emp_cli_debug_connect_to_new_debug_message (
391       proxy, debug_window_new_debug_message_cb, debug_window,
392       NULL, NULL, NULL);
393
394   /* Set Enabled as appropriate */
395   debug_window_set_enabled (debug_window, !priv->paused);
396 }
397
398 static void
399 debug_window_add_log_messages_from_cache (EmpathyDebugWindow *debug_window,
400     const gchar *name)
401 {
402   GList *messages, *l;
403   DebugMessage *dm;
404   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
405
406   DEBUG ("Adding logs from cache for CM %s", name);
407
408   messages = g_hash_table_lookup (priv->cache, name);
409
410   if (messages == NULL)
411     return;
412
413   for (l = messages; l != NULL; l = l->next)
414     {
415       dm = l->data;
416
417       debug_window_add_message (debug_window, FALSE, dm->timestamp,
418           dm->domain, dm->level, dm->message);
419     }
420 }
421
422 static void
423 proxy_invalidated_cb (TpProxy *proxy,
424     guint domain,
425     gint code,
426     gchar *msg,
427     EmpathyDebugWindowPriv *self)
428 {
429   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
430
431   /* Proxy has been invalidated so we can't disconnect the signal any more */
432   priv->new_debug_message_signal = NULL;
433 }
434
435 static void
436 debug_window_service_chooser_changed_cb (GtkComboBox *chooser,
437     EmpathyDebugWindow *debug_window)
438 {
439   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
440   TpDBusDaemon *dbus;
441   GError *error = NULL;
442   gchar *bus_name, *name = NULL;
443   TpProxy *proxy;
444   GtkTreeIter iter;
445   gboolean gone;
446
447   if (!gtk_combo_box_get_active_iter (chooser, &iter))
448     {
449       DEBUG ("No CM is selected");
450       if (gtk_tree_model_iter_n_children (
451           GTK_TREE_MODEL (priv->service_store), NULL) > 0)
452         {
453           gtk_combo_box_set_active (chooser, 0);
454         }
455       return;
456     }
457
458   gtk_list_store_clear (priv->store);
459
460   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
461       COL_NAME, &name, COL_GONE, &gone, -1);
462
463   if (gone)
464     {
465       debug_window_add_log_messages_from_cache (debug_window, name);
466       g_free (name);
467       return;
468     }
469
470   g_free (name);
471
472   dbus = tp_dbus_daemon_dup (&error);
473
474   if (error != NULL)
475     {
476       DEBUG ("Failed at duping the dbus daemon: %s", error->message);
477     }
478
479   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
480       COL_UNIQUE_NAME, &bus_name, -1);
481   proxy = g_object_new (TP_TYPE_PROXY,
482       "bus-name", bus_name,
483       "dbus-daemon", dbus,
484       "object-path", DEBUG_OBJECT_PATH,
485       NULL);
486   g_free (bus_name);
487
488   /* Disable debug signalling */
489   if (priv->proxy != NULL)
490     debug_window_set_enabled (debug_window, FALSE);
491
492   /* Disconnect from previous NewDebugMessage signal */
493   if (priv->new_debug_message_signal != NULL)
494     {
495       tp_proxy_signal_connection_disconnect (priv->new_debug_message_signal);
496       priv->new_debug_message_signal = NULL;
497     }
498
499   if (priv->proxy != NULL)
500     {
501       g_signal_handler_disconnect (priv->proxy, priv->invalid_signal_id);
502       g_object_unref (priv->proxy);
503     }
504
505   priv->proxy = proxy;
506
507   tp_proxy_add_interface_by_id (priv->proxy, emp_iface_quark_debug ());
508
509   emp_cli_debug_call_get_messages (priv->proxy, -1,
510       debug_window_get_messages_cb, debug_window, NULL, NULL);
511
512   priv->invalid_signal_id = g_signal_connect (proxy, "invalidated",
513       G_CALLBACK (proxy_invalidated_cb), debug_window);
514
515   g_object_unref (dbus);
516 }
517
518 typedef struct
519 {
520   const gchar *name;
521   gboolean found;
522   gboolean use_name;
523   GtkTreeIter **found_iter;
524 } CmInModelForeachData;
525
526 static gboolean
527 debug_window_service_foreach (GtkTreeModel *model,
528     GtkTreePath *path,
529     GtkTreeIter *iter,
530     gpointer user_data)
531 {
532   CmInModelForeachData *data = (CmInModelForeachData *) user_data;
533   gchar *store_name;
534
535   gtk_tree_model_get (model, iter,
536       (data->use_name ? COL_NAME : COL_UNIQUE_NAME),
537       &store_name,
538       -1);
539
540   if (!tp_strdiff (store_name, data->name))
541     {
542       data->found = TRUE;
543
544       if (data->found_iter != NULL)
545         *(data->found_iter) = gtk_tree_iter_copy (iter);
546     }
547
548   g_free (store_name);
549
550   return data->found;
551 }
552
553 static gboolean
554 debug_window_service_is_in_model (EmpathyDebugWindow *debug_window,
555     const gchar *name,
556     GtkTreeIter **iter,
557     gboolean use_name)
558 {
559   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
560   CmInModelForeachData *data;
561   gboolean found;
562
563   data = g_slice_new0 (CmInModelForeachData);
564   data->name = name;
565   data->found = FALSE;
566   data->found_iter = iter;
567   data->use_name = use_name;
568
569   gtk_tree_model_foreach (GTK_TREE_MODEL (priv->service_store),
570       debug_window_service_foreach, data);
571
572   found = data->found;
573
574   g_slice_free (CmInModelForeachData, data);
575
576   return found;
577 }
578
579 static gchar *
580 get_cm_display_name (EmpathyDebugWindow *self,
581     const char *cm_name)
582 {
583   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
584   GHashTable *protocols = g_hash_table_new (g_str_hash, g_str_equal);
585   GList *accounts, *ptr;
586   char *retval;
587
588   accounts = tp_account_manager_get_valid_accounts (priv->am);
589
590   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
591     {
592       TpAccount *account = TP_ACCOUNT (ptr->data);
593
594       if (!tp_strdiff (tp_account_get_connection_manager (account), cm_name))
595         {
596           g_hash_table_insert (protocols,
597               (char *) tp_account_get_protocol (account),
598               GUINT_TO_POINTER (TRUE));
599         }
600     }
601
602   g_list_free (accounts);
603
604   if (g_hash_table_size (protocols) > 0)
605     {
606       GHashTableIter iter;
607       char **protocolsv;
608       char *key, *str;
609       guint i;
610
611       protocolsv = g_new0 (char *, g_hash_table_size (protocols) + 1);
612
613       g_hash_table_iter_init (&iter, protocols);
614       for (i = 0; g_hash_table_iter_next (&iter, (gpointer) &key, NULL); i++)
615         {
616           protocolsv[i] = key;
617         }
618
619       str = g_strjoinv (", ", protocolsv);
620       retval = g_strdup_printf ("%s (%s)", cm_name, str);
621
622       g_free (protocolsv);
623       g_free (str);
624     }
625   else
626     {
627       retval = g_strdup (cm_name);
628     }
629
630   g_hash_table_destroy (protocols);
631
632   return retval;
633 }
634
635 typedef struct
636 {
637   EmpathyDebugWindow *debug_window;
638   gchar *name;
639   ServiceType type;
640 } FillServiceChooserData;
641
642 static FillServiceChooserData *
643 fill_service_chooser_data_new (EmpathyDebugWindow *window,
644     const gchar *name,
645     ServiceType type)
646 {
647   FillServiceChooserData * data = g_slice_new (FillServiceChooserData);
648
649   data->debug_window = window;
650   data->name = g_strdup (name);
651   data->type = SERVICE_TYPE_CM;
652   return data;
653 }
654
655 static void
656 fill_service_chooser_data_free (FillServiceChooserData *data)
657 {
658   g_free (data->name);
659   g_slice_free (FillServiceChooserData, data);
660 }
661
662 static void
663 debug_window_get_name_owner_cb (TpDBusDaemon *proxy,
664     const gchar *out,
665     const GError *error,
666     gpointer user_data,
667     GObject *weak_object)
668 {
669   FillServiceChooserData *data = (FillServiceChooserData *) user_data;
670   EmpathyDebugWindow *self = EMPATHY_DEBUG_WINDOW (data->debug_window);
671   EmpathyDebugWindowPriv *priv = GET_PRIV (data->debug_window);
672
673   if (error != NULL)
674     {
675       DEBUG ("GetNameOwner failed: %s", error->message);
676       goto OUT;
677     }
678
679   if (!debug_window_service_is_in_model (data->debug_window, out, NULL, FALSE))
680     {
681       GtkTreeIter iter;
682       char *name;
683
684       DEBUG ("Adding CM to list: %s at unique name: %s",
685           data->name, out);
686
687       name = get_cm_display_name (self, data->name);
688
689       gtk_list_store_append (priv->service_store, &iter);
690       gtk_list_store_set (priv->service_store, &iter,
691           COL_NAME, name,
692           COL_UNIQUE_NAME, out,
693           -1);
694
695       g_free (name);
696     }
697
698 OUT:
699   fill_service_chooser_data_free (data);
700 }
701
702 static void
703 debug_window_list_connection_names_cb (const gchar * const *names,
704     gsize n,
705     const gchar * const *cms,
706     const gchar * const *protocols,
707     const GError *error,
708     gpointer user_data,
709     GObject *weak_object)
710 {
711   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
712   guint i;
713   TpDBusDaemon *dbus;
714   GError *error2 = NULL;
715
716   if (error != NULL)
717     {
718       DEBUG ("list_connection_names failed: %s", error->message);
719       return;
720     }
721
722   dbus = tp_dbus_daemon_dup (&error2);
723
724   if (error2 != NULL)
725     {
726       DEBUG ("Failed to dup TpDBusDaemon.");
727       g_error_free (error2);
728       return;
729     }
730
731   for (i = 0; cms[i] != NULL; i++)
732     {
733       FillServiceChooserData *data = fill_service_chooser_data_new (
734           debug_window, cms[i], SERVICE_TYPE_CM);
735
736       tp_cli_dbus_daemon_call_get_name_owner (dbus, -1,
737           names[i], debug_window_get_name_owner_cb,
738           data, NULL, NULL);
739     }
740
741   g_object_unref (dbus);
742 }
743
744 #define CM_WELL_KNOWN_NAME_PREFIX \
745     "org.freedesktop.Telepathy.ConnectionManager."
746
747 static void
748 debug_window_name_owner_changed_cb (TpDBusDaemon *proxy,
749     const gchar *arg0,
750     const gchar *arg1,
751     const gchar *arg2,
752     gpointer user_data,
753     GObject *weak_object)
754 {
755   EmpathyDebugWindow *self = EMPATHY_DEBUG_WINDOW (user_data);
756   EmpathyDebugWindowPriv *priv = GET_PRIV (user_data);
757
758   /* Wow, I hate all of this code... */
759   if (!g_str_has_prefix (arg0, CM_WELL_KNOWN_NAME_PREFIX))
760     return;
761
762   if (EMP_STR_EMPTY (arg1) && !EMP_STR_EMPTY (arg2))
763     {
764       /* A connection manager joined -- because it's guaranteed
765        * that the CM just joined (because o.fd.Tp.CM.foo
766        * just joined), we don't need to check whether the unique
767        * name is in the CM model. Hooray.
768        */
769       const gchar *name = arg0 + strlen (CM_WELL_KNOWN_NAME_PREFIX);
770
771       if (!g_hash_table_lookup (priv->cache, name))
772         {
773           GtkTreeIter iter;
774           char *str;
775
776           DEBUG ("Adding new CM '%s' at %s.", name, arg2);
777
778           str = get_cm_display_name (self, name);
779
780           gtk_list_store_append (priv->service_store, &iter);
781           gtk_list_store_set (priv->service_store, &iter,
782               COL_NAME, str,
783               COL_UNIQUE_NAME, arg2,
784               -1);
785
786           g_free (str);
787         }
788       else
789         {
790           /* a CM with the same name is already in the hash table,
791            * update it and set it as re-enabled in the model.
792            */
793           GtkTreeIter *iter = NULL;
794
795           if (debug_window_service_is_in_model (user_data, name, &iter, TRUE))
796             {
797               char *str;
798
799               DEBUG ("Refreshing CM '%s' at '%s'.", name, arg2);
800
801               str = get_cm_display_name (self, name);
802
803               gtk_list_store_set (priv->service_store, iter,
804                   COL_NAME, str,
805                   COL_UNIQUE_NAME, arg2,
806                   COL_GONE, FALSE,
807                   -1);
808               gtk_tree_iter_free (iter);
809               g_free (str);
810
811               debug_window_service_chooser_changed_cb
812                 (GTK_COMBO_BOX (priv->chooser), user_data);
813             }
814         }
815     }
816   else if (!EMP_STR_EMPTY (arg1) && EMP_STR_EMPTY (arg2))
817     {
818       /* A connection manager died -- because it's guaranteed
819        * that the CM itself just died (because o.fd.Tp.CM.foo
820        * just died), we don't need to check that it was already
821        * in the model.
822        */
823       GtkTreeIter *iter = NULL;
824
825       DEBUG ("Setting CM disabled from %s.", arg1);
826
827       /* set the CM as disabled in the model */
828       if (debug_window_service_is_in_model (user_data, arg1, &iter, FALSE))
829         {
830           gtk_list_store_set (priv->service_store,
831               iter, COL_GONE, TRUE, -1);
832           gtk_tree_iter_free (iter);
833         }
834     }
835 }
836
837 static void
838 add_client (EmpathyDebugWindow *self,
839     const gchar *name)
840 {
841   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
842   const gchar *suffix;
843   GtkTreeIter iter;
844
845   suffix = name + strlen (TP_CLIENT_BUS_NAME_BASE);
846
847   gtk_list_store_append (priv->service_store, &iter);
848   gtk_list_store_set (priv->service_store, &iter,
849       COL_NAME, suffix,
850       COL_UNIQUE_NAME, name,
851       -1);
852
853   /* Select Empathy by default */
854   if (!tp_strdiff (suffix, "Empathy"))
855     {
856       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->chooser), &iter);
857     }
858 }
859
860 static void
861 list_names_cb (TpDBusDaemon *bus_daemon,
862     const gchar * const *names,
863     const GError *error,
864     gpointer user_data,
865     GObject *weak_object)
866 {
867   EmpathyDebugWindow *self = EMPATHY_DEBUG_WINDOW (weak_object);
868   guint i;
869
870   if (error != NULL)
871     {
872       DEBUG ("Failed to list names: %s", error->message);
873       return;
874     }
875
876   for (i = 0; names[i] != NULL; i++)
877     {
878       if (g_str_has_prefix (names[i], TP_CLIENT_BUS_NAME_BASE))
879         {
880           add_client (self, names[i]);
881         }
882     }
883 }
884
885 static void
886 debug_window_fill_service_chooser (EmpathyDebugWindow *debug_window)
887 {
888   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
889   GError *error = NULL;
890   GtkTreeIter iter;
891
892   priv->dbus = tp_dbus_daemon_dup (&error);
893
894   if (error != NULL)
895     {
896       DEBUG ("Failed to dup dbus daemon: %s", error->message);
897       g_error_free (error);
898       return;
899     }
900
901   /* Add CMs to list */
902   tp_list_connection_names (priv->dbus, debug_window_list_connection_names_cb,
903       debug_window, NULL, NULL);
904
905   /* add Mission Control */
906   gtk_list_store_append (priv->service_store, &iter);
907   gtk_list_store_set (priv->service_store, &iter,
908       COL_NAME, "misson-control",
909       COL_UNIQUE_NAME, "org.freedesktop.Telepathy.MissionControl5",
910       -1);
911
912   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->chooser), 0);
913
914   /* add clients */
915   tp_dbus_daemon_list_names (priv->dbus, 2000,
916       list_names_cb, NULL, NULL, G_OBJECT (debug_window));
917
918   priv->name_owner_changed_signal =
919       tp_cli_dbus_daemon_connect_to_name_owner_changed (priv->dbus,
920       debug_window_name_owner_changed_cb, debug_window, NULL, NULL, NULL);
921 }
922
923 static void
924 debug_window_pause_toggled_cb (GtkToggleToolButton *pause_,
925     EmpathyDebugWindow *debug_window)
926 {
927   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
928
929   priv->paused = gtk_toggle_tool_button_get_active (pause_);
930
931   debug_window_set_enabled (debug_window, !priv->paused);
932 }
933
934 static gboolean
935 debug_window_visible_func (GtkTreeModel *model,
936     GtkTreeIter *iter,
937     gpointer user_data)
938 {
939   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
940   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
941   guint filter_value, level;
942   GtkTreeModel *filter_model;
943   GtkTreeIter filter_iter;
944
945   filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->level_filter));
946   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->level_filter),
947       &filter_iter);
948
949   gtk_tree_model_get (model, iter, COL_DEBUG_LEVEL_VALUE, &level, -1);
950   gtk_tree_model_get (filter_model, &filter_iter,
951       COL_LEVEL_VALUE, &filter_value, -1);
952
953   if (level <= filter_value)
954     return TRUE;
955
956   return FALSE;
957 }
958
959 static void
960 debug_window_filter_changed_cb (GtkComboBox *filter,
961     EmpathyDebugWindow *debug_window)
962 {
963   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
964
965   gtk_tree_model_filter_refilter (
966       GTK_TREE_MODEL_FILTER (priv->store_filter));
967 }
968
969 static void
970 debug_window_clear_clicked_cb (GtkToolButton *clear_button,
971     EmpathyDebugWindow *debug_window)
972 {
973   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
974
975   gtk_list_store_clear (priv->store);
976 }
977
978 static void
979 debug_window_menu_copy_activate_cb (GtkMenuItem *menu_item,
980     EmpathyDebugWindow *debug_window)
981 {
982   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
983   GtkTreePath *path;
984   GtkTreeViewColumn *focus_column;
985   GtkTreeIter iter;
986   gchar *message;
987   GtkClipboard *clipboard;
988
989   gtk_tree_view_get_cursor (GTK_TREE_VIEW (priv->view),
990       &path, &focus_column);
991
992   if (path == NULL)
993     {
994       DEBUG ("No row is in focus");
995       return;
996     }
997
998   gtk_tree_model_get_iter (priv->store_filter, &iter, path);
999
1000   gtk_tree_model_get (priv->store_filter, &iter,
1001       COL_DEBUG_MESSAGE, &message,
1002       -1);
1003
1004   if (EMP_STR_EMPTY (message))
1005     {
1006       DEBUG ("Log message is empty");
1007       return;
1008     }
1009
1010   clipboard = gtk_clipboard_get_for_display (
1011       gtk_widget_get_display (GTK_WIDGET (menu_item)),
1012       GDK_SELECTION_CLIPBOARD);
1013
1014   gtk_clipboard_set_text (clipboard, message, -1);
1015
1016   g_free (message);
1017 }
1018
1019 typedef struct
1020 {
1021   EmpathyDebugWindow *debug_window;
1022   guint button;
1023   guint32 time;
1024 } MenuPopupData;
1025
1026 static gboolean
1027 debug_window_show_menu (gpointer user_data)
1028 {
1029   MenuPopupData *data = (MenuPopupData *) user_data;
1030   GtkWidget *menu, *item;
1031   GtkMenuShell *shell;
1032
1033   menu = gtk_menu_new ();
1034   shell = GTK_MENU_SHELL (menu);
1035
1036   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
1037
1038   g_signal_connect (item, "activate",
1039       G_CALLBACK (debug_window_menu_copy_activate_cb), data->debug_window);
1040
1041   gtk_menu_shell_append (shell, item);
1042   gtk_widget_show (item);
1043
1044   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1045      data->button, data->time);
1046   g_object_ref_sink (menu);
1047   g_object_unref (menu);
1048
1049   g_slice_free (MenuPopupData, user_data);
1050
1051   return FALSE;
1052 }
1053
1054 static gboolean
1055 debug_window_button_press_event_cb (GtkTreeView *view,
1056     GdkEventButton *event,
1057     gpointer user_data)
1058 {
1059   /* A mouse button was pressed on the tree view. */
1060
1061   if (event->button == 3)
1062     {
1063       /* The tree view was right-clicked. (3 == third mouse button) */
1064       MenuPopupData *data;
1065       data = g_slice_new0 (MenuPopupData);
1066       data->debug_window = user_data;
1067       data->button = event->button;
1068       data->time = event->time;
1069       g_idle_add (debug_window_show_menu, data);
1070     }
1071
1072   return FALSE;
1073 }
1074
1075 static gchar *
1076 debug_window_format_timestamp (gdouble timestamp)
1077 {
1078   struct tm *tstruct;
1079   char time_str[32];
1080   gint ms;
1081   time_t sec;
1082   gchar *text;
1083
1084   ms = (int) ((timestamp - (int) timestamp)*1e6);
1085   sec = (long) timestamp;
1086   tstruct = localtime ((time_t *) &sec);
1087   if (!strftime (time_str, sizeof (time_str), "%x %T", tstruct))
1088     {
1089       DEBUG ("Failed to format timestamp: %e", timestamp);
1090       time_str[0] = '\0';
1091     }
1092
1093   text = g_strdup_printf ("%s.%d", time_str, ms);
1094
1095   return text;
1096 }
1097
1098 static void
1099 debug_window_time_formatter (GtkTreeViewColumn *tree_column,
1100     GtkCellRenderer *cell,
1101     GtkTreeModel *tree_model,
1102     GtkTreeIter *iter,
1103     gpointer data)
1104 {
1105   gdouble timestamp;
1106   gchar *time_str;
1107
1108   gtk_tree_model_get (tree_model, iter, COL_DEBUG_TIMESTAMP, &timestamp, -1);
1109
1110   time_str = debug_window_format_timestamp (timestamp);
1111
1112   g_object_set (G_OBJECT (cell), "text", time_str, NULL);
1113
1114   g_free (time_str);
1115 }
1116
1117 static gboolean
1118 debug_window_store_filter_foreach (GtkTreeModel *model,
1119     GtkTreePath *path,
1120     GtkTreeIter *iter,
1121     gpointer user_data)
1122 {
1123   GFileOutputStream *output_stream = (GFileOutputStream *) user_data;
1124   gchar *domain, *category, *message, *level_str, *level_upper;
1125   gdouble timestamp;
1126   gchar *line, *time_str;
1127   GError *error = NULL;
1128   gboolean out = FALSE;
1129
1130   gtk_tree_model_get (model, iter,
1131       COL_DEBUG_TIMESTAMP, &timestamp,
1132       COL_DEBUG_DOMAIN, &domain,
1133       COL_DEBUG_CATEGORY, &category,
1134       COL_DEBUG_LEVEL_STRING, &level_str,
1135       COL_DEBUG_MESSAGE, &message,
1136       -1);
1137
1138   level_upper = g_ascii_strup (level_str, -1);
1139
1140   time_str = debug_window_format_timestamp (timestamp);
1141
1142   line = g_strdup_printf ("%s%s%s-%s: %s: %s\n",
1143       domain, EMP_STR_EMPTY (category) ? "" : "/",
1144       category, level_upper, time_str, message);
1145
1146   g_free (time_str);
1147
1148   g_output_stream_write (G_OUTPUT_STREAM (output_stream), line,
1149       strlen (line), NULL, &error);
1150
1151   if (error != NULL)
1152     {
1153       DEBUG ("Failed to write to file: %s", error->message);
1154       g_error_free (error);
1155       out = TRUE;
1156     }
1157
1158   g_free (line);
1159   g_free (level_upper);
1160   g_free (level_str);
1161   g_free (domain);
1162   g_free (category);
1163   g_free (message);
1164
1165   return out;
1166 }
1167
1168 static void
1169 debug_window_save_file_chooser_response_cb (GtkDialog *dialog,
1170     gint response_id,
1171     EmpathyDebugWindow *debug_window)
1172 {
1173   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1174   gchar *filename = NULL;
1175   GFile *gfile = NULL;
1176   GFileOutputStream *output_stream = NULL;
1177   GError *error = NULL;
1178
1179   if (response_id != GTK_RESPONSE_ACCEPT)
1180     goto OUT;
1181
1182   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1183
1184   DEBUG ("Saving log as %s", filename);
1185
1186   gfile = g_file_new_for_path (filename);
1187   output_stream = g_file_replace (gfile, NULL, FALSE,
1188       G_FILE_CREATE_NONE, NULL, &error);
1189
1190   if (error != NULL)
1191     {
1192       DEBUG ("Failed to open file for writing: %s", error->message);
1193       g_error_free (error);
1194       goto OUT;
1195     }
1196
1197   gtk_tree_model_foreach (priv->store_filter,
1198       debug_window_store_filter_foreach, output_stream);
1199
1200 OUT:
1201   if (gfile != NULL)
1202     g_object_unref (gfile);
1203
1204   if (output_stream != NULL)
1205     g_object_unref (output_stream);
1206
1207   if (filename != NULL)
1208     g_free (filename);
1209
1210   gtk_widget_destroy (GTK_WIDGET (dialog));
1211 }
1212
1213 static void
1214 debug_window_save_clicked_cb (GtkToolButton *tool_button,
1215     EmpathyDebugWindow *debug_window)
1216 {
1217   GtkWidget *file_chooser;
1218   gchar *name, *tmp = NULL;
1219   char time_str[32];
1220   time_t t;
1221   struct tm *tm_s;
1222
1223   file_chooser = gtk_file_chooser_dialog_new (_("Save"),
1224       GTK_WINDOW (debug_window), GTK_FILE_CHOOSER_ACTION_SAVE,
1225       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1226       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1227       NULL);
1228
1229   gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
1230   gtk_file_chooser_set_do_overwrite_confirmation (
1231       GTK_FILE_CHOOSER (file_chooser), TRUE);
1232
1233   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (file_chooser),
1234       g_get_tmp_dir ());
1235
1236   name = get_active_service_name (debug_window);
1237
1238   t = time (NULL);
1239   tm_s = localtime (&t);
1240   if (tm_s != NULL)
1241     {
1242       if (strftime(time_str, sizeof (time_str), "%d-%m-%y_%H-%M-%S", tm_s))
1243         tmp = g_strdup_printf ("%s-%s.log", name, time_str);
1244     }
1245
1246   if (tmp == NULL)
1247     tmp = g_strdup_printf ("%s.log", name);
1248   g_free (name);
1249
1250   gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser), tmp);
1251   g_free (tmp);
1252
1253   g_signal_connect (file_chooser, "response",
1254       G_CALLBACK (debug_window_save_file_chooser_response_cb),
1255       debug_window);
1256
1257   gtk_widget_show (file_chooser);
1258 }
1259
1260 static gboolean
1261 debug_window_copy_model_foreach (GtkTreeModel *model,
1262     GtkTreePath *path,
1263     GtkTreeIter *iter,
1264     gpointer user_data)
1265 {
1266   gchar **text = (gchar **) user_data;
1267   gchar *tmp;
1268   gchar *domain, *category, *message, *level_str, *level_upper;
1269   gdouble timestamp;
1270   gchar *line, *time_str;
1271
1272   gtk_tree_model_get (model, iter,
1273       COL_DEBUG_TIMESTAMP, &timestamp,
1274       COL_DEBUG_DOMAIN, &domain,
1275       COL_DEBUG_CATEGORY, &category,
1276       COL_DEBUG_LEVEL_STRING, &level_str,
1277       COL_DEBUG_MESSAGE, &message,
1278       -1);
1279
1280   level_upper = g_ascii_strup (level_str, -1);
1281
1282   time_str = debug_window_format_timestamp (timestamp);
1283
1284   line = g_strdup_printf ("%s%s%s-%s: %s: %s\n",
1285       domain, EMP_STR_EMPTY (category) ? "" : "/",
1286       category, level_upper, time_str, message);
1287
1288   g_free (time_str);
1289
1290   tmp = g_strconcat (*text, line, NULL);
1291
1292   g_free (*text);
1293   g_free (line);
1294   g_free (level_upper);
1295   g_free (level_str);
1296   g_free (domain);
1297   g_free (category);
1298   g_free (message);
1299
1300   *text = tmp;
1301
1302   return FALSE;
1303 }
1304
1305 static void
1306 debug_window_copy_clicked_cb (GtkToolButton *tool_button,
1307     EmpathyDebugWindow *debug_window)
1308 {
1309   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1310   GtkClipboard *clipboard;
1311   gchar *text;
1312
1313   text = g_strdup ("");
1314
1315   gtk_tree_model_foreach (priv->store_filter,
1316       debug_window_copy_model_foreach, &text);
1317
1318   clipboard = gtk_clipboard_get_for_display (
1319       gtk_widget_get_display (GTK_WIDGET (tool_button)),
1320       GDK_SELECTION_CLIPBOARD);
1321
1322   DEBUG ("Copying text to clipboard (length: %" G_GSIZE_FORMAT ")",
1323       strlen (text));
1324
1325   gtk_clipboard_set_text (clipboard, text, -1);
1326
1327   g_free (text);
1328 }
1329
1330 static gboolean
1331 debug_window_key_press_event_cb (GtkWidget *widget,
1332     GdkEventKey *event,
1333     gpointer user_data)
1334 {
1335   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_w)
1336       || event->keyval == GDK_Escape)
1337     {
1338       gtk_widget_destroy (widget);
1339       return TRUE;
1340     }
1341
1342   return FALSE;
1343 }
1344
1345 static gboolean
1346 tree_view_search_equal_func_cb (GtkTreeModel *model,
1347     gint column,
1348     const gchar *key,
1349     GtkTreeIter *iter,
1350     gpointer search_data)
1351 {
1352   gchar *str;
1353   gint key_len;
1354   gint len;
1355   gint i;
1356   gboolean ret = TRUE; /* The return value is counter-intuitive */
1357
1358   gtk_tree_model_get (model, iter, column, &str, -1);
1359
1360   key_len = strlen (key);
1361   len = strlen (str) - key_len;
1362
1363   for (i = 0; i <= len; ++i)
1364     {
1365       if (!g_ascii_strncasecmp (key, str + i, key_len))
1366         {
1367           ret = FALSE;
1368           break;
1369         }
1370     }
1371
1372   g_free (str);
1373   return ret;
1374 }
1375
1376 static void
1377 am_prepared_cb (GObject *am,
1378     GAsyncResult *res,
1379     gpointer user_data)
1380 {
1381   GObject *object = user_data;
1382   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
1383   GtkWidget *vbox;
1384   GtkWidget *toolbar;
1385   GtkWidget *image;
1386   GtkWidget *label;
1387   GtkToolItem *item;
1388   GtkCellRenderer *renderer;
1389   GtkListStore *level_store;
1390   GtkTreeIter iter;
1391   GError *error = NULL;
1392
1393   if (!tp_proxy_prepare_finish (am, res, &error))
1394     {
1395       g_warning ("Failed to prepare AM: %s", error->message);
1396       g_clear_error (&error);
1397     }
1398
1399   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
1400   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
1401   empathy_geometry_bind (GTK_WINDOW (object), "debug-window");
1402
1403   g_signal_connect (object, "key-press-event",
1404       G_CALLBACK (debug_window_key_press_event_cb), NULL);
1405
1406   vbox = gtk_vbox_new (FALSE, 0);
1407   gtk_container_add (GTK_CONTAINER (object), vbox);
1408   gtk_widget_show (vbox);
1409
1410   toolbar = gtk_toolbar_new ();
1411   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
1412   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
1413   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar),
1414       GTK_ICON_SIZE_SMALL_TOOLBAR);
1415   gtk_widget_show (toolbar);
1416
1417   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
1418
1419   /* CM */
1420   priv->chooser = gtk_combo_box_new_text ();
1421   priv->service_store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING,
1422       G_TYPE_STRING, G_TYPE_BOOLEAN);
1423   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->chooser),
1424       GTK_TREE_MODEL (priv->service_store));
1425   gtk_widget_show (priv->chooser);
1426
1427   item = gtk_tool_item_new ();
1428   gtk_widget_show (GTK_WIDGET (item));
1429   gtk_container_add (GTK_CONTAINER (item), priv->chooser);
1430   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1431   g_signal_connect (priv->chooser, "changed",
1432       G_CALLBACK (debug_window_service_chooser_changed_cb), object);
1433   gtk_widget_show (GTK_WIDGET (priv->chooser));
1434
1435   item = gtk_separator_tool_item_new ();
1436   gtk_widget_show (GTK_WIDGET (item));
1437   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1438
1439   /* Save */
1440   priv->save_button = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
1441   g_signal_connect (priv->save_button, "clicked",
1442       G_CALLBACK (debug_window_save_clicked_cb), object);
1443   gtk_widget_show (GTK_WIDGET (priv->save_button));
1444   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->save_button), TRUE);
1445   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->save_button, -1);
1446
1447   /* Copy */
1448   priv->copy_button = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
1449   g_signal_connect (priv->copy_button, "clicked",
1450       G_CALLBACK (debug_window_copy_clicked_cb), object);
1451   gtk_widget_show (GTK_WIDGET (priv->copy_button));
1452   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->copy_button), TRUE);
1453   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->copy_button, -1);
1454
1455   /* Clear */
1456   priv->clear_button = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
1457   g_signal_connect (priv->clear_button, "clicked",
1458       G_CALLBACK (debug_window_clear_clicked_cb), object);
1459   gtk_widget_show (GTK_WIDGET (priv->clear_button));
1460   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->clear_button), TRUE);
1461   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->clear_button, -1);
1462
1463   item = gtk_separator_tool_item_new ();
1464   gtk_widget_show (GTK_WIDGET (item));
1465   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1466
1467   /* Pause */
1468   priv->paused = FALSE;
1469   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE,
1470       GTK_ICON_SIZE_MENU);
1471   gtk_widget_show (image);
1472   priv->pause_button = gtk_toggle_tool_button_new ();
1473   gtk_toggle_tool_button_set_active (
1474       GTK_TOGGLE_TOOL_BUTTON (priv->pause_button), priv->paused);
1475   g_signal_connect (priv->pause_button, "toggled",
1476       G_CALLBACK (debug_window_pause_toggled_cb), object);
1477   gtk_widget_show (GTK_WIDGET (priv->pause_button));
1478   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->pause_button), TRUE);
1479   gtk_tool_button_set_label (GTK_TOOL_BUTTON (priv->pause_button), _("Pause"));
1480   gtk_tool_button_set_icon_widget (
1481       GTK_TOOL_BUTTON (priv->pause_button), image);
1482   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->pause_button, -1);
1483
1484   item = gtk_separator_tool_item_new ();
1485   gtk_widget_show (GTK_WIDGET (item));
1486   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1487
1488   /* Level */
1489   priv->level_label = gtk_tool_item_new ();
1490   gtk_widget_show (GTK_WIDGET (priv->level_label));
1491   label = gtk_label_new (_("Level "));
1492   gtk_widget_show (label);
1493   gtk_container_add (GTK_CONTAINER (priv->level_label), label);
1494   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->level_label, -1);
1495
1496   priv->level_filter = gtk_combo_box_new_text ();
1497   gtk_widget_show (priv->level_filter);
1498
1499   item = gtk_tool_item_new ();
1500   gtk_widget_show (GTK_WIDGET (item));
1501   gtk_container_add (GTK_CONTAINER (item), priv->level_filter);
1502   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1503
1504   level_store = gtk_list_store_new (NUM_COLS_LEVEL,
1505       G_TYPE_STRING, G_TYPE_UINT);
1506   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->level_filter),
1507       GTK_TREE_MODEL (level_store));
1508
1509   gtk_list_store_append (level_store, &iter);
1510   gtk_list_store_set (level_store, &iter,
1511       COL_LEVEL_NAME, _("Debug"),
1512       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_DEBUG,
1513       -1);
1514
1515   gtk_list_store_append (level_store, &iter);
1516   gtk_list_store_set (level_store, &iter,
1517       COL_LEVEL_NAME, _("Info"),
1518       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_INFO,
1519       -1);
1520
1521   gtk_list_store_append (level_store, &iter);
1522   gtk_list_store_set (level_store, &iter,
1523       COL_LEVEL_NAME, _("Message"),
1524       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_MESSAGE,
1525       -1);
1526
1527   gtk_list_store_append (level_store, &iter);
1528   gtk_list_store_set (level_store, &iter,
1529       COL_LEVEL_NAME, _("Warning"),
1530       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_WARNING,
1531       -1);
1532
1533   gtk_list_store_append (level_store, &iter);
1534   gtk_list_store_set (level_store, &iter,
1535       COL_LEVEL_NAME, _("Critical"),
1536       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_CRITICAL,
1537       -1);
1538
1539   gtk_list_store_append (level_store, &iter);
1540   gtk_list_store_set (level_store, &iter,
1541       COL_LEVEL_NAME, _("Error"),
1542       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_ERROR,
1543       -1);
1544
1545   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->level_filter), 0);
1546   g_signal_connect (priv->level_filter, "changed",
1547       G_CALLBACK (debug_window_filter_changed_cb), object);
1548
1549   /* Debug treeview */
1550   priv->view = gtk_tree_view_new ();
1551   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
1552
1553   g_signal_connect (priv->view, "button-press-event",
1554       G_CALLBACK (debug_window_button_press_event_cb), object);
1555
1556   renderer = gtk_cell_renderer_text_new ();
1557   g_object_set (renderer, "yalign", 0, NULL);
1558
1559   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->view),
1560       -1, _("Time"), renderer,
1561       (GtkTreeCellDataFunc) debug_window_time_formatter, NULL, NULL);
1562   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1563       -1, _("Domain"), renderer, "text", COL_DEBUG_DOMAIN, NULL);
1564   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1565       -1, _("Category"), renderer, "text", COL_DEBUG_CATEGORY, NULL);
1566   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1567       -1, _("Level"), renderer, "text", COL_DEBUG_LEVEL_STRING, NULL);
1568
1569   renderer = gtk_cell_renderer_text_new ();
1570   g_object_set (renderer, "family", "Monospace", NULL);
1571   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1572       -1, _("Message"), renderer, "text", COL_DEBUG_MESSAGE, NULL);
1573
1574   priv->store = gtk_list_store_new (NUM_DEBUG_COLS, G_TYPE_DOUBLE,
1575       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
1576       G_TYPE_UINT);
1577
1578   priv->store_filter = gtk_tree_model_filter_new (
1579       GTK_TREE_MODEL (priv->store), NULL);
1580
1581   gtk_tree_model_filter_set_visible_func (
1582       GTK_TREE_MODEL_FILTER (priv->store_filter),
1583       debug_window_visible_func, object, NULL);
1584
1585   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view), priv->store_filter);
1586
1587   gtk_tree_view_set_search_column (GTK_TREE_VIEW (priv->view),
1588       COL_DEBUG_MESSAGE);
1589   gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (priv->view),
1590       tree_view_search_equal_func_cb, NULL, NULL);
1591
1592   /* Scrolled window */
1593   priv->scrolled_win = g_object_ref (gtk_scrolled_window_new (NULL, NULL));
1594   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_win),
1595       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1596
1597   gtk_widget_show (priv->view);
1598   gtk_container_add (GTK_CONTAINER (priv->scrolled_win), priv->view);
1599
1600   gtk_widget_show (priv->scrolled_win);
1601
1602   /* Not supported label */
1603   priv->not_supported_label = g_object_ref (gtk_label_new (
1604           _("The selected connection manager does not support the remote "
1605               "debugging extension.")));
1606   gtk_widget_show (priv->not_supported_label);
1607   gtk_box_pack_start (GTK_BOX (vbox), priv->not_supported_label,
1608       TRUE, TRUE, 0);
1609
1610   priv->view_visible = FALSE;
1611
1612   debug_window_set_toolbar_sensitivity (EMPATHY_DEBUG_WINDOW (object), FALSE);
1613   debug_window_fill_service_chooser (EMPATHY_DEBUG_WINDOW (object));
1614   gtk_widget_show (GTK_WIDGET (object));
1615 }
1616
1617 static void
1618 debug_window_constructed (GObject *object)
1619 {
1620   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
1621
1622   priv->am = tp_account_manager_dup ();
1623   tp_proxy_prepare_async (priv->am, NULL, am_prepared_cb, object);
1624 }
1625
1626 static void
1627 empathy_debug_window_init (EmpathyDebugWindow *empathy_debug_window)
1628 {
1629   EmpathyDebugWindowPriv *priv =
1630       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_window,
1631       EMPATHY_TYPE_DEBUG_WINDOW, EmpathyDebugWindowPriv);
1632
1633   empathy_debug_window->priv = priv;
1634
1635   priv->dispose_run = FALSE;
1636   priv->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1637       g_free, NULL);
1638 }
1639
1640 static void
1641 debug_window_set_property (GObject *object,
1642     guint prop_id,
1643     const GValue *value,
1644     GParamSpec *pspec)
1645 {
1646   switch (prop_id)
1647     {
1648       default:
1649         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1650         break;
1651     }
1652 }
1653
1654 static void
1655 debug_window_get_property (GObject *object,
1656     guint prop_id,
1657     GValue *value,
1658     GParamSpec *pspec)
1659 {
1660   switch (prop_id)
1661     {
1662       default:
1663         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1664         break;
1665     }
1666 }
1667
1668 static void
1669 debug_window_finalize (GObject *object)
1670 {
1671   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
1672   GHashTableIter iter;
1673   char *key;
1674   GList *values;
1675
1676   g_hash_table_iter_init (&iter, priv->cache);
1677
1678   while (g_hash_table_iter_next (&iter, (gpointer *) &key,
1679           (gpointer *) &values))
1680     {
1681       debug_message_list_free (values);
1682     }
1683
1684   g_hash_table_destroy (priv->cache);
1685
1686   (G_OBJECT_CLASS (empathy_debug_window_parent_class)->finalize) (object);
1687 }
1688
1689 static void
1690 debug_window_dispose (GObject *object)
1691 {
1692   EmpathyDebugWindow *selector = EMPATHY_DEBUG_WINDOW (object);
1693   EmpathyDebugWindowPriv *priv = GET_PRIV (selector);
1694
1695   if (priv->dispose_run)
1696     return;
1697
1698   priv->dispose_run = TRUE;
1699
1700   if (priv->store != NULL)
1701     g_object_unref (priv->store);
1702
1703   if (priv->name_owner_changed_signal != NULL)
1704     tp_proxy_signal_connection_disconnect (priv->name_owner_changed_signal);
1705
1706   if (priv->proxy != NULL)
1707     {
1708       debug_window_set_enabled (EMPATHY_DEBUG_WINDOW (object), FALSE);
1709       g_signal_handler_disconnect (priv->proxy, priv->invalid_signal_id);
1710       g_object_unref (priv->proxy);
1711     }
1712
1713   if (priv->new_debug_message_signal != NULL)
1714     tp_proxy_signal_connection_disconnect (priv->new_debug_message_signal);
1715
1716   if (priv->service_store != NULL)
1717     g_object_unref (priv->service_store);
1718
1719   if (priv->dbus != NULL)
1720     g_object_unref (priv->dbus);
1721
1722   if (priv->am != NULL)
1723     {
1724       g_object_unref (priv->am);
1725       priv->am = NULL;
1726     }
1727
1728   (G_OBJECT_CLASS (empathy_debug_window_parent_class)->dispose) (object);
1729 }
1730
1731 static void
1732 empathy_debug_window_class_init (EmpathyDebugWindowClass *klass)
1733 {
1734   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1735   object_class->constructed = debug_window_constructed;
1736   object_class->dispose = debug_window_dispose;
1737   object_class->finalize = debug_window_finalize;
1738   object_class->set_property = debug_window_set_property;
1739   object_class->get_property = debug_window_get_property;
1740   g_type_class_add_private (klass, sizeof (EmpathyDebugWindowPriv));
1741 }
1742
1743 /* public methods */
1744
1745 GtkWidget *
1746 empathy_debug_window_new (GtkWindow *parent)
1747 {
1748   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
1749
1750   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_WINDOW,
1751       "transient-for", parent, NULL));
1752 }