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