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