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