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