]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
switched notebook pages
[empathy.git] / src / empathy-main-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2007-2010 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
23  */
24
25 #include <config.h>
26
27 #include <sys/stat.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <glib/gi18n.h>
31
32 #include <telepathy-glib/account-manager.h>
33 #include <telepathy-glib/util.h>
34 #include <folks/folks.h>
35
36 #include <libempathy/empathy-contact.h>
37 #include <libempathy/empathy-idle.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-dispatcher.h>
40 #include <libempathy/empathy-chatroom-manager.h>
41 #include <libempathy/empathy-chatroom.h>
42 #include <libempathy/empathy-contact-list.h>
43 #include <libempathy/empathy-contact-manager.h>
44 #include <libempathy/empathy-gsettings.h>
45 #include <libempathy/empathy-individual-manager.h>
46 #include <libempathy/empathy-gsettings.h>
47 #include <libempathy/empathy-status-presets.h>
48 #include <libempathy/empathy-tp-contact-factory.h>
49
50 #include <libempathy-gtk/empathy-contact-dialogs.h>
51 #include <libempathy-gtk/empathy-contact-list-store.h>
52 #include <libempathy-gtk/empathy-contact-list-view.h>
53 #include <libempathy-gtk/empathy-live-search.h>
54 #include <libempathy-gtk/empathy-geometry.h>
55 #include <libempathy-gtk/empathy-gtk-enum-types.h>
56 #include <libempathy-gtk/empathy-individual-dialogs.h>
57 #include <libempathy-gtk/empathy-individual-store.h>
58 #include <libempathy-gtk/empathy-individual-view.h>
59 #include <libempathy-gtk/empathy-new-message-dialog.h>
60 #include <libempathy-gtk/empathy-new-call-dialog.h>
61 #include <libempathy-gtk/empathy-log-window.h>
62 #include <libempathy-gtk/empathy-presence-chooser.h>
63 #include <libempathy-gtk/empathy-sound.h>
64 #include <libempathy-gtk/empathy-ui-utils.h>
65
66 #include "empathy-accounts-dialog.h"
67 #include "empathy-chat-manager.h"
68 #include "empathy-main-window.h"
69 #include "empathy-preferences.h"
70 #include "empathy-about-dialog.h"
71 #include "empathy-debug-window.h"
72 #include "empathy-new-chatroom-dialog.h"
73 #include "empathy-map-view.h"
74 #include "empathy-chatrooms-window.h"
75 #include "empathy-event-manager.h"
76 #include "empathy-ft-manager.h"
77 #include "empathy-migrate-butterfly-logs.h"
78
79 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
80 #include <libempathy/empathy-debug.h>
81
82 /* Flashing delay for icons (milliseconds). */
83 #define FLASH_TIMEOUT 500
84
85 /* Minimum width of roster window if something goes wrong. */
86 #define MIN_WIDTH 50
87
88 /* Accels (menu shortcuts) can be configured and saved */
89 #define ACCELS_FILENAME "accels.txt"
90
91 /* Name in the geometry file */
92 #define GEOMETRY_NAME "main-window"
93
94 enum {
95         PAGE_CONTACT_LIST = 0,
96         PAGE_NO_MATCH
97 };
98
99 G_DEFINE_TYPE (EmpathyMainWindow, empathy_main_window, GTK_TYPE_WINDOW);
100
101 #define GET_PRIV(self) ((EmpathyMainWindowPriv *)((EmpathyMainWindow *) self)->priv)
102
103 struct _EmpathyMainWindowPriv {
104         EmpathyIndividualStore  *individual_store;
105         EmpathyIndividualView   *individual_view;
106         TpAccountManager        *account_manager;
107         EmpathyChatroomManager  *chatroom_manager;
108         EmpathyEventManager     *event_manager;
109         guint                    flash_timeout_id;
110         gboolean                 flash_on;
111         gboolean                 empty;
112
113         GSettings              *gsettings_ui;
114         GSettings              *gsettings_contacts;
115
116         GtkWidget              *preferences;
117         GtkWidget              *main_vbox;
118         GtkWidget              *throbber;
119         GtkWidget              *throbber_tool_item;
120         GtkWidget              *presence_toolbar;
121         GtkWidget              *presence_chooser;
122         GtkWidget              *errors_vbox;
123         GtkWidget              *search_bar;
124         GtkWidget              *notebook;
125         GtkWidget              *no_entry_label;
126
127         GtkToggleAction        *show_protocols;
128         GtkRadioAction         *sort_by_name;
129         GtkRadioAction         *sort_by_status;
130         GtkRadioAction         *normal_with_avatars;
131         GtkRadioAction         *normal_size;
132         GtkRadioAction         *compact_size;
133
134         GtkUIManager           *ui_manager;
135         GtkAction              *view_history;
136         GtkAction              *room_join_favorites;
137         GtkWidget              *room_menu;
138         GtkWidget              *room_separator;
139         GtkWidget              *edit_context;
140         GtkWidget              *edit_context_separator;
141
142         guint                   size_timeout_id;
143         GHashTable             *errors;
144
145         /* stores a mapping from TpAccount to Handler ID to prevent
146          * to listen more than once to the status-changed signal */
147         GHashTable             *status_changed_handlers;
148
149         /* Actions that are enabled when there are connected accounts */
150         GList                  *actions_connected;
151
152         /* The idle event source to migrate butterfly's logs */
153         guint butterfly_log_migration_members_changed_id;
154 };
155
156 static void
157 main_window_flash_stop (EmpathyMainWindow *window)
158 {
159         EmpathyMainWindowPriv *priv = GET_PRIV (window);
160
161         if (priv->flash_timeout_id == 0) {
162                 return;
163         }
164
165         DEBUG ("Stop flashing");
166         g_source_remove (priv->flash_timeout_id);
167         priv->flash_timeout_id = 0;
168         priv->flash_on = FALSE;
169 }
170
171 typedef struct {
172         EmpathyEvent       *event;
173         gboolean            on;
174         EmpathyMainWindow  *window;
175 } FlashForeachData;
176
177 static gboolean
178 main_window_flash_foreach (GtkTreeModel *model,
179                            GtkTreePath  *path,
180                            GtkTreeIter  *iter,
181                            gpointer      user_data)
182 {
183         FlashForeachData *data = (FlashForeachData *) user_data;
184         FolksIndividual *individual;
185         EmpathyContact   *contact;
186         const gchar      *icon_name;
187         GtkTreePath      *parent_path = NULL;
188         GtkTreeIter       parent_iter;
189         GdkPixbuf        *pixbuf = NULL;
190
191         gtk_tree_model_get (model, iter,
192                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
193                                 &individual,
194                             -1);
195
196         if (individual == NULL)
197                 return FALSE;
198
199         contact = empathy_contact_dup_from_folks_individual (individual);
200         if (contact != data->event->contact) {
201                 tp_clear_object (&contact);
202                 return FALSE;
203         }
204
205         if (data->on) {
206                 icon_name = data->event->icon_name;
207                 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
208         } else {
209                 pixbuf = empathy_individual_store_get_individual_status_icon (
210                                                 GET_PRIV (data->window)->individual_store,
211                                                 individual);
212         }
213
214         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
215                             EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf,
216                             -1);
217
218         /* To make sure the parent is shown correctly, we emit
219          * the row-changed signal on the parent so it prompts
220          * it to be refreshed by the filter func.
221          */
222         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
223                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
224         }
225         if (parent_path) {
226                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
227                 gtk_tree_path_free (parent_path);
228         }
229
230         g_object_unref (individual);
231         tp_clear_object (&contact);
232
233         return FALSE;
234 }
235
236 static gboolean
237 main_window_flash_cb (EmpathyMainWindow *window)
238 {
239         EmpathyMainWindowPriv *priv = GET_PRIV (window);
240         GtkTreeModel     *model;
241         GSList           *events, *l;
242         gboolean          found_event = FALSE;
243         FlashForeachData  data;
244
245         priv->flash_on = !priv->flash_on;
246         data.on = priv->flash_on;
247         model = GTK_TREE_MODEL (priv->individual_store);
248
249         events = empathy_event_manager_get_events (priv->event_manager);
250         for (l = events; l; l = l->next) {
251                 data.event = l->data;
252                 data.window = window;
253                 if (!data.event->contact || !data.event->must_ack) {
254                         continue;
255                 }
256
257                 found_event = TRUE;
258                 gtk_tree_model_foreach (model,
259                                         main_window_flash_foreach,
260                                         &data);
261         }
262
263         if (!found_event) {
264                 main_window_flash_stop (window);
265         }
266
267         return TRUE;
268 }
269
270 static void
271 main_window_flash_start (EmpathyMainWindow *window)
272 {
273         EmpathyMainWindowPriv *priv = GET_PRIV (window);
274
275         if (priv->flash_timeout_id != 0) {
276                 return;
277         }
278
279         DEBUG ("Start flashing");
280         priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
281                                                 (GSourceFunc) main_window_flash_cb,
282                                                 window);
283         main_window_flash_cb (window);
284 }
285
286 static void
287 main_window_event_added_cb (EmpathyEventManager *manager,
288                             EmpathyEvent        *event,
289                             EmpathyMainWindow   *window)
290 {
291         if (event->contact) {
292                 main_window_flash_start (window);
293         }
294 }
295
296 static void
297 main_window_event_removed_cb (EmpathyEventManager *manager,
298                               EmpathyEvent        *event,
299                               EmpathyMainWindow   *window)
300 {
301         EmpathyMainWindowPriv *priv = GET_PRIV (window);
302         FlashForeachData data;
303
304         if (!event->contact) {
305                 return;
306         }
307
308         data.on = FALSE;
309         data.event = event;
310         data.window = window;
311         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->individual_store),
312                                 main_window_flash_foreach,
313                                 &data);
314 }
315
316 static void
317 main_window_row_activated_cb (EmpathyContactListView *view,
318                               GtkTreePath            *path,
319                               GtkTreeViewColumn      *col,
320                               EmpathyMainWindow      *window)
321 {
322         EmpathyMainWindowPriv *priv = GET_PRIV (window);
323         EmpathyContact *contact = NULL;
324         FolksIndividual *individual;
325         GtkTreeModel   *model;
326         GtkTreeIter     iter;
327         GSList         *events, *l;
328
329         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
330         gtk_tree_model_get_iter (model, &iter, path);
331
332         gtk_tree_model_get (model, &iter,
333                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
334                                 &individual,
335                             -1);
336
337         if (individual != NULL) {
338                 contact = empathy_contact_dup_from_folks_individual (individual);
339         }
340
341         if (!contact) {
342                 goto OUT;
343         }
344
345         /* If the contact has an event activate it, otherwise the
346          * default handler of row-activated will be called. */
347         events = empathy_event_manager_get_events (priv->event_manager);
348         for (l = events; l; l = l->next) {
349                 EmpathyEvent *event = l->data;
350
351                 if (event->contact == contact) {
352                         DEBUG ("Activate event");
353                         empathy_event_activate (event);
354
355                         /* We don't want the default handler of this signal
356                          * (e.g. open a chat) */
357                         g_signal_stop_emission_by_name (view, "row-activated");
358                         break;
359                 }
360         }
361
362         g_object_unref (contact);
363 OUT:
364         tp_clear_object (&individual);
365 }
366
367 static void
368 main_window_row_deleted_cb (GtkTreeModel      *model,
369                             GtkTreePath       *path,
370                             EmpathyMainWindow *window)
371 {
372         EmpathyMainWindowPriv *priv = GET_PRIV (window);
373         GtkTreeIter help_iter;
374
375         if (!gtk_tree_model_get_iter_first (model, &help_iter)) {
376                 priv->empty = TRUE;
377
378                 if (empathy_individual_view_is_searching (
379                                 priv->individual_view)) {
380                         gchar *tmp;
381
382                         tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>",
383                                 _("No match found"));
384
385                         gtk_label_set_markup (GTK_LABEL (priv->no_entry_label), tmp);
386                         g_free (tmp);
387
388                         gtk_notebook_set_current_page (
389                                         GTK_NOTEBOOK (priv->notebook), PAGE_NO_MATCH);
390                 }
391         }
392 }
393
394 static void
395 main_window_row_inserted_cb (GtkTreeModel      *model,
396                              GtkTreePath       *path,
397                              GtkTreeIter       *iter,
398                              EmpathyMainWindow *window)
399 {
400         EmpathyMainWindowPriv *priv = GET_PRIV (window);
401
402         if (priv->empty) {
403                 priv->empty = FALSE;
404                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
405                                 PAGE_CONTACT_LIST);
406                 gtk_widget_grab_focus (GTK_WIDGET (priv->individual_view));
407         }
408 }
409
410 static void
411 main_window_remove_error (EmpathyMainWindow *window,
412                           TpAccount         *account)
413 {
414         EmpathyMainWindowPriv *priv = GET_PRIV (window);
415         GtkWidget *error_widget;
416
417         error_widget = g_hash_table_lookup (priv->errors, account);
418         if (error_widget != NULL) {
419                 gtk_widget_destroy (error_widget);
420                 g_hash_table_remove (priv->errors, account);
421         }
422 }
423
424 static void
425 main_window_account_disabled_cb (TpAccountManager  *manager,
426                                  TpAccount         *account,
427                                  EmpathyMainWindow *window)
428 {
429         main_window_remove_error (window, account);
430 }
431
432 static void
433 main_window_error_retry_clicked_cb (GtkButton         *button,
434                                     EmpathyMainWindow *window)
435 {
436         TpAccount *account;
437
438         account = g_object_get_data (G_OBJECT (button), "account");
439         tp_account_reconnect_async (account, NULL, NULL);
440
441         main_window_remove_error (window, account);
442 }
443
444 static void
445 main_window_error_edit_clicked_cb (GtkButton         *button,
446                                    EmpathyMainWindow *window)
447 {
448         TpAccount *account;
449
450         account = g_object_get_data (G_OBJECT (button), "account");
451
452         empathy_accounts_dialog_show_application (
453                         gtk_widget_get_screen (GTK_WIDGET (button)),
454                         account, FALSE, FALSE);
455
456         main_window_remove_error (window, account);
457 }
458
459 static void
460 main_window_error_close_clicked_cb (GtkButton         *button,
461                                     EmpathyMainWindow *window)
462 {
463         TpAccount *account;
464
465         account = g_object_get_data (G_OBJECT (button), "account");
466         main_window_remove_error (window, account);
467 }
468
469 static void
470 main_window_error_display (EmpathyMainWindow *window,
471                            TpAccount         *account)
472 {
473         EmpathyMainWindowPriv *priv = GET_PRIV (window);
474         GtkWidget *info_bar;
475         GtkWidget *content_area;
476         GtkWidget *label;
477         GtkWidget *image;
478         GtkWidget *retry_button;
479         GtkWidget *edit_button;
480         GtkWidget *close_button;
481         GtkWidget *action_area;
482         GtkWidget *action_table;
483         gchar     *str;
484         const gchar     *icon_name;
485         const gchar *error_message;
486         gboolean user_requested;
487
488         error_message =
489                 empathy_account_get_error_message (account, &user_requested);
490
491         if (user_requested) {
492                 return;
493         }
494
495         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
496                                                tp_account_get_display_name (account),
497                                                error_message);
498
499         info_bar = g_hash_table_lookup (priv->errors, account);
500         if (info_bar) {
501                 label = g_object_get_data (G_OBJECT (info_bar), "label");
502
503                 /* Just set the latest error and return */
504                 gtk_label_set_markup (GTK_LABEL (label), str);
505                 g_free (str);
506
507                 return;
508         }
509
510         info_bar = gtk_info_bar_new ();
511         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
512
513         gtk_widget_set_no_show_all (info_bar, TRUE);
514         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
515         gtk_widget_show (info_bar);
516
517         icon_name = tp_account_get_icon_name (account);
518         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
519         gtk_widget_show (image);
520
521         label = gtk_label_new (str);
522         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
523         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
524         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
525         gtk_widget_show (label);
526         g_free (str);
527
528         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
529         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
530         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
531
532         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
533         retry_button = gtk_button_new ();
534         gtk_button_set_image (GTK_BUTTON (retry_button), image);
535         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
536         gtk_widget_show (retry_button);
537
538         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
539         edit_button = gtk_button_new ();
540         gtk_button_set_image (GTK_BUTTON (edit_button), image);
541         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
542         gtk_widget_show (edit_button);
543
544         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
545         close_button = gtk_button_new ();
546         gtk_button_set_image (GTK_BUTTON (close_button), image);
547         gtk_widget_set_tooltip_text (close_button, _("Close"));
548         gtk_widget_show (close_button);
549
550         action_table = gtk_table_new (1, 3, FALSE);
551         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
552         gtk_widget_show (action_table);
553
554         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
555         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
556
557         gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
558                                                                                 (GtkAttachOptions) (GTK_SHRINK),
559                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
560         gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
561                                                                                 (GtkAttachOptions) (GTK_SHRINK),
562                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
563         gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
564                                                                                 (GtkAttachOptions) (GTK_SHRINK),
565                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
566
567         g_object_set_data (G_OBJECT (info_bar), "label", label);
568         g_object_set_data_full (G_OBJECT (info_bar),
569                                 "account", g_object_ref (account),
570                                 g_object_unref);
571         g_object_set_data_full (G_OBJECT (edit_button),
572                                 "account", g_object_ref (account),
573                                 g_object_unref);
574         g_object_set_data_full (G_OBJECT (close_button),
575                                 "account", g_object_ref (account),
576                                 g_object_unref);
577         g_object_set_data_full (G_OBJECT (retry_button),
578                                 "account", g_object_ref (account),
579                                 g_object_unref);
580
581         g_signal_connect (edit_button, "clicked",
582                           G_CALLBACK (main_window_error_edit_clicked_cb),
583                           window);
584         g_signal_connect (close_button, "clicked",
585                           G_CALLBACK (main_window_error_close_clicked_cb),
586                           window);
587         g_signal_connect (retry_button, "clicked",
588                           G_CALLBACK (main_window_error_retry_clicked_cb),
589                           window);
590
591         gtk_widget_show (priv->errors_vbox);
592
593         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
594 }
595
596 static void
597 main_window_update_status (EmpathyMainWindow *window)
598 {
599         EmpathyMainWindowPriv *priv = GET_PRIV (window);
600         gboolean connected, connecting;
601         GList *l;
602
603         connected = empathy_account_manager_get_accounts_connected (&connecting);
604
605         /* Update the spinner state */
606         if (connecting) {
607                 gtk_spinner_start (GTK_SPINNER (priv->throbber));
608                 gtk_widget_show (priv->throbber_tool_item);
609         } else {
610                 gtk_spinner_stop (GTK_SPINNER (priv->throbber));
611                 gtk_widget_hide (priv->throbber_tool_item);
612         }
613
614         /* Update widgets sensibility */
615         for (l = priv->actions_connected; l; l = l->next) {
616                 gtk_action_set_sensitive (l->data, connected);
617         }
618 }
619
620 static void
621 main_window_connection_changed_cb (TpAccount  *account,
622                                    guint       old_status,
623                                    guint       current,
624                                    guint       reason,
625                                    gchar      *dbus_error_name,
626                                    GHashTable *details,
627                                    EmpathyMainWindow *window)
628 {
629         main_window_update_status (window);
630
631         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
632             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
633                 main_window_error_display (window, account);
634         }
635
636         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
637                 empathy_sound_play (GTK_WIDGET (window),
638                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
639         }
640
641         if (current == TP_CONNECTION_STATUS_CONNECTED) {
642                 empathy_sound_play (GTK_WIDGET (window),
643                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
644
645                 /* Account connected without error, remove error message if any */
646                 main_window_remove_error (window, account);
647         }
648 }
649
650 static void
651 main_window_accels_load (void)
652 {
653         gchar *filename;
654
655         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
656         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
657                 DEBUG ("Loading from:'%s'", filename);
658                 gtk_accel_map_load (filename);
659         }
660
661         g_free (filename);
662 }
663
664 static void
665 main_window_accels_save (void)
666 {
667         gchar *dir;
668         gchar *file_with_path;
669
670         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
671         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
672         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
673         g_free (dir);
674
675         DEBUG ("Saving to:'%s'", file_with_path);
676         gtk_accel_map_save (file_with_path);
677
678         g_free (file_with_path);
679 }
680
681 static void
682 empathy_main_window_finalize (GObject *window)
683 {
684         EmpathyMainWindowPriv *priv = GET_PRIV (window);
685         GHashTableIter iter;
686         gpointer key, value;
687
688         /* Save user-defined accelerators. */
689         main_window_accels_save ();
690
691         g_list_free (priv->actions_connected);
692
693         g_object_unref (priv->account_manager);
694         g_object_unref (priv->individual_store);
695         g_hash_table_destroy (priv->errors);
696
697         /* disconnect all handlers of status-changed signal */
698         g_hash_table_iter_init (&iter, priv->status_changed_handlers);
699         while (g_hash_table_iter_next (&iter, &key, &value))
700                 g_signal_handler_disconnect (TP_ACCOUNT (key),
701                                              GPOINTER_TO_UINT (value));
702
703         g_hash_table_destroy (priv->status_changed_handlers);
704
705         g_signal_handlers_disconnect_by_func (priv->event_manager,
706                                               main_window_event_added_cb,
707                                               window);
708         g_signal_handlers_disconnect_by_func (priv->event_manager,
709                                               main_window_event_removed_cb,
710                                               window);
711         g_object_unref (priv->event_manager);
712         g_object_unref (priv->ui_manager);
713         g_object_unref (priv->chatroom_manager);
714
715         g_object_unref (priv->gsettings_ui);
716         g_object_unref (priv->gsettings_contacts);
717
718         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
719 }
720
721 static gboolean
722 main_window_key_press_event_cb  (GtkWidget   *window,
723                                  GdkEventKey *event,
724                                  gpointer     user_data)
725 {
726         EmpathyChatManager *chat_manager;
727
728         if (event->keyval == GDK_T
729             && event->state & GDK_SHIFT_MASK
730             && event->state & GDK_CONTROL_MASK) {
731                 chat_manager = empathy_chat_manager_dup_singleton ();
732                 empathy_chat_manager_undo_closed_chat (chat_manager);
733                 g_object_unref (chat_manager);
734         }
735         return FALSE;
736 }
737
738 static void
739 main_window_chat_quit_cb (GtkAction         *action,
740                           EmpathyMainWindow *window)
741 {
742         gtk_main_quit ();
743 }
744
745 static void
746 main_window_view_history_cb (GtkAction         *action,
747                              EmpathyMainWindow *window)
748 {
749         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
750 }
751
752 static void
753 main_window_chat_new_message_cb (GtkAction         *action,
754                                  EmpathyMainWindow *window)
755 {
756         empathy_new_message_dialog_show (GTK_WINDOW (window));
757 }
758
759 static void
760 main_window_chat_new_call_cb (GtkAction         *action,
761                               EmpathyMainWindow *window)
762 {
763         empathy_new_call_dialog_show (GTK_WINDOW (window));
764 }
765
766 static void
767 main_window_chat_add_contact_cb (GtkAction         *action,
768                                  EmpathyMainWindow *window)
769 {
770         empathy_new_individual_dialog_show (GTK_WINDOW (window));
771 }
772
773 static void
774 main_window_view_show_ft_manager (GtkAction         *action,
775                                   EmpathyMainWindow *window)
776 {
777         empathy_ft_manager_show ();
778 }
779
780 static void
781 main_window_view_show_offline_cb (GtkToggleAction   *action,
782                                   EmpathyMainWindow *window)
783 {
784         EmpathyMainWindowPriv *priv = GET_PRIV (window);
785         gboolean current;
786
787         current = gtk_toggle_action_get_active (action);
788         g_settings_set_boolean (priv->gsettings_ui,
789                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
790                                 current);
791
792         empathy_individual_view_set_show_offline (priv->individual_view,
793                         current);
794 }
795
796 static void
797 main_window_notify_sort_contact_cb (GSettings         *gsettings,
798                                     const gchar       *key,
799                                     EmpathyMainWindow *window)
800 {
801         EmpathyMainWindowPriv *priv = GET_PRIV (window);
802         gchar *str;
803
804         str = g_settings_get_string (gsettings, key);
805
806         if (str != NULL) {
807                 GType       type;
808                 GEnumClass *enum_class;
809                 GEnumValue *enum_value;
810
811                 type = empathy_individual_store_sort_get_type ();
812                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
813                 enum_value = g_enum_get_value_by_nick (enum_class, str);
814                 if (enum_value) {
815                         /* By changing the value of the GtkRadioAction,
816                            it emits a signal that calls main_window_view_sort_contacts_cb
817                            which updates the contacts list */
818                         gtk_radio_action_set_current_value (priv->sort_by_name,
819                                                             enum_value->value);
820                 } else {
821                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
822                 }
823                 g_free (str);
824         }
825 }
826
827 static void
828 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
829                                    GtkRadioAction    *current,
830                                    EmpathyMainWindow *window)
831 {
832         EmpathyMainWindowPriv *priv = GET_PRIV (window);
833         EmpathyContactListStoreSort value;
834         GSList      *group;
835         GType        type;
836         GEnumClass  *enum_class;
837         GEnumValue  *enum_value;
838
839         value = gtk_radio_action_get_current_value (action);
840         group = gtk_radio_action_get_group (action);
841
842         /* Get string from index */
843         type = empathy_individual_store_sort_get_type ();
844         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
845         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
846
847         if (!enum_value) {
848                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
849                            g_slist_index (group, action));
850         } else {
851                 g_settings_set_string (priv->gsettings_contacts,
852                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
853                                        enum_value->value_nick);
854         }
855         empathy_individual_store_set_sort_criterium (priv->individual_store,
856                         value);
857 }
858
859 static void
860 main_window_view_show_protocols_cb (GtkToggleAction   *action,
861                                     EmpathyMainWindow *window)
862 {
863         EmpathyMainWindowPriv *priv = GET_PRIV (window);
864         gboolean value;
865
866         value = gtk_toggle_action_get_active (action);
867
868         g_settings_set_boolean (priv->gsettings_ui,
869                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
870                                 value);
871         empathy_individual_store_set_show_protocols (priv->individual_store,
872                                                      value);
873 }
874
875 /* Matches GtkRadioAction values set in empathy-main-window.ui */
876 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
877 #define CONTACT_LIST_NORMAL_SIZE                        1
878 #define CONTACT_LIST_COMPACT_SIZE                       2
879
880 static void
881 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
882                                         GtkRadioAction    *current,
883                                         EmpathyMainWindow *window)
884 {
885         EmpathyMainWindowPriv *priv = GET_PRIV (window);
886         GSettings *gsettings_ui;
887         gint value;
888
889         value = gtk_radio_action_get_current_value (action);
890         /* create a new GSettings, so we can delay the setting until both
891          * values are set */
892         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
893
894         DEBUG ("radio button toggled, value = %i", value);
895
896         g_settings_delay (gsettings_ui);
897         g_settings_set_boolean (gsettings_ui,
898                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
899                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
900
901         g_settings_set_boolean (gsettings_ui,
902                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
903                                 value == CONTACT_LIST_COMPACT_SIZE);
904         g_settings_apply (gsettings_ui);
905
906         /* FIXME: these enums probably have the wrong namespace */
907         empathy_individual_store_set_show_avatars (priv->individual_store,
908                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
909         empathy_individual_store_set_is_compact (priv->individual_store,
910                         value == CONTACT_LIST_COMPACT_SIZE);
911
912         g_object_unref (gsettings_ui);
913 }
914
915 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
916                                                   const gchar       *key,
917                                                   EmpathyMainWindow *window)
918 {
919         EmpathyMainWindowPriv *priv = GET_PRIV (window);
920
921         gtk_toggle_action_set_active (priv->show_protocols,
922                         g_settings_get_boolean (gsettings,
923                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
924 }
925
926
927 static void
928 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
929                                          const gchar       *key,
930                                          EmpathyMainWindow *window)
931 {
932         EmpathyMainWindowPriv *priv = GET_PRIV (window);
933         gint value;
934
935         if (g_settings_get_boolean (gsettings,
936                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
937                 value = CONTACT_LIST_COMPACT_SIZE;
938         } else if (g_settings_get_boolean (gsettings,
939                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
940                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
941         } else {
942                 value = CONTACT_LIST_NORMAL_SIZE;
943         }
944
945         DEBUG ("setting changed, value = %i", value);
946
947         /* By changing the value of the GtkRadioAction,
948            it emits a signal that calls main_window_view_contacts_list_size_cb
949            which updates the contacts list */
950         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
951 }
952
953 static void
954 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
955                               EmpathyMainWindow *window)
956 {
957 #ifdef HAVE_LIBCHAMPLAIN
958         empathy_map_view_show ();
959 #endif
960 }
961
962 static void
963 join_chatroom (EmpathyChatroom *chatroom,
964                gint64 timestamp)
965 {
966         TpAccount      *account;
967         const gchar    *room;
968
969         account = empathy_chatroom_get_account (chatroom);
970         room = empathy_chatroom_get_room (chatroom);
971
972         DEBUG ("Requesting channel for '%s'", room);
973         empathy_dispatcher_join_muc (account, room, timestamp);
974 }
975
976 typedef struct
977 {
978         TpAccount *account;
979         EmpathyChatroom *chatroom;
980         gint64 timestamp;
981         glong sig_id;
982         guint timeout;
983 } join_fav_account_sig_ctx;
984
985 static join_fav_account_sig_ctx *
986 join_fav_account_sig_ctx_new (TpAccount *account,
987                              EmpathyChatroom *chatroom,
988                               gint64 timestamp)
989 {
990         join_fav_account_sig_ctx *ctx = g_slice_new0 (
991                 join_fav_account_sig_ctx);
992
993         ctx->account = g_object_ref (account);
994         ctx->chatroom = g_object_ref (chatroom);
995         ctx->timestamp = timestamp;
996         return ctx;
997 }
998
999 static void
1000 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1001 {
1002         g_object_unref (ctx->account);
1003         g_object_unref (ctx->chatroom);
1004         g_slice_free (join_fav_account_sig_ctx, ctx);
1005 }
1006
1007 static void
1008 account_status_changed_cb (TpAccount  *account,
1009                            TpConnectionStatus old_status,
1010                            TpConnectionStatus new_status,
1011                            guint reason,
1012                            gchar *dbus_error_name,
1013                            GHashTable *details,
1014                            gpointer user_data)
1015 {
1016         join_fav_account_sig_ctx *ctx = user_data;
1017
1018         switch (new_status) {
1019                 case TP_CONNECTION_STATUS_DISCONNECTED:
1020                         /* Don't wait any longer */
1021                         goto finally;
1022                         break;
1023
1024                 case TP_CONNECTION_STATUS_CONNECTING:
1025                         /* Wait a bit */
1026                         return;
1027
1028                 case TP_CONNECTION_STATUS_CONNECTED:
1029                         /* We can join the room */
1030                         break;
1031
1032                 default:
1033                         g_assert_not_reached ();
1034         }
1035
1036         join_chatroom (ctx->chatroom, ctx->timestamp);
1037
1038 finally:
1039         g_source_remove (ctx->timeout);
1040         g_signal_handler_disconnect (account, ctx->sig_id);
1041 }
1042
1043 #define JOIN_FAVORITE_TIMEOUT 5
1044
1045 static gboolean
1046 join_favorite_timeout_cb (gpointer data)
1047 {
1048         join_fav_account_sig_ctx *ctx = data;
1049
1050         /* stop waiting for joining the favorite room */
1051         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1052         return FALSE;
1053 }
1054
1055 static void
1056 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1057 {
1058         TpAccount      *account;
1059
1060         account = empathy_chatroom_get_account (chatroom);
1061         if (tp_account_get_connection_status (account, NULL) !=
1062                                              TP_CONNECTION_STATUS_CONNECTED) {
1063                 join_fav_account_sig_ctx *ctx;
1064
1065                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1066                         gtk_get_current_event_time ());
1067
1068                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1069                         G_CALLBACK (account_status_changed_cb), ctx,
1070                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1071
1072                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1073                         join_favorite_timeout_cb, ctx);
1074                 return;
1075         }
1076
1077         join_chatroom (chatroom, gtk_get_current_event_time ());
1078 }
1079
1080 static void
1081 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1082                                                 EmpathyChatroom *chatroom)
1083 {
1084         main_window_favorite_chatroom_join (chatroom);
1085 }
1086
1087 static void
1088 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1089                                         EmpathyChatroom   *chatroom)
1090 {
1091         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1092         GtkWidget   *menu_item;
1093         const gchar *name;
1094
1095         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1096                 return;
1097         }
1098
1099         name = empathy_chatroom_get_name (chatroom);
1100         menu_item = gtk_menu_item_new_with_label (name);
1101
1102         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1103         g_signal_connect (menu_item, "activate",
1104                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1105                           chatroom);
1106
1107         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1108                                menu_item, 4);
1109
1110         gtk_widget_show (menu_item);
1111 }
1112
1113 static void
1114 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1115                                              EmpathyChatroom        *chatroom,
1116                                              EmpathyMainWindow      *window)
1117 {
1118         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1119
1120         main_window_favorite_chatroom_menu_add (window, chatroom);
1121         gtk_widget_show (priv->room_separator);
1122         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1123 }
1124
1125 static void
1126 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1127                                                EmpathyChatroom        *chatroom,
1128                                                EmpathyMainWindow      *window)
1129 {
1130         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1131         GtkWidget *menu_item;
1132         GList *chatrooms;
1133
1134         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1135         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1136         gtk_widget_destroy (menu_item);
1137
1138         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1139         if (chatrooms) {
1140                 gtk_widget_show (priv->room_separator);
1141         } else {
1142                 gtk_widget_hide (priv->room_separator);
1143         }
1144
1145         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1146         g_list_free (chatrooms);
1147 }
1148
1149 static void
1150 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1151 {
1152         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1153         GList *chatrooms, *l;
1154         GtkWidget *room;
1155
1156         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1157         chatrooms = empathy_chatroom_manager_get_chatrooms (
1158                 priv->chatroom_manager, NULL);
1159         room = gtk_ui_manager_get_widget (priv->ui_manager,
1160                 "/menubar/room");
1161         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1162         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1163                 "/menubar/room/room_separator");
1164
1165         for (l = chatrooms; l; l = l->next) {
1166                 main_window_favorite_chatroom_menu_add (window, l->data);
1167         }
1168
1169         if (!chatrooms) {
1170                 gtk_widget_hide (priv->room_separator);
1171         }
1172
1173         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1174
1175         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1176                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1177                           window);
1178         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1179                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1180                           window);
1181
1182         g_list_free (chatrooms);
1183 }
1184
1185 static void
1186 main_window_room_join_new_cb (GtkAction         *action,
1187                               EmpathyMainWindow *window)
1188 {
1189         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1190 }
1191
1192 static void
1193 main_window_room_join_favorites_cb (GtkAction         *action,
1194                                     EmpathyMainWindow *window)
1195 {
1196         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1197         GList *chatrooms, *l;
1198
1199         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1200         for (l = chatrooms; l; l = l->next) {
1201                 main_window_favorite_chatroom_join (l->data);
1202         }
1203         g_list_free (chatrooms);
1204 }
1205
1206 static void
1207 main_window_room_manage_favorites_cb (GtkAction         *action,
1208                                       EmpathyMainWindow *window)
1209 {
1210         empathy_chatrooms_window_show (GTK_WINDOW (window));
1211 }
1212
1213 static void
1214 main_window_edit_cb (GtkAction         *action,
1215                      EmpathyMainWindow *window)
1216 {
1217         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1218         GtkWidget *submenu;
1219
1220         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1221         submenu = empathy_individual_view_get_individual_menu (
1222                         priv->individual_view);
1223         if (submenu) {
1224                 GtkMenuItem *item;
1225                 GtkWidget   *label;
1226
1227                 item = GTK_MENU_ITEM (priv->edit_context);
1228                 label = gtk_bin_get_child (GTK_BIN (item));
1229                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1230
1231                 gtk_widget_show (priv->edit_context);
1232                 gtk_widget_show (priv->edit_context_separator);
1233
1234                 gtk_menu_item_set_submenu (item, submenu);
1235
1236                 return;
1237         }
1238
1239         submenu = empathy_individual_view_get_group_menu (
1240                         priv->individual_view);
1241         if (submenu) {
1242                 GtkMenuItem *item;
1243                 GtkWidget   *label;
1244
1245                 item = GTK_MENU_ITEM (priv->edit_context);
1246                 label = gtk_bin_get_child (GTK_BIN (item));
1247                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1248
1249                 gtk_widget_show (priv->edit_context);
1250                 gtk_widget_show (priv->edit_context_separator);
1251
1252                 gtk_menu_item_set_submenu (item, submenu);
1253
1254                 return;
1255         }
1256
1257         gtk_widget_hide (priv->edit_context);
1258         gtk_widget_hide (priv->edit_context_separator);
1259
1260         return;
1261 }
1262
1263 static void
1264 main_window_edit_accounts_cb (GtkAction         *action,
1265                               EmpathyMainWindow *window)
1266 {
1267         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1268                         NULL, FALSE, FALSE);
1269 }
1270
1271 static void
1272 main_window_edit_personal_information_cb (GtkAction         *action,
1273                                           EmpathyMainWindow *window)
1274 {
1275         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1276 }
1277
1278 static void
1279 main_window_edit_preferences_cb (GtkAction         *action,
1280                                  EmpathyMainWindow *window)
1281 {
1282         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1283
1284         if (priv->preferences == NULL) {
1285                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window));
1286                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
1287                                            (gpointer) &priv->preferences);
1288
1289                 gtk_widget_show (priv->preferences);
1290         } else {
1291                 gtk_window_present (GTK_WINDOW (priv->preferences));
1292         }
1293 }
1294
1295 static void
1296 main_window_help_about_cb (GtkAction         *action,
1297                            EmpathyMainWindow *window)
1298 {
1299         empathy_about_dialog_new (GTK_WINDOW (window));
1300 }
1301
1302 static void
1303 main_window_help_debug_cb (GtkAction         *action,
1304                            EmpathyMainWindow *window)
1305 {
1306         GdkScreen *screen = gdk_screen_get_default ();
1307         GError *error = NULL;
1308         gchar *argv[2] = { NULL, };
1309         gint i = 0;
1310         gchar *path;
1311
1312         g_return_if_fail (GDK_IS_SCREEN (screen));
1313
1314         /* Try to run from source directory if possible */
1315         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1316                         "empathy-debugger", NULL);
1317
1318         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1319                 g_free (path);
1320                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1321         }
1322
1323         argv[i++] = path;
1324
1325         gdk_spawn_on_screen (screen, NULL, argv, NULL,
1326                         G_SPAWN_SEARCH_PATH,
1327                         NULL, NULL, NULL, &error);
1328
1329         if (error) {
1330                 g_warning ("Failed to open debug window: %s", error->message);
1331                 g_error_free (error);
1332         }
1333
1334         g_free (path);
1335 }
1336
1337 static void
1338 main_window_help_contents_cb (GtkAction         *action,
1339                               EmpathyMainWindow *window)
1340 {
1341         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
1342 }
1343
1344 static gboolean
1345 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
1346                                             GdkEventButton    *event,
1347                                             EmpathyMainWindow *window)
1348 {
1349         if (event->type != GDK_BUTTON_PRESS ||
1350             event->button != 1) {
1351                 return FALSE;
1352         }
1353
1354         empathy_accounts_dialog_show_application (
1355                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
1356                         NULL, FALSE, FALSE);
1357
1358         return FALSE;
1359 }
1360
1361 static void
1362 main_window_account_removed_cb (TpAccountManager  *manager,
1363                                 TpAccount         *account,
1364                                 EmpathyMainWindow *window)
1365 {
1366         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1367         GList *a;
1368
1369         a = tp_account_manager_get_valid_accounts (manager);
1370
1371         gtk_action_set_sensitive (priv->view_history,
1372                 g_list_length (a) > 0);
1373
1374         g_list_free (a);
1375
1376         /* remove errors if any */
1377         main_window_remove_error (window, account);
1378 }
1379
1380 static void
1381 main_window_account_validity_changed_cb (TpAccountManager  *manager,
1382                                          TpAccount         *account,
1383                                          gboolean           valid,
1384                                          EmpathyMainWindow *window)
1385 {
1386         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1387
1388         if (valid) {
1389                 gulong handler_id;
1390                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1391                         priv->status_changed_handlers, account));
1392
1393                 /* connect signal only if it was not connected yet */
1394                 if (handler_id == 0) {
1395                         handler_id = g_signal_connect (account,
1396                                 "status-changed",
1397                                 G_CALLBACK (main_window_connection_changed_cb),
1398                                 window);
1399                         g_hash_table_insert (priv->status_changed_handlers,
1400                                 account, GUINT_TO_POINTER (handler_id));
1401                 }
1402         }
1403
1404         main_window_account_removed_cb (manager, account, window);
1405 }
1406
1407 static void
1408 main_window_notify_show_offline_cb (GSettings   *gsettings,
1409                                     const gchar *key,
1410                                     gpointer     toggle_action)
1411 {
1412         gtk_toggle_action_set_active (toggle_action,
1413                         g_settings_get_boolean (gsettings, key));
1414 }
1415
1416 static void
1417 main_window_connection_items_setup (EmpathyMainWindow *window,
1418                                     GtkBuilder        *gui)
1419 {
1420         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1421         GList         *list;
1422         GObject       *action;
1423         guint          i;
1424         const gchar *actions_connected[] = {
1425                 "room",
1426                 "chat_new_message",
1427                 "chat_new_call",
1428                 "chat_add_contact",
1429                 "edit_personal_information"
1430         };
1431
1432         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1433                 action = gtk_builder_get_object (gui, actions_connected[i]);
1434                 list = g_list_prepend (list, action);
1435         }
1436
1437         priv->actions_connected = list;
1438 }
1439
1440 static void
1441 account_manager_prepared_cb (GObject      *source_object,
1442                              GAsyncResult *result,
1443                              gpointer      user_data)
1444 {
1445         GList *accounts, *j;
1446         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1447         EmpathyMainWindow *window = user_data;
1448         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1449         GError *error = NULL;
1450
1451         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1452                 DEBUG ("Failed to prepare account manager: %s", error->message);
1453                 g_error_free (error);
1454                 return;
1455         }
1456
1457         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1458         for (j = accounts; j != NULL; j = j->next) {
1459                 TpAccount *account = TP_ACCOUNT (j->data);
1460                 gulong handler_id;
1461
1462                 handler_id = g_signal_connect (account, "status-changed",
1463                                   G_CALLBACK (main_window_connection_changed_cb),
1464                                   window);
1465                 g_hash_table_insert (priv->status_changed_handlers,
1466                                      account, GUINT_TO_POINTER (handler_id));
1467         }
1468
1469         g_signal_connect (manager, "account-validity-changed",
1470                           G_CALLBACK (main_window_account_validity_changed_cb),
1471                           window);
1472
1473         main_window_update_status (window);
1474
1475         /* Disable the "Previous Conversations" menu entry if there is no account */
1476         gtk_action_set_sensitive (priv->view_history,
1477                 g_list_length (accounts) > 0);
1478
1479         g_list_free (accounts);
1480 }
1481
1482 static void
1483 main_window_members_changed_cb (EmpathyContactList *list,
1484                                 EmpathyContact     *contact,
1485                                 EmpathyContact     *actor,
1486                                 guint               reason,
1487                                 gchar              *message,
1488                                 gboolean            is_member,
1489                                 EmpathyMainWindow  *window)
1490 {
1491         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1492
1493         if (!is_member)
1494                 return;
1495
1496         if (!empathy_migrate_butterfly_logs (contact)) {
1497                 g_signal_handler_disconnect (list,
1498                         priv->butterfly_log_migration_members_changed_id);
1499                 priv->butterfly_log_migration_members_changed_id = 0;
1500         }
1501 }
1502
1503 static GObject *
1504 empathy_main_window_constructor (GType type,
1505                                  guint n_construct_params,
1506                                  GObjectConstructParam *construct_params)
1507 {
1508         static GObject *window = NULL;
1509
1510         if (window != NULL)
1511                 return g_object_ref (window);
1512
1513         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
1514                 type, n_construct_params, construct_params);
1515
1516         g_object_add_weak_pointer (window, (gpointer) &window);
1517
1518         return window;
1519 }
1520
1521 static void
1522 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
1523 {
1524         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1525
1526         object_class->finalize = empathy_main_window_finalize;
1527         object_class->constructor = empathy_main_window_constructor;
1528
1529         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
1530 }
1531
1532 static void
1533 empathy_main_window_init (EmpathyMainWindow *window)
1534 {
1535         EmpathyMainWindowPriv    *priv;
1536         EmpathyContactList       *list_iface;
1537         EmpathyIndividualManager *individual_manager;
1538         GtkBuilder               *gui;
1539         GtkWidget                *sw;
1540         GtkToggleAction          *show_offline_widget;
1541         GtkAction                *show_map_widget;
1542         GtkToolItem              *item;
1543         gboolean                  show_offline;
1544         gchar                    *filename;
1545         GSList                   *l;
1546         GtkTreeModel             *model;
1547
1548         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1549                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
1550
1551         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1552         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
1553
1554         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
1555         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
1556         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
1557
1558         /* Set up interface */
1559         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1560         gui = empathy_builder_get_file (filename,
1561                                        "main_vbox", &priv->main_vbox,
1562                                        "errors_vbox", &priv->errors_vbox,
1563                                        "ui_manager", &priv->ui_manager,
1564                                        "view_show_offline", &show_offline_widget,
1565                                        "view_show_protocols", &priv->show_protocols,
1566                                        "view_sort_by_name", &priv->sort_by_name,
1567                                        "view_sort_by_status", &priv->sort_by_status,
1568                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
1569                                        "view_normal_size", &priv->normal_size,
1570                                        "view_compact_size", &priv->compact_size,
1571                                        "view_history", &priv->view_history,
1572                                        "view_show_map", &show_map_widget,
1573                                        "room_join_favorites", &priv->room_join_favorites,
1574                                        "presence_toolbar", &priv->presence_toolbar,
1575                                        "notebook", &priv->notebook,
1576                                        "no_entry_label", &priv->no_entry_label,
1577                                        "roster_scrolledwindow", &sw,
1578                                        NULL);
1579         g_free (filename);
1580
1581         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
1582         gtk_widget_show (priv->main_vbox);
1583
1584         g_signal_connect (window, "key-press-event",
1585                           G_CALLBACK (main_window_key_press_event_cb), NULL);
1586
1587         empathy_builder_connect (gui, window,
1588                               "chat_quit", "activate", main_window_chat_quit_cb,
1589                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1590                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1591                               "view_history", "activate", main_window_view_history_cb,
1592                               "room_join_new", "activate", main_window_room_join_new_cb,
1593                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1594                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1595                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1596                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1597                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1598                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1599                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1600                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1601                               "view_show_map", "activate", main_window_view_show_map_cb,
1602                               "edit", "activate", main_window_edit_cb,
1603                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1604                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1605                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1606                               "help_about", "activate", main_window_help_about_cb,
1607                               "help_debug", "activate", main_window_help_debug_cb,
1608                               "help_contents", "activate", main_window_help_contents_cb,
1609                               NULL);
1610
1611         /* Set up connection related widgets. */
1612         main_window_connection_items_setup (window, gui);
1613
1614         g_object_ref (priv->ui_manager);
1615         g_object_unref (gui);
1616
1617 #ifndef HAVE_LIBCHAMPLAIN
1618         gtk_action_set_visible (show_map_widget, FALSE);
1619 #endif
1620
1621         priv->account_manager = tp_account_manager_dup ();
1622
1623         tp_account_manager_prepare_async (priv->account_manager, NULL,
1624                                           account_manager_prepared_cb, window);
1625
1626         priv->errors = g_hash_table_new_full (g_direct_hash,
1627                                               g_direct_equal,
1628                                               g_object_unref,
1629                                               NULL);
1630
1631         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1632                                                                g_direct_equal,
1633                                                                NULL,
1634                                                                NULL);
1635
1636         /* Set up menu */
1637         main_window_favorite_chatroom_menu_setup (window);
1638
1639         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
1640                 "/menubar/edit/edit_context");
1641         priv->edit_context_separator = gtk_ui_manager_get_widget (
1642                 priv->ui_manager,
1643                 "/menubar/edit/edit_context_separator");
1644         gtk_widget_hide (priv->edit_context);
1645         gtk_widget_hide (priv->edit_context_separator);
1646
1647         /* Set up contact list. */
1648         empathy_status_presets_get_all ();
1649
1650         /* Set up presence chooser */
1651         priv->presence_chooser = empathy_presence_chooser_new ();
1652         gtk_widget_show (priv->presence_chooser);
1653         item = gtk_tool_item_new ();
1654         gtk_widget_show (GTK_WIDGET (item));
1655         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
1656         gtk_tool_item_set_is_important (item, TRUE);
1657         gtk_tool_item_set_expand (item, TRUE);
1658         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1659
1660         /* Set up the throbber */
1661         priv->throbber = gtk_spinner_new ();
1662         gtk_widget_set_size_request (priv->throbber, 16, -1);
1663         gtk_widget_set_tooltip_text (priv->throbber, _("Show and edit accounts"));
1664         gtk_widget_set_has_window (GTK_WIDGET (priv->throbber), TRUE);
1665         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
1666         g_signal_connect (priv->throbber, "button-press-event",
1667                 G_CALLBACK (main_window_throbber_button_press_event_cb),
1668                 window);
1669         gtk_widget_show (priv->throbber);
1670
1671         item = gtk_tool_item_new ();
1672         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
1673         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1674         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
1675         priv->throbber_tool_item = GTK_WIDGET (item);
1676
1677         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1678         individual_manager = empathy_individual_manager_dup_singleton ();
1679         priv->individual_store = empathy_individual_store_new (
1680                         individual_manager);
1681         g_object_unref (individual_manager);
1682
1683         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
1684          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
1685          * drop, so allowing them would achieve nothing except confusion. */
1686         priv->individual_view = empathy_individual_view_new (
1687                         priv->individual_store,
1688                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
1689                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
1690
1691         priv->butterfly_log_migration_members_changed_id = g_signal_connect (
1692                         list_iface, "members-changed",
1693                         G_CALLBACK (main_window_members_changed_cb), window);
1694
1695         g_object_unref (list_iface);
1696
1697         gtk_widget_show (GTK_WIDGET (priv->individual_view));
1698         gtk_container_add (GTK_CONTAINER (sw),
1699                            GTK_WIDGET (priv->individual_view));
1700         g_signal_connect (priv->individual_view, "row-activated",
1701                           G_CALLBACK (main_window_row_activated_cb),
1702                           window);
1703
1704         /* Set up search bar */
1705         priv->search_bar = empathy_live_search_new (
1706                 GTK_WIDGET (priv->individual_view));
1707         empathy_individual_view_set_live_search (priv->individual_view,
1708                 EMPATHY_LIVE_SEARCH (priv->search_bar));
1709         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->search_bar,
1710                 FALSE, TRUE, 0);
1711         g_signal_connect_swapped (window, "map",
1712                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
1713
1714         /* Connect to proper signals to check if contact list is empty or not */
1715         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
1716         priv->empty = TRUE;
1717         g_signal_connect (model, "row-inserted",
1718                           G_CALLBACK (main_window_row_inserted_cb),
1719                           window);
1720         g_signal_connect (model, "row-deleted",
1721                           G_CALLBACK (main_window_row_deleted_cb),
1722                           window);
1723
1724         /* Load user-defined accelerators. */
1725         main_window_accels_load ();
1726
1727         /* Set window size. */
1728         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
1729
1730         /* Enable event handling */
1731         priv->event_manager = empathy_event_manager_dup_singleton ();
1732
1733         g_signal_connect (priv->event_manager, "event-added",
1734                           G_CALLBACK (main_window_event_added_cb), window);
1735         g_signal_connect (priv->event_manager, "event-removed",
1736                           G_CALLBACK (main_window_event_removed_cb), window);
1737         g_signal_connect (priv->account_manager, "account-validity-changed",
1738                           G_CALLBACK (main_window_account_validity_changed_cb),
1739                           window);
1740         g_signal_connect (priv->account_manager, "account-removed",
1741                           G_CALLBACK (main_window_account_removed_cb),
1742                           window);
1743         g_signal_connect (priv->account_manager, "account-disabled",
1744                           G_CALLBACK (main_window_account_disabled_cb),
1745                           window);
1746
1747         l = empathy_event_manager_get_events (priv->event_manager);
1748         while (l) {
1749                 main_window_event_added_cb (priv->event_manager, l->data,
1750                                 window);
1751                 l = l->next;
1752         }
1753
1754         /* Show offline ? */
1755         show_offline = g_settings_get_boolean (priv->gsettings_ui,
1756                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
1757         g_signal_connect (priv->gsettings_ui,
1758                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
1759                           G_CALLBACK (main_window_notify_show_offline_cb),
1760                           show_offline_widget);
1761
1762         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1763
1764         /* Show protocol ? */
1765         g_signal_connect (priv->gsettings_ui,
1766                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1767                           G_CALLBACK (main_window_notify_show_protocols_cb),
1768                           window);
1769
1770         main_window_notify_show_protocols_cb (priv->gsettings_ui,
1771                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1772                                               window);
1773
1774         /* Sort by name / by status ? */
1775         g_signal_connect (priv->gsettings_contacts,
1776                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1777                           G_CALLBACK (main_window_notify_sort_contact_cb),
1778                           window);
1779
1780         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
1781                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1782                                             window);
1783
1784         /* Contacts list size */
1785         g_signal_connect (priv->gsettings_ui,
1786                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1787                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1788                           window);
1789         g_signal_connect (priv->gsettings_ui,
1790                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
1791                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1792                           window);
1793
1794         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
1795                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1796                                                  window);
1797 }
1798
1799 GtkWidget *
1800 empathy_main_window_dup (void)
1801 {
1802         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
1803 }