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