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