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