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