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