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