]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
77fa538d673a062aa4f9496df7273bff4ff14e8e
[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-2008 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 <glade/glade.h>
29 #include <glib/gi18n.h>
30
31 #include <libempathy/empathy-contact.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-account-manager.h>
34 #include <libempathy/empathy-dispatcher.h>
35 #include <libempathy/empathy-chatroom-manager.h>
36 #include <libempathy/empathy-chatroom.h>
37 #include <libempathy/empathy-contact-list.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/empathy-contact-factory.h>
40 #include <libempathy/empathy-status-presets.h>
41
42 #include <libempathy-gtk/empathy-contact-dialogs.h>
43 #include <libempathy-gtk/empathy-contact-list-store.h>
44 #include <libempathy-gtk/empathy-contact-list-view.h>
45 #include <libempathy-gtk/empathy-presence-chooser.h>
46 #include <libempathy-gtk/empathy-ui-utils.h>
47 #include <libempathy-gtk/empathy-geometry.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-new-message-dialog.h>
51 #include <libempathy-gtk/empathy-gtk-enum-types.h>
52
53 #include <libmissioncontrol/mission-control.h>
54
55 #include "empathy-accounts-dialog.h"
56 #include "empathy-main-window.h"
57 #include "ephy-spinner.h"
58 #include "empathy-preferences.h"
59 #include "empathy-about-dialog.h"
60 #include "empathy-new-chatroom-dialog.h"
61 #include "empathy-chatrooms-window.h"
62 #include "empathy-event-manager.h"
63
64 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
65 #include <libempathy/empathy-debug.h>
66
67 /* Flashing delay for icons (milliseconds). */
68 #define FLASH_TIMEOUT 500
69
70 /* Minimum width of roster window if something goes wrong. */
71 #define MIN_WIDTH 50
72
73 /* Accels (menu shortcuts) can be configured and saved */
74 #define ACCELS_FILENAME "accels.txt"
75
76 /* Name in the geometry file */
77 #define GEOMETRY_NAME "main-window"
78
79 typedef struct {
80         EmpathyContactListView  *list_view;
81         EmpathyContactListStore *list_store;
82         MissionControl          *mc;
83         EmpathyAccountManager   *account_manager;
84         EmpathyChatroomManager  *chatroom_manager;
85         EmpathyEventManager     *event_manager;
86         guint                    flash_timeout_id;
87         gboolean                 flash_on;
88
89         GtkWidget              *window;
90         GtkWidget              *main_vbox;
91         GtkWidget              *throbber;
92         GtkWidget              *presence_toolbar;
93         GtkWidget              *presence_chooser;
94         GtkWidget              *errors_vbox;
95
96         GtkWidget              *room;
97         GtkWidget              *room_menu;
98         GtkWidget              *room_sep;
99         GtkWidget              *room_join_favorites;
100         GtkWidget              *edit_context;
101         GtkWidget              *edit_context_separator;
102         GtkWidget              *chat_history_menu_item;
103
104         guint                   size_timeout_id;
105         GHashTable             *errors;
106
107         /* Widgets that are enabled when there are connected accounts */
108         GList                  *widgets_connected;
109 } EmpathyMainWindow;
110
111 static EmpathyMainWindow *window = NULL;
112
113 static void     main_window_destroy_cb                         (GtkWidget                *widget,
114                                                                 EmpathyMainWindow        *window);
115 static void     main_window_favorite_chatroom_menu_setup       (EmpathyMainWindow        *window);
116 static void     main_window_favorite_chatroom_menu_added_cb    (EmpathyChatroomManager   *manager,
117                                                                 EmpathyChatroom          *chatroom,
118                                                                 EmpathyMainWindow        *window);
119 static void     main_window_favorite_chatroom_menu_removed_cb  (EmpathyChatroomManager   *manager,
120                                                                 EmpathyChatroom          *chatroom,
121                                                                 EmpathyMainWindow        *window);
122 static void     main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem              *menu_item,
123                                                                 EmpathyChatroom          *chatroom);
124 static void     main_window_favorite_chatroom_menu_update      (EmpathyMainWindow        *window);
125 static void     main_window_favorite_chatroom_menu_add         (EmpathyMainWindow        *window,
126                                                                 EmpathyChatroom          *chatroom);
127 static void     main_window_favorite_chatroom_join             (EmpathyChatroom          *chatroom);
128 static void     main_window_chat_quit_cb                       (GtkWidget                *widget,
129                                                                 EmpathyMainWindow        *window);
130 static void     main_window_chat_new_message_cb                (GtkWidget                *widget,
131                                                                 EmpathyMainWindow        *window);
132 static void     main_window_chat_history_cb                    (GtkWidget                *widget,
133                                                                 EmpathyMainWindow        *window);
134 static void     main_window_room_join_new_cb                   (GtkWidget                *widget,
135                                                                 EmpathyMainWindow        *window);
136 static void     main_window_room_join_favorites_cb             (GtkWidget                *widget,
137                                                                 EmpathyMainWindow        *window);
138 static void     main_window_room_manage_favorites_cb           (GtkWidget                *widget,
139                                                                 EmpathyMainWindow        *window);
140 static void     main_window_chat_add_contact_cb                (GtkWidget                *widget,
141                                                                 EmpathyMainWindow        *window);
142 static void     main_window_chat_show_offline_cb               (GtkCheckMenuItem         *item,
143                                                                 EmpathyMainWindow        *window);
144 static gboolean main_window_edit_button_press_event_cb         (GtkWidget                *widget,
145                                                                 GdkEventButton           *event,
146                                                                 EmpathyMainWindow        *window);
147 static void     main_window_edit_accounts_cb                   (GtkWidget                *widget,
148                                                                 EmpathyMainWindow        *window);
149 static void     main_window_edit_personal_information_cb       (GtkWidget                *widget,
150                                                                 EmpathyMainWindow        *window);
151 static void     main_window_edit_preferences_cb                (GtkWidget                *widget,
152                                                                 EmpathyMainWindow        *window);
153 static void     main_window_help_about_cb                      (GtkWidget                *widget,
154                                                                 EmpathyMainWindow        *window);
155 static void     main_window_help_contents_cb                   (GtkWidget                *widget,
156                                                                 EmpathyMainWindow        *window);
157 static gboolean main_window_throbber_button_press_event_cb     (GtkWidget                *throbber_ebox,
158                                                                 GdkEventButton           *event,
159                                                                 EmpathyMainWindow        *window);
160 static void     main_window_update_status                      (EmpathyMainWindow        *window,
161                                                                 EmpathyAccountManager    *manager);
162 static void     main_window_error_display                      (EmpathyMainWindow        *window,
163                                                                 McAccount                *account,
164                                                                 const gchar              *message);
165 static void     main_window_accels_load                        (void);
166 static void     main_window_accels_save                        (void);
167 static void     main_window_connection_items_setup             (EmpathyMainWindow        *window,
168                                                                 GladeXML                 *glade);
169 static gboolean main_window_configure_event_timeout_cb         (EmpathyMainWindow        *window);
170 static gboolean main_window_configure_event_cb                 (GtkWidget                *widget,
171                                                                 GdkEventConfigure        *event,
172                                                                 EmpathyMainWindow        *window);
173 static void     main_window_notify_show_offline_cb             (EmpathyConf              *conf,
174                                                                 const gchar              *key,
175                                                                 gpointer                  check_menu_item);
176 static void     main_window_notify_show_avatars_cb             (EmpathyConf              *conf,
177                                                                 const gchar              *key,
178                                                                 EmpathyMainWindow        *window);
179 static void     main_window_notify_compact_contact_list_cb     (EmpathyConf              *conf,
180                                                                 const gchar              *key,
181                                                                 EmpathyMainWindow        *window);
182 static void     main_window_notify_sort_criterium_cb           (EmpathyConf              *conf,
183                                                                 const gchar              *key,
184                                                                 EmpathyMainWindow        *window);
185 static void     main_window_account_created_or_deleted_cb      (EmpathyAccountManager    *manager,
186                                                                 McAccount                *account,
187                                                                 EmpathyMainWindow        *window);
188
189 static void
190 main_window_flash_stop (EmpathyMainWindow *window)
191 {
192         if (window->flash_timeout_id == 0) {
193                 return;
194         }
195
196         DEBUG ("Stop flashing");
197         g_source_remove (window->flash_timeout_id);
198         window->flash_timeout_id = 0;
199         window->flash_on = FALSE;
200 }
201
202 typedef struct {
203         EmpathyEvent *event;
204         gboolean      on;
205 } FlashForeachData;
206
207 static gboolean
208 main_window_flash_foreach (GtkTreeModel *model,
209                            GtkTreePath  *path,
210                            GtkTreeIter  *iter,
211                            gpointer      user_data)
212 {
213         FlashForeachData *data = (FlashForeachData*) user_data;
214         EmpathyContact   *contact;
215         const gchar      *icon_name;
216         GtkTreePath      *parent_path = NULL;
217         GtkTreeIter       parent_iter;
218
219         /* To be used with gtk_tree_model_foreach, update the status icon
220          * of the contact to show the event icon (on=TRUE) or the presence
221          * (on=FALSE) */
222         gtk_tree_model_get (model, iter,
223                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
224                             -1);
225
226         if (contact != data->event->contact) {
227                 if (contact) {
228                         g_object_unref (contact);
229                 }
230                 return FALSE;
231         }
232
233         if (data->on) {
234                 icon_name = data->event->icon_name;
235         } else {
236                 icon_name = empathy_icon_name_for_contact (contact);
237         }
238
239         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
240                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
241                             -1);
242
243         /* To make sure the parent is shown correctly, we emit
244          * the row-changed signal on the parent so it prompts
245          * it to be refreshed by the filter func. 
246          */
247         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
248                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
249         }
250         if (parent_path) {
251                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
252                 gtk_tree_path_free (parent_path);
253         }
254
255         g_object_unref (contact);
256
257         return FALSE;
258 }
259
260 static gboolean
261 main_window_flash_cb (EmpathyMainWindow *window)
262 {
263         GtkTreeModel     *model;
264         GSList           *events, *l;
265         gboolean          found_event = FALSE;
266         FlashForeachData  data;
267
268         window->flash_on = !window->flash_on;
269         data.on = window->flash_on;
270         model = GTK_TREE_MODEL (window->list_store);
271
272         events = empathy_event_manager_get_events (window->event_manager);
273         for (l = events; l; l = l->next) {
274                 data.event = l->data;
275                 if (!data.event->contact) {
276                         continue;
277                 }
278
279                 found_event = TRUE;
280                 gtk_tree_model_foreach (model,
281                                         main_window_flash_foreach,
282                                         &data);
283         }
284
285         if (!found_event) {
286                 main_window_flash_stop (window);
287         }
288
289         return TRUE;
290 }
291
292 static void
293 main_window_flash_start (EmpathyMainWindow *window)
294 {
295         if (window->flash_timeout_id != 0) {
296                 return;
297         }
298
299         DEBUG ("Start flashing");
300         window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
301                                                   (GSourceFunc) main_window_flash_cb,
302                                                   window);
303         main_window_flash_cb (window);
304 }
305
306 static void
307 main_window_event_added_cb (EmpathyEventManager *manager,
308                             EmpathyEvent        *event,
309                             EmpathyMainWindow   *window)
310 {
311         if (event->contact) {
312                 main_window_flash_start (window);
313         }
314 }
315
316 static void
317 main_window_event_removed_cb (EmpathyEventManager *manager,
318                               EmpathyEvent        *event,
319                               EmpathyMainWindow   *window)
320 {
321         FlashForeachData data;
322
323         if (!event->contact) {
324                 return;
325         }
326
327         data.on = FALSE;
328         data.event = event;
329         gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
330                                 main_window_flash_foreach,
331                                 &data);
332 }
333
334 static void
335 main_window_row_activated_cb (EmpathyContactListView *view,
336                               GtkTreePath            *path,
337                               GtkTreeViewColumn      *col,
338                               EmpathyMainWindow      *window)
339 {
340         EmpathyContact *contact;
341         GtkTreeModel   *model;
342         GtkTreeIter     iter;
343         GSList         *events, *l;
344
345         model = GTK_TREE_MODEL (window->list_store);
346         gtk_tree_model_get_iter (model, &iter, path);
347         gtk_tree_model_get (model, &iter,
348                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
349                             -1);
350
351         if (!contact) {
352                 return;
353         }
354
355         /* If the contact has an event activate it, otherwise the
356          * default handler of row-activated will be called. */
357         events = empathy_event_manager_get_events (window->event_manager);
358         for (l = events; l; l = l->next) {
359                 EmpathyEvent *event = l->data;
360
361                 if (event->contact == contact) {
362                         DEBUG ("Activate event");
363                         empathy_event_activate (event);
364
365                         /* We don't want the default handler of this signal
366                          * (e.g. open a chat) */
367                         g_signal_stop_emission_by_name (view, "row-activated");
368                         break;
369                 }
370         }
371
372         g_object_unref (contact);
373 }
374
375 static void
376 main_window_connection_changed_cb (EmpathyAccountManager *manager,
377                                    McAccount *account,
378                                    TpConnectionStatusReason reason,
379                                    TpConnectionStatus current,
380                                    TpConnectionStatus previous,
381                                    EmpathyMainWindow *window)
382 {
383         main_window_update_status (window, manager);
384
385         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
386             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
387                 const gchar *message;
388
389                 switch (reason) {
390                 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
391                         message = _("No error specified");
392                         break;
393                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
394                         message = _("Network error");
395                         break;
396                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
397                         message = _("Authentication failed");
398                         break;
399                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
400                         message = _("Encryption error");
401                         break;
402                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
403                         message = _("Name in use");
404                         break;
405                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
406                         message = _("Certificate not provided");
407                         break;
408                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
409                         message = _("Certificate untrusted");
410                         break;
411                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
412                         message = _("Certificate expired");
413                         break;
414                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
415                         message = _("Certificate not activated");
416                         break;
417                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
418                         message = _("Certificate hostname mismatch");
419                         break;
420                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
421                         message = _("Certificate fingerprint mismatch");
422                         break;
423                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
424                         message = _("Certificate self-signed");
425                         break;
426                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
427                         message = _("Certificate error");
428                         break;
429                 default:
430                         message = _("Unknown error");
431                         break;
432                 }
433
434                 main_window_error_display (window, account, message);
435         }
436
437         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
438                 empathy_sound_play (GTK_WIDGET (window->window),
439                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
440         }
441
442         if (current == TP_CONNECTION_STATUS_CONNECTED) {
443                 GtkWidget *error_widget;
444
445                 empathy_sound_play (GTK_WIDGET (window->window),
446                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
447
448                 /* Account connected without error, remove error message if any */
449                 error_widget = g_hash_table_lookup (window->errors, account);
450                 if (error_widget) {
451                         gtk_widget_destroy (error_widget);
452                         g_hash_table_remove (window->errors, account);
453                 }
454         }
455 }
456
457 static void
458 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
459                                          EmpathyContact *contact,
460                                          McPresence current,
461                                          McPresence previous,
462                                          EmpathyMainWindow *window)
463 {
464         McAccount *account;
465         gboolean should_play;
466
467         account = empathy_contact_get_account (contact);
468         should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
469
470         if (!should_play) {
471                 return;
472         }
473
474         if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
475                 /* someone is logging in */
476                 empathy_sound_play (GTK_WIDGET (window->window),
477                                     EMPATHY_SOUND_CONTACT_CONNECTED);
478                 return;
479         }
480
481         if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
482                 /* someone is logging off */
483                 empathy_sound_play (GTK_WIDGET (window->window),
484                                     EMPATHY_SOUND_CONTACT_DISCONNECTED);
485         }
486 }
487
488 GtkWidget *
489 empathy_main_window_get (void)
490 {
491   return window != NULL ? window->window : NULL;
492 }
493
494 GtkWidget *
495 empathy_main_window_show (void)
496 {
497         EmpathyContactList       *list_iface;
498         EmpathyContactMonitor    *monitor;
499         GladeXML                 *glade;
500         EmpathyConf               *conf;
501         GtkWidget                *sw;
502         GtkWidget                *show_offline_widget;
503         GtkWidget                *ebox;
504         GtkToolItem              *item;
505         gboolean                  show_offline;
506         gboolean                  show_avatars;
507         gboolean                  compact_contact_list;
508         gint                      x, y, w, h;
509         gchar                    *filename;
510         GSList                   *l;
511
512         if (window) {
513                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
514                 return window->window;
515         }
516
517         window = g_new0 (EmpathyMainWindow, 1);
518
519         /* Set up interface */
520         filename = empathy_file_lookup ("empathy-main-window.glade", "src");
521         glade = empathy_glade_get_file (filename,
522                                        "main_window",
523                                        NULL,
524                                        "main_window", &window->window,
525                                        "main_vbox", &window->main_vbox,
526                                        "errors_vbox", &window->errors_vbox,
527                                        "chat_show_offline", &show_offline_widget,
528                                        "room", &window->room,
529                                        "room_sep", &window->room_sep,
530                                        "room_join_favorites", &window->room_join_favorites,
531                                        "edit_context", &window->edit_context,
532                                        "edit_context_separator", &window->edit_context_separator,
533                                        "presence_toolbar", &window->presence_toolbar,
534                                        "roster_scrolledwindow", &sw,
535                                        "chat_history", &window->chat_history_menu_item,
536                                        NULL);
537         g_free (filename);
538
539         empathy_glade_connect (glade,
540                               window,
541                               "main_window", "destroy", main_window_destroy_cb,
542                               "main_window", "configure_event", main_window_configure_event_cb,
543                               "chat_quit", "activate", main_window_chat_quit_cb,
544                               "chat_new_message", "activate", main_window_chat_new_message_cb,
545                               "chat_history", "activate", main_window_chat_history_cb,
546                               "room_join_new", "activate", main_window_room_join_new_cb,
547                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
548                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
549                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
550                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
551                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
552                               "edit_accounts", "activate", main_window_edit_accounts_cb,
553                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
554                               "edit_preferences", "activate", main_window_edit_preferences_cb,
555                               "help_about", "activate", main_window_help_about_cb,
556                               "help_contents", "activate", main_window_help_contents_cb,
557                               NULL);
558
559         /* Set up connection related widgets. */
560         main_window_connection_items_setup (window, glade);
561         g_object_unref (glade);
562
563         window->mc = empathy_mission_control_dup_singleton ();
564         window->account_manager = empathy_account_manager_dup_singleton ();
565
566         g_signal_connect (window->account_manager,
567                           "account-connection-changed",
568                           G_CALLBACK (main_window_connection_changed_cb), window);
569
570         window->errors = g_hash_table_new_full (empathy_account_hash,
571                                                 empathy_account_equal,
572                                                 g_object_unref,
573                                                 NULL);
574
575         /* Set up menu */
576         main_window_favorite_chatroom_menu_setup (window);
577
578         gtk_widget_hide (window->edit_context);
579         gtk_widget_hide (window->edit_context_separator);
580
581         /* Set up presence chooser */
582         window->presence_chooser = empathy_presence_chooser_new ();
583         gtk_widget_show (window->presence_chooser);
584         item = gtk_tool_item_new ();
585         gtk_widget_show (GTK_WIDGET (item));
586         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
587         gtk_tool_item_set_is_important (item, TRUE);
588         gtk_tool_item_set_expand (item, TRUE);
589         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
590
591         /* Set up the throbber */
592         ebox = gtk_event_box_new ();
593         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
594         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
595         g_signal_connect (ebox,
596                           "button-press-event",
597                           G_CALLBACK (main_window_throbber_button_press_event_cb),
598                           window);
599         gtk_widget_show (ebox);
600
601         window->throbber = ephy_spinner_new ();
602         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
603         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
604         gtk_widget_show (window->throbber);
605
606         item = gtk_tool_item_new ();
607         gtk_container_add (GTK_CONTAINER (item), ebox);
608         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
609         gtk_widget_show (GTK_WIDGET (item));
610
611         /* Set up contact list. */
612         empathy_status_presets_get_all ();
613
614         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
615         monitor = empathy_contact_list_get_monitor (list_iface);
616         window->list_store = empathy_contact_list_store_new (list_iface);
617         window->list_view = empathy_contact_list_view_new (window->list_store,
618                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
619                                                            EMPATHY_CONTACT_FEATURE_ALL);
620         g_signal_connect (monitor, "contact-presence-changed",
621                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
622         g_object_unref (list_iface);
623
624         gtk_widget_show (GTK_WIDGET (window->list_view));
625         gtk_container_add (GTK_CONTAINER (sw),
626                            GTK_WIDGET (window->list_view));
627         g_signal_connect (window->list_view, "row-activated",
628                           G_CALLBACK (main_window_row_activated_cb),
629                           window);
630
631         /* Load user-defined accelerators. */
632         main_window_accels_load ();
633
634         /* Set window size. */
635         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
636
637         if (w >= 1 && h >= 1) {
638                 /* Use the defaults from the glade file if we
639                  * don't have good w, h geometry.
640                  */
641                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
642                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
643         }
644
645         if (x >= 0 && y >= 0) {
646                 /* Let the window manager position it if we
647                  * don't have good x, y coordinates.
648                  */
649                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
650                 gtk_window_move (GTK_WINDOW (window->window), x, y);
651         }
652
653         /* Enable event handling */
654         window->event_manager = empathy_event_manager_dup_singleton ();
655         g_signal_connect (window->event_manager, "event-added",
656                           G_CALLBACK (main_window_event_added_cb),
657                           window);
658         g_signal_connect (window->event_manager, "event-removed",
659                           G_CALLBACK (main_window_event_removed_cb),
660                           window);
661
662         g_signal_connect (window->account_manager, "account-created",
663                           G_CALLBACK (main_window_account_created_or_deleted_cb),
664                           window);
665         g_signal_connect (window->account_manager, "account-deleted",
666                           G_CALLBACK (main_window_account_created_or_deleted_cb),
667                           window);
668         main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
669
670         l = empathy_event_manager_get_events (window->event_manager);
671         while (l) {
672                 main_window_event_added_cb (window->event_manager,
673                                             l->data, window);
674                 l = l->next;
675         }
676
677         conf = empathy_conf_get ();
678
679         /* Show offline ? */
680         empathy_conf_get_bool (conf,
681                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
682                               &show_offline);
683         empathy_conf_notify_add (conf,
684                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
685                                 main_window_notify_show_offline_cb,
686                                 show_offline_widget);
687
688         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
689                                         show_offline);
690
691         /* Show avatars ? */
692         empathy_conf_get_bool (conf,
693                               EMPATHY_PREFS_UI_SHOW_AVATARS,
694                               &show_avatars);
695         empathy_conf_notify_add (conf,
696                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
697                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
698                                 window);
699         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
700
701         /* Is compact ? */
702         empathy_conf_get_bool (conf,
703                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
704                               &compact_contact_list);
705         empathy_conf_notify_add (conf,
706                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
707                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
708                                 window);
709         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
710
711         /* Sort criterium */
712         empathy_conf_notify_add (conf,
713                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
714                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
715                                 window);
716         main_window_notify_sort_criterium_cb (conf,
717                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
718                                               window);
719
720         main_window_update_status (window, window->account_manager);
721
722         return window->window;
723 }
724
725 static void
726 main_window_destroy_cb (GtkWidget         *widget,
727                         EmpathyMainWindow *window)
728 {
729         /* Save user-defined accelerators. */
730         main_window_accels_save ();
731
732         g_signal_handlers_disconnect_by_func (window->account_manager,
733                                               main_window_connection_changed_cb,
734                                               window);
735
736         if (window->size_timeout_id) {
737                 g_source_remove (window->size_timeout_id);
738         }
739
740         g_list_free (window->widgets_connected);
741
742         g_object_unref (window->mc);
743         g_object_unref (window->account_manager);
744         g_object_unref (window->list_store);
745         g_hash_table_destroy (window->errors);
746
747         g_signal_handlers_disconnect_by_func (window->event_manager,
748                                               main_window_event_added_cb,
749                                               window);
750         g_signal_handlers_disconnect_by_func (window->event_manager,
751                                               main_window_event_removed_cb,
752                                               window);
753         g_object_unref (window->event_manager);
754
755         g_free (window);
756 }
757
758 static void
759 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
760 {
761         GList *chatrooms, *l;
762
763         window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
764         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
765         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
766
767         for (l = chatrooms; l; l = l->next) {
768                 main_window_favorite_chatroom_menu_add (window, l->data);
769         }
770
771         if (!chatrooms) {
772                 gtk_widget_hide (window->room_sep);
773         }
774
775         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
776
777         g_signal_connect (window->chatroom_manager, "chatroom-added",
778                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
779                           window);
780         g_signal_connect (window->chatroom_manager, "chatroom-removed",
781                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
782                           window);
783
784         g_list_free (chatrooms);
785 }
786
787 static void
788 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
789                                              EmpathyChatroom        *chatroom,
790                                              EmpathyMainWindow     *window)
791 {
792         main_window_favorite_chatroom_menu_add (window, chatroom);
793         gtk_widget_show (window->room_sep);
794         gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
795 }
796
797 static void
798 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
799                                                EmpathyChatroom        *chatroom,
800                                                EmpathyMainWindow     *window)
801 {
802         GtkWidget *menu_item;
803
804         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
805
806         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
807         gtk_widget_destroy (menu_item);
808
809         main_window_favorite_chatroom_menu_update (window);
810 }
811
812 static void
813 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
814                                                 EmpathyChatroom *chatroom)
815 {
816         main_window_favorite_chatroom_join (chatroom);
817 }
818
819 static void
820 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
821 {
822         GList *chatrooms;
823
824         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
825
826         if (chatrooms) {
827                 gtk_widget_show (window->room_sep);
828         } else {
829                 gtk_widget_hide (window->room_sep);
830         }
831
832         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
833         g_list_free (chatrooms);
834 }
835
836 static void
837 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
838                                         EmpathyChatroom    *chatroom)
839 {
840         GtkWidget   *menu_item;
841         const gchar *name;
842
843         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
844                 return;
845         }
846
847         name = empathy_chatroom_get_name (chatroom);
848         menu_item = gtk_menu_item_new_with_label (name);
849
850         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
851         g_signal_connect (menu_item, "activate",
852                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
853                           chatroom);
854
855         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
856                                menu_item, 3);
857
858         gtk_widget_show (menu_item);
859 }
860
861 static void
862 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
863 {
864         MissionControl *mc;
865         McAccount      *account;
866         const gchar    *room;
867
868         mc = empathy_mission_control_dup_singleton ();
869         account = empathy_chatroom_get_account (chatroom);
870         room = empathy_chatroom_get_room (chatroom);
871
872          if (mission_control_get_connection_status (mc, account, NULL) !=
873                         TP_CONNECTION_STATUS_CONNECTED) {
874                 return;
875         }
876
877         DEBUG ("Requesting channel for '%s'", room);
878         empathy_dispatcher_join_muc (account, room, NULL, NULL);
879
880         g_object_unref (mc);
881 }
882
883 static void
884 main_window_chat_quit_cb (GtkWidget         *widget,
885                           EmpathyMainWindow *window)
886 {
887         gtk_main_quit ();
888 }
889
890 static void
891 main_window_chat_new_message_cb (GtkWidget         *widget,
892                                  EmpathyMainWindow *window)
893 {
894         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
895 }
896
897 static void
898 main_window_chat_history_cb (GtkWidget         *widget,
899                              EmpathyMainWindow *window)
900 {
901         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
902 }
903
904 static void
905 main_window_room_join_new_cb (GtkWidget         *widget,
906                               EmpathyMainWindow *window)
907 {
908         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
909 }
910
911 static void
912 main_window_room_join_favorites_cb (GtkWidget         *widget,
913                                     EmpathyMainWindow *window)
914 {
915         GList *chatrooms, *l;
916
917         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
918         for (l = chatrooms; l; l = l->next) {
919                 main_window_favorite_chatroom_join (l->data);
920         }
921         g_list_free (chatrooms);
922 }
923
924 static void
925 main_window_room_manage_favorites_cb (GtkWidget         *widget,
926                                       EmpathyMainWindow *window)
927 {
928         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
929 }
930
931 static void
932 main_window_chat_add_contact_cb (GtkWidget         *widget,
933                                  EmpathyMainWindow *window)
934 {
935         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
936 }
937
938 static void
939 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
940                                   EmpathyMainWindow *window)
941 {
942         gboolean current;
943
944         current = gtk_check_menu_item_get_active (item);
945
946         empathy_conf_set_bool (empathy_conf_get (),
947                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
948                               current);
949
950         /* Turn off sound just while we alter the contact list. */
951         // FIXME: empathy_sound_set_enabled (FALSE);
952         empathy_contact_list_store_set_show_offline (window->list_store, current);
953         //empathy_sound_set_enabled (TRUE);
954 }
955
956 static gboolean
957 main_window_edit_button_press_event_cb (GtkWidget         *widget,
958                                         GdkEventButton    *event,
959                                         EmpathyMainWindow *window)
960 {
961         GtkWidget *submenu;
962
963         if (!event->button == 1) {
964                 return FALSE;
965         }
966
967         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
968         if (submenu) {
969                 GtkMenuItem *item;
970                 GtkWidget   *label;
971
972                 item = GTK_MENU_ITEM (window->edit_context);
973                 label = gtk_bin_get_child (GTK_BIN (item));
974                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
975
976                 gtk_widget_show (window->edit_context);
977                 gtk_widget_show (window->edit_context_separator);
978
979                 gtk_menu_item_set_submenu (item, submenu);
980
981                 return FALSE;
982         }
983
984         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
985         if (submenu) {
986                 GtkMenuItem *item;
987                 GtkWidget   *label;
988
989                 item = GTK_MENU_ITEM (window->edit_context);
990                 label = gtk_bin_get_child (GTK_BIN (item));
991                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
992
993                 gtk_widget_show (window->edit_context);
994                 gtk_widget_show (window->edit_context_separator);
995
996                 gtk_menu_item_set_submenu (item, submenu);
997
998                 return FALSE;
999         }
1000
1001         gtk_widget_hide (window->edit_context);
1002         gtk_widget_hide (window->edit_context_separator);
1003
1004         return FALSE;
1005 }
1006
1007 static void
1008 main_window_edit_accounts_cb (GtkWidget         *widget,
1009                               EmpathyMainWindow *window)
1010 {
1011         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1012 }
1013
1014 static void
1015 main_window_edit_personal_information_cb (GtkWidget         *widget,
1016                                           EmpathyMainWindow *window)
1017 {
1018         GSList *accounts;
1019
1020         accounts = mission_control_get_online_connections (window->mc, NULL);
1021         if (accounts) {
1022                 EmpathyContactFactory *factory;
1023                 EmpathyContact        *contact;
1024                 McAccount             *account;
1025
1026                 account = accounts->data;
1027                 factory = empathy_contact_factory_dup_singleton ();
1028                 contact = empathy_contact_factory_get_user (factory, account);
1029                 empathy_contact_run_until_ready (contact,
1030                                                  EMPATHY_CONTACT_READY_HANDLE |
1031                                                  EMPATHY_CONTACT_READY_ID,
1032                                                  NULL);
1033
1034                 empathy_contact_information_dialog_show (contact,
1035                                                          GTK_WINDOW (window->window),
1036                                                          TRUE, TRUE);
1037
1038                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
1039                 g_slist_free (accounts);
1040                 g_object_unref (factory);
1041                 g_object_unref (contact);
1042         }
1043 }
1044
1045 static void
1046 main_window_edit_preferences_cb (GtkWidget         *widget,
1047                                  EmpathyMainWindow *window)
1048 {
1049         empathy_preferences_show (GTK_WINDOW (window->window));
1050 }
1051
1052 static void
1053 main_window_help_about_cb (GtkWidget         *widget,
1054                            EmpathyMainWindow *window)
1055 {
1056         empathy_about_dialog_new (GTK_WINDOW (window->window));
1057 }
1058
1059 static void
1060 main_window_help_contents_cb (GtkWidget         *widget,
1061                               EmpathyMainWindow *window)
1062 {
1063         empathy_url_show (widget, "ghelp:empathy");
1064 }
1065
1066 static gboolean
1067 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
1068                                             GdkEventButton    *event,
1069                                             EmpathyMainWindow *window)
1070 {
1071         if (event->type != GDK_BUTTON_PRESS ||
1072             event->button != 1) {
1073                 return FALSE;
1074         }
1075
1076         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1077
1078         return FALSE;
1079 }
1080
1081 static void
1082 main_window_error_edit_clicked_cb (GtkButton         *button,
1083                                    EmpathyMainWindow *window)
1084 {
1085         McAccount *account;
1086         GtkWidget *error_widget;
1087
1088         account = g_object_get_data (G_OBJECT (button), "account");
1089         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
1090
1091         error_widget = g_hash_table_lookup (window->errors, account);
1092         gtk_widget_destroy (error_widget);
1093         g_hash_table_remove (window->errors, account);
1094 }
1095
1096 static void
1097 main_window_error_clear_clicked_cb (GtkButton         *button,
1098                                     EmpathyMainWindow *window)
1099 {
1100         McAccount *account;
1101         GtkWidget *error_widget;
1102
1103         account = g_object_get_data (G_OBJECT (button), "account");
1104         error_widget = g_hash_table_lookup (window->errors, account);
1105         gtk_widget_destroy (error_widget);
1106         g_hash_table_remove (window->errors, account);
1107 }
1108
1109 static void
1110 main_window_error_display (EmpathyMainWindow *window,
1111                            McAccount         *account,
1112                            const gchar       *message)
1113 {
1114         GtkWidget *child;
1115         GtkWidget *table;
1116         GtkWidget *image;
1117         GtkWidget *button_edit;
1118         GtkWidget *alignment;
1119         GtkWidget *hbox;
1120         GtkWidget *label;
1121         GtkWidget *fixed;
1122         GtkWidget *vbox;
1123         GtkWidget *button_close;
1124         gchar     *str;
1125
1126         child = g_hash_table_lookup (window->errors, account);
1127         if (child) {
1128                 label = g_object_get_data (G_OBJECT (child), "label");
1129
1130                 /* Just set the latest error and return */
1131                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1132                                                mc_account_get_display_name (account),
1133                                                message);
1134                 gtk_label_set_markup (GTK_LABEL (label), str);
1135                 g_free (str);
1136
1137                 return;
1138         }
1139
1140         child = gtk_vbox_new (FALSE, 0);
1141         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
1142         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
1143         gtk_widget_show (child);
1144
1145         table = gtk_table_new (2, 4, FALSE);
1146         gtk_widget_show (table);
1147         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
1148         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1149         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
1150
1151         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
1152         gtk_widget_show (image);
1153         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
1154                           (GtkAttachOptions) (GTK_FILL),
1155                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1156         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
1157
1158         button_edit = gtk_button_new ();
1159         gtk_widget_show (button_edit);
1160         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
1161                           (GtkAttachOptions) (GTK_FILL),
1162                           (GtkAttachOptions) (0), 0, 0);
1163
1164         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1165         gtk_widget_show (alignment);
1166         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
1167
1168         hbox = gtk_hbox_new (FALSE, 2);
1169         gtk_widget_show (hbox);
1170         gtk_container_add (GTK_CONTAINER (alignment), hbox);
1171
1172         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
1173         gtk_widget_show (image);
1174         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1175
1176         label = gtk_label_new_with_mnemonic (_("_Edit account"));
1177         gtk_widget_show (label);
1178         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1179
1180         fixed = gtk_fixed_new ();
1181         gtk_widget_show (fixed);
1182         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
1183                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1184                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1185
1186         vbox = gtk_vbox_new (FALSE, 6);
1187         gtk_widget_show (vbox);
1188         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
1189                           (GtkAttachOptions) (GTK_FILL),
1190                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1191
1192         button_close = gtk_button_new ();
1193         gtk_widget_show (button_close);
1194         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
1195         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
1196
1197
1198         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
1199         gtk_widget_show (image);
1200         gtk_container_add (GTK_CONTAINER (button_close), image);
1201
1202         label = gtk_label_new ("");
1203         gtk_widget_show (label);
1204         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
1205                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
1206                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
1207         gtk_widget_set_size_request (label, 175, -1);
1208         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1209         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1210         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1211
1212         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1213                                        mc_account_get_display_name (account),
1214                                        message);
1215         gtk_label_set_markup (GTK_LABEL (label), str);
1216         g_free (str);
1217
1218         g_object_set_data (G_OBJECT (child), "label", label);
1219         g_object_set_data_full (G_OBJECT (button_edit),
1220                                 "account", g_object_ref (account),
1221                                 g_object_unref);
1222         g_object_set_data_full (G_OBJECT (button_close),
1223                                 "account", g_object_ref (account),
1224                                 g_object_unref);
1225
1226         g_signal_connect (button_edit, "clicked",
1227                           G_CALLBACK (main_window_error_edit_clicked_cb),
1228                           window);
1229
1230         g_signal_connect (button_close, "clicked",
1231                           G_CALLBACK (main_window_error_clear_clicked_cb),
1232                           window);
1233
1234         gtk_widget_show (window->errors_vbox);
1235
1236         g_hash_table_insert (window->errors, g_object_ref (account), child);
1237 }
1238
1239 static void
1240 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
1241 {
1242         int  connected;
1243         int  connecting;
1244         GList *l;
1245
1246         /* Count number of connected/connecting/disconnected accounts */
1247         connected = empathy_account_manager_get_connected_accounts (manager);
1248         connecting = empathy_account_manager_get_connecting_accounts (manager);
1249
1250         /* Update the spinner state */
1251         if (connecting > 0) {
1252                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
1253         } else {
1254                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
1255         }
1256
1257         /* Update widgets sensibility */
1258         for (l = window->widgets_connected; l; l = l->next) {
1259                 gtk_widget_set_sensitive (l->data, (connected > 0));
1260         }
1261 }
1262
1263 /*
1264  * Accels
1265  */
1266 static void
1267 main_window_accels_load (void)
1268 {
1269         gchar *filename;
1270
1271         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1272         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1273                 DEBUG ("Loading from:'%s'", filename);
1274                 gtk_accel_map_load (filename);
1275         }
1276
1277         g_free (filename);
1278 }
1279
1280 static void
1281 main_window_accels_save (void)
1282 {
1283         gchar *dir;
1284         gchar *file_with_path;
1285
1286         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1287         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1288         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1289         g_free (dir);
1290
1291         DEBUG ("Saving to:'%s'", file_with_path);
1292         gtk_accel_map_save (file_with_path);
1293
1294         g_free (file_with_path);
1295 }
1296
1297 static void
1298 main_window_connection_items_setup (EmpathyMainWindow *window,
1299                                     GladeXML          *glade)
1300 {
1301         GList         *list;
1302         GtkWidget     *w;
1303         gint           i;
1304         const gchar *widgets_connected[] = {
1305                 "room",
1306                 "chat_new_message",
1307                 "chat_add_contact",
1308                 "edit_personal_information"
1309         };
1310
1311         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1312                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1313                 list = g_list_prepend (list, w);
1314         }
1315
1316         window->widgets_connected = list;
1317 }
1318
1319 static gboolean
1320 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1321 {
1322         gint x, y, w, h;
1323
1324         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1325         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1326
1327         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1328
1329         window->size_timeout_id = 0;
1330
1331         return FALSE;
1332 }
1333
1334 static gboolean
1335 main_window_configure_event_cb (GtkWidget         *widget,
1336                                 GdkEventConfigure *event,
1337                                 EmpathyMainWindow *window)
1338 {
1339         if (window->size_timeout_id) {
1340                 g_source_remove (window->size_timeout_id);
1341         }
1342
1343         window->size_timeout_id = g_timeout_add_seconds (1,
1344                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1345                                                          window);
1346
1347         return FALSE;
1348 }
1349
1350 static void
1351 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1352                                     const gchar *key,
1353                                     gpointer     check_menu_item)
1354 {
1355         gboolean show_offline;
1356
1357         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1358                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1359                                                 show_offline);
1360         }
1361 }
1362
1363 static void
1364 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1365                                     const gchar       *key,
1366                                     EmpathyMainWindow *window)
1367 {
1368         gboolean show_avatars;
1369
1370         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1371                 empathy_contact_list_store_set_show_avatars (window->list_store,
1372                                                             show_avatars);
1373         }
1374 }
1375
1376 static void
1377 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1378                                             const gchar       *key,
1379                                             EmpathyMainWindow *window)
1380 {
1381         gboolean compact_contact_list;
1382
1383         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1384                 empathy_contact_list_store_set_is_compact (window->list_store,
1385                                                           compact_contact_list);
1386         }
1387 }
1388
1389 static void
1390 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1391                                       const gchar       *key,
1392                                       EmpathyMainWindow *window)
1393 {
1394         gchar *str = NULL;
1395
1396         if (empathy_conf_get_string (conf, key, &str) && str) {
1397                 GType       type;
1398                 GEnumClass *enum_class;
1399                 GEnumValue *enum_value;
1400
1401                 type = empathy_contact_list_store_sort_get_type ();
1402                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1403                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1404                 g_free (str);
1405
1406                 if (enum_value) {
1407                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1408                                                                        enum_value->value);
1409                 }
1410         }
1411 }
1412
1413 static void
1414 main_window_account_created_or_deleted_cb (EmpathyAccountManager  *manager,
1415                                            McAccount              *account,
1416                                            EmpathyMainWindow      *window)
1417 {
1418         gtk_widget_set_sensitive (GTK_WIDGET (window->chat_history_menu_item),
1419                 empathy_account_manager_get_count (manager) > 0);
1420 }