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