]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Remove the contact pending event when the contact is not pending anymore.
[empathy.git] / src / empathy-main-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <config.h>
25
26 #include <sys/stat.h>
27 #include <gtk/gtk.h>
28 #include <glade/glade.h>
29 #include <glib/gi18n.h>
30
31 #include <libempathy/empathy-contact.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-chatroom-manager.h>
34 #include <libempathy/empathy-chatroom.h>
35 #include <libempathy/empathy-contact-list.h>
36 #include <libempathy/empathy-contact-manager.h>
37 #include <libempathy/empathy-contact-factory.h>
38 #include <libempathy/empathy-status-presets.h>
39
40 #include <libempathy-gtk/empathy-contact-dialogs.h>
41 #include <libempathy-gtk/empathy-contact-list-store.h>
42 #include <libempathy-gtk/empathy-contact-list-view.h>
43 #include <libempathy-gtk/empathy-presence-chooser.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-geometry.h>
46 #include <libempathy-gtk/empathy-conf.h>
47 #include <libempathy-gtk/empathy-log-window.h>
48 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include <libempathy-gtk/empathy-gtk-enum-types.h>
50
51 #include "empathy-accounts-dialog.h"
52 #include "empathy-main-window.h"
53 #include "ephy-spinner.h"
54 #include "empathy-preferences.h"
55 #include "empathy-about-dialog.h"
56 #include "empathy-new-chatroom-dialog.h"
57 #include "empathy-chatrooms-window.h"
58
59 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
60 #include <libempathy/empathy-debug.h>
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                                                            EMPATHY_CONTACT_FEATURE_ALL);
296         g_object_unref (list_iface);
297
298         gtk_widget_show (GTK_WIDGET (window->list_view));
299         gtk_container_add (GTK_CONTAINER (sw),
300                            GTK_WIDGET (window->list_view));
301
302         /* Load user-defined accelerators. */
303         main_window_accels_load ();
304
305         /* Set window size. */
306         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
307
308         if (w >= 1 && h >= 1) {
309                 /* Use the defaults from the glade file if we
310                  * don't have good w, h geometry.
311                  */
312                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
313                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
314         }
315
316         if (x >= 0 && y >= 0) {
317                 /* Let the window manager position it if we
318                  * don't have good x, y coordinates.
319                  */
320                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
321                 gtk_window_move (GTK_WINDOW (window->window), x, y);
322         }
323
324         conf = empathy_conf_get ();
325         
326         /* Show offline ? */
327         empathy_conf_get_bool (conf,
328                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
329                               &show_offline);
330         empathy_conf_notify_add (conf,
331                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
332                                 main_window_notify_show_offline_cb,
333                                 show_offline_widget);
334
335         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
336                                         show_offline);
337
338         /* Show avatars ? */
339         empathy_conf_get_bool (conf,
340                               EMPATHY_PREFS_UI_SHOW_AVATARS,
341                               &show_avatars);
342         empathy_conf_notify_add (conf,
343                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
344                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
345                                 window);
346         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
347
348         /* Is compact ? */
349         empathy_conf_get_bool (conf,
350                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
351                               &compact_contact_list);
352         empathy_conf_notify_add (conf,
353                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
354                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
355                                 window);
356         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
357
358         /* Sort criterium */
359         empathy_conf_notify_add (conf,
360                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
361                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
362                                 window);
363         main_window_notify_sort_criterium_cb (conf,
364                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
365                                               window);
366
367         main_window_update_status (window);
368
369         return window->window;
370 }
371
372 static void
373 main_window_destroy_cb (GtkWidget         *widget,
374                         EmpathyMainWindow *window)
375 {
376         /* Save user-defined accelerators. */
377         main_window_accels_save ();
378
379         empathy_disconnect_account_status_changed (window->token);
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         DEBUG ("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         GtkWidget *submenu;
599
600         if (!event->button == 1) {
601                 return FALSE;
602         }
603
604         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
605         if (submenu) {
606                 GtkMenuItem *item;
607                 GtkWidget   *label;
608
609                 item = GTK_MENU_ITEM (window->edit_context);
610                 label = gtk_bin_get_child (GTK_BIN (item));
611                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
612
613                 gtk_widget_show (window->edit_context);
614                 gtk_widget_show (window->edit_context_separator);
615
616                 gtk_menu_item_set_submenu (item, submenu);
617
618                 return FALSE;
619         }
620
621         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
622         if (submenu) {
623                 GtkMenuItem *item;
624                 GtkWidget   *label;
625
626                 item = GTK_MENU_ITEM (window->edit_context);
627                 label = gtk_bin_get_child (GTK_BIN (item));
628                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
629
630                 gtk_widget_show (window->edit_context);
631                 gtk_widget_show (window->edit_context_separator);
632
633                 gtk_menu_item_set_submenu (item, submenu);
634
635                 return FALSE;
636         }
637
638         gtk_widget_hide (window->edit_context);
639         gtk_widget_hide (window->edit_context_separator);
640
641         return FALSE;
642 }
643
644 static void
645 main_window_edit_accounts_cb (GtkWidget         *widget,
646                               EmpathyMainWindow *window)
647 {
648         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
649 }
650
651 static void
652 main_window_edit_personal_information_cb (GtkWidget         *widget,
653                                           EmpathyMainWindow *window)
654 {
655         GSList *accounts;
656
657         accounts = mission_control_get_online_connections (window->mc, NULL);
658         if (accounts) {
659                 EmpathyContactFactory *factory;
660                 EmpathyContact        *contact;
661                 McAccount             *account;
662
663                 account = accounts->data;
664                 factory = empathy_contact_factory_new ();
665                 contact = empathy_contact_factory_get_user (factory, account);
666                 empathy_contact_run_until_ready (contact,
667                                                  EMPATHY_CONTACT_READY_HANDLE |
668                                                  EMPATHY_CONTACT_READY_ID,
669                                                  NULL);
670
671                 empathy_contact_information_dialog_show (contact,
672                                                          GTK_WINDOW (window->window),
673                                                          TRUE, TRUE);
674
675                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
676                 g_slist_free (accounts);
677                 g_object_unref (factory);
678                 g_object_unref (contact);
679         }
680 }
681
682 static void
683 main_window_edit_preferences_cb (GtkWidget         *widget,
684                                  EmpathyMainWindow *window)
685 {
686         empathy_preferences_show (GTK_WINDOW (window->window));
687 }
688
689 static void
690 main_window_help_about_cb (GtkWidget         *widget,
691                            EmpathyMainWindow *window)
692 {
693         empathy_about_dialog_new (GTK_WINDOW (window->window));
694 }
695
696 static void
697 main_window_help_contents_cb (GtkWidget         *widget,
698                               EmpathyMainWindow *window)
699 {
700         empathy_url_show ("ghelp:empathy");
701 }
702
703 static gboolean
704 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
705                                             GdkEventButton    *event,
706                                             EmpathyMainWindow *window)
707 {
708         if (event->type != GDK_BUTTON_PRESS ||
709             event->button != 1) {
710                 return FALSE;
711         }
712
713         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
714
715         return FALSE;
716 }
717
718 static void
719 main_window_error_edit_clicked_cb (GtkButton         *button,
720                                    EmpathyMainWindow *window)
721 {
722         McAccount *account;
723         GtkWidget *error_widget;
724
725         account = g_object_get_data (G_OBJECT (button), "account");
726         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
727
728         error_widget = g_hash_table_lookup (window->errors, account);
729         gtk_widget_destroy (error_widget);
730         g_hash_table_remove (window->errors, account);
731 }
732
733 static void
734 main_window_error_clear_clicked_cb (GtkButton         *button,
735                                     EmpathyMainWindow *window)
736 {
737         McAccount *account;
738         GtkWidget *error_widget;
739
740         account = g_object_get_data (G_OBJECT (button), "account");
741         error_widget = g_hash_table_lookup (window->errors, account);
742         gtk_widget_destroy (error_widget);
743         g_hash_table_remove (window->errors, account);
744 }
745
746 static void
747 main_window_error_display (EmpathyMainWindow *window,
748                            McAccount         *account,
749                            const gchar       *message)
750 {
751         GtkWidget *child;
752         GtkWidget *table;
753         GtkWidget *image;
754         GtkWidget *button_edit;
755         GtkWidget *alignment;
756         GtkWidget *hbox;
757         GtkWidget *label;
758         GtkWidget *fixed;
759         GtkWidget *vbox;
760         GtkWidget *button_close;
761         gchar     *str;
762
763         child = g_hash_table_lookup (window->errors, account);
764         if (child) {
765                 label = g_object_get_data (G_OBJECT (child), "label");
766
767                 /* Just set the latest error and return */
768                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
769                                                mc_account_get_display_name (account),
770                                                message);
771                 gtk_label_set_markup (GTK_LABEL (label), str);
772                 g_free (str);
773
774                 return;
775         }
776
777         child = gtk_vbox_new (FALSE, 0);
778         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
779         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
780         gtk_widget_show (child);
781
782         table = gtk_table_new (2, 4, FALSE);
783         gtk_widget_show (table);
784         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
785         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
786         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
787
788         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
789         gtk_widget_show (image);
790         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
791                           (GtkAttachOptions) (GTK_FILL),
792                           (GtkAttachOptions) (GTK_FILL), 0, 0);
793         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
794
795         button_edit = gtk_button_new ();
796         gtk_widget_show (button_edit);
797         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
798                           (GtkAttachOptions) (GTK_FILL),
799                           (GtkAttachOptions) (0), 0, 0);
800
801         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
802         gtk_widget_show (alignment);
803         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
804
805         hbox = gtk_hbox_new (FALSE, 2);
806         gtk_widget_show (hbox);
807         gtk_container_add (GTK_CONTAINER (alignment), hbox);
808
809         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
810         gtk_widget_show (image);
811         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
812
813         label = gtk_label_new_with_mnemonic (_("_Edit account"));
814         gtk_widget_show (label);
815         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
816
817         fixed = gtk_fixed_new ();
818         gtk_widget_show (fixed);
819         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
820                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
821                           (GtkAttachOptions) (GTK_FILL), 0, 0);
822
823         vbox = gtk_vbox_new (FALSE, 6);
824         gtk_widget_show (vbox);
825         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
826                           (GtkAttachOptions) (GTK_FILL),
827                           (GtkAttachOptions) (GTK_FILL), 0, 0);
828
829         button_close = gtk_button_new ();
830         gtk_widget_show (button_close);
831         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
832         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
833
834
835         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
836         gtk_widget_show (image);
837         gtk_container_add (GTK_CONTAINER (button_close), image);
838
839         label = gtk_label_new ("");
840         gtk_widget_show (label);
841         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
842                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
843                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
844         gtk_widget_set_size_request (label, 175, -1);
845         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
846         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
847         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
848
849         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
850                                        mc_account_get_display_name (account),
851                                        message);
852         gtk_label_set_markup (GTK_LABEL (label), str);
853         g_free (str);
854
855         g_object_set_data (G_OBJECT (child), "label", label);
856         g_object_set_data_full (G_OBJECT (button_edit),
857                                 "account", g_object_ref (account),
858                                 g_object_unref);
859         g_object_set_data_full (G_OBJECT (button_close),
860                                 "account", g_object_ref (account),
861                                 g_object_unref);
862
863         g_signal_connect (button_edit, "clicked",
864                           G_CALLBACK (main_window_error_edit_clicked_cb),
865                           window);
866
867         g_signal_connect (button_close, "clicked",
868                           G_CALLBACK (main_window_error_clear_clicked_cb),
869                           window);
870
871         gtk_widget_show (window->errors_vbox);
872
873         g_hash_table_insert (window->errors, g_object_ref (account), child);
874 }
875
876 static void
877 main_window_status_changed_cb (MissionControl           *mc,
878                                TpConnectionStatus        status,
879                                McPresence                presence,
880                                TpConnectionStatusReason  reason,
881                                const gchar              *unique_name,
882                                EmpathyMainWindow        *window)
883 {
884         McAccount *account;
885
886         main_window_update_status (window);
887
888         account = mc_account_lookup (unique_name);
889
890         if (status == TP_CONNECTION_STATUS_DISCONNECTED &&
891             reason > TP_CONNECTION_STATUS_REASON_REQUESTED) {
892                 const gchar *message;
893
894                 switch (reason) {
895                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
896                         message = _("Network error");
897                         break;
898                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
899                         message = _("Authentication failed");
900                         break;
901                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
902                         message = _("Encryption error");
903                         break;
904                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
905                         message = _("Name in use");
906                         break;
907                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
908                         message = _("Certificate not provided");
909                         break;
910                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
911                         message = _("Certificate untrusted");
912                         break;
913                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
914                         message = _("Certificate expired");
915                         break;
916                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
917                         message = _("Certificate not activated");
918                         break;
919                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
920                         message = _("Certificate hostname mismatch");
921                         break;
922                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
923                         message = _("Certificate fingerprint mismatch");
924                         break;
925                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
926                         message = _("Certificate self-signed");
927                         break;
928                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
929                         message = _("Certificate error");
930                         break;
931                 default:
932                         message = _("Unknown error");
933                         break;
934                 }
935
936                 main_window_error_display (window, account, message);
937         }
938
939         if (status == TP_CONNECTION_STATUS_CONNECTED) {
940                 GtkWidget *error_widget;
941
942                 /* Account connected without error, remove error message if any */
943                 error_widget = g_hash_table_lookup (window->errors, account);
944                 if (error_widget) {
945                         gtk_widget_destroy (error_widget);
946                         g_hash_table_remove (window->errors, account);
947                 }
948         }
949
950         g_object_unref (account);
951 }
952
953 static void
954 main_window_update_status (EmpathyMainWindow *window)
955 {
956         GList *accounts, *l;
957         guint  connected = 0;
958         guint  connecting = 0;
959         guint  disconnected = 0;
960
961         /* Count number of connected/connecting/disconnected accounts */
962         accounts = mc_accounts_list (); 
963         for (l = accounts; l; l = l->next) {
964                 McAccount *account;
965                 guint      status;
966
967                 account = l->data;
968
969                 status = mission_control_get_connection_status (window->mc,
970                                                                 account,
971                                                                 NULL);
972
973                 if (status == 0) {
974                         connected++;
975                 } else if (status == 1) {
976                         connecting++;
977                 } else if (status == 2) {
978                         disconnected++;
979                 }
980
981                 g_object_unref (account);
982         }
983         g_list_free (accounts);
984
985         /* Update the spinner state */
986         if (connecting > 0) {
987                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
988         } else {
989                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
990         }
991
992         /* Update widgets sensibility */
993         for (l = window->widgets_connected; l; l = l->next) {
994                 gtk_widget_set_sensitive (l->data, (connected > 0));
995         }
996
997         for (l = window->widgets_disconnected; l; l = l->next) {
998                 gtk_widget_set_sensitive (l->data, (disconnected > 0));
999         }
1000 }
1001
1002 /*
1003  * Accels
1004  */
1005 static void
1006 main_window_accels_load (void)
1007 {
1008         gchar *filename;
1009
1010         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1011         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1012                 DEBUG ("Loading from:'%s'", filename);
1013                 gtk_accel_map_load (filename);
1014         }
1015
1016         g_free (filename);
1017 }
1018
1019 static void
1020 main_window_accels_save (void)
1021 {
1022         gchar *dir;
1023         gchar *file_with_path;
1024
1025         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1026         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1027         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1028         g_free (dir);
1029
1030         DEBUG ("Saving to:'%s'", file_with_path);
1031         gtk_accel_map_save (file_with_path);
1032
1033         g_free (file_with_path);
1034 }
1035
1036 static void
1037 main_window_connection_items_setup (EmpathyMainWindow *window,
1038                                     GladeXML          *glade)
1039 {
1040         GList         *list;
1041         GtkWidget     *w;
1042         gint           i;
1043         const gchar *widgets_connected[] = {
1044                 "room",
1045                 "chat_new_message",
1046                 "chat_add_contact",
1047                 "edit_personal_information"
1048         };
1049         const gchar *widgets_disconnected[] = {
1050         };
1051
1052         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1053                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1054                 list = g_list_prepend (list, w);
1055         }
1056
1057         window->widgets_connected = list;
1058
1059         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
1060                 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
1061                 list = g_list_prepend (list, w);
1062         }
1063
1064         window->widgets_disconnected = list;
1065 }
1066
1067 static gboolean
1068 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1069 {
1070         gint x, y, w, h;
1071
1072         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1073         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1074
1075         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1076
1077         window->size_timeout_id = 0;
1078
1079         return FALSE;
1080 }
1081
1082 static gboolean
1083 main_window_configure_event_cb (GtkWidget         *widget,
1084                                 GdkEventConfigure *event,
1085                                 EmpathyMainWindow *window)
1086 {
1087         if (window->size_timeout_id) {
1088                 g_source_remove (window->size_timeout_id);
1089         }
1090
1091         window->size_timeout_id = g_timeout_add_seconds (1,
1092                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1093                                                          window);
1094
1095         return FALSE;
1096 }
1097
1098 static void
1099 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1100                                     const gchar *key,
1101                                     gpointer     check_menu_item)
1102 {
1103         gboolean show_offline;
1104
1105         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1106                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1107                                                 show_offline);
1108         }
1109 }
1110
1111 static void
1112 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1113                                     const gchar       *key,
1114                                     EmpathyMainWindow *window)
1115 {
1116         gboolean show_avatars;
1117
1118         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1119                 empathy_contact_list_store_set_show_avatars (window->list_store,
1120                                                             show_avatars);
1121         }
1122 }
1123
1124 static void
1125 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1126                                             const gchar       *key,
1127                                             EmpathyMainWindow *window)
1128 {
1129         gboolean compact_contact_list;
1130
1131         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1132                 empathy_contact_list_store_set_is_compact (window->list_store,
1133                                                           compact_contact_list);
1134         }
1135 }
1136
1137 static void
1138 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1139                                       const gchar       *key,
1140                                       EmpathyMainWindow *window)
1141 {
1142         gchar *str = NULL;
1143
1144         if (empathy_conf_get_string (conf, key, &str) && str) {
1145                 GType       type;
1146                 GEnumClass *enum_class;
1147                 GEnumValue *enum_value;
1148
1149                 type = empathy_contact_list_store_sort_get_type ();
1150                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1151                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1152                 g_free (str);
1153
1154                 if (enum_value) {
1155                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1156                                                                        enum_value->value);
1157                 }
1158         }
1159 }
1160