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