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