]> git.0d.be Git - empathy.git/blob - src/empathy-debug-window.c
Un-pausing incoming messages should update All's active buffer too
[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 #include <libempathy-gtk/empathy-ui-utils.h>
36
37 #include <telepathy-glib/dbus.h>
38 #include <telepathy-glib/interfaces.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-glib/proxy-subclass.h>
41 #include <telepathy-glib/account-manager.h>
42
43 #include "extensions/extensions.h"
44
45 #include "empathy-debug-window.h"
46
47 G_DEFINE_TYPE (EmpathyDebugWindow, empathy_debug_window,
48     GTK_TYPE_WINDOW)
49
50 typedef enum
51 {
52   SERVICE_TYPE_CM = 0,
53   SERVICE_TYPE_CLIENT,
54 } ServiceType;
55
56 enum
57 {
58   COL_DEBUG_TIMESTAMP = 0,
59   COL_DEBUG_DOMAIN,
60   COL_DEBUG_CATEGORY,
61   COL_DEBUG_LEVEL_STRING,
62   COL_DEBUG_MESSAGE,
63   COL_DEBUG_LEVEL_VALUE,
64   NUM_DEBUG_COLS
65 };
66
67 enum
68 {
69   COL_NAME = 0,
70   COL_UNIQUE_NAME,
71   COL_GONE,
72   COL_ACTIVE_BUFFER,
73   COL_PAUSE_BUFFER,
74   COL_PROXY,
75   NUM_COLS
76 };
77
78 enum
79 {
80   COL_LEVEL_NAME,
81   COL_LEVEL_VALUE,
82   NUM_COLS_LEVEL
83 };
84
85 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDebugWindow)
86 typedef struct
87 {
88   /* Toolbar items */
89   GtkWidget *chooser;
90   GtkToolItem *save_button;
91   GtkToolItem *copy_button;
92   GtkToolItem *clear_button;
93   GtkToolItem *pause_button;
94   GtkToolItem *level_label;
95   GtkWidget *level_filter;
96
97   /* TreeView */
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   TpProxySignalConnection *name_owner_changed_signal;
107
108   /* Whether NewDebugMessage will be fired */
109   gboolean paused;
110
111   /* Service (CM, Client) chooser store */
112   GtkListStore *service_store;
113
114   /* Counters on services detected and added */
115   guint services_detected;
116   guint name_owner_cb_count;
117
118   /* Debug to show upon creation */
119   gchar *select_name;
120
121   /* Misc. */
122   gboolean dispose_run;
123   TpAccountManager *am;
124   GtkListStore *all_active_buffer;
125 } EmpathyDebugWindowPriv;
126
127 static const gchar *
128 log_level_to_string (guint level)
129 {
130   switch (level)
131     {
132     case TP_DEBUG_LEVEL_ERROR:
133       return "Error";
134       break;
135     case TP_DEBUG_LEVEL_CRITICAL:
136       return "Critical";
137       break;
138     case TP_DEBUG_LEVEL_WARNING:
139       return "Warning";
140       break;
141     case TP_DEBUG_LEVEL_MESSAGE:
142       return "Message";
143       break;
144     case TP_DEBUG_LEVEL_INFO:
145       return "Info";
146       break;
147     case TP_DEBUG_LEVEL_DEBUG:
148       return "Debug";
149       break;
150     default:
151       g_assert_not_reached ();
152       break;
153     }
154 }
155
156 static gchar *
157 get_active_service_name (EmpathyDebugWindow *self)
158 {
159   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
160   GtkTreeIter iter;
161   gchar *name;
162
163   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->chooser), &iter))
164     return NULL;
165
166   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
167       COL_NAME, &name, -1);
168
169   return name;
170 }
171
172 static gboolean
173 copy_buffered_messages (GtkTreeModel *buffer,
174     GtkTreePath *path,
175     GtkTreeIter *iter,
176     gpointer data)
177 {
178   GtkListStore *active_buffer = data;
179   GtkTreeIter active_buffer_iter;
180   gdouble timestamp;
181   gchar *domain, *category, *message, *level_string;
182   guint level;
183
184   gtk_tree_model_get (buffer, iter,
185       COL_DEBUG_TIMESTAMP, &timestamp,
186       COL_DEBUG_DOMAIN, &domain,
187       COL_DEBUG_CATEGORY, &category,
188       COL_DEBUG_LEVEL_STRING, &level_string,
189       COL_DEBUG_MESSAGE, &message,
190       COL_DEBUG_LEVEL_VALUE, &level,
191       -1);
192   gtk_list_store_append (active_buffer, &active_buffer_iter);
193   gtk_list_store_set (active_buffer, &active_buffer_iter,
194       COL_DEBUG_TIMESTAMP, timestamp,
195       COL_DEBUG_DOMAIN, domain,
196       COL_DEBUG_CATEGORY, category,
197       COL_DEBUG_LEVEL_STRING, level_string,
198       COL_DEBUG_MESSAGE, message,
199       COL_DEBUG_LEVEL_VALUE, level,
200       -1);
201
202   g_free (domain);
203   g_free (category);
204   g_free (level_string);
205   g_free (message);
206
207   return FALSE;
208 }
209
210 static void
211 insert_values_in_buffer (GtkListStore *store,
212         gdouble timestamp,
213         const gchar *domain,
214         const gchar *category,
215         guint level,
216         const gchar *string)
217 {
218   GtkTreeIter iter;
219
220   gtk_list_store_insert_with_values (store, &iter, -1,
221       COL_DEBUG_TIMESTAMP, timestamp,
222       COL_DEBUG_DOMAIN, domain,
223       COL_DEBUG_CATEGORY, category,
224       COL_DEBUG_LEVEL_STRING, log_level_to_string (level),
225       COL_DEBUG_MESSAGE, string,
226       COL_DEBUG_LEVEL_VALUE, level,
227       -1); 
228 }
229
230 static void
231 debug_window_add_message (EmpathyDebugWindow *debug_window,
232     TpProxy *proxy,
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   gchar *string;
241   GtkListStore *active_buffer, *pause_buffer;
242
243   if (g_strrstr (domain_category, "/"))
244     {
245       gchar **parts = g_strsplit (domain_category, "/", 2);
246       domain = g_strdup (parts[0]);
247       category = g_strdup (parts[1]);
248       g_strfreev (parts);
249     }
250   else
251     {
252       domain = g_strdup (domain_category);
253       category = g_strdup ("");
254     }
255
256   if (g_str_has_suffix (message, "\n"))
257     string = g_strchomp (g_strdup (message));
258   else
259     string = g_strdup (message);
260
261   pause_buffer = g_object_get_data (G_OBJECT (proxy), "pause-buffer");
262   active_buffer = g_object_get_data (G_OBJECT (proxy), "active-buffer");
263
264   if (priv->paused)
265     {
266       insert_values_in_buffer (pause_buffer, timestamp,
267           domain, category, level,
268           string);
269     }
270   else
271     {
272       /* Append 'this' message to this service's and All's active-buffers */
273       insert_values_in_buffer (active_buffer, timestamp,
274           domain, category, level,
275           string);
276
277       insert_values_in_buffer (priv->all_active_buffer, timestamp,
278           domain, category, level,
279           string);
280     }
281
282   g_free (string);
283   g_free (domain);
284   g_free (category);
285 }
286
287 static void
288 debug_window_new_debug_message_cb (TpProxy *proxy,
289     gdouble timestamp,
290     const gchar *domain,
291     guint level,
292     const gchar *message,
293     gpointer user_data,
294     GObject *weak_object)
295 {
296   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
297
298   debug_window_add_message (debug_window, proxy, timestamp, domain, level,
299       message);
300 }
301
302 static void
303 debug_window_set_enabled (TpProxy *proxy,
304     gboolean enabled)
305 {
306   GValue *val;
307
308   g_return_if_fail (proxy != NULL);
309
310   val = tp_g_value_slice_new_boolean (enabled);
311
312   tp_cli_dbus_properties_call_set (proxy, -1, TP_IFACE_DEBUG,
313       "Enabled", val, NULL, NULL, NULL, NULL);
314
315   tp_g_value_slice_free (val);
316 }
317
318 static void
319 debug_window_set_toolbar_sensitivity (EmpathyDebugWindow *debug_window,
320     gboolean sensitive)
321 {
322   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
323   GtkWidget *vbox = gtk_bin_get_child (GTK_BIN (debug_window));
324
325   gtk_widget_set_sensitive (GTK_WIDGET (priv->save_button), sensitive);
326   gtk_widget_set_sensitive (GTK_WIDGET (priv->copy_button), sensitive);
327   gtk_widget_set_sensitive (GTK_WIDGET (priv->clear_button), sensitive);
328   gtk_widget_set_sensitive (GTK_WIDGET (priv->pause_button), sensitive);
329   gtk_widget_set_sensitive (GTK_WIDGET (priv->level_label), sensitive);
330   gtk_widget_set_sensitive (GTK_WIDGET (priv->level_filter), sensitive);
331   gtk_widget_set_sensitive (GTK_WIDGET (priv->view), sensitive);
332
333   if (sensitive && !priv->view_visible)
334     {
335       /* Add view and remove label */
336       gtk_container_remove (GTK_CONTAINER (vbox), priv->not_supported_label);
337       gtk_box_pack_start (GTK_BOX (vbox), priv->scrolled_win, TRUE, TRUE, 0);
338       priv->view_visible = TRUE;
339     }
340   else if (!sensitive && priv->view_visible)
341     {
342       /* Add label and remove view */
343       gtk_container_remove (GTK_CONTAINER (vbox), priv->scrolled_win);
344       gtk_box_pack_start (GTK_BOX (vbox), priv->not_supported_label,
345           TRUE, TRUE, 0);
346       priv->view_visible = FALSE;
347     }
348 }
349
350 static gboolean
351 debug_window_get_iter_for_active_buffer (GtkListStore *active_buffer,
352     GtkTreeIter *iter,
353     EmpathyDebugWindow *debug_window)
354 {
355   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
356   gboolean valid_iter;
357   GtkTreeModel *model = GTK_TREE_MODEL (priv->service_store);
358
359   gtk_tree_model_get_iter_first (model, iter);
360   for (valid_iter = gtk_tree_model_iter_next (model, iter);
361        valid_iter;
362        valid_iter = gtk_tree_model_iter_next (model, iter))
363     {
364       GtkListStore *stored_active_buffer;
365
366       gtk_tree_model_get (model, iter,
367           COL_ACTIVE_BUFFER, &stored_active_buffer,
368           -1);
369       if (active_buffer == stored_active_buffer)
370         {
371           g_object_unref (stored_active_buffer);
372           return valid_iter;
373         }
374       g_object_unref (stored_active_buffer);
375     }
376
377   return valid_iter;
378 }
379
380 static void refresh_all_buffer (EmpathyDebugWindow *debug_window);
381
382 static void
383 proxy_invalidated_cb (TpProxy *proxy,
384     guint domain,
385     gint code,
386     gchar *msg,
387     gpointer user_data)
388 {
389   EmpathyDebugWindow *self = (EmpathyDebugWindow *) user_data;
390   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
391   GtkTreeModel *service_store = GTK_TREE_MODEL (priv->service_store);
392   TpProxy *stored_proxy;
393   GtkTreeIter iter;
394   gboolean valid_iter;
395
396   /* Proxy has been invalidated so we find and set it to NULL
397    * in service store */
398   gtk_tree_model_get_iter_first (service_store, &iter);
399   for (valid_iter = gtk_tree_model_iter_next (service_store, &iter);
400        valid_iter;
401        valid_iter = gtk_tree_model_iter_next (service_store, &iter))
402     {
403       gtk_tree_model_get (service_store, &iter,
404           COL_PROXY, &stored_proxy,
405           -1);
406
407       if (proxy == stored_proxy)
408         gtk_list_store_set (priv->service_store, &iter,
409             COL_PROXY, NULL,
410             -1);
411     }
412
413   /* Also, we refresh "All" selection's active buffer since it should not
414    * show messages obtained from the proxy getting destroyed above */
415   refresh_all_buffer (self);
416 }
417
418 static void
419 debug_window_get_messages_cb (TpProxy *proxy,
420     const GPtrArray *messages,
421     const GError *error,
422     gpointer user_data,
423     GObject *weak_object)
424 {
425   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
426   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
427   gchar *active_service_name;
428   guint i;
429   GtkListStore *active_buffer;
430   gboolean valid_iter;
431   GtkTreeIter iter;
432   gchar *proxy_service_name;
433
434   active_buffer = g_object_get_data (G_OBJECT (proxy), "active-buffer");
435   valid_iter = debug_window_get_iter_for_active_buffer (active_buffer, &iter,
436       debug_window);
437   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
438       COL_NAME, &proxy_service_name,
439       -1);
440
441   active_service_name = get_active_service_name (debug_window);
442   if (error != NULL)
443     {
444       DEBUG ("GetMessages failed: %s", error->message);
445
446       /* We want to set the window sensitivity to false only when proxy for the
447        * selected service is unable to fetch debug messages */
448       if (!tp_strdiff (active_service_name, proxy_service_name))
449         debug_window_set_toolbar_sensitivity (debug_window, FALSE);
450
451       /* We created the proxy for GetMessages call. Now destroy it. */
452       tp_clear_object (&proxy);
453       return;
454     }
455
456   DEBUG ("Retrieved debug messages for %s", active_service_name);
457   g_free (active_service_name);
458   debug_window_set_toolbar_sensitivity (debug_window, TRUE);
459
460
461   for (i = 0; i < messages->len; i++)
462     {
463       GValueArray *values = g_ptr_array_index (messages, i);
464
465       debug_window_add_message (debug_window, proxy,
466           g_value_get_double (g_value_array_get_nth (values, 0)),
467           g_value_get_string (g_value_array_get_nth (values, 1)),
468           g_value_get_uint (g_value_array_get_nth (values, 2)),
469           g_value_get_string (g_value_array_get_nth (values, 3)));
470     }
471
472   /* Now we save this precious proxy in the service_store along its service */
473   if (valid_iter)
474     {
475       DEBUG ("Proxy for service: %s was successful in fetching debug"
476           " messages. Saving it.", proxy_service_name);
477
478       gtk_list_store_set (priv->service_store, &iter,
479           COL_PROXY, proxy,
480           -1);
481     }
482
483   g_free (proxy_service_name);
484
485   /* Connect to "invalidated" signal */
486   g_signal_connect (proxy, "invalidated",
487       G_CALLBACK (proxy_invalidated_cb), debug_window);
488
489  /* Connect to NewDebugMessage */
490   emp_cli_debug_connect_to_new_debug_message (
491       proxy, debug_window_new_debug_message_cb, debug_window,
492       NULL, NULL, NULL);
493
494   /* Now that active-buffer is up to date, we can see which messages are
495    * to be visible */
496   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->store_filter));
497
498   /* Set the proxy to signal for new debug messages */
499   debug_window_set_enabled (proxy, TRUE);
500 }
501
502 static void
503 create_proxy_to_get_messages (EmpathyDebugWindow *debug_window,
504     GtkTreeIter *iter,
505     TpDBusDaemon *dbus)
506 {
507   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
508   gchar *bus_name, *name = NULL;
509   TpProxy *new_proxy, *stored_proxy = NULL;
510   GtkTreeModel *pause_buffer, *active_buffer;
511   gboolean gone;
512
513   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), iter,
514       COL_NAME, &name,
515       COL_GONE, &gone,
516       COL_ACTIVE_BUFFER, &active_buffer,
517       COL_PAUSE_BUFFER, &pause_buffer,
518       COL_PROXY, &stored_proxy,
519       -1);
520
521   /* If the stored_proxy is not NULL then messages have been obtained and
522    * new-debug-message-signal has been set on it. Also, the proxy is valid.
523    * If the service is gone, we still display the messages-cached till now. */
524   if (gone ||
525       (!gone && stored_proxy != NULL))
526     {
527       /* Nothing needs to be done. The associated active-buffer has already
528        * been set as view's model */
529       goto finally;
530     }
531
532   DEBUG ("Preparing proxy to obtain messages for service %s", name);
533
534   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), iter,
535       COL_UNIQUE_NAME, &bus_name, -1);
536   new_proxy = g_object_new (TP_TYPE_PROXY,
537       "bus-name", bus_name,
538       "dbus-daemon", dbus,
539       "object-path", DEBUG_OBJECT_PATH,
540       NULL);
541   g_free (bus_name);
542
543   g_object_set_data (G_OBJECT (new_proxy), "active-buffer", active_buffer);
544   g_object_set_data (G_OBJECT (new_proxy), "pause-buffer", pause_buffer);
545
546   /* Now we call GetMessages with fresh proxy.
547    * The old proxy is NULL due to one of the following -
548    * * Wasn't saved as last GetMessages call failed
549    * * The service has newly arrived and no proxy has been prepared yet for it
550    * * A service with the same name has reappeared but the owner maybe new */
551   tp_proxy_add_interface_by_id (new_proxy, emp_iface_quark_debug ());
552
553   emp_cli_debug_call_get_messages (new_proxy, -1,
554       debug_window_get_messages_cb, debug_window, NULL, NULL);
555
556 finally:
557   g_free (name);
558   tp_clear_object (&stored_proxy);
559   g_object_unref (active_buffer);
560   g_object_unref (pause_buffer);
561 }
562
563 static GtkListStore*
564 new_list_store_for_service (void)
565 {
566   return gtk_list_store_new (NUM_DEBUG_COLS,
567              G_TYPE_DOUBLE, /* COL_DEBUG_TIMESTAMP */
568              G_TYPE_STRING, /* COL_DEBUG_DOMAIN */
569              G_TYPE_STRING, /* COL_DEBUG_CATEGORY */
570              G_TYPE_STRING, /* COL_DEBUG_LEVEL_STRING */
571              G_TYPE_STRING, /* COL_DEBUG_MESSAGE */
572              G_TYPE_UINT);  /* COL_DEBUG_LEVEL_VALUE */
573 }
574
575 static gboolean
576 debug_window_visible_func (GtkTreeModel *model,
577     GtkTreeIter *iter,
578     gpointer user_data)
579 {
580   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
581   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
582   guint filter_value, level;
583   GtkTreeModel *filter_model;
584   GtkTreeIter filter_iter;
585
586   filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->level_filter));
587   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->level_filter),
588       &filter_iter);
589
590   gtk_tree_model_get (model, iter, COL_DEBUG_LEVEL_VALUE, &level, -1);
591   gtk_tree_model_get (filter_model, &filter_iter,
592       COL_LEVEL_VALUE, &filter_value, -1);
593
594   if (level <= filter_value)
595     return TRUE;
596
597   return FALSE;
598 }
599
600 static gboolean
601 tree_view_search_equal_func_cb (GtkTreeModel *model,
602     gint column,
603     const gchar *key,
604
605     GtkTreeIter *iter,
606     gpointer search_data)
607 {
608   gchar *str;
609   gint key_len;
610   gint len;
611   gint i;
612   gboolean ret = TRUE; /* The return value is counter-intuitive */
613
614   gtk_tree_model_get (model, iter, column, &str, -1);
615
616   key_len = strlen (key);
617   len = strlen (str) - key_len;
618
619   for (i = 0; i <= len; ++i)
620     {
621       if (!g_ascii_strncasecmp (key, str + i, key_len))
622         {
623           ret = FALSE;
624           break;
625         }
626     }
627
628   g_free (str);
629   return ret;
630 }
631
632 static void
633 update_store_filter (EmpathyDebugWindow *debug_window,
634     GtkListStore *active_buffer)
635 {
636   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
637   debug_window_set_toolbar_sensitivity (debug_window, FALSE);
638
639   tp_clear_object (&priv->store_filter);
640   priv->store_filter = gtk_tree_model_filter_new (
641       GTK_TREE_MODEL (active_buffer), NULL);
642
643   gtk_tree_model_filter_set_visible_func (
644       GTK_TREE_MODEL_FILTER (priv->store_filter),
645       debug_window_visible_func, debug_window, NULL);
646   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view),
647       priv->store_filter);
648
649   /* Since view's model has changed, reset the search column and
650    * search_equal_func */
651   gtk_tree_view_set_search_column (GTK_TREE_VIEW (priv->view),
652       COL_DEBUG_MESSAGE);
653   gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (priv->view),
654       tree_view_search_equal_func_cb, NULL, NULL);
655
656   debug_window_set_toolbar_sensitivity (debug_window, TRUE);
657 }
658
659 static void
660 refresh_all_buffer (EmpathyDebugWindow *debug_window)
661 {
662   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
663   gboolean valid_iter;
664   GtkTreeIter iter;
665   GtkTreeModel *service_store = GTK_TREE_MODEL (priv->service_store);
666
667   /* Clear All's active-buffer */
668   gtk_list_store_clear (priv->all_active_buffer);
669
670   /* Skipping the first service store iter which is reserved for "All" */
671   gtk_tree_model_get_iter_first (service_store, &iter);
672   for (valid_iter = gtk_tree_model_iter_next (service_store, &iter);
673        valid_iter;
674        valid_iter = gtk_tree_model_iter_next (service_store, &iter))
675     {
676       TpProxy *proxy = NULL;
677       GtkListStore *service_active_buffer;
678       gboolean gone;
679
680       gtk_tree_model_get (service_store, &iter,
681           COL_GONE, &gone,
682           COL_PROXY, &proxy,
683           COL_ACTIVE_BUFFER, &service_active_buffer,
684           -1);
685
686       if (gone)
687         {
688           gtk_tree_model_foreach (GTK_TREE_MODEL (service_active_buffer),
689               copy_buffered_messages, priv->all_active_buffer);
690         }
691       else
692         {
693           if (proxy != NULL)
694             {
695               if (service_active_buffer == NULL)
696                 break;
697
698               /* Copy the debug messages to all_active_buffer */
699               gtk_tree_model_foreach (GTK_TREE_MODEL (service_active_buffer),
700                   copy_buffered_messages, priv->all_active_buffer);
701             }
702           else
703             {
704               GError *error = NULL;
705               TpDBusDaemon *dbus = tp_dbus_daemon_dup (&error);
706
707               if (error != NULL)
708                 {
709                   DEBUG ("Failed at duping the dbus daemon: %s", error->message);
710                   g_error_free (error);
711                 }
712
713               create_proxy_to_get_messages (debug_window, &iter, dbus);
714
715               g_object_unref (dbus);
716             }
717         }
718
719       g_object_unref (service_active_buffer);
720       tp_clear_object (&proxy);
721     }
722 }
723
724 static void
725 debug_window_service_chooser_changed_cb (GtkComboBox *chooser,
726     EmpathyDebugWindow *debug_window)
727 {
728   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
729   TpDBusDaemon *dbus;
730   GError *error = NULL;
731   GtkListStore *stored_active_buffer = NULL;
732   gchar *name = NULL;
733   GtkTreeIter iter;
734   gboolean gone;
735
736   if (!gtk_combo_box_get_active_iter (chooser, &iter))
737     {
738       DEBUG ("No CM is selected");
739       if (gtk_tree_model_iter_n_children (
740           GTK_TREE_MODEL (priv->service_store), NULL) > 0)
741         {
742           gtk_combo_box_set_active (chooser, 0);
743         }
744       return;
745     }
746
747   debug_window_set_toolbar_sensitivity (debug_window, TRUE);
748
749   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
750       COL_NAME, &name,
751       COL_GONE, &gone,
752       COL_ACTIVE_BUFFER, &stored_active_buffer,
753       -1);
754
755   DEBUG ("Service chosen: %s", name);
756
757   if (tp_strdiff (name, "All") && stored_active_buffer == NULL)
758     {
759       DEBUG ("No list store assigned to service %s", name);
760       goto finally;
761     }
762
763   if (!tp_strdiff (name, "All"))
764     {
765       update_store_filter (debug_window, priv->all_active_buffer);
766       goto finally;
767     }
768
769   update_store_filter (debug_window, stored_active_buffer);
770
771   dbus = tp_dbus_daemon_dup (&error);
772
773   if (error != NULL)
774     {
775       DEBUG ("Failed at duping the dbus daemon: %s", error->message);
776     }
777
778   create_proxy_to_get_messages (debug_window, &iter, dbus);
779
780   g_object_unref (dbus);
781
782 finally:
783   g_free (name);
784   tp_clear_object (&stored_active_buffer);
785 }
786
787 typedef struct
788 {
789   const gchar *name;
790   gboolean found;
791   gboolean use_name;
792   GtkTreeIter **found_iter;
793 } CmInModelForeachData;
794
795 static gboolean
796 debug_window_service_foreach (GtkTreeModel *model,
797     GtkTreePath *path,
798     GtkTreeIter *iter,
799     gpointer user_data)
800 {
801   CmInModelForeachData *data = (CmInModelForeachData *) user_data;
802   gchar *store_name;
803
804   gtk_tree_model_get (model, iter,
805       (data->use_name ? COL_NAME : COL_UNIQUE_NAME),
806       &store_name,
807       -1);
808
809   if (!tp_strdiff (store_name, data->name))
810     {
811       data->found = TRUE;
812
813       if (data->found_iter != NULL)
814         *(data->found_iter) = gtk_tree_iter_copy (iter);
815     }
816
817   g_free (store_name);
818
819   return data->found;
820 }
821
822 static gboolean
823 debug_window_service_is_in_model (EmpathyDebugWindow *debug_window,
824     const gchar *name,
825     GtkTreeIter **iter,
826     gboolean use_name)
827 {
828   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
829   CmInModelForeachData *data;
830   gboolean found;
831
832   data = g_slice_new0 (CmInModelForeachData);
833   data->name = name;
834   data->found = FALSE;
835   data->found_iter = iter;
836   data->use_name = use_name;
837
838   gtk_tree_model_foreach (GTK_TREE_MODEL (priv->service_store),
839       debug_window_service_foreach, data);
840
841   found = data->found;
842
843   g_slice_free (CmInModelForeachData, data);
844
845   return found;
846 }
847
848 static gchar *
849 get_cm_display_name (EmpathyDebugWindow *self,
850     const char *cm_name)
851 {
852   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
853   GHashTable *protocols = g_hash_table_new (g_str_hash, g_str_equal);
854   GList *accounts, *ptr;
855   char *retval;
856
857   accounts = tp_account_manager_get_valid_accounts (priv->am);
858
859   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
860     {
861       TpAccount *account = TP_ACCOUNT (ptr->data);
862
863       if (!tp_strdiff (tp_account_get_connection_manager (account), cm_name))
864         {
865           g_hash_table_insert (protocols,
866               (char *) tp_account_get_protocol (account),
867               GUINT_TO_POINTER (TRUE));
868         }
869     }
870
871   g_list_free (accounts);
872
873   if (g_hash_table_size (protocols) > 0)
874     {
875       GHashTableIter iter;
876       char **protocolsv;
877       char *key, *str;
878       guint i;
879
880       protocolsv = g_new0 (char *, g_hash_table_size (protocols) + 1);
881
882       g_hash_table_iter_init (&iter, protocols);
883       for (i = 0; g_hash_table_iter_next (&iter, (gpointer) &key, NULL); i++)
884         {
885           protocolsv[i] = key;
886         }
887
888       str = g_strjoinv (", ", protocolsv);
889       retval = g_strdup_printf ("%s (%s)", cm_name, str);
890
891       g_free (protocolsv);
892       g_free (str);
893     }
894   else
895     {
896       retval = g_strdup (cm_name);
897     }
898
899   g_hash_table_unref (protocols);
900
901   return retval;
902 }
903
904 typedef struct
905 {
906   EmpathyDebugWindow *debug_window;
907   gchar *name;
908   ServiceType type;
909 } FillServiceChooserData;
910
911 static FillServiceChooserData *
912 fill_service_chooser_data_new (EmpathyDebugWindow *window,
913     const gchar *name,
914     ServiceType type)
915 {
916   FillServiceChooserData * data = g_slice_new (FillServiceChooserData);
917
918   data->debug_window = window;
919   data->name = g_strdup (name);
920   data->type = SERVICE_TYPE_CM;
921   return data;
922 }
923
924 static void
925 fill_service_chooser_data_free (FillServiceChooserData *data)
926 {
927   g_free (data->name);
928   g_slice_free (FillServiceChooserData, data);
929 }
930
931 static void
932 debug_window_get_name_owner_cb (TpDBusDaemon *proxy,
933     const gchar *out,
934     const GError *error,
935     gpointer user_data,
936     GObject *weak_object)
937 {
938   FillServiceChooserData *data = (FillServiceChooserData *) user_data;
939   EmpathyDebugWindow *self = EMPATHY_DEBUG_WINDOW (data->debug_window);
940   EmpathyDebugWindowPriv *priv = GET_PRIV (data->debug_window);
941   GtkTreeIter iter;
942
943   priv->name_owner_cb_count++;
944
945   if (error != NULL)
946     {
947       DEBUG ("GetNameOwner failed: %s", error->message);
948       goto OUT;
949     }
950
951   if (!debug_window_service_is_in_model (data->debug_window, out, NULL, FALSE))
952     {
953       char *name;
954       GtkListStore *active_buffer, *pause_buffer;
955
956       DEBUG ("Adding %s to list: %s at unique name: %s",
957           data->type == SERVICE_TYPE_CM? "CM": "Client",
958           data->name, out);
959
960       if (data->type == SERVICE_TYPE_CM)
961         name = get_cm_display_name (self, data->name);
962       else
963         name = g_strdup (data->name);
964
965       active_buffer = new_list_store_for_service ();
966       pause_buffer = new_list_store_for_service ();
967
968       gtk_list_store_append (priv->service_store, &iter);
969       gtk_list_store_set (priv->service_store, &iter,
970           COL_NAME, name,
971           COL_UNIQUE_NAME, out,
972           COL_GONE, FALSE,
973           COL_ACTIVE_BUFFER, active_buffer,
974           COL_PAUSE_BUFFER, pause_buffer,
975           COL_PROXY, NULL,
976           -1);
977
978       g_object_unref (active_buffer);
979       g_object_unref (pause_buffer);
980
981       if (priv->select_name != NULL &&
982           !tp_strdiff (name, priv->select_name))
983         {
984           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->chooser), &iter);
985           tp_clear_pointer (&priv->select_name, g_free);
986         }
987
988       g_free (name);
989     }
990
991     if (priv->services_detected == priv->name_owner_cb_count)
992       {
993         /* Time to add "All" selection to service_store */
994         gtk_list_store_prepend (priv->service_store, &iter);
995         gtk_list_store_set (priv->service_store, &iter,
996             COL_NAME, "All",
997             COL_ACTIVE_BUFFER, NULL,
998             -1);
999
1000         priv->all_active_buffer = new_list_store_for_service ();
1001
1002         /* Populate active buffers for all services */
1003         refresh_all_buffer (self);
1004
1005         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->chooser), 0);
1006       }
1007
1008 OUT:
1009   fill_service_chooser_data_free (data);
1010 }
1011
1012 static void
1013 debug_window_list_connection_names_cb (const gchar * const *names,
1014     gsize n,
1015     const gchar * const *cms,
1016     const gchar * const *protocols,
1017     const GError *error,
1018     gpointer user_data,
1019     GObject *weak_object)
1020 {
1021   EmpathyDebugWindow *debug_window = (EmpathyDebugWindow *) user_data;
1022   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1023   guint i;
1024   TpDBusDaemon *dbus;
1025   GError *error2 = NULL;
1026
1027   if (error != NULL)
1028     {
1029       DEBUG ("list_connection_names failed: %s", error->message);
1030       return;
1031     }
1032
1033   dbus = tp_dbus_daemon_dup (&error2);
1034
1035   if (error2 != NULL)
1036     {
1037       DEBUG ("Failed to dup TpDBusDaemon.");
1038       g_error_free (error2);
1039       return;
1040     }
1041
1042   for (i = 0; cms[i] != NULL; i++)
1043     {
1044       FillServiceChooserData *data = fill_service_chooser_data_new (
1045           debug_window, cms[i], SERVICE_TYPE_CM);
1046
1047       tp_cli_dbus_daemon_call_get_name_owner (dbus, -1,
1048           names[i], debug_window_get_name_owner_cb,
1049           data, NULL, NULL);
1050
1051       priv->services_detected ++;
1052     }
1053
1054   g_object_unref (dbus);
1055 }
1056
1057 static void
1058 debug_window_name_owner_changed_cb (TpDBusDaemon *proxy,
1059     const gchar *arg0,
1060     const gchar *arg1,
1061     const gchar *arg2,
1062     gpointer user_data,
1063     GObject *weak_object)
1064 {
1065   EmpathyDebugWindow *self = EMPATHY_DEBUG_WINDOW (user_data);
1066   EmpathyDebugWindowPriv *priv = GET_PRIV (user_data);
1067   ServiceType type;
1068   const gchar *name;
1069
1070   if (g_str_has_prefix (arg0, TP_CM_BUS_NAME_BASE))
1071     {
1072       type = SERVICE_TYPE_CM;
1073       name = arg0 + strlen (TP_CM_BUS_NAME_BASE);
1074     }
1075   else if (g_str_has_prefix (arg0, TP_CLIENT_BUS_NAME_BASE))
1076     {
1077       type = SERVICE_TYPE_CLIENT;
1078       name = arg0 + strlen (TP_CLIENT_BUS_NAME_BASE);
1079     }
1080   else
1081     {
1082       return;
1083     }
1084
1085   if (EMP_STR_EMPTY (arg1) && !EMP_STR_EMPTY (arg2))
1086     {
1087       GtkTreeIter *found_at_iter = NULL;
1088       gchar *display_name;
1089
1090       if (type == SERVICE_TYPE_CM)
1091         display_name = get_cm_display_name (self, name);
1092       else
1093         display_name = g_strdup (name);
1094
1095       /* A service joined */
1096       if (!debug_window_service_is_in_model (user_data, display_name,
1097            &found_at_iter, TRUE))
1098         {
1099           GtkTreeIter iter;
1100           GtkListStore *active_buffer, *pause_buffer;
1101
1102           DEBUG ("Adding new service '%s' at %s.", name, arg2);
1103
1104           active_buffer = new_list_store_for_service ();
1105           pause_buffer = new_list_store_for_service ();
1106
1107           gtk_list_store_append (priv->service_store, &iter);
1108           gtk_list_store_set (priv->service_store, &iter,
1109               COL_NAME, display_name,
1110               COL_UNIQUE_NAME, arg2,
1111               COL_GONE, FALSE,
1112               COL_ACTIVE_BUFFER, active_buffer,
1113               COL_PAUSE_BUFFER, pause_buffer,
1114               COL_PROXY, NULL,
1115               -1);
1116
1117           g_object_unref (active_buffer);
1118           g_object_unref (pause_buffer);
1119         }
1120       else
1121         {
1122           /* a service with the same name is already in the service_store,
1123            * update it and set it as re-enabled.
1124            */
1125           GtkListStore *active_buffer, *pause_buffer;
1126           TpProxy *stored_proxy;
1127
1128           DEBUG ("Refreshing CM '%s' at '%s'.", name, arg2);
1129
1130           active_buffer= new_list_store_for_service ();
1131           pause_buffer = new_list_store_for_service ();
1132
1133           gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store),
1134               found_at_iter, COL_PROXY, &stored_proxy, -1);
1135
1136           tp_clear_object (&stored_proxy);
1137
1138           gtk_list_store_set (priv->service_store, found_at_iter,
1139               COL_NAME, display_name,
1140               COL_UNIQUE_NAME, arg2,
1141               COL_GONE, FALSE,
1142               COL_ACTIVE_BUFFER, active_buffer,
1143               COL_PAUSE_BUFFER, pause_buffer,
1144               COL_PROXY, NULL,
1145               -1);
1146
1147           g_object_unref (active_buffer);
1148           g_object_unref (pause_buffer);
1149
1150           gtk_tree_iter_free (found_at_iter);
1151
1152           debug_window_service_chooser_changed_cb
1153             (GTK_COMBO_BOX (priv->chooser), user_data);
1154         }
1155
1156       /* If a new service arrives when "All" is selected, the view will
1157        * not show its messages which we do not want. So we refresh All's
1158        * active buffer.
1159        * Similarly for when a service with an already seen service name
1160        * appears. */
1161       refresh_all_buffer (self);
1162
1163       g_free (display_name);
1164     }
1165   else if (!EMP_STR_EMPTY (arg1) && EMP_STR_EMPTY (arg2))
1166     {
1167       /* A service died */
1168       GtkTreeIter *iter = NULL;
1169
1170       DEBUG ("Setting service disabled from %s.", arg1);
1171
1172       /* set the service as disabled in the model */
1173       if (debug_window_service_is_in_model (user_data, arg1, &iter, FALSE))
1174         {
1175           gtk_list_store_set (priv->service_store,
1176               iter, COL_GONE, TRUE, -1);
1177           gtk_tree_iter_free (iter);
1178         }
1179
1180       /* Refresh all's active buffer */
1181       refresh_all_buffer (self);
1182     }
1183 }
1184
1185 static void
1186 add_client (EmpathyDebugWindow *self,
1187     const gchar *name)
1188 {
1189   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
1190   const gchar *suffix;
1191   FillServiceChooserData *data;
1192
1193   suffix = name + strlen (TP_CLIENT_BUS_NAME_BASE);
1194
1195   data = fill_service_chooser_data_new (self, suffix, SERVICE_TYPE_CLIENT);
1196
1197   tp_cli_dbus_daemon_call_get_name_owner (priv->dbus, -1,
1198       name, debug_window_get_name_owner_cb, data, NULL, NULL);
1199
1200   priv->services_detected ++;
1201 }
1202
1203 static void
1204 list_names_cb (TpDBusDaemon *bus_daemon,
1205     const gchar * const *names,
1206     const GError *error,
1207     gpointer user_data,
1208     GObject *weak_object)
1209 {
1210   EmpathyDebugWindow *self = EMPATHY_DEBUG_WINDOW (weak_object);
1211   guint i;
1212
1213   if (error != NULL)
1214     {
1215       DEBUG ("Failed to list names: %s", error->message);
1216       return;
1217     }
1218
1219   for (i = 0; names[i] != NULL; i++)
1220     {
1221       if (g_str_has_prefix (names[i], TP_CLIENT_BUS_NAME_BASE))
1222         {
1223           add_client (self, names[i]);
1224         }
1225     }
1226 }
1227
1228 static void
1229 debug_window_fill_service_chooser (EmpathyDebugWindow *debug_window)
1230 {
1231   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1232   GError *error = NULL;
1233   GtkTreeIter iter;
1234   GtkListStore *active_buffer, *pause_buffer;
1235
1236   priv->dbus = tp_dbus_daemon_dup (&error);
1237
1238   if (error != NULL)
1239     {
1240       DEBUG ("Failed to dup dbus daemon: %s", error->message);
1241       g_error_free (error);
1242       return;
1243     }
1244
1245   /* Keep a count of the services detected and added */
1246   priv->services_detected = 0;
1247   priv->name_owner_cb_count = 0;
1248
1249   /* Add CMs to list */
1250   tp_list_connection_names (priv->dbus, debug_window_list_connection_names_cb,
1251       debug_window, NULL, NULL);
1252
1253   /* add Mission Control */
1254   active_buffer= new_list_store_for_service ();
1255   pause_buffer = new_list_store_for_service ();
1256
1257   gtk_list_store_append (priv->service_store, &iter);
1258   gtk_list_store_set (priv->service_store, &iter,
1259       COL_NAME, "mission-control",
1260       COL_UNIQUE_NAME, "org.freedesktop.Telepathy.MissionControl5",
1261       COL_GONE, FALSE,
1262       COL_ACTIVE_BUFFER, active_buffer,
1263       COL_PAUSE_BUFFER, pause_buffer,
1264       COL_PROXY, NULL,
1265       -1);
1266   g_object_unref (active_buffer);
1267   g_object_unref (pause_buffer);
1268
1269   /* add clients */
1270   tp_dbus_daemon_list_names (priv->dbus, 2000,
1271       list_names_cb, NULL, NULL, G_OBJECT (debug_window));
1272
1273   priv->name_owner_changed_signal =
1274       tp_cli_dbus_daemon_connect_to_name_owner_changed (priv->dbus,
1275       debug_window_name_owner_changed_cb, debug_window, NULL, NULL, NULL);
1276 }
1277
1278 static void
1279 debug_window_pause_toggled_cb (GtkToggleToolButton *pause_,
1280     EmpathyDebugWindow *debug_window)
1281 {
1282   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1283   GtkTreeIter iter;
1284   gboolean valid_iter;
1285   GtkTreeModel *model = GTK_TREE_MODEL (priv->service_store);
1286
1287   priv->paused = gtk_toggle_tool_button_get_active (pause_);
1288
1289   if (!priv->paused)
1290     {
1291       /* Pause has been released - flush all pause buffers */
1292       GtkTreeModel *service_store = GTK_TREE_MODEL (priv->service_store);
1293
1294       /* Skipping the first iter which is reserved for "All" */
1295       gtk_tree_model_get_iter_first (model, &iter);
1296       for (valid_iter = gtk_tree_model_iter_next (model, &iter);
1297            valid_iter;
1298            valid_iter = gtk_tree_model_iter_next (model, &iter))
1299         {
1300           GtkListStore *pause_buffer, *active_buffer;
1301
1302           gtk_tree_model_get (service_store, &iter,
1303               COL_PAUSE_BUFFER, &pause_buffer,
1304               COL_ACTIVE_BUFFER, &active_buffer,
1305               -1);
1306
1307           gtk_tree_model_foreach (GTK_TREE_MODEL (pause_buffer),
1308               copy_buffered_messages, active_buffer);
1309           gtk_tree_model_foreach (GTK_TREE_MODEL (pause_buffer),
1310               copy_buffered_messages, priv->all_active_buffer);
1311
1312           gtk_list_store_clear (pause_buffer);
1313
1314           g_object_unref (active_buffer);
1315           g_object_unref (pause_buffer);
1316         }
1317     }
1318 }
1319
1320 static void
1321 debug_window_filter_changed_cb (GtkComboBox *filter,
1322     EmpathyDebugWindow *debug_window)
1323 {
1324   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1325
1326   gtk_tree_model_filter_refilter (
1327       GTK_TREE_MODEL_FILTER (priv->store_filter));
1328 }
1329
1330 static void
1331 debug_window_clear_clicked_cb (GtkToolButton *clear_button,
1332     EmpathyDebugWindow *debug_window)
1333 {
1334   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1335   GtkTreeIter iter;
1336   GtkListStore *active_buffer;
1337
1338   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->chooser), &iter);
1339   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
1340       COL_ACTIVE_BUFFER, &active_buffer, -1);
1341
1342   gtk_list_store_clear (active_buffer);
1343
1344   g_object_unref (active_buffer);
1345 }
1346
1347 static void
1348 debug_window_menu_copy_activate_cb (GtkMenuItem *menu_item,
1349     EmpathyDebugWindow *debug_window)
1350 {
1351   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1352   GtkTreePath *path;
1353   GtkTreeViewColumn *focus_column;
1354   GtkTreeIter iter;
1355   gchar *message;
1356   GtkClipboard *clipboard;
1357
1358   gtk_tree_view_get_cursor (GTK_TREE_VIEW (priv->view),
1359       &path, &focus_column);
1360
1361   if (path == NULL)
1362     {
1363       DEBUG ("No row is in focus");
1364       return;
1365     }
1366
1367   gtk_tree_model_get_iter (priv->store_filter, &iter, path);
1368
1369   gtk_tree_model_get (priv->store_filter, &iter,
1370       COL_DEBUG_MESSAGE, &message,
1371       -1);
1372
1373   if (EMP_STR_EMPTY (message))
1374     {
1375       DEBUG ("Log message is empty");
1376       return;
1377     }
1378
1379   clipboard = gtk_clipboard_get_for_display (
1380       gtk_widget_get_display (GTK_WIDGET (menu_item)),
1381       GDK_SELECTION_CLIPBOARD);
1382
1383   gtk_clipboard_set_text (clipboard, message, -1);
1384
1385   g_free (message);
1386 }
1387
1388 typedef struct
1389 {
1390   EmpathyDebugWindow *debug_window;
1391   guint button;
1392   guint32 time;
1393 } MenuPopupData;
1394
1395 static gboolean
1396 debug_window_show_menu (gpointer user_data)
1397 {
1398   MenuPopupData *data = (MenuPopupData *) user_data;
1399   GtkWidget *menu, *item;
1400   GtkMenuShell *shell;
1401
1402   menu = empathy_context_menu_new (GTK_WIDGET (data->debug_window));
1403   shell = GTK_MENU_SHELL (menu);
1404
1405   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
1406
1407   g_signal_connect (item, "activate",
1408       G_CALLBACK (debug_window_menu_copy_activate_cb), data->debug_window);
1409
1410   gtk_menu_shell_append (shell, item);
1411   gtk_widget_show (item);
1412
1413   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1414      data->button, data->time);
1415
1416   g_slice_free (MenuPopupData, user_data);
1417
1418   return FALSE;
1419 }
1420
1421 static gboolean
1422 debug_window_button_press_event_cb (GtkTreeView *view,
1423     GdkEventButton *event,
1424     gpointer user_data)
1425 {
1426   /* A mouse button was pressed on the tree view. */
1427
1428   if (event->button == 3)
1429     {
1430       /* The tree view was right-clicked. (3 == third mouse button) */
1431       MenuPopupData *data;
1432       data = g_slice_new0 (MenuPopupData);
1433       data->debug_window = user_data;
1434       data->button = event->button;
1435       data->time = event->time;
1436       g_idle_add (debug_window_show_menu, data);
1437     }
1438
1439   return FALSE;
1440 }
1441
1442 static gchar *
1443 debug_window_format_timestamp (gdouble timestamp)
1444 {
1445   struct tm *tstruct;
1446   char time_str[32];
1447   gint ms;
1448   time_t sec;
1449   gchar *text;
1450
1451   ms = (int) ((timestamp - (int) timestamp)*1e6);
1452   sec = (long) timestamp;
1453   tstruct = localtime ((time_t *) &sec);
1454   if (!strftime (time_str, sizeof (time_str), "%x %T", tstruct))
1455     {
1456       DEBUG ("Failed to format timestamp: %e", timestamp);
1457       time_str[0] = '\0';
1458     }
1459
1460   text = g_strdup_printf ("%s.%d", time_str, ms);
1461
1462   return text;
1463 }
1464
1465 static void
1466 debug_window_time_formatter (GtkTreeViewColumn *tree_column,
1467     GtkCellRenderer *cell,
1468     GtkTreeModel *tree_model,
1469     GtkTreeIter *iter,
1470     gpointer data)
1471 {
1472   gdouble timestamp;
1473   gchar *time_str;
1474
1475   gtk_tree_model_get (tree_model, iter, COL_DEBUG_TIMESTAMP, &timestamp, -1);
1476
1477   time_str = debug_window_format_timestamp (timestamp);
1478
1479   g_object_set (G_OBJECT (cell), "text", time_str, NULL);
1480
1481   g_free (time_str);
1482 }
1483
1484 static gboolean
1485 debug_window_store_filter_foreach (GtkTreeModel *model,
1486     GtkTreePath *path,
1487     GtkTreeIter *iter,
1488     gpointer user_data)
1489 {
1490   GFileOutputStream *output_stream = (GFileOutputStream *) user_data;
1491   gchar *domain, *category, *message, *level_str, *level_upper;
1492   gdouble timestamp;
1493   gchar *line, *time_str;
1494   GError *error = NULL;
1495   gboolean out = FALSE;
1496
1497   gtk_tree_model_get (model, iter,
1498       COL_DEBUG_TIMESTAMP, &timestamp,
1499       COL_DEBUG_DOMAIN, &domain,
1500       COL_DEBUG_CATEGORY, &category,
1501       COL_DEBUG_LEVEL_STRING, &level_str,
1502       COL_DEBUG_MESSAGE, &message,
1503       -1);
1504
1505   level_upper = g_ascii_strup (level_str, -1);
1506
1507   time_str = debug_window_format_timestamp (timestamp);
1508
1509   line = g_strdup_printf ("%s%s%s-%s: %s: %s\n",
1510       domain, EMP_STR_EMPTY (category) ? "" : "/",
1511       category, level_upper, time_str, message);
1512
1513   g_free (time_str);
1514
1515   g_output_stream_write (G_OUTPUT_STREAM (output_stream), line,
1516       strlen (line), NULL, &error);
1517
1518   if (error != NULL)
1519     {
1520       DEBUG ("Failed to write to file: %s", error->message);
1521       g_error_free (error);
1522       out = TRUE;
1523     }
1524
1525   g_free (line);
1526   g_free (level_upper);
1527   g_free (level_str);
1528   g_free (domain);
1529   g_free (category);
1530   g_free (message);
1531
1532   return out;
1533 }
1534
1535 static void
1536 debug_window_save_file_chooser_response_cb (GtkDialog *dialog,
1537     gint response_id,
1538     EmpathyDebugWindow *debug_window)
1539 {
1540   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1541   gchar *filename = NULL;
1542   GFile *gfile = NULL;
1543   GFileOutputStream *output_stream = NULL;
1544   GError *error = NULL;
1545
1546   if (response_id != GTK_RESPONSE_ACCEPT)
1547     goto OUT;
1548
1549   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1550
1551   DEBUG ("Saving log as %s", filename);
1552
1553   gfile = g_file_new_for_path (filename);
1554   output_stream = g_file_replace (gfile, NULL, FALSE,
1555       G_FILE_CREATE_NONE, NULL, &error);
1556
1557   if (error != NULL)
1558     {
1559       DEBUG ("Failed to open file for writing: %s", error->message);
1560       g_error_free (error);
1561       goto OUT;
1562     }
1563
1564   gtk_tree_model_foreach (priv->store_filter,
1565       debug_window_store_filter_foreach, output_stream);
1566
1567 OUT:
1568   if (gfile != NULL)
1569     g_object_unref (gfile);
1570
1571   if (output_stream != NULL)
1572     g_object_unref (output_stream);
1573
1574   if (filename != NULL)
1575     g_free (filename);
1576
1577   gtk_widget_destroy (GTK_WIDGET (dialog));
1578 }
1579
1580 static void
1581 debug_window_save_clicked_cb (GtkToolButton *tool_button,
1582     EmpathyDebugWindow *debug_window)
1583 {
1584   GtkWidget *file_chooser;
1585   gchar *name, *tmp = NULL;
1586   char time_str[32];
1587   time_t t;
1588   struct tm *tm_s;
1589
1590   file_chooser = gtk_file_chooser_dialog_new (_("Save"),
1591       GTK_WINDOW (debug_window), GTK_FILE_CHOOSER_ACTION_SAVE,
1592       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1593       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1594       NULL);
1595
1596   gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
1597   gtk_file_chooser_set_do_overwrite_confirmation (
1598       GTK_FILE_CHOOSER (file_chooser), TRUE);
1599
1600   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (file_chooser),
1601       g_get_home_dir ());
1602
1603   name = get_active_service_name (debug_window);
1604
1605   t = time (NULL);
1606   tm_s = localtime (&t);
1607   if (tm_s != NULL)
1608     {
1609       if (strftime(time_str, sizeof (time_str), "%d-%m-%y_%H-%M-%S", tm_s))
1610         tmp = g_strdup_printf ("%s-%s.log", name, time_str);
1611     }
1612
1613   if (tmp == NULL)
1614     tmp = g_strdup_printf ("%s.log", name);
1615   g_free (name);
1616
1617   gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser), tmp);
1618   g_free (tmp);
1619
1620   g_signal_connect (file_chooser, "response",
1621       G_CALLBACK (debug_window_save_file_chooser_response_cb),
1622       debug_window);
1623
1624   gtk_widget_show (file_chooser);
1625 }
1626
1627 static gboolean
1628 debug_window_copy_model_foreach (GtkTreeModel *model,
1629     GtkTreePath *path,
1630     GtkTreeIter *iter,
1631     gpointer user_data)
1632 {
1633   gchar **text = (gchar **) user_data;
1634   gchar *tmp;
1635   gchar *domain, *category, *message, *level_str, *level_upper;
1636   gdouble timestamp;
1637   gchar *line, *time_str;
1638
1639   gtk_tree_model_get (model, iter,
1640       COL_DEBUG_TIMESTAMP, &timestamp,
1641       COL_DEBUG_DOMAIN, &domain,
1642       COL_DEBUG_CATEGORY, &category,
1643       COL_DEBUG_LEVEL_STRING, &level_str,
1644       COL_DEBUG_MESSAGE, &message,
1645       -1);
1646
1647   level_upper = g_ascii_strup (level_str, -1);
1648
1649   time_str = debug_window_format_timestamp (timestamp);
1650
1651   line = g_strdup_printf ("%s%s%s-%s: %s: %s\n",
1652       domain, EMP_STR_EMPTY (category) ? "" : "/",
1653       category, level_upper, time_str, message);
1654
1655   g_free (time_str);
1656
1657   tmp = g_strconcat (*text, line, NULL);
1658
1659   g_free (*text);
1660   g_free (line);
1661   g_free (level_upper);
1662   g_free (level_str);
1663   g_free (domain);
1664   g_free (category);
1665   g_free (message);
1666
1667   *text = tmp;
1668
1669   return FALSE;
1670 }
1671
1672 static void
1673 debug_window_copy_clicked_cb (GtkToolButton *tool_button,
1674     EmpathyDebugWindow *debug_window)
1675 {
1676   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1677   GtkClipboard *clipboard;
1678   gchar *text;
1679
1680   text = g_strdup ("");
1681
1682   gtk_tree_model_foreach (priv->store_filter,
1683       debug_window_copy_model_foreach, &text);
1684
1685   clipboard = gtk_clipboard_get_for_display (
1686       gtk_widget_get_display (GTK_WIDGET (tool_button)),
1687       GDK_SELECTION_CLIPBOARD);
1688
1689   DEBUG ("Copying text to clipboard (length: %" G_GSIZE_FORMAT ")",
1690       strlen (text));
1691
1692   gtk_clipboard_set_text (clipboard, text, -1);
1693
1694   g_free (text);
1695 }
1696
1697 static gboolean
1698 debug_window_key_press_event_cb (GtkWidget *widget,
1699     GdkEventKey *event,
1700     gpointer user_data)
1701 {
1702   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
1703       || event->keyval == GDK_KEY_Escape)
1704     {
1705       gtk_widget_destroy (widget);
1706       return TRUE;
1707     }
1708
1709   return FALSE;
1710 }
1711
1712 static void
1713 empathy_debug_window_select_name (EmpathyDebugWindow *self,
1714     const gchar *name)
1715 {
1716   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
1717   GtkTreeModel *model = GTK_TREE_MODEL (priv->service_store);
1718   GtkTreeIter iter;
1719   gchar *iter_name;
1720   gboolean valid, found = FALSE;
1721
1722   for (valid = gtk_tree_model_get_iter_first (model, &iter);
1723        valid;
1724        valid = gtk_tree_model_iter_next (model, &iter))
1725     {
1726       gtk_tree_model_get (model, &iter,
1727           COL_NAME, &iter_name,
1728           -1);
1729
1730       if (!tp_strdiff (name, iter_name))
1731         found = TRUE;
1732
1733       g_free (iter_name);
1734
1735       if (found)
1736         break;
1737     }
1738
1739   if (found)
1740     gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->chooser), &iter);
1741 }
1742
1743 static void
1744 am_prepared_cb (GObject *am,
1745     GAsyncResult *res,
1746     gpointer user_data)
1747 {
1748   GObject *object = user_data;
1749   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
1750   GtkWidget *vbox;
1751   GtkWidget *toolbar;
1752   GtkWidget *image;
1753   GtkWidget *label;
1754   GtkToolItem *item;
1755   GtkCellRenderer *renderer;
1756   GtkListStore *level_store;
1757   GtkTreeIter iter;
1758   GError *error = NULL;
1759
1760   if (!tp_proxy_prepare_finish (am, res, &error))
1761     {
1762       g_warning ("Failed to prepare AM: %s", error->message);
1763       g_clear_error (&error);
1764     }
1765
1766   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
1767   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
1768   empathy_geometry_bind (GTK_WINDOW (object), "debug-window");
1769
1770   g_signal_connect (object, "key-press-event",
1771       G_CALLBACK (debug_window_key_press_event_cb), NULL);
1772
1773   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1774   gtk_container_add (GTK_CONTAINER (object), vbox);
1775   gtk_widget_show (vbox);
1776
1777   toolbar = gtk_toolbar_new ();
1778   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
1779   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
1780   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar),
1781       GTK_ICON_SIZE_SMALL_TOOLBAR);
1782   gtk_style_context_add_class (gtk_widget_get_style_context (toolbar),
1783                                GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
1784   gtk_widget_show (toolbar);
1785
1786   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
1787
1788   /* CM */
1789   priv->chooser = gtk_combo_box_text_new ();
1790   priv->service_store = gtk_list_store_new (NUM_COLS,
1791       G_TYPE_STRING,  /* COL_NAME */
1792       G_TYPE_STRING,  /* COL_UNIQUE_NAME */
1793       G_TYPE_BOOLEAN, /* COL_GONE */
1794       G_TYPE_OBJECT,  /* COL_ACTIVE_BUFFER */
1795       G_TYPE_OBJECT,  /* COL_PAUSE_BUFFER */
1796       TP_TYPE_PROXY); /* COL_PROXY */
1797   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->chooser),
1798       GTK_TREE_MODEL (priv->service_store));
1799   gtk_widget_show (priv->chooser);
1800
1801   item = gtk_tool_item_new ();
1802   gtk_widget_show (GTK_WIDGET (item));
1803   gtk_container_add (GTK_CONTAINER (item), priv->chooser);
1804   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1805   g_signal_connect (priv->chooser, "changed",
1806       G_CALLBACK (debug_window_service_chooser_changed_cb), object);
1807   gtk_widget_show (GTK_WIDGET (priv->chooser));
1808
1809   item = gtk_separator_tool_item_new ();
1810   gtk_widget_show (GTK_WIDGET (item));
1811   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1812
1813   /* Save */
1814   priv->save_button = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
1815   g_signal_connect (priv->save_button, "clicked",
1816       G_CALLBACK (debug_window_save_clicked_cb), object);
1817   gtk_widget_show (GTK_WIDGET (priv->save_button));
1818   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->save_button), TRUE);
1819   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->save_button, -1);
1820
1821   /* Copy */
1822   priv->copy_button = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
1823   g_signal_connect (priv->copy_button, "clicked",
1824       G_CALLBACK (debug_window_copy_clicked_cb), object);
1825   gtk_widget_show (GTK_WIDGET (priv->copy_button));
1826   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->copy_button), TRUE);
1827   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->copy_button, -1);
1828
1829   /* Clear */
1830   priv->clear_button = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
1831   g_signal_connect (priv->clear_button, "clicked",
1832       G_CALLBACK (debug_window_clear_clicked_cb), object);
1833   gtk_widget_show (GTK_WIDGET (priv->clear_button));
1834   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->clear_button), TRUE);
1835   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->clear_button, -1);
1836
1837   item = gtk_separator_tool_item_new ();
1838   gtk_widget_show (GTK_WIDGET (item));
1839   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1840
1841   /* Pause */
1842   priv->paused = FALSE;
1843   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE,
1844       GTK_ICON_SIZE_MENU);
1845   gtk_widget_show (image);
1846   priv->pause_button = gtk_toggle_tool_button_new ();
1847   gtk_toggle_tool_button_set_active (
1848       GTK_TOGGLE_TOOL_BUTTON (priv->pause_button), priv->paused);
1849   g_signal_connect (priv->pause_button, "toggled",
1850       G_CALLBACK (debug_window_pause_toggled_cb), object);
1851   gtk_widget_show (GTK_WIDGET (priv->pause_button));
1852   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->pause_button), TRUE);
1853   gtk_tool_button_set_label (GTK_TOOL_BUTTON (priv->pause_button), _("Pause"));
1854   gtk_tool_button_set_icon_widget (
1855       GTK_TOOL_BUTTON (priv->pause_button), image);
1856   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->pause_button, -1);
1857
1858   item = gtk_separator_tool_item_new ();
1859   gtk_widget_show (GTK_WIDGET (item));
1860   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1861
1862   /* Level */
1863   priv->level_label = gtk_tool_item_new ();
1864   gtk_widget_show (GTK_WIDGET (priv->level_label));
1865   label = gtk_label_new (_("Level "));
1866   gtk_widget_show (label);
1867   gtk_container_add (GTK_CONTAINER (priv->level_label), label);
1868   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->level_label, -1);
1869
1870   priv->level_filter = gtk_combo_box_text_new ();
1871   gtk_widget_show (priv->level_filter);
1872
1873   item = gtk_tool_item_new ();
1874   gtk_widget_show (GTK_WIDGET (item));
1875   gtk_container_add (GTK_CONTAINER (item), priv->level_filter);
1876   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1877
1878   level_store = gtk_list_store_new (NUM_COLS_LEVEL,
1879       G_TYPE_STRING, G_TYPE_UINT);
1880   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->level_filter),
1881       GTK_TREE_MODEL (level_store));
1882
1883   gtk_list_store_append (level_store, &iter);
1884   gtk_list_store_set (level_store, &iter,
1885       COL_LEVEL_NAME, _("Debug"),
1886       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_DEBUG,
1887       -1);
1888
1889   gtk_list_store_append (level_store, &iter);
1890   gtk_list_store_set (level_store, &iter,
1891       COL_LEVEL_NAME, _("Info"),
1892       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_INFO,
1893       -1);
1894
1895   gtk_list_store_append (level_store, &iter);
1896   gtk_list_store_set (level_store, &iter,
1897       COL_LEVEL_NAME, _("Message"),
1898       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_MESSAGE,
1899       -1);
1900
1901   gtk_list_store_append (level_store, &iter);
1902   gtk_list_store_set (level_store, &iter,
1903       COL_LEVEL_NAME, _("Warning"),
1904       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_WARNING,
1905       -1);
1906
1907   gtk_list_store_append (level_store, &iter);
1908   gtk_list_store_set (level_store, &iter,
1909       COL_LEVEL_NAME, _("Critical"),
1910       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_CRITICAL,
1911       -1);
1912
1913   gtk_list_store_append (level_store, &iter);
1914   gtk_list_store_set (level_store, &iter,
1915       COL_LEVEL_NAME, _("Error"),
1916       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_ERROR,
1917       -1);
1918
1919   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->level_filter), 0);
1920   g_signal_connect (priv->level_filter, "changed",
1921       G_CALLBACK (debug_window_filter_changed_cb), object);
1922
1923   /* Debug treeview */
1924   priv->view = gtk_tree_view_new ();
1925   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
1926
1927   g_signal_connect (priv->view, "button-press-event",
1928       G_CALLBACK (debug_window_button_press_event_cb), object);
1929
1930   renderer = gtk_cell_renderer_text_new ();
1931   g_object_set (renderer, "yalign", 0, NULL);
1932
1933   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->view),
1934       -1, _("Time"), renderer,
1935       (GtkTreeCellDataFunc) debug_window_time_formatter, NULL, NULL);
1936   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1937       -1, _("Domain"), renderer, "text", COL_DEBUG_DOMAIN, NULL);
1938   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1939       -1, _("Category"), renderer, "text", COL_DEBUG_CATEGORY, NULL);
1940   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1941       -1, _("Level"), renderer, "text", COL_DEBUG_LEVEL_STRING, NULL);
1942
1943   renderer = gtk_cell_renderer_text_new ();
1944   g_object_set (renderer, "family", "Monospace", NULL);
1945   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
1946       -1, _("Message"), renderer, "text", COL_DEBUG_MESSAGE, NULL);
1947
1948   priv->store_filter = NULL;
1949
1950   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view), priv->store_filter);
1951
1952   /* Scrolled window */
1953   priv->scrolled_win = g_object_ref (gtk_scrolled_window_new (NULL, NULL));
1954   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_win),
1955       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1956
1957   gtk_widget_show (priv->view);
1958   gtk_container_add (GTK_CONTAINER (priv->scrolled_win), priv->view);
1959
1960   gtk_widget_show (priv->scrolled_win);
1961
1962   /* Not supported label */
1963   priv->not_supported_label = g_object_ref (gtk_label_new (
1964           _("The selected connection manager does not support the remote "
1965               "debugging extension.")));
1966   gtk_widget_show (priv->not_supported_label);
1967   gtk_box_pack_start (GTK_BOX (vbox), priv->not_supported_label,
1968       TRUE, TRUE, 0);
1969
1970   priv->view_visible = FALSE;
1971
1972   priv->all_active_buffer = NULL;
1973
1974   debug_window_set_toolbar_sensitivity (EMPATHY_DEBUG_WINDOW (object), FALSE);
1975   debug_window_fill_service_chooser (EMPATHY_DEBUG_WINDOW (object));
1976   gtk_widget_show (GTK_WIDGET (object));
1977 }
1978
1979 static void
1980 debug_window_constructed (GObject *object)
1981 {
1982   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
1983
1984   priv->am = tp_account_manager_dup ();
1985   tp_proxy_prepare_async (priv->am, NULL, am_prepared_cb, object);
1986 }
1987
1988 static void
1989 empathy_debug_window_init (EmpathyDebugWindow *empathy_debug_window)
1990 {
1991   EmpathyDebugWindowPriv *priv =
1992       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_window,
1993       EMPATHY_TYPE_DEBUG_WINDOW, EmpathyDebugWindowPriv);
1994
1995   empathy_debug_window->priv = priv;
1996
1997   priv->dispose_run = FALSE;
1998 }
1999
2000 static void
2001 debug_window_set_property (GObject *object,
2002     guint prop_id,
2003     const GValue *value,
2004     GParamSpec *pspec)
2005 {
2006   switch (prop_id)
2007     {
2008       default:
2009         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2010         break;
2011     }
2012 }
2013
2014 static void
2015 debug_window_get_property (GObject *object,
2016     guint prop_id,
2017     GValue *value,
2018     GParamSpec *pspec)
2019 {
2020   switch (prop_id)
2021     {
2022       default:
2023         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2024         break;
2025     }
2026 }
2027
2028 static void
2029 debug_window_finalize (GObject *object)
2030 {
2031   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
2032
2033   g_free (priv->select_name);
2034
2035   (G_OBJECT_CLASS (empathy_debug_window_parent_class)->finalize) (object);
2036 }
2037
2038 static void
2039 debug_window_dispose (GObject *object)
2040 {
2041   EmpathyDebugWindow *selector = EMPATHY_DEBUG_WINDOW (object);
2042   EmpathyDebugWindowPriv *priv = GET_PRIV (selector);
2043
2044   if (priv->dispose_run)
2045     return;
2046
2047   priv->dispose_run = TRUE;
2048
2049   if (priv->name_owner_changed_signal != NULL)
2050     tp_proxy_signal_connection_disconnect (priv->name_owner_changed_signal);
2051
2052   if (priv->service_store != NULL)
2053     g_object_unref (priv->service_store);
2054
2055   if (priv->dbus != NULL)
2056     g_object_unref (priv->dbus);
2057
2058   if (priv->am != NULL)
2059     {
2060       g_object_unref (priv->am);
2061       priv->am = NULL;
2062     }
2063
2064   tp_clear_object (&priv->all_active_buffer);
2065
2066   (G_OBJECT_CLASS (empathy_debug_window_parent_class)->dispose) (object);
2067 }
2068
2069 static void
2070 empathy_debug_window_class_init (EmpathyDebugWindowClass *klass)
2071 {
2072   GObjectClass *object_class = G_OBJECT_CLASS (klass);
2073   object_class->constructed = debug_window_constructed;
2074   object_class->dispose = debug_window_dispose;
2075   object_class->finalize = debug_window_finalize;
2076   object_class->set_property = debug_window_set_property;
2077   object_class->get_property = debug_window_get_property;
2078   g_type_class_add_private (klass, sizeof (EmpathyDebugWindowPriv));
2079 }
2080
2081 /* public methods */
2082
2083 GtkWidget *
2084 empathy_debug_window_new (GtkWindow *parent)
2085 {
2086   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
2087
2088   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_WINDOW,
2089       "transient-for", parent, NULL));
2090 }
2091
2092 void
2093 empathy_debug_window_show (EmpathyDebugWindow *self,
2094     const gchar *name)
2095 {
2096   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
2097
2098   if (priv->service_store != NULL)
2099     {
2100       empathy_debug_window_select_name (self, name);
2101     }
2102   else
2103     {
2104       g_free (priv->select_name);
2105       priv->select_name = g_strdup (name);
2106     }
2107 }