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