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