]> git.0d.be Git - empathy.git/blob - src/empathy-debug-dialog.c
Use guint instead of int for for loop counter.
[empathy.git] / src / empathy-debug-dialog.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 */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <glib/gstdio.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
35 #include <telepathy-glib/dbus.h>
36 #include <telepathy-glib/util.h>
37 #include <telepathy-glib/proxy-subclass.h>
38
39 #include "extensions/extensions.h"
40
41 #include "empathy-debug-dialog.h"
42
43 G_DEFINE_TYPE (EmpathyDebugDialog, empathy_debug_dialog,
44     GTK_TYPE_DIALOG)
45
46 enum
47 {
48   PROP_0,
49   PROP_PARENT
50 };
51
52 enum
53 {
54   COL_DEBUG_TIMESTAMP = 0,
55   COL_DEBUG_DOMAIN,
56   COL_DEBUG_CATEGORY,
57   COL_DEBUG_LEVEL_STRING,
58   COL_DEBUG_MESSAGE,
59   COL_DEBUG_LEVEL_VALUE,
60   NUM_DEBUG_COLS
61 };
62
63 enum
64 {
65   COL_CM_NAME = 0,
66   COL_CM_BUS,
67   NUM_COLS_CM
68 };
69
70 enum
71 {
72   COL_LEVEL_NAME,
73   COL_LEVEL_VALUE,
74   NUM_COLS_LEVEL
75 };
76
77 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDebugDialog)
78 typedef struct
79 {
80   GtkWidget *filter;
81   GtkWindow *parent;
82   GtkWidget *view;
83   GtkWidget *cm_chooser;
84   GtkListStore *store;
85   GtkTreeModel *store_filter;
86   TpProxy *proxy;
87   TpProxySignalConnection *signal_connection;
88   gboolean paused;
89   GtkListStore *cms;
90   gboolean dispose_run;
91 } EmpathyDebugDialogPriv;
92
93 static const gchar *
94 log_level_to_string (guint level)
95 {
96   switch (level)
97     {
98     case EMP_DEBUG_LEVEL_ERROR:
99       return _("Error");
100       break;
101     case EMP_DEBUG_LEVEL_CRITICAL:
102       return _("Critical");
103       break;
104     case EMP_DEBUG_LEVEL_WARNING:
105       return _("Warning");
106       break;
107     case EMP_DEBUG_LEVEL_MESSAGE:
108       return _("Message");
109       break;
110     case EMP_DEBUG_LEVEL_INFO:
111       return _("Info");
112       break;
113     case EMP_DEBUG_LEVEL_DEBUG:
114       return _("Debug");
115       break;
116     default:
117       g_assert_not_reached ();
118       break;
119     }
120 }
121
122 static void
123 debug_dialog_add_message (EmpathyDebugDialog *debug_dialog,
124     gdouble timestamp,
125     const gchar *domain_category,
126     guint level,
127     const gchar *message)
128 {
129   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
130   gchar *domain, *category;
131   GtkTreeIter iter;
132   gchar *string;
133
134   if (g_strrstr (domain_category, "/"))
135     {
136       gchar **parts = g_strsplit (domain_category, "/", 2);
137       domain = g_strdup (parts[0]);
138       category = g_strdup (parts[1]);
139       g_strfreev (parts);
140     }
141   else
142     {
143       domain = g_strdup (domain_category);
144       category = g_strdup ("");
145     }
146
147   if (g_str_has_suffix (message, "\n"))
148     string = g_strchomp (g_strdup (message));
149   else
150     string = g_strdup (message);
151
152
153   gtk_list_store_append (priv->store, &iter);
154   gtk_list_store_set (priv->store, &iter,
155       COL_DEBUG_TIMESTAMP, timestamp,
156       COL_DEBUG_DOMAIN, domain,
157       COL_DEBUG_CATEGORY, category,
158       COL_DEBUG_LEVEL_STRING, log_level_to_string (level),
159       COL_DEBUG_MESSAGE, string,
160       COL_DEBUG_LEVEL_VALUE, level,
161       -1);
162
163   g_free (string);
164   g_free (domain);
165   g_free (category);
166 }
167
168 static void
169 debug_dialog_new_debug_message_cb (TpProxy *proxy,
170     gdouble timestamp,
171     const gchar *domain,
172     guint level,
173     const gchar *message,
174     gpointer user_data,
175     GObject *weak_object)
176 {
177   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
178
179   debug_dialog_add_message (debug_dialog, timestamp, domain, level,
180       message);
181 }
182
183 static void
184 debug_dialog_set_enabled (EmpathyDebugDialog *debug_dialog,
185     gboolean enabled)
186 {
187   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
188   GValue *val;
189
190   val = tp_g_value_slice_new_boolean (enabled);
191
192   tp_cli_dbus_properties_call_set (priv->proxy, -1, EMP_IFACE_DEBUG,
193       "Enabled", val, NULL, NULL, NULL, NULL);
194
195   tp_g_value_slice_free (val);
196 }
197
198 static void
199 debug_dialog_get_messages_cb (TpProxy *proxy,
200     const GPtrArray *messages,
201     const GError *error,
202     gpointer user_data,
203     GObject *weak_object)
204 {
205   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
206   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
207   gint i;
208
209   if (error != NULL)
210     {
211       DEBUG ("GetMessages failed: %s", error->message);
212       return;
213     }
214
215   for (i = 0; i < messages->len; i++)
216     {
217       GValueArray *values = g_ptr_array_index (messages, i);
218
219       debug_dialog_add_message (debug_dialog,
220           g_value_get_double (g_value_array_get_nth (values, 0)),
221           g_value_get_string (g_value_array_get_nth (values, 1)),
222           g_value_get_uint (g_value_array_get_nth (values, 2)),
223           g_value_get_string (g_value_array_get_nth (values, 3)));
224     }
225
226   /* Connect to NewDebugMessage */
227   priv->signal_connection = emp_cli_debug_connect_to_new_debug_message (
228       proxy, debug_dialog_new_debug_message_cb, debug_dialog,
229       NULL, NULL, NULL);
230
231   /* Set Enabled as appropriate */
232   debug_dialog_set_enabled (debug_dialog, !priv->paused);
233 }
234
235 static void
236 debug_dialog_cm_chooser_changed_cb (GtkComboBox *cm_chooser,
237     EmpathyDebugDialog *debug_dialog)
238 {
239   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
240   MissionControl *mc;
241   TpDBusDaemon *dbus;
242   GError *error = NULL;
243   gchar *bus_name;
244   TpConnection *connection;
245   GtkTreeIter iter;
246
247   if (!gtk_combo_box_get_active_iter (cm_chooser, &iter))
248     {
249       DEBUG ("No CM is selected");
250       return;
251     }
252
253   mc = empathy_mission_control_dup_singleton ();
254   dbus = tp_dbus_daemon_dup (&error);
255
256   if (error != NULL)
257     {
258       DEBUG ("Failed at duping the dbus daemon: %s", error->message);
259       g_object_unref (mc);
260     }
261
262   gtk_tree_model_get (GTK_TREE_MODEL (priv->cms), &iter,
263       COL_CM_BUS, &bus_name, -1);
264   connection = tp_connection_new (dbus, bus_name, DEBUG_OBJECT_PATH, &error);
265   g_free (bus_name);
266
267   if (error != NULL)
268     {
269       DEBUG ("Getting a new TpConnection failed: %s", error->message);
270       g_error_free (error);
271       g_object_unref (dbus);
272       g_object_unref (mc);
273       return;
274     }
275
276   gtk_list_store_clear (priv->store);
277
278   /* Disable debug signalling */
279   if (priv->proxy != NULL)
280     debug_dialog_set_enabled (debug_dialog, FALSE);
281
282   /* Disconnect from previous NewDebugMessage signal */
283   if (priv->signal_connection)
284     {
285       tp_proxy_signal_connection_disconnect (priv->signal_connection);
286       priv->signal_connection = NULL;
287     }
288
289   if (priv->proxy)
290     g_object_unref (priv->proxy);
291
292   priv->proxy = TP_PROXY (connection);
293
294   tp_proxy_add_interface_by_id (priv->proxy, emp_iface_quark_debug ());
295
296   emp_cli_debug_call_get_messages (priv->proxy, -1,
297       debug_dialog_get_messages_cb, debug_dialog, NULL, NULL);
298
299   g_object_unref (dbus);
300   g_object_unref (mc);
301 }
302
303 static void
304 debug_dialog_list_connection_names_cb (const gchar * const *names,
305     gsize n,
306     const gchar * const *cms,
307     const gchar * const *protocols,
308     const GError *error,
309     gpointer user_data,
310     GObject *weak_object)
311 {
312   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
313   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
314   guint i;
315
316   if (error != NULL)
317     {
318       DEBUG ("list_connection_names failed: %s", error->message);
319       return;
320     }
321
322   for (i = 0; cms[i] != NULL; i++)
323     {
324       GtkTreeIter iter;
325       gtk_list_store_append (priv->cms, &iter);
326       gtk_list_store_set (priv->cms, &iter,
327           COL_CM_NAME, cms[i],
328           COL_CM_BUS, names[i],
329           -1);
330     }
331
332   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cm_chooser), 0);
333
334   /* Fill treeview */
335   debug_dialog_cm_chooser_changed_cb (
336       GTK_COMBO_BOX (priv->cm_chooser), debug_dialog);
337 }
338
339 static void
340 debug_dialog_fill_cm_chooser (EmpathyDebugDialog *debug_dialog)
341 {
342   TpDBusDaemon *dbus;
343   GError *error = NULL;
344
345   dbus = tp_dbus_daemon_dup (&error);
346
347   if (error != NULL)
348     {
349       DEBUG ("Failed to dup dbus daemon: %s", error->message);
350       g_error_free (error);
351       return;
352     }
353
354   tp_list_connection_names (dbus, debug_dialog_list_connection_names_cb,
355       debug_dialog, NULL, NULL);
356
357   g_object_unref (dbus);
358 }
359
360 static void
361 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
362     EmpathyDebugDialog *debug_dialog)
363 {
364   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
365
366   priv->paused = gtk_toggle_tool_button_get_active (pause);
367
368   debug_dialog_set_enabled (debug_dialog, !priv->paused);
369 }
370
371 static gboolean
372 debug_dialog_visible_func (GtkTreeModel *model,
373     GtkTreeIter *iter,
374     gpointer user_data)
375 {
376   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
377   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
378   guint filter_value, level;
379   GtkTreeModel *filter_model;
380   GtkTreeIter filter_iter;
381
382   filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->filter));
383   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->filter),
384       &filter_iter);
385
386   gtk_tree_model_get (model, iter, COL_DEBUG_LEVEL_VALUE, &level, -1);
387   gtk_tree_model_get (filter_model, &filter_iter,
388       COL_LEVEL_VALUE, &filter_value, -1);
389
390   if (level <= filter_value)
391     return TRUE;
392
393   return FALSE;
394 }
395
396 static void
397 debug_dialog_filter_changed_cb (GtkComboBox *filter,
398     EmpathyDebugDialog *debug_dialog)
399 {
400   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
401
402   gtk_tree_model_filter_refilter (
403       GTK_TREE_MODEL_FILTER (priv->store_filter));
404 }
405
406 static void
407 debug_dialog_clear_clicked_cb (GtkToolButton *clear_button,
408     EmpathyDebugDialog *debug_dialog)
409 {
410   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
411
412   gtk_list_store_clear (priv->store);
413 }
414
415 static void
416 debug_dialog_menu_copy_activate_cb (GtkMenuItem *menu_item,
417     EmpathyDebugDialog *debug_dialog)
418 {
419   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
420   GtkTreePath *path;
421   GtkTreeViewColumn *focus_column;
422   GtkTreeIter iter;
423   gchar *message;
424   GtkClipboard *clipboard;
425
426   gtk_tree_view_get_cursor (GTK_TREE_VIEW (priv->view),
427       &path, &focus_column);
428
429   if (path == NULL)
430     {
431       DEBUG ("No row is in focus");
432       return;
433     }
434
435   gtk_tree_model_get_iter (priv->store_filter, &iter, path);
436
437   gtk_tree_model_get (priv->store_filter, &iter,
438       COL_DEBUG_MESSAGE, &message,
439       -1);
440
441   if (EMP_STR_EMPTY (message))
442     {
443       DEBUG ("Log message is empty");
444       return;
445     }
446
447   clipboard = gtk_clipboard_get_for_display (
448       gtk_widget_get_display (GTK_WIDGET (menu_item)),
449       GDK_SELECTION_CLIPBOARD);
450
451   gtk_clipboard_set_text (clipboard, message, -1);
452
453   g_free (message);
454 }
455
456 typedef struct
457 {
458   EmpathyDebugDialog *debug_dialog;
459   guint button;
460   guint32 time;
461 } MenuPopupData;
462
463 static gboolean
464 debug_dialog_show_menu (gpointer user_data)
465 {
466   MenuPopupData *data = (MenuPopupData *) user_data;
467   GtkWidget *menu, *item;
468   GtkMenuShell *shell;
469
470   menu = gtk_menu_new ();
471   shell = GTK_MENU_SHELL (menu);
472
473   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
474
475   g_signal_connect (item, "activate",
476       G_CALLBACK (debug_dialog_menu_copy_activate_cb), data->debug_dialog);
477
478   gtk_menu_shell_append (shell, item);
479   gtk_widget_show (item);
480
481   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
482      data->button, data->time);
483
484   g_slice_free (MenuPopupData, user_data);
485
486   return FALSE;
487 }
488
489 static gboolean
490 debug_dialog_button_press_event_cb (GtkTreeView *view,
491     GdkEventButton *event,
492     gpointer user_data)
493 {
494   if (event->button == 3)
495     {
496       MenuPopupData *data;
497       data = g_slice_new0 (MenuPopupData);
498       data->debug_dialog = user_data;
499       data->button = event->button;
500       data->time = event->time;
501       g_idle_add (debug_dialog_show_menu, data);
502     }
503
504   return FALSE;
505 }
506
507 static gboolean
508 debug_dialog_store_filter_foreach (GtkTreeModel *model,
509     GtkTreePath *path,
510     GtkTreeIter *iter,
511     gpointer user_data)
512 {
513   FILE *file = (FILE *) user_data;
514   gchar *domain, *category, *message, *level_str, *level_upper;
515   gdouble timestamp;
516
517   gtk_tree_model_get (model, iter,
518       COL_DEBUG_TIMESTAMP, &timestamp,
519       COL_DEBUG_DOMAIN, &domain,
520       COL_DEBUG_CATEGORY, &category,
521       COL_DEBUG_LEVEL_STRING, &level_str,
522       COL_DEBUG_MESSAGE, &message,
523       -1);
524
525   level_upper = g_ascii_strup (level_str, -1);
526
527   g_fprintf (file, "%s%s%s-%s: %e: %s\n",
528       domain, EMP_STR_EMPTY (category) ? "" : "/",
529       category, level_upper, timestamp, message);
530
531   g_free (level_upper);
532   g_free (level_str);
533   g_free (domain);
534   g_free (category);
535   g_free (message);
536
537   return FALSE;
538 }
539
540 static void
541 debug_dialog_save_file_chooser_response_cb (GtkDialog *dialog,
542     gint response_id,
543     EmpathyDebugDialog *debug_dialog)
544 {
545   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
546   gchar *filename = NULL;
547   FILE *file;
548
549   if (response_id != GTK_RESPONSE_ACCEPT)
550     goto OUT;
551
552   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
553
554   DEBUG ("Saving log as %s", filename);
555
556   file = g_fopen (filename, "w");
557   if (file == NULL)
558     {
559       DEBUG ("Failed to open file: %s", g_strerror (errno));
560       goto OUT;
561     }
562
563   gtk_tree_model_foreach (priv->store_filter,
564       debug_dialog_store_filter_foreach, file);
565
566   fclose (file);
567
568 OUT:
569   if (filename != NULL)
570     g_free (filename);
571
572   gtk_widget_destroy (GTK_WIDGET (dialog));
573 }
574
575 static void
576 debug_dialog_save_clicked_cb (GtkToolButton *tool_button,
577     EmpathyDebugDialog *debug_dialog)
578 {
579   GtkWidget *file_chooser;
580
581   file_chooser = gtk_file_chooser_dialog_new (_("Save"),
582       GTK_WINDOW (debug_dialog), GTK_FILE_CHOOSER_ACTION_SAVE,
583       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
584       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
585       NULL);
586
587   gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
588   gtk_file_chooser_set_do_overwrite_confirmation (
589       GTK_FILE_CHOOSER (file_chooser), TRUE);
590
591   g_signal_connect (file_chooser, "response",
592       G_CALLBACK (debug_dialog_save_file_chooser_response_cb),
593       debug_dialog);
594
595   gtk_widget_show (file_chooser);
596 }
597
598 static GObject *
599 debug_dialog_constructor (GType type,
600     guint n_construct_params,
601     GObjectConstructParam *construct_params)
602 {
603   GObject *object;
604   EmpathyDebugDialogPriv *priv;
605   GtkWidget *vbox;
606   GtkWidget *toolbar;
607   GtkWidget *image;
608   GtkWidget *label;
609   GtkToolItem *item;
610   GtkCellRenderer *renderer;
611   GtkWidget *scrolled_win;
612   GtkListStore *level_store;
613   GtkTreeIter iter;
614
615   object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
616     (type, n_construct_params, construct_params);
617   priv = GET_PRIV (object);
618
619   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
620   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
621   gtk_window_set_transient_for (GTK_WINDOW (object), priv->parent);
622
623   vbox = GTK_DIALOG (object)->vbox;
624
625   toolbar = gtk_toolbar_new ();
626   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
627   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
628   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar),
629       GTK_ICON_SIZE_SMALL_TOOLBAR);
630   gtk_widget_show (toolbar);
631
632   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
633
634   /* CM */
635   priv->cm_chooser = gtk_combo_box_new_text ();
636   priv->cms = gtk_list_store_new (NUM_COLS_CM, G_TYPE_STRING, G_TYPE_STRING);
637   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->cm_chooser),
638       GTK_TREE_MODEL (priv->cms));
639   gtk_widget_show (priv->cm_chooser);
640
641   item = gtk_tool_item_new ();
642   gtk_widget_show (GTK_WIDGET (item));
643   gtk_container_add (GTK_CONTAINER (item), priv->cm_chooser);
644   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
645   debug_dialog_fill_cm_chooser (EMPATHY_DEBUG_DIALOG (object));
646   g_signal_connect (priv->cm_chooser, "changed",
647       G_CALLBACK (debug_dialog_cm_chooser_changed_cb), object);
648   gtk_widget_show (GTK_WIDGET (priv->cm_chooser));
649
650   item = gtk_separator_tool_item_new ();
651   gtk_widget_show (GTK_WIDGET (item));
652   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
653
654   /* Save */
655   item = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
656   g_signal_connect (item, "clicked",
657       G_CALLBACK (debug_dialog_save_clicked_cb), object);
658   gtk_widget_show (GTK_WIDGET (item));
659   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
660   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
661
662   /* Clear */
663   item = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
664   g_signal_connect (item, "clicked",
665       G_CALLBACK (debug_dialog_clear_clicked_cb), object);
666   gtk_widget_show (GTK_WIDGET (item));
667   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
668   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
669
670   item = gtk_separator_tool_item_new ();
671   gtk_widget_show (GTK_WIDGET (item));
672   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
673
674   /* Pause */
675   priv->paused = FALSE;
676   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE,
677       GTK_ICON_SIZE_MENU);
678   gtk_widget_show (image);
679   item = gtk_toggle_tool_button_new ();
680   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (item),
681       priv->paused);
682   g_signal_connect (item, "toggled",
683       G_CALLBACK (debug_dialog_pause_toggled_cb), object);
684   gtk_widget_show (GTK_WIDGET (item));
685   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
686   gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), _("Pause"));
687   gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
688   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
689
690   item = gtk_separator_tool_item_new ();
691   gtk_widget_show (GTK_WIDGET (item));
692   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
693
694   /* Level */
695   item = gtk_tool_item_new ();
696   gtk_widget_show (GTK_WIDGET (item));
697   label = gtk_label_new (_("Level "));
698   gtk_widget_show (label);
699   gtk_container_add (GTK_CONTAINER (item), label);
700   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
701
702   priv->filter = gtk_combo_box_new_text ();
703   gtk_widget_show (priv->filter);
704
705   item = gtk_tool_item_new ();
706   gtk_widget_show (GTK_WIDGET (item));
707   gtk_container_add (GTK_CONTAINER (item), priv->filter);
708   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
709
710   level_store = gtk_list_store_new (NUM_COLS_LEVEL,
711       G_TYPE_STRING, G_TYPE_UINT);
712   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->filter),
713       GTK_TREE_MODEL (level_store));
714
715   gtk_list_store_append (level_store, &iter);
716   gtk_list_store_set (level_store, &iter,
717       COL_LEVEL_NAME, _("Debug"),
718       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_DEBUG,
719       -1);
720
721   gtk_list_store_append (level_store, &iter);
722   gtk_list_store_set (level_store, &iter,
723       COL_LEVEL_NAME, _("Info"),
724       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_INFO,
725       -1);
726
727   gtk_list_store_append (level_store, &iter);
728   gtk_list_store_set (level_store, &iter,
729       COL_LEVEL_NAME, _("Message"),
730       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_MESSAGE,
731       -1);
732
733   gtk_list_store_append (level_store, &iter);
734   gtk_list_store_set (level_store, &iter,
735       COL_LEVEL_NAME, _("Warning"),
736       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_WARNING,
737       -1);
738
739   gtk_list_store_append (level_store, &iter);
740   gtk_list_store_set (level_store, &iter,
741       COL_LEVEL_NAME, _("Critical"),
742       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_CRITICAL,
743       -1);
744
745   gtk_list_store_append (level_store, &iter);
746   gtk_list_store_set (level_store, &iter,
747       COL_LEVEL_NAME, _("Error"),
748       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_ERROR,
749       -1);
750
751   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->filter), 0);
752   g_signal_connect (priv->filter, "changed",
753       G_CALLBACK (debug_dialog_filter_changed_cb), object);
754
755   /* Debug treeview */
756   priv->view = gtk_tree_view_new ();
757   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
758
759   g_signal_connect (priv->view, "button-press-event",
760       G_CALLBACK (debug_dialog_button_press_event_cb), object);
761
762   renderer = gtk_cell_renderer_text_new ();
763   g_object_set (renderer, "yalign", 0, NULL);
764
765   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
766       -1, _("Time"), renderer, "text", COL_DEBUG_TIMESTAMP, NULL);
767   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
768       -1, _("Domain"), renderer, "text", COL_DEBUG_DOMAIN, NULL);
769   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
770       -1, _("Category"), renderer, "text", COL_DEBUG_CATEGORY, NULL);
771   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
772       -1, _("Level"), renderer, "text", COL_DEBUG_LEVEL_STRING, NULL);
773
774   renderer = gtk_cell_renderer_text_new ();
775   g_object_set (renderer, "family", "Monospace", NULL);
776   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
777       -1, _("Message"), renderer, "text", COL_DEBUG_MESSAGE, NULL);
778
779   priv->store = gtk_list_store_new (NUM_DEBUG_COLS, G_TYPE_DOUBLE,
780       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
781       G_TYPE_UINT);
782
783   priv->store_filter = gtk_tree_model_filter_new (
784       GTK_TREE_MODEL (priv->store), NULL);
785
786   gtk_tree_model_filter_set_visible_func (
787       GTK_TREE_MODEL_FILTER (priv->store_filter),
788       debug_dialog_visible_func, object, NULL);
789
790   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view), priv->store_filter);
791
792   /* Scrolled window */
793   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
794   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
795       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
796
797   gtk_widget_show (priv->view);
798   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->view);
799
800   gtk_widget_show (scrolled_win);
801   gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
802
803   gtk_widget_show (GTK_WIDGET (object));
804
805   return object;
806 }
807
808 static void
809 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
810 {
811   EmpathyDebugDialogPriv *priv =
812       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
813       EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
814
815   empathy_debug_dialog->priv = priv;
816
817   priv->dispose_run = FALSE;
818 }
819
820 static void
821 debug_dialog_set_property (GObject *object,
822     guint prop_id,
823     const GValue *value,
824     GParamSpec *pspec)
825 {
826   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
827
828   switch (prop_id)
829     {
830       case PROP_PARENT:
831         priv->parent = GTK_WINDOW (g_value_dup_object (value));
832         break;
833       default:
834         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
835         break;
836     }
837 }
838
839 static void
840 debug_dialog_get_property (GObject *object,
841     guint prop_id,
842     GValue *value,
843     GParamSpec *pspec)
844 {
845   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
846
847   switch (prop_id)
848     {
849       case PROP_PARENT:
850         g_value_set_object (value, priv->parent);
851         break;
852       default:
853         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
854         break;
855     }
856 }
857
858 static void
859 debug_dialog_dispose (GObject *object)
860 {
861   EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
862   EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
863
864   if (priv->dispose_run)
865     return;
866
867   priv->dispose_run = TRUE;
868
869   if (priv->parent)
870     g_object_unref (priv->parent);
871
872   if (priv->store)
873     g_object_unref (priv->store);
874
875   if (priv->proxy)
876     {
877       debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
878       g_object_unref (priv->proxy);
879     }
880
881   if (priv->signal_connection)
882     tp_proxy_signal_connection_disconnect (priv->signal_connection);
883
884   if (priv->cms)
885     g_object_unref (priv->cms);
886
887   (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
888 }
889
890 static void
891 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
892 {
893   GObjectClass *object_class = G_OBJECT_CLASS (klass);
894   object_class->constructor = debug_dialog_constructor;
895   object_class->dispose = debug_dialog_dispose;
896   object_class->set_property = debug_dialog_set_property;
897   object_class->get_property = debug_dialog_get_property;
898   g_type_class_add_private (klass, sizeof (EmpathyDebugDialogPriv));
899
900   g_object_class_install_property (object_class, PROP_PARENT,
901       g_param_spec_object ("parent", "parent", "parent",
902       GTK_TYPE_WINDOW, G_PARAM_CONSTRUCT_ONLY |
903       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
904 }
905
906 /* public methods */
907
908 GtkWidget *
909 empathy_debug_dialog_new (GtkWindow *parent)
910 {
911   g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
912
913   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
914       "parent", parent, NULL));
915 }