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