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