]> git.0d.be Git - empathy.git/blob - src/empathy-debug-window.c
Make clear work on 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 <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   /* "All" is the first choice in the service chooser and it's buffer is
1338    * not saved in the service-store but is accessed using a private
1339    * reference */
1340   if (gtk_combo_box_get_active (GTK_COMBO_BOX (priv->chooser)) == 0)
1341     {
1342       gtk_list_store_clear (priv->all_active_buffer);
1343       return;
1344     }
1345
1346   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->chooser), &iter);
1347   gtk_tree_model_get (GTK_TREE_MODEL (priv->service_store), &iter,
1348       COL_ACTIVE_BUFFER, &active_buffer, -1);
1349
1350   gtk_list_store_clear (active_buffer);
1351
1352   g_object_unref (active_buffer);
1353 }
1354
1355 static void
1356 debug_window_menu_copy_activate_cb (GtkMenuItem *menu_item,
1357     EmpathyDebugWindow *debug_window)
1358 {
1359   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1360   GtkTreePath *path;
1361   GtkTreeViewColumn *focus_column;
1362   GtkTreeIter iter;
1363   gchar *message;
1364   GtkClipboard *clipboard;
1365
1366   gtk_tree_view_get_cursor (GTK_TREE_VIEW (priv->view),
1367       &path, &focus_column);
1368
1369   if (path == NULL)
1370     {
1371       DEBUG ("No row is in focus");
1372       return;
1373     }
1374
1375   gtk_tree_model_get_iter (priv->store_filter, &iter, path);
1376
1377   gtk_tree_model_get (priv->store_filter, &iter,
1378       COL_DEBUG_MESSAGE, &message,
1379       -1);
1380
1381   if (EMP_STR_EMPTY (message))
1382     {
1383       DEBUG ("Log message is empty");
1384       return;
1385     }
1386
1387   clipboard = gtk_clipboard_get_for_display (
1388       gtk_widget_get_display (GTK_WIDGET (menu_item)),
1389       GDK_SELECTION_CLIPBOARD);
1390
1391   gtk_clipboard_set_text (clipboard, message, -1);
1392
1393   g_free (message);
1394 }
1395
1396 typedef struct
1397 {
1398   EmpathyDebugWindow *debug_window;
1399   guint button;
1400   guint32 time;
1401 } MenuPopupData;
1402
1403 static gboolean
1404 debug_window_show_menu (gpointer user_data)
1405 {
1406   MenuPopupData *data = (MenuPopupData *) user_data;
1407   GtkWidget *menu, *item;
1408   GtkMenuShell *shell;
1409
1410   menu = empathy_context_menu_new (GTK_WIDGET (data->debug_window));
1411   shell = GTK_MENU_SHELL (menu);
1412
1413   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
1414
1415   g_signal_connect (item, "activate",
1416       G_CALLBACK (debug_window_menu_copy_activate_cb), data->debug_window);
1417
1418   gtk_menu_shell_append (shell, item);
1419   gtk_widget_show (item);
1420
1421   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1422      data->button, data->time);
1423
1424   g_slice_free (MenuPopupData, user_data);
1425
1426   return FALSE;
1427 }
1428
1429 static gboolean
1430 debug_window_button_press_event_cb (GtkTreeView *view,
1431     GdkEventButton *event,
1432     gpointer user_data)
1433 {
1434   /* A mouse button was pressed on the tree view. */
1435
1436   if (event->button == 3)
1437     {
1438       /* The tree view was right-clicked. (3 == third mouse button) */
1439       MenuPopupData *data;
1440       data = g_slice_new0 (MenuPopupData);
1441       data->debug_window = user_data;
1442       data->button = event->button;
1443       data->time = event->time;
1444       g_idle_add (debug_window_show_menu, data);
1445     }
1446
1447   return FALSE;
1448 }
1449
1450 static gchar *
1451 debug_window_format_timestamp (gdouble timestamp)
1452 {
1453   struct tm *tstruct;
1454   char time_str[32];
1455   gint ms;
1456   time_t sec;
1457   gchar *text;
1458
1459   ms = (int) ((timestamp - (int) timestamp)*1e6);
1460   sec = (long) timestamp;
1461   tstruct = localtime ((time_t *) &sec);
1462   if (!strftime (time_str, sizeof (time_str), "%x %T", tstruct))
1463     {
1464       DEBUG ("Failed to format timestamp: %e", timestamp);
1465       time_str[0] = '\0';
1466     }
1467
1468   text = g_strdup_printf ("%s.%d", time_str, ms);
1469
1470   return text;
1471 }
1472
1473 static void
1474 debug_window_time_formatter (GtkTreeViewColumn *tree_column,
1475     GtkCellRenderer *cell,
1476     GtkTreeModel *tree_model,
1477     GtkTreeIter *iter,
1478     gpointer data)
1479 {
1480   gdouble timestamp;
1481   gchar *time_str;
1482
1483   gtk_tree_model_get (tree_model, iter, COL_DEBUG_TIMESTAMP, &timestamp, -1);
1484
1485   time_str = debug_window_format_timestamp (timestamp);
1486
1487   g_object_set (G_OBJECT (cell), "text", time_str, NULL);
1488
1489   g_free (time_str);
1490 }
1491
1492 static gboolean
1493 debug_window_store_filter_foreach (GtkTreeModel *model,
1494     GtkTreePath *path,
1495     GtkTreeIter *iter,
1496     gpointer user_data)
1497 {
1498   gchar **debug_data = (gchar **)user_data;
1499   gchar *domain, *category, *message, *level_str, *level_upper;
1500   gdouble timestamp;
1501   gchar *line, *time_str, *tmp;
1502
1503   gtk_tree_model_get (model, iter,
1504       COL_DEBUG_TIMESTAMP, &timestamp,
1505       COL_DEBUG_DOMAIN, &domain,
1506       COL_DEBUG_CATEGORY, &category,
1507       COL_DEBUG_LEVEL_STRING, &level_str,
1508       COL_DEBUG_MESSAGE, &message,
1509       -1);
1510
1511   level_upper = g_ascii_strup (level_str, -1);
1512
1513   time_str = debug_window_format_timestamp (timestamp);
1514
1515   line = g_strdup_printf ("%s%s%s-%s: %s: %s\n",
1516       domain, EMP_STR_EMPTY (category) ? "" : "/",
1517       category, level_upper, time_str, message);
1518
1519   g_free (time_str);
1520
1521   /* Compact all message lines in the out parameter debug_data */
1522   if (!tp_str_empty (*debug_data))
1523     tmp = g_strconcat (*debug_data, line, NULL);
1524   else
1525     tmp = g_strdup (line);
1526
1527   g_free (*debug_data);
1528   *debug_data = tmp;
1529
1530   g_free (line);
1531   g_free (level_upper);
1532   g_free (level_str);
1533   g_free (domain);
1534   g_free (category);
1535   g_free (message);
1536
1537   return FALSE;
1538 }
1539
1540 static void
1541 debug_window_save_file_chooser_response_cb (GtkDialog *dialog,
1542     gint response_id,
1543     EmpathyDebugWindow *debug_window)
1544 {
1545   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1546   gchar *filename = NULL;
1547   GFile *gfile = NULL;
1548   gchar *debug_data = NULL;
1549   GFileOutputStream *output_stream = NULL;
1550   GError *file_open_error = NULL;
1551   GError *file_write_error = NULL;
1552
1553   if (response_id != GTK_RESPONSE_ACCEPT)
1554     goto OUT;
1555
1556   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1557
1558   DEBUG ("Saving log as %s", filename);
1559
1560   gfile = g_file_new_for_path (filename);
1561   output_stream = g_file_replace (gfile, NULL, FALSE,
1562       G_FILE_CREATE_NONE, NULL, &file_open_error);
1563
1564   if (file_open_error != NULL)
1565     {
1566       DEBUG ("Failed to open file for writing: %s", file_open_error->message);
1567       g_error_free (file_open_error);
1568       goto OUT;
1569     }
1570
1571   gtk_tree_model_foreach (priv->store_filter,
1572       debug_window_store_filter_foreach, &debug_data);
1573
1574   g_output_stream_write (G_OUTPUT_STREAM (output_stream), debug_data,
1575       strlen (debug_data), NULL, &file_write_error);
1576   g_free (debug_data);
1577
1578   if (file_write_error != NULL)
1579     {
1580       DEBUG ("Failed to write to file: %s", file_write_error->message);
1581       g_error_free (file_write_error);
1582     }
1583
1584 OUT:
1585   if (gfile != NULL)
1586     g_object_unref (gfile);
1587
1588   if (output_stream != NULL)
1589     g_object_unref (output_stream);
1590
1591   if (filename != NULL)
1592     g_free (filename);
1593
1594   gtk_widget_destroy (GTK_WIDGET (dialog));
1595 }
1596
1597 static void
1598 debug_window_save_clicked_cb (GtkToolButton *tool_button,
1599     EmpathyDebugWindow *debug_window)
1600 {
1601   GtkWidget *file_chooser;
1602   gchar *name, *tmp = NULL;
1603   char time_str[32];
1604   time_t t;
1605   struct tm *tm_s;
1606
1607   file_chooser = gtk_file_chooser_dialog_new (_("Save"),
1608       GTK_WINDOW (debug_window), GTK_FILE_CHOOSER_ACTION_SAVE,
1609       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1610       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1611       NULL);
1612
1613   gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
1614   gtk_file_chooser_set_do_overwrite_confirmation (
1615       GTK_FILE_CHOOSER (file_chooser), TRUE);
1616
1617   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (file_chooser),
1618       g_get_home_dir ());
1619
1620   name = get_active_service_name (debug_window);
1621
1622   t = time (NULL);
1623   tm_s = localtime (&t);
1624   if (tm_s != NULL)
1625     {
1626       if (strftime (time_str, sizeof (time_str), "%d-%m-%y_%H-%M-%S", tm_s))
1627         tmp = g_strdup_printf ("%s-%s.log", name, time_str);
1628     }
1629
1630   if (tmp == NULL)
1631     tmp = g_strdup_printf ("%s.log", name);
1632   g_free (name);
1633
1634   gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser), tmp);
1635   g_free (tmp);
1636
1637   g_signal_connect (file_chooser, "response",
1638       G_CALLBACK (debug_window_save_file_chooser_response_cb),
1639       debug_window);
1640
1641   gtk_widget_show (file_chooser);
1642 }
1643
1644 static void
1645 debug_window_pastebin_response_dialog_closed_cb (GtkDialog *dialog,
1646     gint response_id,
1647     SoupBuffer *buffer)
1648 {
1649   soup_buffer_free (buffer);
1650
1651   gtk_widget_destroy (GTK_WIDGET (dialog));
1652 }
1653
1654 static void
1655 debug_window_pastebin_callback (SoupSession *session,
1656     SoupMessage *msg,
1657     gpointer debug_window)
1658 {
1659   GtkWidget *dialog;
1660   SoupBuffer *buffer;
1661
1662   buffer = soup_message_body_flatten (msg->response_body);
1663   if (g_str_has_prefix (buffer->data, "http://pastebin.com/"))
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 link"));
1668
1669       gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
1670           "<a href=\"%s\">%s</a>", buffer->data, buffer->data);
1671     }
1672   else
1673     {
1674       dialog = gtk_message_dialog_new (GTK_WINDOW (debug_window),
1675           GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
1676           _("Pastebin response"));
1677
1678       if (!tp_str_empty (buffer->data))
1679         gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
1680             _("%s"), buffer->data);
1681       else
1682         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1683             _("Data too large for a single paste. Please save logs to file."));
1684     }
1685
1686   g_object_unref (session);
1687
1688   gtk_window_set_transient_for (GTK_WINDOW (dialog), debug_window);
1689
1690   gtk_widget_show_all (GTK_WIDGET (dialog));
1691
1692   g_signal_connect_after (dialog, "response", G_CALLBACK (
1693       debug_window_pastebin_response_dialog_closed_cb), buffer);
1694 }
1695
1696 static void
1697 debug_window_message_dialog (EmpathyDebugWindow *debug_window,
1698     const gchar *primary_text,
1699     const gchar *secondary_text)
1700 {
1701   GtkWidget *dialog;
1702
1703   dialog = gtk_message_dialog_new (GTK_WINDOW (debug_window),
1704       GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
1705       "%s", _(primary_text));
1706   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1707       "%s", _(secondary_text));
1708   gtk_window_set_transient_for (GTK_WINDOW (dialog),
1709       GTK_WINDOW (debug_window));
1710
1711   gtk_dialog_run (GTK_DIALOG (dialog));
1712   gtk_widget_destroy (dialog);
1713 }
1714
1715 static void
1716 debug_window_send_to_pastebin (EmpathyDebugWindow *debug_window,
1717     gchar *debug_data)
1718 {
1719   SoupSession *session;
1720   SoupMessage *msg;
1721   gchar       *api_dev_key, *api_paste_code, *api_paste_name, *formdata;
1722
1723   if (tp_str_empty (debug_data))
1724     {
1725       debug_window_message_dialog (debug_window, "Error", "No data to send");
1726       return;
1727     }
1728
1729   /* Constructing a valid URL for http post. See http://pastebin.com/api#2 */
1730
1731   /* The api_dev_key is the author's developer key to access the Pastebin API
1732    * This developer key is published here with the autorization of pastebin;
1733    * see PASTEBIN-API-KEY.txt */
1734   api_dev_key = soup_uri_encode ("f6ccfabfdcd4b77b825ee38a30d11d52", NULL);
1735   api_paste_code = soup_uri_encode (debug_data, NULL);
1736   api_paste_name = soup_uri_encode ("Empathy debug data", NULL);
1737   formdata = g_strdup_printf ("api_dev_key=%s&api_paste_code=%s"
1738       "&api_paste_name=%s&api_paste_format=text&api_option=paste",
1739       api_dev_key, api_paste_code, api_paste_name);
1740
1741   session = soup_session_async_new ();
1742
1743   msg = soup_message_new ("POST", "http://pastebin.com/api/api_post.php");
1744   soup_message_set_request (msg,
1745       "application/x-www-form-urlencoded;charset=UTF-8", SOUP_MEMORY_COPY,
1746       formdata, strlen (formdata));
1747
1748   g_free (api_dev_key);
1749   g_free (api_paste_code);
1750   g_free (api_paste_name);
1751   g_free (formdata);
1752
1753   soup_session_queue_message (session, msg, debug_window_pastebin_callback,
1754       debug_window);
1755 }
1756
1757 static void
1758 debug_window_send_to_pastebin_cb (GtkToolButton *tool_button,
1759     EmpathyDebugWindow *debug_window)
1760 {
1761   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1762   gchar *debug_data = NULL;
1763
1764   DEBUG ("Preparing debug data for sending to pastebin.");
1765
1766   gtk_tree_model_foreach (priv->store_filter,
1767       debug_window_store_filter_foreach, &debug_data);
1768
1769   debug_window_send_to_pastebin (debug_window, debug_data);
1770   g_free (debug_data);
1771 }
1772
1773 static gboolean
1774 debug_window_copy_model_foreach (GtkTreeModel *model,
1775     GtkTreePath *path,
1776     GtkTreeIter *iter,
1777     gpointer user_data)
1778 {
1779   gchar **text = (gchar **) user_data;
1780   gchar *tmp;
1781   gchar *domain, *category, *message, *level_str, *level_upper;
1782   gdouble timestamp;
1783   gchar *line, *time_str;
1784
1785   gtk_tree_model_get (model, iter,
1786       COL_DEBUG_TIMESTAMP, &timestamp,
1787       COL_DEBUG_DOMAIN, &domain,
1788       COL_DEBUG_CATEGORY, &category,
1789       COL_DEBUG_LEVEL_STRING, &level_str,
1790       COL_DEBUG_MESSAGE, &message,
1791       -1);
1792
1793   level_upper = g_ascii_strup (level_str, -1);
1794
1795   time_str = debug_window_format_timestamp (timestamp);
1796
1797   line = g_strdup_printf ("%s%s%s-%s: %s: %s\n",
1798       domain, EMP_STR_EMPTY (category) ? "" : "/",
1799       category, level_upper, time_str, message);
1800
1801   g_free (time_str);
1802
1803   tmp = g_strconcat (*text, line, NULL);
1804
1805   g_free (*text);
1806   g_free (line);
1807   g_free (level_upper);
1808   g_free (level_str);
1809   g_free (domain);
1810   g_free (category);
1811   g_free (message);
1812
1813   *text = tmp;
1814
1815   return FALSE;
1816 }
1817
1818 static void
1819 debug_window_copy_clicked_cb (GtkToolButton *tool_button,
1820     EmpathyDebugWindow *debug_window)
1821 {
1822   EmpathyDebugWindowPriv *priv = GET_PRIV (debug_window);
1823   GtkClipboard *clipboard;
1824   gchar *text;
1825
1826   text = g_strdup ("");
1827
1828   gtk_tree_model_foreach (priv->store_filter,
1829       debug_window_copy_model_foreach, &text);
1830
1831   clipboard = gtk_clipboard_get_for_display (
1832       gtk_widget_get_display (GTK_WIDGET (tool_button)),
1833       GDK_SELECTION_CLIPBOARD);
1834
1835   DEBUG ("Copying text to clipboard (length: %" G_GSIZE_FORMAT ")",
1836       strlen (text));
1837
1838   gtk_clipboard_set_text (clipboard, text, -1);
1839
1840   g_free (text);
1841 }
1842
1843 static gboolean
1844 debug_window_key_press_event_cb (GtkWidget *widget,
1845     GdkEventKey *event,
1846     gpointer user_data)
1847 {
1848   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
1849       || event->keyval == GDK_KEY_Escape)
1850     {
1851       gtk_widget_destroy (widget);
1852       return TRUE;
1853     }
1854
1855   return FALSE;
1856 }
1857
1858 static void
1859 empathy_debug_window_select_name (EmpathyDebugWindow *self,
1860     const gchar *name)
1861 {
1862   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
1863   GtkTreeModel *model = GTK_TREE_MODEL (priv->service_store);
1864   GtkTreeIter iter;
1865   gchar *iter_name;
1866   gboolean valid, found = FALSE;
1867
1868   for (valid = gtk_tree_model_get_iter_first (model, &iter);
1869        valid;
1870        valid = gtk_tree_model_iter_next (model, &iter))
1871     {
1872       gtk_tree_model_get (model, &iter,
1873           COL_NAME, &iter_name,
1874           -1);
1875
1876       if (!tp_strdiff (name, iter_name))
1877         found = TRUE;
1878
1879       g_free (iter_name);
1880
1881       if (found)
1882         break;
1883     }
1884
1885   if (found)
1886     gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->chooser), &iter);
1887 }
1888
1889 static void
1890 am_prepared_cb (GObject *am,
1891     GAsyncResult *res,
1892     gpointer user_data)
1893 {
1894   GObject *object = user_data;
1895   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
1896   GtkWidget *vbox;
1897   GtkWidget *toolbar;
1898   GtkWidget *image;
1899   GtkWidget *label;
1900   GtkToolItem *item;
1901   GtkCellRenderer *renderer;
1902   GtkListStore *level_store;
1903   GtkTreeIter iter;
1904   GError *error = NULL;
1905
1906   if (!tp_proxy_prepare_finish (am, res, &error))
1907     {
1908       g_warning ("Failed to prepare AM: %s", error->message);
1909       g_clear_error (&error);
1910     }
1911
1912   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
1913   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
1914   empathy_geometry_bind (GTK_WINDOW (object), "debug-window");
1915
1916   g_signal_connect (object, "key-press-event",
1917       G_CALLBACK (debug_window_key_press_event_cb), NULL);
1918
1919   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1920   gtk_container_add (GTK_CONTAINER (object), vbox);
1921   gtk_widget_show (vbox);
1922
1923   toolbar = gtk_toolbar_new ();
1924   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
1925   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
1926   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar),
1927       GTK_ICON_SIZE_SMALL_TOOLBAR);
1928   gtk_style_context_add_class (gtk_widget_get_style_context (toolbar),
1929                                GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
1930   gtk_widget_show (toolbar);
1931
1932   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
1933
1934   /* CM */
1935   priv->chooser = gtk_combo_box_text_new ();
1936   priv->service_store = gtk_list_store_new (NUM_COLS,
1937       G_TYPE_STRING,  /* COL_NAME */
1938       G_TYPE_STRING,  /* COL_UNIQUE_NAME */
1939       G_TYPE_BOOLEAN, /* COL_GONE */
1940       G_TYPE_OBJECT,  /* COL_ACTIVE_BUFFER */
1941       G_TYPE_OBJECT,  /* COL_PAUSE_BUFFER */
1942       TP_TYPE_PROXY); /* COL_PROXY */
1943   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->chooser),
1944       GTK_TREE_MODEL (priv->service_store));
1945   gtk_widget_show (priv->chooser);
1946
1947   item = gtk_tool_item_new ();
1948   gtk_widget_show (GTK_WIDGET (item));
1949   gtk_container_add (GTK_CONTAINER (item), priv->chooser);
1950   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1951   g_signal_connect (priv->chooser, "changed",
1952       G_CALLBACK (debug_window_service_chooser_changed_cb), object);
1953   gtk_widget_show (GTK_WIDGET (priv->chooser));
1954
1955   item = gtk_separator_tool_item_new ();
1956   gtk_widget_show (GTK_WIDGET (item));
1957   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1958
1959   /* Save */
1960   priv->save_button = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
1961   g_signal_connect (priv->save_button, "clicked",
1962       G_CALLBACK (debug_window_save_clicked_cb), object);
1963   gtk_widget_show (GTK_WIDGET (priv->save_button));
1964   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->save_button), TRUE);
1965   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->save_button, -1);
1966
1967   /* Send to pastebin */
1968   priv->send_to_pastebin = gtk_tool_button_new_from_stock (GTK_STOCK_PASTE);
1969   gtk_tool_button_set_label (GTK_TOOL_BUTTON (priv->send_to_pastebin),
1970       _("Send to pastebin"));
1971   g_signal_connect (priv->send_to_pastebin, "clicked",
1972       G_CALLBACK (debug_window_send_to_pastebin_cb), object);
1973   gtk_widget_show (GTK_WIDGET (priv->send_to_pastebin));
1974   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->send_to_pastebin), TRUE);
1975   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->send_to_pastebin, -1);
1976
1977   /* Copy */
1978   priv->copy_button = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
1979   g_signal_connect (priv->copy_button, "clicked",
1980       G_CALLBACK (debug_window_copy_clicked_cb), object);
1981   gtk_widget_show (GTK_WIDGET (priv->copy_button));
1982   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->copy_button), TRUE);
1983   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->copy_button, -1);
1984
1985   /* Clear */
1986   priv->clear_button = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
1987   g_signal_connect (priv->clear_button, "clicked",
1988       G_CALLBACK (debug_window_clear_clicked_cb), object);
1989   gtk_widget_show (GTK_WIDGET (priv->clear_button));
1990   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->clear_button), TRUE);
1991   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->clear_button, -1);
1992
1993   item = gtk_separator_tool_item_new ();
1994   gtk_widget_show (GTK_WIDGET (item));
1995   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
1996
1997   /* Pause */
1998   priv->paused = FALSE;
1999   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE,
2000       GTK_ICON_SIZE_MENU);
2001   gtk_widget_show (image);
2002   priv->pause_button = gtk_toggle_tool_button_new ();
2003   gtk_toggle_tool_button_set_active (
2004       GTK_TOGGLE_TOOL_BUTTON (priv->pause_button), priv->paused);
2005   g_signal_connect (priv->pause_button, "toggled",
2006       G_CALLBACK (debug_window_pause_toggled_cb), object);
2007   gtk_widget_show (GTK_WIDGET (priv->pause_button));
2008   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (priv->pause_button), TRUE);
2009   gtk_tool_button_set_label (GTK_TOOL_BUTTON (priv->pause_button), _("Pause"));
2010   gtk_tool_button_set_icon_widget (
2011       GTK_TOOL_BUTTON (priv->pause_button), image);
2012   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->pause_button, -1);
2013
2014   item = gtk_separator_tool_item_new ();
2015   gtk_widget_show (GTK_WIDGET (item));
2016   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
2017
2018   /* Level */
2019   priv->level_label = gtk_tool_item_new ();
2020   gtk_widget_show (GTK_WIDGET (priv->level_label));
2021   label = gtk_label_new (_("Level "));
2022   gtk_widget_show (label);
2023   gtk_container_add (GTK_CONTAINER (priv->level_label), label);
2024   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->level_label, -1);
2025
2026   priv->level_filter = gtk_combo_box_text_new ();
2027   gtk_widget_show (priv->level_filter);
2028
2029   item = gtk_tool_item_new ();
2030   gtk_widget_show (GTK_WIDGET (item));
2031   gtk_container_add (GTK_CONTAINER (item), priv->level_filter);
2032   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
2033
2034   level_store = gtk_list_store_new (NUM_COLS_LEVEL,
2035       G_TYPE_STRING, G_TYPE_UINT);
2036   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->level_filter),
2037       GTK_TREE_MODEL (level_store));
2038
2039   gtk_list_store_insert_with_values (level_store, &iter, -1,
2040       COL_LEVEL_NAME, _("Debug"),
2041       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_DEBUG,
2042       -1);
2043
2044   gtk_list_store_insert_with_values (level_store, &iter, -1,
2045       COL_LEVEL_NAME, _("Info"),
2046       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_INFO,
2047       -1);
2048
2049   gtk_list_store_insert_with_values (level_store, &iter, -1,
2050       COL_LEVEL_NAME, _("Message"),
2051       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_MESSAGE,
2052       -1);
2053
2054   gtk_list_store_insert_with_values (level_store, &iter, -1,
2055       COL_LEVEL_NAME, _("Warning"),
2056       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_WARNING,
2057       -1);
2058
2059   gtk_list_store_insert_with_values (level_store, &iter, -1,
2060       COL_LEVEL_NAME, _("Critical"),
2061       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_CRITICAL,
2062       -1);
2063
2064   gtk_list_store_insert_with_values (level_store, &iter, -1,
2065       COL_LEVEL_NAME, _("Error"),
2066       COL_LEVEL_VALUE, TP_DEBUG_LEVEL_ERROR,
2067       -1);
2068
2069   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->level_filter), 0);
2070   g_signal_connect (priv->level_filter, "changed",
2071       G_CALLBACK (debug_window_filter_changed_cb), object);
2072
2073   /* Debug treeview */
2074   priv->view = gtk_tree_view_new ();
2075   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
2076
2077   g_signal_connect (priv->view, "button-press-event",
2078       G_CALLBACK (debug_window_button_press_event_cb), object);
2079
2080   renderer = gtk_cell_renderer_text_new ();
2081   g_object_set (renderer, "yalign", 0, NULL);
2082
2083   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->view),
2084       -1, _("Time"), renderer,
2085       (GtkTreeCellDataFunc) debug_window_time_formatter, NULL, NULL);
2086   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
2087       -1, _("Domain"), renderer, "text", COL_DEBUG_DOMAIN, NULL);
2088   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
2089       -1, _("Category"), renderer, "text", COL_DEBUG_CATEGORY, NULL);
2090   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
2091       -1, _("Level"), renderer, "text", COL_DEBUG_LEVEL_STRING, NULL);
2092
2093   renderer = gtk_cell_renderer_text_new ();
2094   g_object_set (renderer, "family", "Monospace", NULL);
2095   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
2096       -1, _("Message"), renderer, "text", COL_DEBUG_MESSAGE, NULL);
2097
2098   priv->store_filter = NULL;
2099
2100   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view), priv->store_filter);
2101
2102   /* Scrolled window */
2103   priv->scrolled_win = g_object_ref (gtk_scrolled_window_new (NULL, NULL));
2104   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_win),
2105       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2106
2107   gtk_widget_show (priv->view);
2108   gtk_container_add (GTK_CONTAINER (priv->scrolled_win), priv->view);
2109
2110   gtk_widget_show (priv->scrolled_win);
2111
2112   /* Not supported label */
2113   priv->not_supported_label = g_object_ref (gtk_label_new (
2114           _("The selected connection manager does not support the remote "
2115               "debugging extension.")));
2116   gtk_widget_show (priv->not_supported_label);
2117   gtk_box_pack_start (GTK_BOX (vbox), priv->not_supported_label,
2118       TRUE, TRUE, 0);
2119
2120   priv->view_visible = FALSE;
2121
2122   priv->all_active_buffer = NULL;
2123
2124   debug_window_set_toolbar_sensitivity (EMPATHY_DEBUG_WINDOW (object), FALSE);
2125   debug_window_fill_service_chooser (EMPATHY_DEBUG_WINDOW (object));
2126   gtk_widget_show (GTK_WIDGET (object));
2127 }
2128
2129 static void
2130 debug_window_constructed (GObject *object)
2131 {
2132   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
2133
2134   priv->am = tp_account_manager_dup ();
2135   tp_proxy_prepare_async (priv->am, NULL, am_prepared_cb, object);
2136 }
2137
2138 static void
2139 empathy_debug_window_init (EmpathyDebugWindow *empathy_debug_window)
2140 {
2141   EmpathyDebugWindowPriv *priv =
2142       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_window,
2143       EMPATHY_TYPE_DEBUG_WINDOW, EmpathyDebugWindowPriv);
2144
2145   empathy_debug_window->priv = priv;
2146
2147   priv->dispose_run = FALSE;
2148 }
2149
2150 static void
2151 debug_window_set_property (GObject *object,
2152     guint prop_id,
2153     const GValue *value,
2154     GParamSpec *pspec)
2155 {
2156   switch (prop_id)
2157     {
2158       default:
2159         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2160         break;
2161     }
2162 }
2163
2164 static void
2165 debug_window_get_property (GObject *object,
2166     guint prop_id,
2167     GValue *value,
2168     GParamSpec *pspec)
2169 {
2170   switch (prop_id)
2171     {
2172       default:
2173         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2174         break;
2175     }
2176 }
2177
2178 static void
2179 debug_window_finalize (GObject *object)
2180 {
2181   EmpathyDebugWindowPriv *priv = GET_PRIV (object);
2182
2183   g_free (priv->select_name);
2184
2185   (G_OBJECT_CLASS (empathy_debug_window_parent_class)->finalize) (object);
2186 }
2187
2188 static void
2189 debug_window_dispose (GObject *object)
2190 {
2191   EmpathyDebugWindow *selector = EMPATHY_DEBUG_WINDOW (object);
2192   EmpathyDebugWindowPriv *priv = GET_PRIV (selector);
2193
2194   if (priv->dispose_run)
2195     return;
2196
2197   priv->dispose_run = TRUE;
2198
2199   if (priv->name_owner_changed_signal != NULL)
2200     tp_proxy_signal_connection_disconnect (priv->name_owner_changed_signal);
2201
2202   if (priv->service_store != NULL)
2203     g_object_unref (priv->service_store);
2204
2205   if (priv->dbus != NULL)
2206     g_object_unref (priv->dbus);
2207
2208   if (priv->am != NULL)
2209     {
2210       g_object_unref (priv->am);
2211       priv->am = NULL;
2212     }
2213
2214   tp_clear_object (&priv->all_active_buffer);
2215
2216   (G_OBJECT_CLASS (empathy_debug_window_parent_class)->dispose) (object);
2217 }
2218
2219 static void
2220 empathy_debug_window_class_init (EmpathyDebugWindowClass *klass)
2221 {
2222   GObjectClass *object_class = G_OBJECT_CLASS (klass);
2223   object_class->constructed = debug_window_constructed;
2224   object_class->dispose = debug_window_dispose;
2225   object_class->finalize = debug_window_finalize;
2226   object_class->set_property = debug_window_set_property;
2227   object_class->get_property = debug_window_get_property;
2228   g_type_class_add_private (klass, sizeof (EmpathyDebugWindowPriv));
2229 }
2230
2231 /* public methods */
2232
2233 GtkWidget *
2234 empathy_debug_window_new (GtkWindow *parent)
2235 {
2236   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
2237
2238   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_WINDOW,
2239       "transient-for", parent, NULL));
2240 }
2241
2242 void
2243 empathy_debug_window_show (EmpathyDebugWindow *self,
2244     const gchar *name)
2245 {
2246   EmpathyDebugWindowPriv *priv = GET_PRIV (self);
2247
2248   if (priv->service_store != NULL)
2249     {
2250       empathy_debug_window_select_name (self, name);
2251     }
2252   else
2253     {
2254       g_free (priv->select_name);
2255       priv->select_name = g_strdup (name);
2256     }
2257 }