]> git.0d.be Git - empathy.git/blob - src/empathy-debug-dialog.c
Remove tabs, sigh.
[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   int 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 (GTK_COMBO_BOX (priv->cm_chooser), debug_dialog);
336 }
337
338 static void
339 debug_dialog_fill_cm_chooser (EmpathyDebugDialog *debug_dialog)
340 {
341   TpDBusDaemon *dbus;
342   GError *error = NULL;
343
344   dbus = tp_dbus_daemon_dup (&error);
345
346   if (error != NULL)
347     {
348       DEBUG ("Failed to dup dbus daemon: %s", error->message);
349       g_error_free (error);
350       return;
351     }
352
353   tp_list_connection_names (dbus, debug_dialog_list_connection_names_cb,
354       debug_dialog, NULL, NULL);
355
356   g_object_unref (dbus);
357 }
358
359 static void
360 debug_dialog_pause_toggled_cb (GtkToggleToolButton *pause,
361                                EmpathyDebugDialog *debug_dialog)
362 {
363   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
364
365   priv->paused = gtk_toggle_tool_button_get_active (pause);
366
367   debug_dialog_set_enabled (debug_dialog, !priv->paused);
368 }
369
370 static gboolean
371 debug_dialog_visible_func (GtkTreeModel *model,
372                            GtkTreeIter *iter,
373                            gpointer user_data)
374 {
375   EmpathyDebugDialog *debug_dialog = (EmpathyDebugDialog *) user_data;
376   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
377   guint filter_value, level;
378   GtkTreeModel *filter_model;
379   GtkTreeIter filter_iter;
380
381   filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->filter));
382   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->filter),
383       &filter_iter);
384
385   gtk_tree_model_get (model, iter, COL_DEBUG_LEVEL_VALUE, &level, -1);
386   gtk_tree_model_get (filter_model, &filter_iter,
387       COL_LEVEL_VALUE, &filter_value, -1);
388
389   if (level <= filter_value)
390     return TRUE;
391
392   return FALSE;
393 }
394
395 static void
396 debug_dialog_filter_changed_cb (GtkComboBox *filter,
397                                 EmpathyDebugDialog *debug_dialog)
398 {
399   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
400
401   gtk_tree_model_filter_refilter (
402       GTK_TREE_MODEL_FILTER (priv->store_filter));
403 }
404
405 static void
406 debug_dialog_clear_clicked_cb (GtkToolButton *clear_button,
407                                EmpathyDebugDialog *debug_dialog)
408 {
409   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
410
411   gtk_list_store_clear (priv->store);
412 }
413
414 static void
415 debug_dialog_menu_copy_activate_cb (GtkMenuItem *menu_item,
416                                     EmpathyDebugDialog *debug_dialog)
417 {
418   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
419   GtkTreePath *path;
420   GtkTreeViewColumn *focus_column;
421   GtkTreeIter iter;
422   gchar *message;
423   GtkClipboard *clipboard;
424
425   gtk_tree_view_get_cursor (GTK_TREE_VIEW (priv->view),
426       &path, &focus_column);
427
428   if (path == NULL)
429     {
430       DEBUG ("No row is in focus");
431       return;
432     }
433
434   gtk_tree_model_get_iter (priv->store_filter, &iter, path);
435
436   gtk_tree_model_get (priv->store_filter, &iter,
437       COL_DEBUG_MESSAGE, &message,
438       -1);
439
440   if (EMP_STR_EMPTY (message))
441     {
442       DEBUG ("Log message is empty");
443       return;
444     }
445
446   clipboard = gtk_clipboard_get_for_display (
447       gtk_widget_get_display (GTK_WIDGET (menu_item)),
448       GDK_SELECTION_CLIPBOARD);
449
450   gtk_clipboard_set_text (clipboard, message, -1);
451
452   g_free (message);
453 }
454
455 typedef struct
456 {
457   EmpathyDebugDialog *debug_dialog;
458   guint button;
459   guint32 time;
460 } MenuPopupData;
461
462 static gboolean
463 debug_dialog_show_menu (gpointer user_data)
464 {
465   MenuPopupData *data = (MenuPopupData *) user_data;
466   GtkWidget *menu, *item;
467   GtkMenuShell *shell;
468
469   menu = gtk_menu_new ();
470   shell = GTK_MENU_SHELL (menu);
471
472   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
473
474   g_signal_connect (item, "activate",
475       G_CALLBACK (debug_dialog_menu_copy_activate_cb), data->debug_dialog);
476
477   gtk_menu_shell_append (shell, item);
478   gtk_widget_show (item);
479
480   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
481      data->button, data->time);
482
483   g_slice_free (MenuPopupData, user_data);
484
485   return FALSE;
486 }
487
488 static gboolean
489 debug_dialog_button_press_event_cb (GtkTreeView *view,
490                                     GdkEventButton *event,
491                                     gpointer user_data)
492 {
493   if (event->button == 3)
494     {
495       MenuPopupData *data;
496       data = g_slice_new0 (MenuPopupData);
497       data->debug_dialog = user_data;
498       data->button = event->button;
499       data->time = event->time;
500       g_idle_add (debug_dialog_show_menu, data);
501     }
502
503   return FALSE;
504 }
505
506 static gboolean
507 debug_dialog_store_filter_foreach (GtkTreeModel *model,
508                                    GtkTreePath *path,
509                                    GtkTreeIter *iter,
510                                    gpointer user_data)
511 {
512   FILE *file = (FILE *) user_data;
513   gchar *domain, *category, *message, *level_str, *level_upper;
514   gdouble timestamp;
515
516   gtk_tree_model_get (model, iter,
517       COL_DEBUG_TIMESTAMP, &timestamp,
518       COL_DEBUG_DOMAIN, &domain,
519       COL_DEBUG_CATEGORY, &category,
520       COL_DEBUG_LEVEL_STRING, &level_str,
521       COL_DEBUG_MESSAGE, &message,
522       -1);
523
524   level_upper = g_ascii_strup (level_str, -1);
525
526   g_fprintf (file, "%s%s%s-%s: %e: %s\n",
527       domain, EMP_STR_EMPTY (category) ? "" : "/",
528       category, level_upper, timestamp, message);
529
530   g_free (level_upper);
531   g_free (level_str);
532   g_free (domain);
533   g_free (category);
534   g_free (message);
535
536   return FALSE;
537 }
538
539 static void
540 debug_dialog_save_file_chooser_response_cb (GtkDialog *dialog,
541                                             gint response_id,
542                                             EmpathyDebugDialog *debug_dialog)
543 {
544   EmpathyDebugDialogPriv *priv = GET_PRIV (debug_dialog);
545   gchar *filename = NULL;
546   FILE *file;
547
548   if (response_id != GTK_RESPONSE_ACCEPT)
549     goto OUT;
550
551   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
552
553   DEBUG ("Saving log as %s", filename);
554
555   file = g_fopen (filename, "w");
556   if (file == NULL)
557     {
558       DEBUG ("Failed to open file: %s", g_strerror (errno));
559       goto OUT;
560     }
561
562   gtk_tree_model_foreach (priv->store_filter,
563       debug_dialog_store_filter_foreach, file);
564
565   fclose (file);
566
567 OUT:
568   if (filename != NULL)
569     g_free (filename);
570
571   gtk_widget_destroy (GTK_WIDGET (dialog));
572 }
573
574 static void
575 debug_dialog_save_clicked_cb (GtkToolButton *tool_button,
576                               EmpathyDebugDialog *debug_dialog)
577 {
578   GtkWidget *file_chooser;
579
580   file_chooser = gtk_file_chooser_dialog_new (_("Save"),
581       GTK_WINDOW (debug_dialog), GTK_FILE_CHOOSER_ACTION_SAVE,
582       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
583       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
584       NULL);
585
586   gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
587   gtk_file_chooser_set_do_overwrite_confirmation (
588       GTK_FILE_CHOOSER (file_chooser), TRUE);
589
590   g_signal_connect (file_chooser, "response",
591       G_CALLBACK (debug_dialog_save_file_chooser_response_cb),
592       debug_dialog);
593
594   gtk_widget_show (file_chooser);
595 }
596
597 static GObject *
598 debug_dialog_constructor (GType type,
599                           guint n_construct_params,
600                           GObjectConstructParam *construct_params)
601 {
602   GObject *object;
603   EmpathyDebugDialogPriv *priv;
604   GtkWidget *vbox;
605   GtkWidget *toolbar;
606   GtkWidget *image;
607   GtkWidget *label;
608   GtkToolItem *item;
609   GtkCellRenderer *renderer;
610   GtkWidget *scrolled_win;
611   GtkListStore *level_store;
612   GtkTreeIter iter;
613
614   object = G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->constructor
615     (type, n_construct_params, construct_params);
616   priv = GET_PRIV (object);
617
618   gtk_window_set_title (GTK_WINDOW (object), _("Debug Window"));
619   gtk_window_set_default_size (GTK_WINDOW (object), 800, 400);
620   gtk_window_set_transient_for (GTK_WINDOW (object), priv->parent);
621
622   vbox = GTK_DIALOG (object)->vbox;
623
624   toolbar = gtk_toolbar_new ();
625   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
626   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
627   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_SMALL_TOOLBAR);
628   gtk_widget_show (toolbar);
629
630   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
631
632   /* CM */
633   priv->cm_chooser = gtk_combo_box_new_text ();
634   priv->cms = gtk_list_store_new (NUM_COLS_CM, G_TYPE_STRING, G_TYPE_STRING);
635   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->cm_chooser),
636       GTK_TREE_MODEL (priv->cms));
637   gtk_widget_show (priv->cm_chooser);
638
639   item = gtk_tool_item_new ();
640   gtk_widget_show (GTK_WIDGET (item));
641   gtk_container_add (GTK_CONTAINER (item), priv->cm_chooser);
642   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
643   debug_dialog_fill_cm_chooser (EMPATHY_DEBUG_DIALOG (object));
644   g_signal_connect (priv->cm_chooser, "changed",
645       G_CALLBACK (debug_dialog_cm_chooser_changed_cb), object);
646   gtk_widget_show (GTK_WIDGET (priv->cm_chooser));
647
648   item = gtk_separator_tool_item_new ();
649   gtk_widget_show (GTK_WIDGET (item));
650   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
651
652   /* Save */
653   item = gtk_tool_button_new_from_stock (GTK_STOCK_SAVE);
654   g_signal_connect (item, "clicked",
655       G_CALLBACK (debug_dialog_save_clicked_cb), object);
656   gtk_widget_show (GTK_WIDGET (item));
657   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
658   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
659
660   /* Clear */
661   item = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
662   g_signal_connect (item, "clicked",
663       G_CALLBACK (debug_dialog_clear_clicked_cb), object);
664   gtk_widget_show (GTK_WIDGET (item));
665   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
666   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
667
668   item = gtk_separator_tool_item_new ();
669   gtk_widget_show (GTK_WIDGET (item));
670   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
671
672   /* Pause */
673   priv->paused = FALSE;
674   image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PAUSE, GTK_ICON_SIZE_MENU);
675   gtk_widget_show (image);
676   item = gtk_toggle_tool_button_new ();
677   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (item), priv->paused);
678   g_signal_connect (item, "toggled", G_CALLBACK (debug_dialog_pause_toggled_cb),
679       object);
680   gtk_widget_show (GTK_WIDGET (item));
681   gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
682   gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), _("Pause"));
683   gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
684   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
685
686   item = gtk_separator_tool_item_new ();
687   gtk_widget_show (GTK_WIDGET (item));
688   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
689
690   /* Level */
691   item = gtk_tool_item_new ();
692   gtk_widget_show (GTK_WIDGET (item));
693   label = gtk_label_new (_("Level "));
694   gtk_widget_show (label);
695   gtk_container_add (GTK_CONTAINER (item), label);
696   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
697
698   priv->filter = gtk_combo_box_new_text ();
699   gtk_widget_show (priv->filter);
700
701   item = gtk_tool_item_new ();
702   gtk_widget_show (GTK_WIDGET (item));
703   gtk_container_add (GTK_CONTAINER (item), priv->filter);
704   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
705
706   level_store = gtk_list_store_new (NUM_COLS_LEVEL,
707       G_TYPE_STRING, G_TYPE_UINT);
708   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->filter),
709       GTK_TREE_MODEL (level_store));
710
711   gtk_list_store_append (level_store, &iter);
712   gtk_list_store_set (level_store, &iter,
713       COL_LEVEL_NAME, _("Debug"),
714       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_DEBUG,
715       -1);
716
717   gtk_list_store_append (level_store, &iter);
718   gtk_list_store_set (level_store, &iter,
719       COL_LEVEL_NAME, _("Info"),
720       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_INFO,
721       -1);
722
723   gtk_list_store_append (level_store, &iter);
724   gtk_list_store_set (level_store, &iter,
725       COL_LEVEL_NAME, _("Message"),
726       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_MESSAGE,
727       -1);
728
729   gtk_list_store_append (level_store, &iter);
730   gtk_list_store_set (level_store, &iter,
731       COL_LEVEL_NAME, _("Warning"),
732       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_WARNING,
733       -1);
734
735   gtk_list_store_append (level_store, &iter);
736   gtk_list_store_set (level_store, &iter,
737       COL_LEVEL_NAME, _("Critical"),
738       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_CRITICAL,
739       -1);
740
741   gtk_list_store_append (level_store, &iter);
742   gtk_list_store_set (level_store, &iter,
743       COL_LEVEL_NAME, _("Error"),
744       COL_LEVEL_VALUE, EMP_DEBUG_LEVEL_ERROR,
745       -1);
746
747   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->filter), 0);
748   g_signal_connect (priv->filter, "changed",
749       G_CALLBACK (debug_dialog_filter_changed_cb), object);
750
751   /* Debug treeview */
752   priv->view = gtk_tree_view_new ();
753   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (priv->view), TRUE);
754
755   g_signal_connect (priv->view, "button-press-event",
756       G_CALLBACK (debug_dialog_button_press_event_cb), object);
757
758   renderer = gtk_cell_renderer_text_new ();
759   g_object_set (renderer, "yalign", 0, NULL);
760
761   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
762       -1, _("Time"), renderer, "text", COL_DEBUG_TIMESTAMP, NULL);
763   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
764       -1, _("Domain"), renderer, "text", COL_DEBUG_DOMAIN, NULL);
765   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
766       -1, _("Category"), renderer, "text", COL_DEBUG_CATEGORY, NULL);
767   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
768       -1, _("Level"), renderer, "text", COL_DEBUG_LEVEL_STRING, NULL);
769
770   renderer = gtk_cell_renderer_text_new ();
771   g_object_set (renderer, "family", "Monospace", NULL);
772   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->view),
773       -1, _("Message"), renderer, "text", COL_DEBUG_MESSAGE, NULL);
774
775   priv->store = gtk_list_store_new (NUM_DEBUG_COLS, G_TYPE_DOUBLE,
776       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
777       G_TYPE_UINT);
778
779   priv->store_filter = gtk_tree_model_filter_new (
780       GTK_TREE_MODEL (priv->store), NULL);
781
782   gtk_tree_model_filter_set_visible_func (
783       GTK_TREE_MODEL_FILTER (priv->store_filter),
784       debug_dialog_visible_func, object, NULL);
785
786   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->view), priv->store_filter);
787
788   /* Scrolled window */
789   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
790   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
791       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
792
793   gtk_widget_show (priv->view);
794   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->view);
795
796   gtk_widget_show (scrolled_win);
797   gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
798
799   gtk_widget_show (GTK_WIDGET (object));
800
801   return object;
802 }
803
804 static void
805 empathy_debug_dialog_init (EmpathyDebugDialog *empathy_debug_dialog)
806 {
807   EmpathyDebugDialogPriv *priv =
808       G_TYPE_INSTANCE_GET_PRIVATE (empathy_debug_dialog,
809       EMPATHY_TYPE_DEBUG_DIALOG, EmpathyDebugDialogPriv);
810
811   empathy_debug_dialog->priv = priv;
812
813   priv->dispose_run = FALSE;
814 }
815
816 static void
817 debug_dialog_set_property (GObject *object,
818                            guint prop_id,
819                            const GValue *value,
820                            GParamSpec *pspec)
821 {
822   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
823
824   switch (prop_id)
825     {
826       case PROP_PARENT:
827         priv->parent = GTK_WINDOW (g_value_dup_object (value));
828         break;
829       default:
830         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
831         break;
832     }
833 }
834
835 static void
836 debug_dialog_get_property (GObject *object,
837                            guint prop_id,
838                            GValue *value,
839                            GParamSpec *pspec)
840 {
841   EmpathyDebugDialogPriv *priv = GET_PRIV (object);
842
843   switch (prop_id)
844     {
845       case PROP_PARENT:
846         g_value_set_object (value, priv->parent);
847         break;
848       default:
849         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
850         break;
851     }
852 }
853
854 static void
855 debug_dialog_dispose (GObject *object)
856 {
857   EmpathyDebugDialog *selector = EMPATHY_DEBUG_DIALOG (object);
858   EmpathyDebugDialogPriv *priv = GET_PRIV (selector);
859
860   if (priv->dispose_run)
861     return;
862
863   priv->dispose_run = TRUE;
864
865   if (priv->parent)
866     g_object_unref (priv->parent);
867
868   if (priv->store)
869     g_object_unref (priv->store);
870
871   if (priv->proxy)
872     {
873       debug_dialog_set_enabled (EMPATHY_DEBUG_DIALOG (object), FALSE);
874       g_object_unref (priv->proxy);
875     }
876
877   if (priv->signal_connection)
878     tp_proxy_signal_connection_disconnect (priv->signal_connection);
879
880   if (priv->cms)
881     g_object_unref (priv->cms);
882
883   (G_OBJECT_CLASS (empathy_debug_dialog_parent_class)->dispose) (object);
884 }
885
886 static void
887 empathy_debug_dialog_class_init (EmpathyDebugDialogClass *klass)
888 {
889   GObjectClass *object_class = G_OBJECT_CLASS (klass);
890   object_class->constructor = debug_dialog_constructor;
891   object_class->dispose = debug_dialog_dispose;
892   object_class->set_property = debug_dialog_set_property;
893   object_class->get_property = debug_dialog_get_property;
894   g_type_class_add_private (klass, sizeof (EmpathyDebugDialogPriv));
895
896   g_object_class_install_property (object_class, PROP_PARENT,
897       g_param_spec_object ("parent", "parent", "parent",
898       GTK_TYPE_WINDOW, G_PARAM_CONSTRUCT_ONLY |
899       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
900 }
901
902 /* public methods */
903
904 GtkWidget *
905 empathy_debug_dialog_new (GtkWindow *parent)
906 {
907   g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
908
909   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_DEBUG_DIALOG,
910       "parent", parent, NULL));
911 }