]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-main-window.c
c447c473d839a8015e1606abc526dab6dd3c36ee
[empathy.git] / libempathy-gtk / 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-debug.h>
33 #include <libempathy/empathy-utils.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 "empathy-main-window.h"
42 #include "empathy-contact-dialogs.h"
43 #include "ephy-spinner.h"
44 #include "empathy-contact-list-store.h"
45 #include "empathy-contact-list-view.h"
46 #include "empathy-presence-chooser.h"
47 #include "empathy-ui-utils.h"
48 #include "empathy-geometry.h"
49 #include "empathy-conf.h"
50 #include "empathy-preferences.h"
51 #include "empathy-accounts-dialog.h"
52 #include "empathy-about-dialog.h"
53 #include "empathy-new-chatroom-dialog.h"
54 #include "empathy-chatrooms-window.h"
55 #include "empathy-log-window.h"
56 #include "empathy-new-message-dialog.h"
57 #include "empathy-gtk-enum-types.h"
58
59 #define DEBUG_DOMAIN "MainWindow"
60
61 /* Minimum width of roster window if something goes wrong. */
62 #define MIN_WIDTH 50
63
64 /* Accels (menu shortcuts) can be configured and saved */
65 #define ACCELS_FILENAME "accels.txt"
66
67 /* Name in the geometry file */
68 #define GEOMETRY_NAME "main-window"
69
70 typedef struct {
71         EmpathyContactListView  *list_view;
72         EmpathyContactListStore *list_store;
73         MissionControl          *mc;
74         EmpathyChatroomManager  *chatroom_manager;
75
76         GtkWidget              *window;
77         GtkWidget              *main_vbox;
78         GtkWidget              *throbber;
79         GtkWidget              *presence_toolbar;
80         GtkWidget              *presence_chooser;
81         GtkWidget              *errors_vbox;
82
83         GtkWidget              *room;
84         GtkWidget              *room_menu;
85         GtkWidget              *room_sep;
86         GtkWidget              *room_join_favorites;
87         GtkWidget              *edit_context;
88         GtkWidget              *edit_context_separator;
89
90         guint                   size_timeout_id;
91         GHashTable             *errors;
92
93         /* Widgets that are enabled when there is... */
94         GList                  *widgets_connected;      /* ... connected accounts */
95         GList                  *widgets_disconnected;   /* ... disconnected accounts */
96 } EmpathyMainWindow;
97
98 static void     main_window_destroy_cb                         (GtkWidget                *widget,
99                                                                 EmpathyMainWindow        *window);
100 static void     main_window_favorite_chatroom_menu_setup       (EmpathyMainWindow        *window);
101 static void     main_window_favorite_chatroom_menu_added_cb    (EmpathyChatroomManager   *manager,
102                                                                 EmpathyChatroom          *chatroom,
103                                                                 EmpathyMainWindow        *window);
104 static void     main_window_favorite_chatroom_menu_removed_cb  (EmpathyChatroomManager   *manager,
105                                                                 EmpathyChatroom          *chatroom,
106                                                                 EmpathyMainWindow        *window);
107 static void     main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem              *menu_item,
108                                                                 EmpathyChatroom          *chatroom);
109 static void     main_window_favorite_chatroom_menu_update      (EmpathyMainWindow        *window);
110 static void     main_window_favorite_chatroom_menu_add         (EmpathyMainWindow        *window,
111                                                                 EmpathyChatroom          *chatroom);
112 static void     main_window_favorite_chatroom_join             (EmpathyChatroom          *chatroom);
113 static void     main_window_chat_quit_cb                       (GtkWidget                *widget,
114                                                                 EmpathyMainWindow        *window);
115 static void     main_window_chat_new_message_cb                (GtkWidget                *widget,
116                                                                 EmpathyMainWindow        *window);
117 static void     main_window_chat_history_cb                    (GtkWidget                *widget,
118                                                                 EmpathyMainWindow        *window);
119 static void     main_window_room_join_new_cb                   (GtkWidget                *widget,
120                                                                 EmpathyMainWindow        *window);
121 static void     main_window_room_join_favorites_cb             (GtkWidget                *widget,
122                                                                 EmpathyMainWindow        *window);
123 static void     main_window_room_manage_favorites_cb           (GtkWidget                *widget,
124                                                                 EmpathyMainWindow        *window);
125 static void     main_window_chat_add_contact_cb                (GtkWidget                *widget,
126                                                                 EmpathyMainWindow        *window);
127 static void     main_window_chat_show_offline_cb               (GtkCheckMenuItem         *item,
128                                                                 EmpathyMainWindow        *window);
129 static gboolean main_window_edit_button_press_event_cb         (GtkWidget                *widget,
130                                                                 GdkEventButton           *event,
131                                                                 EmpathyMainWindow        *window);
132 static void     main_window_edit_accounts_cb                   (GtkWidget                *widget,
133                                                                 EmpathyMainWindow        *window);
134 static void     main_window_edit_personal_information_cb       (GtkWidget                *widget,
135                                                                 EmpathyMainWindow        *window);
136 static void     main_window_edit_preferences_cb                (GtkWidget                *widget,
137                                                                 EmpathyMainWindow        *window);
138 static void     main_window_help_about_cb                      (GtkWidget                *widget,
139                                                                 EmpathyMainWindow        *window);
140 static void     main_window_help_contents_cb                   (GtkWidget                *widget,
141                                                                 EmpathyMainWindow        *window);
142 static gboolean main_window_throbber_button_press_event_cb     (GtkWidget                *throbber_ebox,
143                                                                 GdkEventButton           *event,
144                                                                 EmpathyMainWindow        *window);
145 static void     main_window_status_changed_cb                  (MissionControl           *mc,
146                                                                 TpConnectionStatus        status,
147                                                                 McPresence                presence,
148                                                                 TpConnectionStatusReason  reason,
149                                                                 const gchar              *unique_name,
150                                                                 EmpathyMainWindow        *window);
151 static void     main_window_update_status                      (EmpathyMainWindow        *window);
152 static void     main_window_accels_load                        (void);
153 static void     main_window_accels_save                        (void);
154 static void     main_window_connection_items_setup             (EmpathyMainWindow        *window,
155                                                                 GladeXML                 *glade);
156 static gboolean main_window_configure_event_timeout_cb         (EmpathyMainWindow        *window);
157 static gboolean main_window_configure_event_cb                 (GtkWidget                *widget,
158                                                                 GdkEventConfigure        *event,
159                                                                 EmpathyMainWindow        *window);
160 static void     main_window_notify_show_offline_cb             (EmpathyConf              *conf,
161                                                                 const gchar              *key,
162                                                                 gpointer                  check_menu_item);
163 static void     main_window_notify_show_avatars_cb             (EmpathyConf              *conf,
164                                                                 const gchar              *key,
165                                                                 EmpathyMainWindow        *window);
166 static void     main_window_notify_compact_contact_list_cb     (EmpathyConf              *conf,
167                                                                 const gchar              *key,
168                                                                 EmpathyMainWindow        *window);
169 static void     main_window_notify_sort_criterium_cb           (EmpathyConf              *conf,
170                                                                 const gchar              *key,
171                                                                 EmpathyMainWindow        *window);
172
173 GtkWidget *
174 empathy_main_window_show (void)
175 {
176         static EmpathyMainWindow *window = NULL;
177         EmpathyContactList       *list_iface;
178         GladeXML                 *glade;
179         EmpathyConf               *conf;
180         GtkWidget                *sw;
181         GtkWidget                *show_offline_widget;
182         GtkWidget                *ebox;
183         GtkToolItem              *item;
184         gboolean                  show_offline;
185         gboolean                  show_avatars;
186         gboolean                  compact_contact_list;
187         gint                      x, y, w, h;
188
189         if (window) {
190                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
191                 return window->window;
192         }
193
194         window = g_new0 (EmpathyMainWindow, 1);
195
196         /* Set up interface */
197         glade = empathy_glade_get_file ("empathy-main-window.glade",
198                                        "main_window",
199                                        NULL,
200                                        "main_window", &window->window,
201                                        "main_vbox", &window->main_vbox,
202                                        "errors_vbox", &window->errors_vbox,
203                                        "chat_show_offline", &show_offline_widget,
204                                        "room", &window->room,
205                                        "room_sep", &window->room_sep,
206                                        "room_join_favorites", &window->room_join_favorites,
207                                        "edit_context", &window->edit_context,
208                                        "edit_context_separator", &window->edit_context_separator,
209                                        "presence_toolbar", &window->presence_toolbar,
210                                        "roster_scrolledwindow", &sw,
211                                        NULL);
212
213         empathy_glade_connect (glade,
214                               window,
215                               "main_window", "destroy", main_window_destroy_cb,
216                               "main_window", "configure_event", main_window_configure_event_cb,
217                               "chat_quit", "activate", main_window_chat_quit_cb,
218                               "chat_new_message", "activate", main_window_chat_new_message_cb,
219                               "chat_history", "activate", main_window_chat_history_cb,
220                               "room_join_new", "activate", main_window_room_join_new_cb,
221                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
222                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
223                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
224                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
225                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
226                               "edit_accounts", "activate", main_window_edit_accounts_cb,
227                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
228                               "edit_preferences", "activate", main_window_edit_preferences_cb,
229                               "help_about", "activate", main_window_help_about_cb,
230                               "help_contents", "activate", main_window_help_contents_cb,
231                               NULL);
232
233         /* Set up connection related widgets. */
234         main_window_connection_items_setup (window, glade);
235         g_object_unref (glade);
236
237         window->mc = empathy_mission_control_new ();
238         dbus_g_proxy_connect_signal (DBUS_G_PROXY (window->mc), "AccountStatusChanged",
239                                      G_CALLBACK (main_window_status_changed_cb),
240                                      window, NULL);
241
242         window->errors = g_hash_table_new_full (empathy_account_hash,
243                                                 empathy_account_equal,
244                                                 g_object_unref,
245                                                 NULL);
246
247         /* Set up menu */
248         main_window_favorite_chatroom_menu_setup (window);
249
250         gtk_widget_hide (window->edit_context);
251         gtk_widget_hide (window->edit_context_separator);
252
253         /* Set up presence chooser */
254         window->presence_chooser = empathy_presence_chooser_new ();
255         gtk_widget_show (window->presence_chooser);
256         item = gtk_tool_item_new ();
257         gtk_widget_show (GTK_WIDGET (item));
258         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
259         gtk_tool_item_set_is_important (item, TRUE);
260         gtk_tool_item_set_expand (item, TRUE);
261         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
262
263         /* Set up the throbber */
264         ebox = gtk_event_box_new ();
265         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
266         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
267         g_signal_connect (ebox,
268                           "button-press-event",
269                           G_CALLBACK (main_window_throbber_button_press_event_cb),
270                           window);
271         gtk_widget_show (ebox);
272
273         window->throbber = ephy_spinner_new ();
274         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
275         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
276         gtk_widget_show (window->throbber);
277
278         item = gtk_tool_item_new ();
279         gtk_container_add (GTK_CONTAINER (item), ebox);
280         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
281         gtk_widget_show (GTK_WIDGET (item));
282
283         /* Set up contact list. */
284         empathy_status_presets_get_all ();
285
286         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_new ());
287         window->list_store = empathy_contact_list_store_new (list_iface);
288         window->list_view = empathy_contact_list_view_new (window->list_store,
289                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL);
290         g_object_unref (list_iface);
291
292         gtk_widget_show (GTK_WIDGET (window->list_view));
293         gtk_container_add (GTK_CONTAINER (sw),
294                            GTK_WIDGET (window->list_view));
295
296         /* Load user-defined accelerators. */
297         main_window_accels_load ();
298
299         /* Set window size. */
300         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
301
302         if (w >= 1 && h >= 1) {
303                 /* Use the defaults from the glade file if we
304                  * don't have good w, h geometry.
305                  */
306                 empathy_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
307                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
308         }
309
310         if (x >= 0 && y >= 0) {
311                 /* Let the window manager position it if we
312                  * don't have good x, y coordinates.
313                  */
314                 empathy_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
315                 gtk_window_move (GTK_WINDOW (window->window), x, y);
316         }
317
318         conf = empathy_conf_get ();
319         
320         /* Show offline ? */
321         empathy_conf_get_bool (conf,
322                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
323                               &show_offline);
324         empathy_conf_notify_add (conf,
325                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
326                                 main_window_notify_show_offline_cb,
327                                 show_offline_widget);
328
329         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
330                                         show_offline);
331
332         /* Show avatars ? */
333         empathy_conf_get_bool (conf,
334                               EMPATHY_PREFS_UI_SHOW_AVATARS,
335                               &show_avatars);
336         empathy_conf_notify_add (conf,
337                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
338                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
339                                 window);
340         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
341
342         /* Is compact ? */
343         empathy_conf_get_bool (conf,
344                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
345                               &compact_contact_list);
346         empathy_conf_notify_add (conf,
347                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
348                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
349                                 window);
350         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
351
352         /* Sort criterium */
353         empathy_conf_notify_add (conf,
354                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
355                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
356                                 window);
357         main_window_notify_sort_criterium_cb (conf,
358                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
359                                               window);
360
361         main_window_update_status (window);
362
363         return window->window;
364 }
365
366 static void
367 main_window_destroy_cb (GtkWidget         *widget,
368                         EmpathyMainWindow *window)
369 {
370         /* Save user-defined accelerators. */
371         main_window_accels_save ();
372
373         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (window->mc), "AccountStatusChanged",
374                                         G_CALLBACK (main_window_status_changed_cb),
375                                         window);
376
377         if (window->size_timeout_id) {
378                 g_source_remove (window->size_timeout_id);
379         }
380
381         g_list_free (window->widgets_connected);
382         g_list_free (window->widgets_disconnected);
383
384         g_object_unref (window->mc);
385         g_object_unref (window->list_store);
386         g_hash_table_destroy (window->errors);
387
388         g_free (window);
389 }
390
391 static void
392 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
393 {
394         GList *chatrooms, *l;
395
396         window->chatroom_manager = empathy_chatroom_manager_new ();
397         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
398         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
399
400         for (l = chatrooms; l; l = l->next) {
401                 main_window_favorite_chatroom_menu_add (window, l->data);
402         }
403
404         if (!chatrooms) {
405                 gtk_widget_hide (window->room_sep);
406         }
407
408         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
409
410         g_signal_connect (window->chatroom_manager, "chatroom-added",
411                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
412                           window);
413         g_signal_connect (window->chatroom_manager, "chatroom-removed",
414                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
415                           window);
416
417         g_list_free (chatrooms);
418 }
419
420 static void
421 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
422                                              EmpathyChatroom        *chatroom,
423                                              EmpathyMainWindow     *window)
424 {
425         main_window_favorite_chatroom_menu_add (window, chatroom);
426         gtk_widget_show (window->room_sep);
427         gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
428 }
429
430 static void
431 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
432                                                EmpathyChatroom        *chatroom,
433                                                EmpathyMainWindow     *window)
434 {
435         GtkWidget *menu_item;
436
437         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
438
439         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
440         gtk_widget_destroy (menu_item);
441
442         main_window_favorite_chatroom_menu_update (window);
443 }
444
445 static void
446 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
447                                                 EmpathyChatroom *chatroom)
448 {
449         main_window_favorite_chatroom_join (chatroom);
450 }
451
452 static void
453 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
454 {
455         GList *chatrooms;
456
457         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
458
459         if (chatrooms) {
460                 gtk_widget_show (window->room_sep);
461         } else {
462                 gtk_widget_hide (window->room_sep);
463         }
464
465         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
466         g_list_free (chatrooms);
467 }
468
469 static void
470 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
471                                         EmpathyChatroom    *chatroom)
472 {
473         GtkWidget   *menu_item;
474         const gchar *name;
475
476         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
477                 return;
478         }
479
480         name = empathy_chatroom_get_name (chatroom);
481         menu_item = gtk_menu_item_new_with_label (name);
482
483         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
484         g_signal_connect (menu_item, "activate",
485                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
486                           chatroom);
487
488         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
489                                menu_item, 3);
490
491         gtk_widget_show (menu_item);
492 }
493
494 static void
495 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
496 {
497         MissionControl *mc;
498         McAccount      *account;
499         const gchar    *room;
500
501         mc = empathy_mission_control_new ();
502         account = empathy_chatroom_get_account (chatroom);
503         room = empathy_chatroom_get_room (chatroom);
504
505         empathy_debug (DEBUG_DOMAIN, "Requesting channel for '%s'", room);
506
507         mission_control_request_channel_with_string_handle (mc,
508                                                             account,
509                                                             TP_IFACE_CHANNEL_TYPE_TEXT,
510                                                             room,
511                                                             TP_HANDLE_TYPE_ROOM,
512                                                             NULL, NULL);        
513         g_object_unref (mc);
514 }
515
516 static void
517 main_window_chat_quit_cb (GtkWidget         *widget,
518                           EmpathyMainWindow *window)
519 {
520         gtk_main_quit ();
521 }
522
523 static void
524 main_window_chat_new_message_cb (GtkWidget         *widget,
525                                  EmpathyMainWindow *window)
526 {
527         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
528 }
529
530 static void
531 main_window_chat_history_cb (GtkWidget         *widget,
532                              EmpathyMainWindow *window)
533 {
534         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
535 }
536
537 static void
538 main_window_room_join_new_cb (GtkWidget         *widget,
539                               EmpathyMainWindow *window)
540 {
541         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
542 }
543
544 static void
545 main_window_room_join_favorites_cb (GtkWidget         *widget,
546                                     EmpathyMainWindow *window)
547 {
548         GList *chatrooms, *l;
549
550         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
551         for (l = chatrooms; l; l = l->next) {
552                 main_window_favorite_chatroom_join (l->data);
553         }
554         g_list_free (chatrooms);
555 }
556
557 static void
558 main_window_room_manage_favorites_cb (GtkWidget         *widget,
559                                       EmpathyMainWindow *window)
560 {
561         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
562 }
563
564 static void
565 main_window_chat_add_contact_cb (GtkWidget         *widget,
566                                  EmpathyMainWindow *window)
567 {
568         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
569 }
570
571 static void
572 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
573                                   EmpathyMainWindow *window)
574 {
575         gboolean current;
576
577         current = gtk_check_menu_item_get_active (item);
578
579         empathy_conf_set_bool (empathy_conf_get (),
580                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
581                               current);
582
583         /* Turn off sound just while we alter the contact list. */
584         // FIXME: empathy_sound_set_enabled (FALSE);
585         empathy_contact_list_store_set_show_offline (window->list_store, current);
586         //empathy_sound_set_enabled (TRUE);
587 }
588
589 static gboolean
590 main_window_edit_button_press_event_cb (GtkWidget         *widget,
591                                         GdkEventButton    *event,
592                                         EmpathyMainWindow *window)
593 {
594         EmpathyContact *contact;
595         gchar         *group;
596
597         if (!event->button == 1) {
598                 return FALSE;
599         }
600
601         group = empathy_contact_list_view_get_selected_group (window->list_view);
602         if (group) {
603                 GtkMenuItem *item;
604                 GtkWidget   *label;
605                 GtkWidget   *submenu;
606
607                 item = GTK_MENU_ITEM (window->edit_context);
608                 label = gtk_bin_get_child (GTK_BIN (item));
609                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
610
611                 gtk_widget_show (window->edit_context);
612                 gtk_widget_show (window->edit_context_separator);
613
614                 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
615                 gtk_menu_item_set_submenu (item, submenu);
616
617                 g_free (group);
618
619                 return FALSE;
620         }
621
622         contact = empathy_contact_list_view_get_selected (window->list_view);
623         if (contact) {
624                 GtkMenuItem *item;
625                 GtkWidget   *label;
626                 GtkWidget   *submenu;
627
628                 item = GTK_MENU_ITEM (window->edit_context);
629                 label = gtk_bin_get_child (GTK_BIN (item));
630                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
631
632                 gtk_widget_show (window->edit_context);
633                 gtk_widget_show (window->edit_context_separator);
634
635                 submenu = empathy_contact_list_view_get_contact_menu (window->list_view,
636                                                                      contact);
637                 gtk_menu_item_set_submenu (item, submenu);
638
639                 g_object_unref (contact);
640
641                 return FALSE;
642         }
643
644         gtk_widget_hide (window->edit_context);
645         gtk_widget_hide (window->edit_context_separator);
646
647         return FALSE;
648 }
649
650 static void
651 main_window_edit_accounts_cb (GtkWidget         *widget,
652                               EmpathyMainWindow *window)
653 {
654         empathy_accounts_dialog_show (GTK_WINDOW (window->window));
655 }
656
657 static void
658 main_window_edit_personal_information_cb (GtkWidget         *widget,
659                                           EmpathyMainWindow *window)
660 {
661         GSList *accounts;
662
663         accounts = mission_control_get_online_connections (window->mc, NULL);
664         if (accounts) {
665                 EmpathyContactFactory *factory;
666                 EmpathyContact        *contact;
667                 McAccount             *account;
668
669                 account = accounts->data;
670                 factory = empathy_contact_factory_new ();
671                 contact = empathy_contact_factory_get_user (factory, account);
672                 empathy_contact_run_until_ready (contact,
673                                                  EMPATHY_CONTACT_READY_HANDLE |
674                                                  EMPATHY_CONTACT_READY_ID,
675                                                  NULL);
676
677                 empathy_contact_information_dialog_show (contact,
678                                                          GTK_WINDOW (window->window),
679                                                          TRUE, TRUE);
680
681                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
682                 g_slist_free (accounts);
683                 g_object_unref (factory);
684                 g_object_unref (contact);
685         }
686 }
687
688 static void
689 main_window_edit_preferences_cb (GtkWidget         *widget,
690                                  EmpathyMainWindow *window)
691 {
692         empathy_preferences_show (GTK_WINDOW (window->window));
693 }
694
695 static void
696 main_window_help_about_cb (GtkWidget         *widget,
697                            EmpathyMainWindow *window)
698 {
699         empathy_about_dialog_new (GTK_WINDOW (window->window));
700 }
701
702 static void
703 main_window_help_contents_cb (GtkWidget         *widget,
704                               EmpathyMainWindow *window)
705 {
706         //empathy_help_show ();
707 }
708
709 static gboolean
710 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
711                                             GdkEventButton    *event,
712                                             EmpathyMainWindow *window)
713 {
714         if (event->type != GDK_BUTTON_PRESS ||
715             event->button != 1) {
716                 return FALSE;
717         }
718
719         empathy_accounts_dialog_show (GTK_WINDOW (window->window));
720
721         return FALSE;
722 }
723
724 static void
725 main_window_error_edit_clicked_cb (GtkButton         *button,
726                                    EmpathyMainWindow *window)
727 {
728         McAccount *account;
729         GtkWidget *error_widget;
730
731         empathy_accounts_dialog_show (GTK_WINDOW (window->window));
732
733         account = g_object_get_data (G_OBJECT (button), "account");
734         error_widget = g_hash_table_lookup (window->errors, account);
735         gtk_widget_destroy (error_widget);
736         g_hash_table_remove (window->errors, account);
737 }
738
739 static void
740 main_window_error_clear_clicked_cb (GtkButton         *button,
741                                     EmpathyMainWindow *window)
742 {
743         McAccount *account;
744         GtkWidget *error_widget;
745
746         account = g_object_get_data (G_OBJECT (button), "account");
747         error_widget = g_hash_table_lookup (window->errors, account);
748         gtk_widget_destroy (error_widget);
749         g_hash_table_remove (window->errors, account);
750 }
751
752 static void
753 main_window_error_display (EmpathyMainWindow *window,
754                            McAccount         *account,
755                            const gchar       *message)
756 {
757         GtkWidget *child;
758         GtkWidget *table;
759         GtkWidget *image;
760         GtkWidget *button_edit;
761         GtkWidget *alignment;
762         GtkWidget *hbox;
763         GtkWidget *label;
764         GtkWidget *fixed;
765         GtkWidget *vbox;
766         GtkWidget *button_close;
767         gchar     *str;
768
769         child = g_hash_table_lookup (window->errors, account);
770         if (child) {
771                 label = g_object_get_data (G_OBJECT (child), "label");
772
773                 /* Just set the latest error and return */
774                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
775                                                mc_account_get_display_name (account),
776                                                message);
777                 gtk_label_set_markup (GTK_LABEL (label), str);
778                 g_free (str);
779
780                 return;
781         }
782
783         child = gtk_vbox_new (FALSE, 0);
784         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
785         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
786         gtk_widget_show (child);
787
788         table = gtk_table_new (2, 4, FALSE);
789         gtk_widget_show (table);
790         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
791         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
792         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
793
794         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
795         gtk_widget_show (image);
796         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
797                           (GtkAttachOptions) (GTK_FILL),
798                           (GtkAttachOptions) (GTK_FILL), 0, 0);
799         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
800
801         button_edit = gtk_button_new ();
802         gtk_widget_show (button_edit);
803         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
804                           (GtkAttachOptions) (GTK_FILL),
805                           (GtkAttachOptions) (0), 0, 0);
806
807         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
808         gtk_widget_show (alignment);
809         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
810
811         hbox = gtk_hbox_new (FALSE, 2);
812         gtk_widget_show (hbox);
813         gtk_container_add (GTK_CONTAINER (alignment), hbox);
814
815         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
816         gtk_widget_show (image);
817         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
818
819         label = gtk_label_new_with_mnemonic (_("_Edit account"));
820         gtk_widget_show (label);
821         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
822
823         fixed = gtk_fixed_new ();
824         gtk_widget_show (fixed);
825         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
826                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
827                           (GtkAttachOptions) (GTK_FILL), 0, 0);
828
829         vbox = gtk_vbox_new (FALSE, 6);
830         gtk_widget_show (vbox);
831         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
832                           (GtkAttachOptions) (GTK_FILL),
833                           (GtkAttachOptions) (GTK_FILL), 0, 0);
834
835         button_close = gtk_button_new ();
836         gtk_widget_show (button_close);
837         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
838         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
839
840
841         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
842         gtk_widget_show (image);
843         gtk_container_add (GTK_CONTAINER (button_close), image);
844
845         label = gtk_label_new ("");
846         gtk_widget_show (label);
847         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
848                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
849                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
850         gtk_widget_set_size_request (label, 175, -1);
851         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
852         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
853         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
854
855         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
856                                        mc_account_get_display_name (account),
857                                        message);
858         gtk_label_set_markup (GTK_LABEL (label), str);
859         g_free (str);
860
861         g_object_set_data (G_OBJECT (child), "label", label);
862         g_object_set_data_full (G_OBJECT (button_edit),
863                                 "account", g_object_ref (account),
864                                 g_object_unref);
865         g_object_set_data_full (G_OBJECT (button_close),
866                                 "account", g_object_ref (account),
867                                 g_object_unref);
868
869         g_signal_connect (button_edit, "clicked",
870                           G_CALLBACK (main_window_error_edit_clicked_cb),
871                           window);
872
873         g_signal_connect (button_close, "clicked",
874                           G_CALLBACK (main_window_error_clear_clicked_cb),
875                           window);
876
877         gtk_widget_show (window->errors_vbox);
878
879         g_hash_table_insert (window->errors, g_object_ref (account), child);
880 }
881
882 static void
883 main_window_status_changed_cb (MissionControl           *mc,
884                                TpConnectionStatus        status,
885                                McPresence                presence,
886                                TpConnectionStatusReason  reason,
887                                const gchar              *unique_name,
888                                EmpathyMainWindow        *window)
889 {
890         McAccount *account;
891
892         main_window_update_status (window);
893
894         account = mc_account_lookup (unique_name);
895
896         if (status == TP_CONNECTION_STATUS_DISCONNECTED &&
897             reason > TP_CONNECTION_STATUS_REASON_REQUESTED) {
898                 const gchar *message;
899
900                 switch (reason) {
901                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
902                         message = _("Network error");
903                         break;
904                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
905                         message = _("Authentication failed");
906                         break;
907                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
908                         message = _("Encryption error");
909                         break;
910                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
911                         message = _("Name in use");
912                         break;
913                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
914                         message = _("Certificate not provided");
915                         break;
916                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
917                         message = _("Certificate untrusted");
918                         break;
919                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
920                         message = _("Certificate expired");
921                         break;
922                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
923                         message = _("Certificate not activated");
924                         break;
925                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
926                         message = _("Certificate hostname mismatch");
927                         break;
928                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
929                         message = _("Certificate fingerprint mismatch");
930                         break;
931                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
932                         message = _("Certificate self signed");
933                         break;
934                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
935                         message = _("Certificate error");
936                         break;
937                 default:
938                         message = _("Unknown error");
939                         break;
940                 }
941
942                 main_window_error_display (window, account, message);
943         }
944
945         if (status == TP_CONNECTION_STATUS_CONNECTED) {
946                 GtkWidget *error_widget;
947
948                 /* Account connected without error, remove error message if any */
949                 error_widget = g_hash_table_lookup (window->errors, account);
950                 if (error_widget) {
951                         gtk_widget_destroy (error_widget);
952                         g_hash_table_remove (window->errors, account);
953                 }
954         }
955
956         g_object_unref (account);
957 }
958
959 static void
960 main_window_update_status (EmpathyMainWindow *window)
961 {
962         GList *accounts, *l;
963         guint  connected = 0;
964         guint  connecting = 0;
965         guint  disconnected = 0;
966
967         /* Count number of connected/connecting/disconnected accounts */
968         accounts = mc_accounts_list (); 
969         for (l = accounts; l; l = l->next) {
970                 McAccount *account;
971                 guint      status;
972
973                 account = l->data;
974
975                 status = mission_control_get_connection_status (window->mc,
976                                                                 account,
977                                                                 NULL);
978
979                 if (status == 0) {
980                         connected++;
981                 } else if (status == 1) {
982                         connecting++;
983                 } else if (status == 2) {
984                         disconnected++;
985                 }
986
987                 g_object_unref (account);
988         }
989         g_list_free (accounts);
990
991         /* Update the spinner state */
992         if (connecting > 0) {
993                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
994         } else {
995                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
996         }
997
998         /* Update widgets sensibility */
999         for (l = window->widgets_connected; l; l = l->next) {
1000                 gtk_widget_set_sensitive (l->data, (connected > 0));
1001         }
1002
1003         for (l = window->widgets_disconnected; l; l = l->next) {
1004                 gtk_widget_set_sensitive (l->data, (disconnected > 0));
1005         }
1006 }
1007
1008 /*
1009  * Accels
1010  */
1011 static void
1012 main_window_accels_load (void)
1013 {
1014         gchar *filename;
1015
1016         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1017         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1018                 empathy_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
1019                 gtk_accel_map_load (filename);
1020         }
1021
1022         g_free (filename);
1023 }
1024
1025 static void
1026 main_window_accels_save (void)
1027 {
1028         gchar *dir;
1029         gchar *file_with_path;
1030
1031         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1032         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1033         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1034         g_free (dir);
1035
1036         empathy_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
1037         gtk_accel_map_save (file_with_path);
1038
1039         g_free (file_with_path);
1040 }
1041
1042 static void
1043 main_window_connection_items_setup (EmpathyMainWindow *window,
1044                                     GladeXML          *glade)
1045 {
1046         GList         *list;
1047         GtkWidget     *w;
1048         gint           i;
1049         const gchar *widgets_connected[] = {
1050                 "room",
1051                 "chat_new_message",
1052                 "chat_add_contact",
1053                 "edit_personal_information"
1054         };
1055         const gchar *widgets_disconnected[] = {
1056         };
1057
1058         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1059                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1060                 list = g_list_prepend (list, w);
1061         }
1062
1063         window->widgets_connected = list;
1064
1065         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
1066                 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
1067                 list = g_list_prepend (list, w);
1068         }
1069
1070         window->widgets_disconnected = list;
1071 }
1072
1073 static gboolean
1074 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1075 {
1076         gint x, y, w, h;
1077
1078         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1079         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1080
1081         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1082
1083         window->size_timeout_id = 0;
1084
1085         return FALSE;
1086 }
1087
1088 static gboolean
1089 main_window_configure_event_cb (GtkWidget         *widget,
1090                                 GdkEventConfigure *event,
1091                                 EmpathyMainWindow *window)
1092 {
1093         if (window->size_timeout_id) {
1094                 g_source_remove (window->size_timeout_id);
1095         }
1096
1097         window->size_timeout_id = g_timeout_add_seconds (1,
1098                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1099                                                          window);
1100
1101         return FALSE;
1102 }
1103
1104 static void
1105 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1106                                     const gchar *key,
1107                                     gpointer     check_menu_item)
1108 {
1109         gboolean show_offline;
1110
1111         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1112                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1113                                                 show_offline);
1114         }
1115 }
1116
1117 static void
1118 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1119                                     const gchar       *key,
1120                                     EmpathyMainWindow *window)
1121 {
1122         gboolean show_avatars;
1123
1124         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1125                 empathy_contact_list_store_set_show_avatars (window->list_store,
1126                                                             show_avatars);
1127         }
1128 }
1129
1130 static void
1131 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1132                                             const gchar       *key,
1133                                             EmpathyMainWindow *window)
1134 {
1135         gboolean compact_contact_list;
1136
1137         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1138                 empathy_contact_list_store_set_is_compact (window->list_store,
1139                                                           compact_contact_list);
1140         }
1141 }
1142
1143 static void
1144 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1145                                       const gchar       *key,
1146                                       EmpathyMainWindow *window)
1147 {
1148         gchar *str = NULL;
1149
1150         if (empathy_conf_get_string (conf, key, &str) && str) {
1151                 GType       type;
1152                 GEnumClass *enum_class;
1153                 GEnumValue *enum_value;
1154
1155                 type = empathy_contact_list_store_sort_get_type ();
1156                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1157                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1158                 g_free (str);
1159
1160                 if (enum_value) {
1161                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1162                                                                        enum_value->value);
1163                 }
1164         }
1165 }
1166