]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Use new singleton policy for EmpathyAccountManager.
[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         McAccount *account;
471         gboolean should_play;
472
473         account = empathy_contact_get_account (contact);
474         should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
475
476         if (!should_play) {
477                 return;
478         }
479
480         if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
481                 /* someone is logging in */
482                 if (empathy_sound_pref_is_enabled (EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN)) {
483                         ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
484                                                 CA_PROP_EVENT_ID, "service-login",
485                                                 CA_PROP_EVENT_DESCRIPTION, _("Contact logged in"),
486                                                 NULL);
487                 }
488                 return;
489         }
490
491         if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
492                 /* someone is logging off */
493                 if (empathy_sound_pref_is_enabled (EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT)) {
494                         ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
495                                                 CA_PROP_EVENT_ID, "service-logout",
496                                                 CA_PROP_EVENT_DESCRIPTION, _("Contact logged out"),
497                                                 NULL);
498                 }
499         }
500 }
501
502 GtkWidget *
503 empathy_main_window_show (void)
504 {
505         static EmpathyMainWindow *window = NULL;
506         EmpathyContactList       *list_iface;
507         EmpathyContactMonitor    *monitor;
508         GladeXML                 *glade;
509         EmpathyConf               *conf;
510         GtkWidget                *sw;
511         GtkWidget                *show_offline_widget;
512         GtkWidget                *ebox;
513         GtkToolItem              *item;
514         gboolean                  show_offline;
515         gboolean                  show_avatars;
516         gboolean                  compact_contact_list;
517         gint                      x, y, w, h;
518         gchar                    *filename;
519         GSList                   *l;
520
521         if (window) {
522                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
523                 return window->window;
524         }
525
526         window = g_new0 (EmpathyMainWindow, 1);
527
528         /* Set up interface */
529         filename = empathy_file_lookup ("empathy-main-window.glade", "src");
530         glade = empathy_glade_get_file (filename,
531                                        "main_window",
532                                        NULL,
533                                        "main_window", &window->window,
534                                        "main_vbox", &window->main_vbox,
535                                        "errors_vbox", &window->errors_vbox,
536                                        "chat_show_offline", &show_offline_widget,
537                                        "room", &window->room,
538                                        "room_sep", &window->room_sep,
539                                        "room_join_favorites", &window->room_join_favorites,
540                                        "edit_context", &window->edit_context,
541                                        "edit_context_separator", &window->edit_context_separator,
542                                        "presence_toolbar", &window->presence_toolbar,
543                                        "roster_scrolledwindow", &sw,
544                                        NULL);
545         g_free (filename);
546
547         empathy_glade_connect (glade,
548                               window,
549                               "main_window", "destroy", main_window_destroy_cb,
550                               "main_window", "configure_event", main_window_configure_event_cb,
551                               "chat_quit", "activate", main_window_chat_quit_cb,
552                               "chat_new_message", "activate", main_window_chat_new_message_cb,
553                               "chat_history", "activate", main_window_chat_history_cb,
554                               "room_join_new", "activate", main_window_room_join_new_cb,
555                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
556                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
557                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
558                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
559                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
560                               "edit_accounts", "activate", main_window_edit_accounts_cb,
561                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
562                               "edit_preferences", "activate", main_window_edit_preferences_cb,
563                               "help_about", "activate", main_window_help_about_cb,
564                               "help_contents", "activate", main_window_help_contents_cb,
565                               NULL);
566
567         /* Set up connection related widgets. */
568         main_window_connection_items_setup (window, glade);
569         g_object_unref (glade);
570
571         window->mc = empathy_mission_control_new ();
572         window->account_manager = empathy_account_manager_dup_singleton ();
573
574         g_signal_connect (window->account_manager,
575                           "account-connection-changed",
576                           G_CALLBACK (main_window_connection_changed_cb), window);
577
578         window->errors = g_hash_table_new_full (empathy_account_hash,
579                                                 empathy_account_equal,
580                                                 g_object_unref,
581                                                 NULL);
582
583         /* Set up menu */
584         main_window_favorite_chatroom_menu_setup (window);
585
586         gtk_widget_hide (window->edit_context);
587         gtk_widget_hide (window->edit_context_separator);
588
589         /* Set up presence chooser */
590         window->presence_chooser = empathy_presence_chooser_new ();
591         gtk_widget_show (window->presence_chooser);
592         item = gtk_tool_item_new ();
593         gtk_widget_show (GTK_WIDGET (item));
594         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
595         gtk_tool_item_set_is_important (item, TRUE);
596         gtk_tool_item_set_expand (item, TRUE);
597         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
598
599         /* Set up the throbber */
600         ebox = gtk_event_box_new ();
601         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
602         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
603         g_signal_connect (ebox,
604                           "button-press-event",
605                           G_CALLBACK (main_window_throbber_button_press_event_cb),
606                           window);
607         gtk_widget_show (ebox);
608
609         window->throbber = ephy_spinner_new ();
610         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
611         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
612         gtk_widget_show (window->throbber);
613
614         item = gtk_tool_item_new ();
615         gtk_container_add (GTK_CONTAINER (item), ebox);
616         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
617         gtk_widget_show (GTK_WIDGET (item));
618
619         /* Set up contact list. */
620         empathy_status_presets_get_all ();
621
622         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_new ());
623         monitor = empathy_contact_list_get_monitor (list_iface);
624         window->list_store = empathy_contact_list_store_new (list_iface);
625         window->list_view = empathy_contact_list_view_new (window->list_store,
626                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
627                                                            EMPATHY_CONTACT_FEATURE_ALL);
628         g_signal_connect (monitor, "contact-presence-changed",
629                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
630         g_object_unref (list_iface);
631
632         gtk_widget_show (GTK_WIDGET (window->list_view));
633         gtk_container_add (GTK_CONTAINER (sw),
634                            GTK_WIDGET (window->list_view));
635         g_signal_connect (window->list_view, "row-activated",
636                           G_CALLBACK (main_window_row_activated_cb),
637                           window);
638
639         /* Load user-defined accelerators. */
640         main_window_accels_load ();
641
642         /* Set window size. */
643         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
644
645         if (w >= 1 && h >= 1) {
646                 /* Use the defaults from the glade file if we
647                  * don't have good w, h geometry.
648                  */
649                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
650                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
651         }
652
653         if (x >= 0 && y >= 0) {
654                 /* Let the window manager position it if we
655                  * don't have good x, y coordinates.
656                  */
657                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
658                 gtk_window_move (GTK_WINDOW (window->window), x, y);
659         }
660
661         /* Enable event handling */
662         window->event_manager = empathy_event_manager_new ();
663         g_signal_connect (window->event_manager, "event-added",
664                           G_CALLBACK (main_window_event_added_cb),
665                           window);
666         g_signal_connect (window->event_manager, "event-removed",
667                           G_CALLBACK (main_window_event_removed_cb),
668                           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_new (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         TpConnection   *connection;
867         const gchar    *room;
868
869         mc = empathy_mission_control_new ();
870         account = empathy_chatroom_get_account (chatroom);
871         room = empathy_chatroom_get_room (chatroom);
872
873         DEBUG ("Requesting channel for '%s'", room);
874
875         connection = mission_control_get_tpconnection (mc, account, NULL);
876         if (connection) {
877                 tp_connection_run_until_ready (connection, TRUE, NULL, NULL);   
878                 empathy_connection_request_channel (connection, -1,
879                                                     TP_IFACE_CHANNEL_TYPE_TEXT,
880                                                     TP_HANDLE_TYPE_ROOM,
881                                                     room, TRUE,
882                                                     NULL, NULL, NULL, NULL);
883                 g_object_unref (connection);
884         }
885         g_object_unref (mc);
886 }
887
888 static void
889 main_window_chat_quit_cb (GtkWidget         *widget,
890                           EmpathyMainWindow *window)
891 {
892         gtk_main_quit ();
893 }
894
895 static void
896 main_window_chat_new_message_cb (GtkWidget         *widget,
897                                  EmpathyMainWindow *window)
898 {
899         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
900 }
901
902 static void
903 main_window_chat_history_cb (GtkWidget         *widget,
904                              EmpathyMainWindow *window)
905 {
906         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
907 }
908
909 static void
910 main_window_room_join_new_cb (GtkWidget         *widget,
911                               EmpathyMainWindow *window)
912 {
913         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
914 }
915
916 static void
917 main_window_room_join_favorites_cb (GtkWidget         *widget,
918                                     EmpathyMainWindow *window)
919 {
920         GList *chatrooms, *l;
921
922         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
923         for (l = chatrooms; l; l = l->next) {
924                 main_window_favorite_chatroom_join (l->data);
925         }
926         g_list_free (chatrooms);
927 }
928
929 static void
930 main_window_room_manage_favorites_cb (GtkWidget         *widget,
931                                       EmpathyMainWindow *window)
932 {
933         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
934 }
935
936 static void
937 main_window_chat_add_contact_cb (GtkWidget         *widget,
938                                  EmpathyMainWindow *window)
939 {
940         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
941 }
942
943 static void
944 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
945                                   EmpathyMainWindow *window)
946 {
947         gboolean current;
948
949         current = gtk_check_menu_item_get_active (item);
950
951         empathy_conf_set_bool (empathy_conf_get (),
952                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
953                               current);
954
955         /* Turn off sound just while we alter the contact list. */
956         // FIXME: empathy_sound_set_enabled (FALSE);
957         empathy_contact_list_store_set_show_offline (window->list_store, current);
958         //empathy_sound_set_enabled (TRUE);
959 }
960
961 static gboolean
962 main_window_edit_button_press_event_cb (GtkWidget         *widget,
963                                         GdkEventButton    *event,
964                                         EmpathyMainWindow *window)
965 {
966         GtkWidget *submenu;
967
968         if (!event->button == 1) {
969                 return FALSE;
970         }
971
972         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
973         if (submenu) {
974                 GtkMenuItem *item;
975                 GtkWidget   *label;
976
977                 item = GTK_MENU_ITEM (window->edit_context);
978                 label = gtk_bin_get_child (GTK_BIN (item));
979                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
980
981                 gtk_widget_show (window->edit_context);
982                 gtk_widget_show (window->edit_context_separator);
983
984                 gtk_menu_item_set_submenu (item, submenu);
985
986                 return FALSE;
987         }
988
989         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
990         if (submenu) {
991                 GtkMenuItem *item;
992                 GtkWidget   *label;
993
994                 item = GTK_MENU_ITEM (window->edit_context);
995                 label = gtk_bin_get_child (GTK_BIN (item));
996                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
997
998                 gtk_widget_show (window->edit_context);
999                 gtk_widget_show (window->edit_context_separator);
1000
1001                 gtk_menu_item_set_submenu (item, submenu);
1002
1003                 return FALSE;
1004         }
1005
1006         gtk_widget_hide (window->edit_context);
1007         gtk_widget_hide (window->edit_context_separator);
1008
1009         return FALSE;
1010 }
1011
1012 static void
1013 main_window_edit_accounts_cb (GtkWidget         *widget,
1014                               EmpathyMainWindow *window)
1015 {
1016         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1017 }
1018
1019 static void
1020 main_window_edit_personal_information_cb (GtkWidget         *widget,
1021                                           EmpathyMainWindow *window)
1022 {
1023         GSList *accounts;
1024
1025         accounts = mission_control_get_online_connections (window->mc, NULL);
1026         if (accounts) {
1027                 EmpathyContactFactory *factory;
1028                 EmpathyContact        *contact;
1029                 McAccount             *account;
1030
1031                 account = accounts->data;
1032                 factory = empathy_contact_factory_new ();
1033                 contact = empathy_contact_factory_get_user (factory, account);
1034                 empathy_contact_run_until_ready (contact,
1035                                                  EMPATHY_CONTACT_READY_HANDLE |
1036                                                  EMPATHY_CONTACT_READY_ID,
1037                                                  NULL);
1038
1039                 empathy_contact_information_dialog_show (contact,
1040                                                          GTK_WINDOW (window->window),
1041                                                          TRUE, TRUE);
1042
1043                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
1044                 g_slist_free (accounts);
1045                 g_object_unref (factory);
1046                 g_object_unref (contact);
1047         }
1048 }
1049
1050 static void
1051 main_window_edit_preferences_cb (GtkWidget         *widget,
1052                                  EmpathyMainWindow *window)
1053 {
1054         empathy_preferences_show (GTK_WINDOW (window->window));
1055 }
1056
1057 static void
1058 main_window_help_about_cb (GtkWidget         *widget,
1059                            EmpathyMainWindow *window)
1060 {
1061         empathy_about_dialog_new (GTK_WINDOW (window->window));
1062 }
1063
1064 static void
1065 main_window_help_contents_cb (GtkWidget         *widget,
1066                               EmpathyMainWindow *window)
1067 {
1068         empathy_url_show (widget, "ghelp:empathy");
1069 }
1070
1071 static gboolean
1072 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
1073                                             GdkEventButton    *event,
1074                                             EmpathyMainWindow *window)
1075 {
1076         if (event->type != GDK_BUTTON_PRESS ||
1077             event->button != 1) {
1078                 return FALSE;
1079         }
1080
1081         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1082
1083         return FALSE;
1084 }
1085
1086 static void
1087 main_window_error_edit_clicked_cb (GtkButton         *button,
1088                                    EmpathyMainWindow *window)
1089 {
1090         McAccount *account;
1091         GtkWidget *error_widget;
1092
1093         account = g_object_get_data (G_OBJECT (button), "account");
1094         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
1095
1096         error_widget = g_hash_table_lookup (window->errors, account);
1097         gtk_widget_destroy (error_widget);
1098         g_hash_table_remove (window->errors, account);
1099 }
1100
1101 static void
1102 main_window_error_clear_clicked_cb (GtkButton         *button,
1103                                     EmpathyMainWindow *window)
1104 {
1105         McAccount *account;
1106         GtkWidget *error_widget;
1107
1108         account = g_object_get_data (G_OBJECT (button), "account");
1109         error_widget = g_hash_table_lookup (window->errors, account);
1110         gtk_widget_destroy (error_widget);
1111         g_hash_table_remove (window->errors, account);
1112 }
1113
1114 static void
1115 main_window_error_display (EmpathyMainWindow *window,
1116                            McAccount         *account,
1117                            const gchar       *message)
1118 {
1119         GtkWidget *child;
1120         GtkWidget *table;
1121         GtkWidget *image;
1122         GtkWidget *button_edit;
1123         GtkWidget *alignment;
1124         GtkWidget *hbox;
1125         GtkWidget *label;
1126         GtkWidget *fixed;
1127         GtkWidget *vbox;
1128         GtkWidget *button_close;
1129         gchar     *str;
1130
1131         child = g_hash_table_lookup (window->errors, account);
1132         if (child) {
1133                 label = g_object_get_data (G_OBJECT (child), "label");
1134
1135                 /* Just set the latest error and return */
1136                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1137                                                mc_account_get_display_name (account),
1138                                                message);
1139                 gtk_label_set_markup (GTK_LABEL (label), str);
1140                 g_free (str);
1141
1142                 return;
1143         }
1144
1145         child = gtk_vbox_new (FALSE, 0);
1146         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
1147         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
1148         gtk_widget_show (child);
1149
1150         table = gtk_table_new (2, 4, FALSE);
1151         gtk_widget_show (table);
1152         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
1153         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1154         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
1155
1156         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
1157         gtk_widget_show (image);
1158         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
1159                           (GtkAttachOptions) (GTK_FILL),
1160                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1161         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
1162
1163         button_edit = gtk_button_new ();
1164         gtk_widget_show (button_edit);
1165         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
1166                           (GtkAttachOptions) (GTK_FILL),
1167                           (GtkAttachOptions) (0), 0, 0);
1168
1169         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1170         gtk_widget_show (alignment);
1171         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
1172
1173         hbox = gtk_hbox_new (FALSE, 2);
1174         gtk_widget_show (hbox);
1175         gtk_container_add (GTK_CONTAINER (alignment), hbox);
1176
1177         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
1178         gtk_widget_show (image);
1179         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1180
1181         label = gtk_label_new_with_mnemonic (_("_Edit account"));
1182         gtk_widget_show (label);
1183         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1184
1185         fixed = gtk_fixed_new ();
1186         gtk_widget_show (fixed);
1187         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
1188                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1189                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1190
1191         vbox = gtk_vbox_new (FALSE, 6);
1192         gtk_widget_show (vbox);
1193         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
1194                           (GtkAttachOptions) (GTK_FILL),
1195                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1196
1197         button_close = gtk_button_new ();
1198         gtk_widget_show (button_close);
1199         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
1200         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
1201
1202
1203         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
1204         gtk_widget_show (image);
1205         gtk_container_add (GTK_CONTAINER (button_close), image);
1206
1207         label = gtk_label_new ("");
1208         gtk_widget_show (label);
1209         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
1210                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
1211                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
1212         gtk_widget_set_size_request (label, 175, -1);
1213         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1214         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1215         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1216
1217         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1218                                        mc_account_get_display_name (account),
1219                                        message);
1220         gtk_label_set_markup (GTK_LABEL (label), str);
1221         g_free (str);
1222
1223         g_object_set_data (G_OBJECT (child), "label", label);
1224         g_object_set_data_full (G_OBJECT (button_edit),
1225                                 "account", g_object_ref (account),
1226                                 g_object_unref);
1227         g_object_set_data_full (G_OBJECT (button_close),
1228                                 "account", g_object_ref (account),
1229                                 g_object_unref);
1230
1231         g_signal_connect (button_edit, "clicked",
1232                           G_CALLBACK (main_window_error_edit_clicked_cb),
1233                           window);
1234
1235         g_signal_connect (button_close, "clicked",
1236                           G_CALLBACK (main_window_error_clear_clicked_cb),
1237                           window);
1238
1239         gtk_widget_show (window->errors_vbox);
1240
1241         g_hash_table_insert (window->errors, g_object_ref (account), child);
1242 }
1243
1244 static void
1245 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
1246 {
1247         int  connected;
1248         int  connecting;
1249         GList *l;
1250
1251         /* Count number of connected/connecting/disconnected accounts */
1252         connected = empathy_account_manager_get_connected_accounts (manager);
1253         connecting = empathy_account_manager_get_connecting_accounts (manager);
1254
1255         /* Update the spinner state */
1256         if (connecting > 0) {
1257                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
1258         } else {
1259                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
1260         }
1261
1262         /* Update widgets sensibility */
1263         for (l = window->widgets_connected; l; l = l->next) {
1264                 gtk_widget_set_sensitive (l->data, (connected > 0));
1265         }
1266 }
1267
1268 /*
1269  * Accels
1270  */
1271 static void
1272 main_window_accels_load (void)
1273 {
1274         gchar *filename;
1275
1276         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1277         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1278                 DEBUG ("Loading from:'%s'", filename);
1279                 gtk_accel_map_load (filename);
1280         }
1281
1282         g_free (filename);
1283 }
1284
1285 static void
1286 main_window_accels_save (void)
1287 {
1288         gchar *dir;
1289         gchar *file_with_path;
1290
1291         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1292         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1293         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1294         g_free (dir);
1295
1296         DEBUG ("Saving to:'%s'", file_with_path);
1297         gtk_accel_map_save (file_with_path);
1298
1299         g_free (file_with_path);
1300 }
1301
1302 static void
1303 main_window_connection_items_setup (EmpathyMainWindow *window,
1304                                     GladeXML          *glade)
1305 {
1306         GList         *list;
1307         GtkWidget     *w;
1308         gint           i;
1309         const gchar *widgets_connected[] = {
1310                 "room",
1311                 "chat_new_message",
1312                 "chat_add_contact",
1313                 "edit_personal_information"
1314         };
1315
1316         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1317                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1318                 list = g_list_prepend (list, w);
1319         }
1320
1321         window->widgets_connected = list;
1322 }
1323
1324 static gboolean
1325 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1326 {
1327         gint x, y, w, h;
1328
1329         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1330         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1331
1332         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1333
1334         window->size_timeout_id = 0;
1335
1336         return FALSE;
1337 }
1338
1339 static gboolean
1340 main_window_configure_event_cb (GtkWidget         *widget,
1341                                 GdkEventConfigure *event,
1342                                 EmpathyMainWindow *window)
1343 {
1344         if (window->size_timeout_id) {
1345                 g_source_remove (window->size_timeout_id);
1346         }
1347
1348         window->size_timeout_id = g_timeout_add_seconds (1,
1349                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1350                                                          window);
1351
1352         return FALSE;
1353 }
1354
1355 static void
1356 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1357                                     const gchar *key,
1358                                     gpointer     check_menu_item)
1359 {
1360         gboolean show_offline;
1361
1362         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1363                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1364                                                 show_offline);
1365         }
1366 }
1367
1368 static void
1369 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1370                                     const gchar       *key,
1371                                     EmpathyMainWindow *window)
1372 {
1373         gboolean show_avatars;
1374
1375         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1376                 empathy_contact_list_store_set_show_avatars (window->list_store,
1377                                                             show_avatars);
1378         }
1379 }
1380
1381 static void
1382 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1383                                             const gchar       *key,
1384                                             EmpathyMainWindow *window)
1385 {
1386         gboolean compact_contact_list;
1387
1388         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1389                 empathy_contact_list_store_set_is_compact (window->list_store,
1390                                                           compact_contact_list);
1391         }
1392 }
1393
1394 static void
1395 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1396                                       const gchar       *key,
1397                                       EmpathyMainWindow *window)
1398 {
1399         gchar *str = NULL;
1400
1401         if (empathy_conf_get_string (conf, key, &str) && str) {
1402                 GType       type;
1403                 GEnumClass *enum_class;
1404                 GEnumValue *enum_value;
1405
1406                 type = empathy_contact_list_store_sort_get_type ();
1407                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1408                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1409                 g_free (str);
1410
1411                 if (enum_value) {
1412                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1413                                                                        enum_value->value);
1414                 }
1415         }
1416 }
1417