]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Adapt Patryk's patch to trunk.
[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 #include <canberra-gtk.h>
31
32 #include <libempathy/empathy-contact.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-log-window.h>
49 #include <libempathy-gtk/empathy-new-message-dialog.h>
50 #include <libempathy-gtk/empathy-gtk-enum-types.h>
51
52 #include "empathy-accounts-dialog.h"
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 #include "empathy-event-manager.h"
60
61 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
62 #include <libempathy/empathy-debug.h>
63
64 /* Flashing delay for icons (milliseconds). */
65 #define FLASH_TIMEOUT 500
66
67 /* Minimum width of roster window if something goes wrong. */
68 #define MIN_WIDTH 50
69
70 /* Accels (menu shortcuts) can be configured and saved */
71 #define ACCELS_FILENAME "accels.txt"
72
73 /* Name in the geometry file */
74 #define GEOMETRY_NAME "main-window"
75
76 typedef struct {
77         EmpathyContactListView  *list_view;
78         EmpathyContactListStore *list_store;
79         MissionControl          *mc;
80         EmpathyChatroomManager  *chatroom_manager;
81         EmpathyEventManager     *event_manager;
82         guint                    flash_timeout_id;
83         gboolean                 flash_on;
84         gpointer                 token;
85
86         GtkWidget              *window;
87         GtkWidget              *main_vbox;
88         GtkWidget              *throbber;
89         GtkWidget              *presence_toolbar;
90         GtkWidget              *presence_chooser;
91         GtkWidget              *errors_vbox;
92
93         GtkWidget              *room;
94         GtkWidget              *room_menu;
95         GtkWidget              *room_sep;
96         GtkWidget              *room_join_favorites;
97         GtkWidget              *edit_context;
98         GtkWidget              *edit_context_separator;
99
100         guint                   size_timeout_id;
101         GHashTable             *errors;
102
103         /* Widgets that are enabled when there is... */
104         GList                  *widgets_connected;      /* ... connected accounts */
105         GList                  *widgets_disconnected;   /* ... disconnected accounts */
106 } EmpathyMainWindow;
107
108 static void     main_window_destroy_cb                         (GtkWidget                *widget,
109                                                                 EmpathyMainWindow        *window);
110 static void     main_window_favorite_chatroom_menu_setup       (EmpathyMainWindow        *window);
111 static void     main_window_favorite_chatroom_menu_added_cb    (EmpathyChatroomManager   *manager,
112                                                                 EmpathyChatroom          *chatroom,
113                                                                 EmpathyMainWindow        *window);
114 static void     main_window_favorite_chatroom_menu_removed_cb  (EmpathyChatroomManager   *manager,
115                                                                 EmpathyChatroom          *chatroom,
116                                                                 EmpathyMainWindow        *window);
117 static void     main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem              *menu_item,
118                                                                 EmpathyChatroom          *chatroom);
119 static void     main_window_favorite_chatroom_menu_update      (EmpathyMainWindow        *window);
120 static void     main_window_favorite_chatroom_menu_add         (EmpathyMainWindow        *window,
121                                                                 EmpathyChatroom          *chatroom);
122 static void     main_window_favorite_chatroom_join             (EmpathyChatroom          *chatroom);
123 static void     main_window_chat_quit_cb                       (GtkWidget                *widget,
124                                                                 EmpathyMainWindow        *window);
125 static void     main_window_chat_new_message_cb                (GtkWidget                *widget,
126                                                                 EmpathyMainWindow        *window);
127 static void     main_window_chat_history_cb                    (GtkWidget                *widget,
128                                                                 EmpathyMainWindow        *window);
129 static void     main_window_room_join_new_cb                   (GtkWidget                *widget,
130                                                                 EmpathyMainWindow        *window);
131 static void     main_window_room_join_favorites_cb             (GtkWidget                *widget,
132                                                                 EmpathyMainWindow        *window);
133 static void     main_window_room_manage_favorites_cb           (GtkWidget                *widget,
134                                                                 EmpathyMainWindow        *window);
135 static void     main_window_chat_add_contact_cb                (GtkWidget                *widget,
136                                                                 EmpathyMainWindow        *window);
137 static void     main_window_chat_show_offline_cb               (GtkCheckMenuItem         *item,
138                                                                 EmpathyMainWindow        *window);
139 static gboolean main_window_edit_button_press_event_cb         (GtkWidget                *widget,
140                                                                 GdkEventButton           *event,
141                                                                 EmpathyMainWindow        *window);
142 static void     main_window_edit_accounts_cb                   (GtkWidget                *widget,
143                                                                 EmpathyMainWindow        *window);
144 static void     main_window_edit_personal_information_cb       (GtkWidget                *widget,
145                                                                 EmpathyMainWindow        *window);
146 static void     main_window_edit_preferences_cb                (GtkWidget                *widget,
147                                                                 EmpathyMainWindow        *window);
148 static void     main_window_help_about_cb                      (GtkWidget                *widget,
149                                                                 EmpathyMainWindow        *window);
150 static void     main_window_help_contents_cb                   (GtkWidget                *widget,
151                                                                 EmpathyMainWindow        *window);
152 static gboolean main_window_throbber_button_press_event_cb     (GtkWidget                *throbber_ebox,
153                                                                 GdkEventButton           *event,
154                                                                 EmpathyMainWindow        *window);
155 static void     main_window_status_changed_cb                  (MissionControl           *mc,
156                                                                 TpConnectionStatus        status,
157                                                                 McPresence                presence,
158                                                                 TpConnectionStatusReason  reason,
159                                                                 const gchar              *unique_name,
160                                                                 EmpathyMainWindow        *window);
161 static void     main_window_update_status                      (EmpathyMainWindow        *window);
162 static void     main_window_accels_load                        (void);
163 static void     main_window_accels_save                        (void);
164 static void     main_window_connection_items_setup             (EmpathyMainWindow        *window,
165                                                                 GladeXML                 *glade);
166 static gboolean main_window_configure_event_timeout_cb         (EmpathyMainWindow        *window);
167 static gboolean main_window_configure_event_cb                 (GtkWidget                *widget,
168                                                                 GdkEventConfigure        *event,
169                                                                 EmpathyMainWindow        *window);
170 static void     main_window_notify_show_offline_cb             (EmpathyConf              *conf,
171                                                                 const gchar              *key,
172                                                                 gpointer                  check_menu_item);
173 static void     main_window_notify_show_avatars_cb             (EmpathyConf              *conf,
174                                                                 const gchar              *key,
175                                                                 EmpathyMainWindow        *window);
176 static void     main_window_notify_compact_contact_list_cb     (EmpathyConf              *conf,
177                                                                 const gchar              *key,
178                                                                 EmpathyMainWindow        *window);
179 static void     main_window_notify_sort_criterium_cb           (EmpathyConf              *conf,
180                                                                 const gchar              *key,
181                                                                 EmpathyMainWindow        *window);
182
183 static void
184 main_window_flash_stop (EmpathyMainWindow *window)
185 {
186         if (window->flash_timeout_id == 0) {
187                 return;
188         }
189
190         DEBUG ("Stop flashing");
191         g_source_remove (window->flash_timeout_id);
192         window->flash_timeout_id = 0;
193         window->flash_on = FALSE;
194 }
195
196 typedef struct {
197         EmpathyEvent *event;
198         gboolean      on;
199 } FlashForeachData;
200
201 static gboolean
202 main_window_flash_foreach (GtkTreeModel *model,
203                            GtkTreePath  *path,
204                            GtkTreeIter  *iter,
205                            gpointer      user_data)
206 {
207         FlashForeachData *data = (FlashForeachData*) user_data;
208         EmpathyContact   *contact;
209         const gchar      *icon_name;
210         GtkTreePath      *parent_path = NULL;
211         GtkTreeIter       parent_iter;
212
213         /* To be used with gtk_tree_model_foreach, update the status icon
214          * of the contact to show the event icon (on=TRUE) or the presence
215          * (on=FALSE) */
216         gtk_tree_model_get (model, iter,
217                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
218                             -1);
219
220         if (contact != data->event->contact) {
221                 if (contact) {
222                         g_object_unref (contact);
223                 }
224                 return FALSE;
225         }
226
227         if (data->on) {
228                 icon_name = data->event->icon_name;
229         } else {
230                 icon_name = empathy_icon_name_for_contact (contact);
231         }
232
233         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
234                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
235                             -1);
236
237         /* To make sure the parent is shown correctly, we emit
238          * the row-changed signal on the parent so it prompts
239          * it to be refreshed by the filter func. 
240          */
241         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
242                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
243         }
244         if (parent_path) {
245                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
246                 gtk_tree_path_free (parent_path);
247         }
248
249         g_object_unref (contact);
250
251         return FALSE;
252 }
253
254 static gboolean
255 main_window_flash_cb (EmpathyMainWindow *window)
256 {
257         GtkTreeModel     *model;
258         GSList           *events, *l;
259         gboolean          found_event = FALSE;
260         FlashForeachData  data;
261
262         window->flash_on = !window->flash_on;
263         data.on = window->flash_on;
264         model = GTK_TREE_MODEL (window->list_store);
265
266         events = empathy_event_manager_get_events (window->event_manager);
267         for (l = events; l; l = l->next) {
268                 data.event = l->data;
269                 if (!data.event->contact) {
270                         continue;
271                 }
272
273                 found_event = TRUE;
274                 gtk_tree_model_foreach (model,
275                                         main_window_flash_foreach,
276                                         &data);
277         }
278
279         if (!found_event) {
280                 main_window_flash_stop (window);
281         }
282
283         return TRUE;
284 }
285
286 static void
287 main_window_flash_start (EmpathyMainWindow *window)
288 {
289         ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
290                                 CA_PROP_EVENT_ID, "message-new-instant",
291                                 CA_PROP_EVENT_DESCRIPTION, _("Incoming chat request"),
292                                 CA_PROP_APPLICATION_NAME, g_get_application_name (),
293                                 NULL);
294
295         if (window->flash_timeout_id != 0) {
296                 return;
297         }
298
299         DEBUG ("Start flashing");
300         window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
301                                                   (GSourceFunc) main_window_flash_cb,
302                                                   window);
303         main_window_flash_cb (window);
304 }
305
306 static void
307 main_window_event_added_cb (EmpathyEventManager *manager,
308                             EmpathyEvent        *event,
309                             EmpathyMainWindow   *window)
310 {
311         if (event->contact) {
312                 main_window_flash_start (window);
313         }
314 }
315
316 static void
317 main_window_event_removed_cb (EmpathyEventManager *manager,
318                               EmpathyEvent        *event,
319                               EmpathyMainWindow   *window)
320 {
321         FlashForeachData data;
322
323         if (!event->contact) {
324                 return;
325         }
326
327         data.on = FALSE;
328         data.event = event;
329         gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
330                                 main_window_flash_foreach,
331                                 &data);
332 }
333
334 static void
335 main_window_row_activated_cb (EmpathyContactListView *view,
336                               GtkTreePath            *path,
337                               GtkTreeViewColumn      *col,
338                               EmpathyMainWindow      *window)
339 {
340         EmpathyContact *contact;
341         GtkTreeModel   *model;
342         GtkTreeIter     iter;
343         GSList         *events, *l;
344
345         model = GTK_TREE_MODEL (window->list_store);
346         gtk_tree_model_get_iter (model, &iter, path);
347         gtk_tree_model_get (model, &iter,
348                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
349                             -1);
350
351         if (!contact) {
352                 return;
353         }
354
355         /* If the contact has an event activate it, otherwise the
356          * default handler of row-activated will be called. */
357         events = empathy_event_manager_get_events (window->event_manager);
358         for (l = events; l; l = l->next) {
359                 EmpathyEvent *event = l->data;
360
361                 if (event->contact == contact) {
362                         DEBUG ("Activate event");
363                         empathy_event_activate (event);
364
365                         /* We don't want the default handler of this signal
366                          * (e.g. open a chat) */
367                         g_signal_stop_emission_by_name (view, "row-activated");
368                         break;
369                 }
370         }
371
372         g_object_unref (contact);
373 }
374
375 GtkWidget *
376 empathy_main_window_show (void)
377 {
378         static EmpathyMainWindow *window = NULL;
379         EmpathyContactList       *list_iface;
380         GladeXML                 *glade;
381         EmpathyConf               *conf;
382         GtkWidget                *sw;
383         GtkWidget                *show_offline_widget;
384         GtkWidget                *ebox;
385         GtkToolItem              *item;
386         gboolean                  show_offline;
387         gboolean                  show_avatars;
388         gboolean                  compact_contact_list;
389         gint                      x, y, w, h;
390         gchar                    *filename;
391         GSList                   *l;
392
393         if (window) {
394                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
395                 return window->window;
396         }
397
398         window = g_new0 (EmpathyMainWindow, 1);
399
400         /* Set up interface */
401         filename = empathy_file_lookup ("empathy-main-window.glade", "src");
402         glade = empathy_glade_get_file (filename,
403                                        "main_window",
404                                        NULL,
405                                        "main_window", &window->window,
406                                        "main_vbox", &window->main_vbox,
407                                        "errors_vbox", &window->errors_vbox,
408                                        "chat_show_offline", &show_offline_widget,
409                                        "room", &window->room,
410                                        "room_sep", &window->room_sep,
411                                        "room_join_favorites", &window->room_join_favorites,
412                                        "edit_context", &window->edit_context,
413                                        "edit_context_separator", &window->edit_context_separator,
414                                        "presence_toolbar", &window->presence_toolbar,
415                                        "roster_scrolledwindow", &sw,
416                                        NULL);
417         g_free (filename);
418
419         empathy_glade_connect (glade,
420                               window,
421                               "main_window", "destroy", main_window_destroy_cb,
422                               "main_window", "configure_event", main_window_configure_event_cb,
423                               "chat_quit", "activate", main_window_chat_quit_cb,
424                               "chat_new_message", "activate", main_window_chat_new_message_cb,
425                               "chat_history", "activate", main_window_chat_history_cb,
426                               "room_join_new", "activate", main_window_room_join_new_cb,
427                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
428                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
429                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
430                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
431                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
432                               "edit_accounts", "activate", main_window_edit_accounts_cb,
433                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
434                               "edit_preferences", "activate", main_window_edit_preferences_cb,
435                               "help_about", "activate", main_window_help_about_cb,
436                               "help_contents", "activate", main_window_help_contents_cb,
437                               NULL);
438
439         /* Set up connection related widgets. */
440         main_window_connection_items_setup (window, glade);
441         g_object_unref (glade);
442
443         window->mc = empathy_mission_control_new ();
444         window->token = empathy_connect_to_account_status_changed (window->mc,
445                                                    G_CALLBACK (main_window_status_changed_cb),
446                                                    window, NULL);
447
448         window->errors = g_hash_table_new_full (empathy_account_hash,
449                                                 empathy_account_equal,
450                                                 g_object_unref,
451                                                 NULL);
452
453         /* Set up menu */
454         main_window_favorite_chatroom_menu_setup (window);
455
456         gtk_widget_hide (window->edit_context);
457         gtk_widget_hide (window->edit_context_separator);
458
459         /* Set up presence chooser */
460         window->presence_chooser = empathy_presence_chooser_new ();
461         gtk_widget_show (window->presence_chooser);
462         item = gtk_tool_item_new ();
463         gtk_widget_show (GTK_WIDGET (item));
464         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
465         gtk_tool_item_set_is_important (item, TRUE);
466         gtk_tool_item_set_expand (item, TRUE);
467         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
468
469         /* Set up the throbber */
470         ebox = gtk_event_box_new ();
471         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
472         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
473         g_signal_connect (ebox,
474                           "button-press-event",
475                           G_CALLBACK (main_window_throbber_button_press_event_cb),
476                           window);
477         gtk_widget_show (ebox);
478
479         window->throbber = ephy_spinner_new ();
480         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
481         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
482         gtk_widget_show (window->throbber);
483
484         item = gtk_tool_item_new ();
485         gtk_container_add (GTK_CONTAINER (item), ebox);
486         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
487         gtk_widget_show (GTK_WIDGET (item));
488
489         /* Set up contact list. */
490         empathy_status_presets_get_all ();
491
492         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_new ());
493         window->list_store = empathy_contact_list_store_new (list_iface);
494         window->list_view = empathy_contact_list_view_new (window->list_store,
495                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
496                                                            EMPATHY_CONTACT_FEATURE_ALL);
497         g_object_unref (list_iface);
498
499         gtk_widget_show (GTK_WIDGET (window->list_view));
500         gtk_container_add (GTK_CONTAINER (sw),
501                            GTK_WIDGET (window->list_view));
502         g_signal_connect (window->list_view, "row-activated",
503                           G_CALLBACK (main_window_row_activated_cb),
504                           window);
505
506         /* Load user-defined accelerators. */
507         main_window_accels_load ();
508
509         /* Set window size. */
510         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
511
512         if (w >= 1 && h >= 1) {
513                 /* Use the defaults from the glade file if we
514                  * don't have good w, h geometry.
515                  */
516                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
517                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
518         }
519
520         if (x >= 0 && y >= 0) {
521                 /* Let the window manager position it if we
522                  * don't have good x, y coordinates.
523                  */
524                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
525                 gtk_window_move (GTK_WINDOW (window->window), x, y);
526         }
527
528         /* Enable event handling */
529         window->event_manager = empathy_event_manager_new ();
530         g_signal_connect (window->event_manager, "event-added",
531                           G_CALLBACK (main_window_event_added_cb),
532                           window);
533         g_signal_connect (window->event_manager, "event-removed",
534                           G_CALLBACK (main_window_event_removed_cb),
535                           window);
536
537         l = empathy_event_manager_get_events (window->event_manager);
538         while (l) {
539                 main_window_event_added_cb (window->event_manager,
540                                             l->data, window);
541                 l = l->next;
542         }
543
544         conf = empathy_conf_get ();
545
546         /* Show offline ? */
547         empathy_conf_get_bool (conf,
548                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
549                               &show_offline);
550         empathy_conf_notify_add (conf,
551                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
552                                 main_window_notify_show_offline_cb,
553                                 show_offline_widget);
554
555         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
556                                         show_offline);
557
558         /* Show avatars ? */
559         empathy_conf_get_bool (conf,
560                               EMPATHY_PREFS_UI_SHOW_AVATARS,
561                               &show_avatars);
562         empathy_conf_notify_add (conf,
563                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
564                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
565                                 window);
566         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
567
568         /* Is compact ? */
569         empathy_conf_get_bool (conf,
570                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
571                               &compact_contact_list);
572         empathy_conf_notify_add (conf,
573                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
574                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
575                                 window);
576         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
577
578         /* Sort criterium */
579         empathy_conf_notify_add (conf,
580                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
581                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
582                                 window);
583         main_window_notify_sort_criterium_cb (conf,
584                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
585                                               window);
586
587         main_window_update_status (window);
588
589         return window->window;
590 }
591
592 static void
593 main_window_destroy_cb (GtkWidget         *widget,
594                         EmpathyMainWindow *window)
595 {
596         /* Save user-defined accelerators. */
597         main_window_accels_save ();
598
599         empathy_disconnect_account_status_changed (window->token);
600
601         if (window->size_timeout_id) {
602                 g_source_remove (window->size_timeout_id);
603         }
604
605         g_list_free (window->widgets_connected);
606         g_list_free (window->widgets_disconnected);
607
608         g_object_unref (window->mc);
609         g_object_unref (window->list_store);
610         g_hash_table_destroy (window->errors);
611
612         g_signal_handlers_disconnect_by_func (window->event_manager,
613                                               main_window_event_added_cb,
614                                               window);
615         g_signal_handlers_disconnect_by_func (window->event_manager,
616                                               main_window_event_removed_cb,
617                                               window);
618         g_object_unref (window->event_manager);
619
620         g_free (window);
621 }
622
623 static void
624 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
625 {
626         GList *chatrooms, *l;
627
628         window->chatroom_manager = empathy_chatroom_manager_new (NULL);
629         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
630         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
631
632         for (l = chatrooms; l; l = l->next) {
633                 main_window_favorite_chatroom_menu_add (window, l->data);
634         }
635
636         if (!chatrooms) {
637                 gtk_widget_hide (window->room_sep);
638         }
639
640         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
641
642         g_signal_connect (window->chatroom_manager, "chatroom-added",
643                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
644                           window);
645         g_signal_connect (window->chatroom_manager, "chatroom-removed",
646                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
647                           window);
648
649         g_list_free (chatrooms);
650 }
651
652 static void
653 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
654                                              EmpathyChatroom        *chatroom,
655                                              EmpathyMainWindow     *window)
656 {
657         main_window_favorite_chatroom_menu_add (window, chatroom);
658         gtk_widget_show (window->room_sep);
659         gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
660 }
661
662 static void
663 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
664                                                EmpathyChatroom        *chatroom,
665                                                EmpathyMainWindow     *window)
666 {
667         GtkWidget *menu_item;
668
669         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
670
671         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
672         gtk_widget_destroy (menu_item);
673
674         main_window_favorite_chatroom_menu_update (window);
675 }
676
677 static void
678 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
679                                                 EmpathyChatroom *chatroom)
680 {
681         main_window_favorite_chatroom_join (chatroom);
682 }
683
684 static void
685 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
686 {
687         GList *chatrooms;
688
689         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
690
691         if (chatrooms) {
692                 gtk_widget_show (window->room_sep);
693         } else {
694                 gtk_widget_hide (window->room_sep);
695         }
696
697         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
698         g_list_free (chatrooms);
699 }
700
701 static void
702 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
703                                         EmpathyChatroom    *chatroom)
704 {
705         GtkWidget   *menu_item;
706         const gchar *name;
707
708         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
709                 return;
710         }
711
712         name = empathy_chatroom_get_name (chatroom);
713         menu_item = gtk_menu_item_new_with_label (name);
714
715         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
716         g_signal_connect (menu_item, "activate",
717                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
718                           chatroom);
719
720         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
721                                menu_item, 3);
722
723         gtk_widget_show (menu_item);
724 }
725
726 static void
727 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
728 {
729         MissionControl *mc;
730         McAccount      *account;
731         TpConnection   *connection;
732         const gchar    *room;
733
734         mc = empathy_mission_control_new ();
735         account = empathy_chatroom_get_account (chatroom);
736         room = empathy_chatroom_get_room (chatroom);
737
738         DEBUG ("Requesting channel for '%s'", room);
739
740         connection = mission_control_get_tpconnection (mc, account, NULL);
741         if (connection) {
742                 tp_connection_run_until_ready (connection, TRUE, NULL, NULL);   
743                 empathy_connection_request_channel (connection, -1,
744                                                     TP_IFACE_CHANNEL_TYPE_TEXT,
745                                                     TP_HANDLE_TYPE_ROOM,
746                                                     room, TRUE,
747                                                     NULL, NULL, NULL, NULL);
748                 g_object_unref (connection);
749         }
750         g_object_unref (mc);
751 }
752
753 static void
754 main_window_chat_quit_cb (GtkWidget         *widget,
755                           EmpathyMainWindow *window)
756 {
757         gtk_main_quit ();
758 }
759
760 static void
761 main_window_chat_new_message_cb (GtkWidget         *widget,
762                                  EmpathyMainWindow *window)
763 {
764         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
765 }
766
767 static void
768 main_window_chat_history_cb (GtkWidget         *widget,
769                              EmpathyMainWindow *window)
770 {
771         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
772 }
773
774 static void
775 main_window_room_join_new_cb (GtkWidget         *widget,
776                               EmpathyMainWindow *window)
777 {
778         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
779 }
780
781 static void
782 main_window_room_join_favorites_cb (GtkWidget         *widget,
783                                     EmpathyMainWindow *window)
784 {
785         GList *chatrooms, *l;
786
787         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
788         for (l = chatrooms; l; l = l->next) {
789                 main_window_favorite_chatroom_join (l->data);
790         }
791         g_list_free (chatrooms);
792 }
793
794 static void
795 main_window_room_manage_favorites_cb (GtkWidget         *widget,
796                                       EmpathyMainWindow *window)
797 {
798         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
799 }
800
801 static void
802 main_window_chat_add_contact_cb (GtkWidget         *widget,
803                                  EmpathyMainWindow *window)
804 {
805         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
806 }
807
808 static void
809 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
810                                   EmpathyMainWindow *window)
811 {
812         gboolean current;
813
814         current = gtk_check_menu_item_get_active (item);
815
816         empathy_conf_set_bool (empathy_conf_get (),
817                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
818                               current);
819
820         /* Turn off sound just while we alter the contact list. */
821         // FIXME: empathy_sound_set_enabled (FALSE);
822         empathy_contact_list_store_set_show_offline (window->list_store, current);
823         //empathy_sound_set_enabled (TRUE);
824 }
825
826 static gboolean
827 main_window_edit_button_press_event_cb (GtkWidget         *widget,
828                                         GdkEventButton    *event,
829                                         EmpathyMainWindow *window)
830 {
831         GtkWidget *submenu;
832
833         if (!event->button == 1) {
834                 return FALSE;
835         }
836
837         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
838         if (submenu) {
839                 GtkMenuItem *item;
840                 GtkWidget   *label;
841
842                 item = GTK_MENU_ITEM (window->edit_context);
843                 label = gtk_bin_get_child (GTK_BIN (item));
844                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
845
846                 gtk_widget_show (window->edit_context);
847                 gtk_widget_show (window->edit_context_separator);
848
849                 gtk_menu_item_set_submenu (item, submenu);
850
851                 return FALSE;
852         }
853
854         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
855         if (submenu) {
856                 GtkMenuItem *item;
857                 GtkWidget   *label;
858
859                 item = GTK_MENU_ITEM (window->edit_context);
860                 label = gtk_bin_get_child (GTK_BIN (item));
861                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
862
863                 gtk_widget_show (window->edit_context);
864                 gtk_widget_show (window->edit_context_separator);
865
866                 gtk_menu_item_set_submenu (item, submenu);
867
868                 return FALSE;
869         }
870
871         gtk_widget_hide (window->edit_context);
872         gtk_widget_hide (window->edit_context_separator);
873
874         return FALSE;
875 }
876
877 static void
878 main_window_edit_accounts_cb (GtkWidget         *widget,
879                               EmpathyMainWindow *window)
880 {
881         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
882 }
883
884 static void
885 main_window_edit_personal_information_cb (GtkWidget         *widget,
886                                           EmpathyMainWindow *window)
887 {
888         GSList *accounts;
889
890         accounts = mission_control_get_online_connections (window->mc, NULL);
891         if (accounts) {
892                 EmpathyContactFactory *factory;
893                 EmpathyContact        *contact;
894                 McAccount             *account;
895
896                 account = accounts->data;
897                 factory = empathy_contact_factory_new ();
898                 contact = empathy_contact_factory_get_user (factory, account);
899                 empathy_contact_run_until_ready (contact,
900                                                  EMPATHY_CONTACT_READY_HANDLE |
901                                                  EMPATHY_CONTACT_READY_ID,
902                                                  NULL);
903
904                 empathy_contact_information_dialog_show (contact,
905                                                          GTK_WINDOW (window->window),
906                                                          TRUE, TRUE);
907
908                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
909                 g_slist_free (accounts);
910                 g_object_unref (factory);
911                 g_object_unref (contact);
912         }
913 }
914
915 static void
916 main_window_edit_preferences_cb (GtkWidget         *widget,
917                                  EmpathyMainWindow *window)
918 {
919         empathy_preferences_show (GTK_WINDOW (window->window));
920 }
921
922 static void
923 main_window_help_about_cb (GtkWidget         *widget,
924                            EmpathyMainWindow *window)
925 {
926         empathy_about_dialog_new (GTK_WINDOW (window->window));
927 }
928
929 static void
930 main_window_help_contents_cb (GtkWidget         *widget,
931                               EmpathyMainWindow *window)
932 {
933         empathy_url_show (widget, "ghelp:empathy");
934 }
935
936 static gboolean
937 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
938                                             GdkEventButton    *event,
939                                             EmpathyMainWindow *window)
940 {
941         if (event->type != GDK_BUTTON_PRESS ||
942             event->button != 1) {
943                 return FALSE;
944         }
945
946         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
947
948         return FALSE;
949 }
950
951 static void
952 main_window_error_edit_clicked_cb (GtkButton         *button,
953                                    EmpathyMainWindow *window)
954 {
955         McAccount *account;
956         GtkWidget *error_widget;
957
958         account = g_object_get_data (G_OBJECT (button), "account");
959         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
960
961         error_widget = g_hash_table_lookup (window->errors, account);
962         gtk_widget_destroy (error_widget);
963         g_hash_table_remove (window->errors, account);
964 }
965
966 static void
967 main_window_error_clear_clicked_cb (GtkButton         *button,
968                                     EmpathyMainWindow *window)
969 {
970         McAccount *account;
971         GtkWidget *error_widget;
972
973         account = g_object_get_data (G_OBJECT (button), "account");
974         error_widget = g_hash_table_lookup (window->errors, account);
975         gtk_widget_destroy (error_widget);
976         g_hash_table_remove (window->errors, account);
977 }
978
979 static void
980 main_window_error_display (EmpathyMainWindow *window,
981                            McAccount         *account,
982                            const gchar       *message)
983 {
984         GtkWidget *child;
985         GtkWidget *table;
986         GtkWidget *image;
987         GtkWidget *button_edit;
988         GtkWidget *alignment;
989         GtkWidget *hbox;
990         GtkWidget *label;
991         GtkWidget *fixed;
992         GtkWidget *vbox;
993         GtkWidget *button_close;
994         gchar     *str;
995
996         child = g_hash_table_lookup (window->errors, account);
997         if (child) {
998                 label = g_object_get_data (G_OBJECT (child), "label");
999
1000                 /* Just set the latest error and return */
1001                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1002                                                mc_account_get_display_name (account),
1003                                                message);
1004                 gtk_label_set_markup (GTK_LABEL (label), str);
1005                 g_free (str);
1006
1007                 return;
1008         }
1009
1010         child = gtk_vbox_new (FALSE, 0);
1011         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
1012         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
1013         gtk_widget_show (child);
1014
1015         table = gtk_table_new (2, 4, FALSE);
1016         gtk_widget_show (table);
1017         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
1018         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1019         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
1020
1021         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
1022         gtk_widget_show (image);
1023         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
1024                           (GtkAttachOptions) (GTK_FILL),
1025                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1026         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
1027
1028         button_edit = gtk_button_new ();
1029         gtk_widget_show (button_edit);
1030         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
1031                           (GtkAttachOptions) (GTK_FILL),
1032                           (GtkAttachOptions) (0), 0, 0);
1033
1034         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1035         gtk_widget_show (alignment);
1036         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
1037
1038         hbox = gtk_hbox_new (FALSE, 2);
1039         gtk_widget_show (hbox);
1040         gtk_container_add (GTK_CONTAINER (alignment), hbox);
1041
1042         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
1043         gtk_widget_show (image);
1044         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1045
1046         label = gtk_label_new_with_mnemonic (_("_Edit account"));
1047         gtk_widget_show (label);
1048         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1049
1050         fixed = gtk_fixed_new ();
1051         gtk_widget_show (fixed);
1052         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
1053                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1054                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1055
1056         vbox = gtk_vbox_new (FALSE, 6);
1057         gtk_widget_show (vbox);
1058         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
1059                           (GtkAttachOptions) (GTK_FILL),
1060                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1061
1062         button_close = gtk_button_new ();
1063         gtk_widget_show (button_close);
1064         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
1065         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
1066
1067
1068         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
1069         gtk_widget_show (image);
1070         gtk_container_add (GTK_CONTAINER (button_close), image);
1071
1072         label = gtk_label_new ("");
1073         gtk_widget_show (label);
1074         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
1075                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
1076                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
1077         gtk_widget_set_size_request (label, 175, -1);
1078         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1079         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1080         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1081
1082         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1083                                        mc_account_get_display_name (account),
1084                                        message);
1085         gtk_label_set_markup (GTK_LABEL (label), str);
1086         g_free (str);
1087
1088         g_object_set_data (G_OBJECT (child), "label", label);
1089         g_object_set_data_full (G_OBJECT (button_edit),
1090                                 "account", g_object_ref (account),
1091                                 g_object_unref);
1092         g_object_set_data_full (G_OBJECT (button_close),
1093                                 "account", g_object_ref (account),
1094                                 g_object_unref);
1095
1096         g_signal_connect (button_edit, "clicked",
1097                           G_CALLBACK (main_window_error_edit_clicked_cb),
1098                           window);
1099
1100         g_signal_connect (button_close, "clicked",
1101                           G_CALLBACK (main_window_error_clear_clicked_cb),
1102                           window);
1103
1104         gtk_widget_show (window->errors_vbox);
1105
1106         g_hash_table_insert (window->errors, g_object_ref (account), child);
1107 }
1108
1109 static void
1110 main_window_status_changed_cb (MissionControl           *mc,
1111                                TpConnectionStatus        status,
1112                                McPresence                presence,
1113                                TpConnectionStatusReason  reason,
1114                                const gchar              *unique_name,
1115                                EmpathyMainWindow        *window)
1116 {
1117         McAccount *account;
1118
1119         main_window_update_status (window);
1120
1121         account = mc_account_lookup (unique_name);
1122
1123         if (status == TP_CONNECTION_STATUS_DISCONNECTED &&
1124             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
1125                 const gchar *message;
1126
1127                 switch (reason) {
1128                 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
1129                         message = _("No error specified");
1130                         break;
1131                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
1132                         message = _("Network error");
1133                         break;
1134                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
1135                         message = _("Authentication failed");
1136                         break;
1137                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
1138                         message = _("Encryption error");
1139                         break;
1140                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
1141                         message = _("Name in use");
1142                         break;
1143                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
1144                         message = _("Certificate not provided");
1145                         break;
1146                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
1147                         message = _("Certificate untrusted");
1148                         break;
1149                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
1150                         message = _("Certificate expired");
1151                         break;
1152                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
1153                         message = _("Certificate not activated");
1154                         break;
1155                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
1156                         message = _("Certificate hostname mismatch");
1157                         break;
1158                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
1159                         message = _("Certificate fingerprint mismatch");
1160                         break;
1161                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
1162                         message = _("Certificate self-signed");
1163                         break;
1164                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
1165                         message = _("Certificate error");
1166                         break;
1167                 default:
1168                         message = _("Unknown error");
1169                         break;
1170                 }
1171
1172                 main_window_error_display (window, account, message);
1173         }
1174
1175         if (status == TP_CONNECTION_STATUS_DISCONNECTED) {
1176                 ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
1177                                         CA_PROP_EVENT_ID, "service-logout",
1178                                         CA_PROP_EVENT_DESCRIPTION, _("Disconnected from server"),
1179                                         CA_PROP_APPLICATION_NAME, g_get_application_name (),
1180                                         NULL);
1181         }
1182
1183         if (status == TP_CONNECTION_STATUS_CONNECTED) {
1184                 GtkWidget *error_widget;
1185
1186                 ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
1187                                         CA_PROP_EVENT_ID, "service-login",
1188                                         CA_PROP_EVENT_DESCRIPTION, _("Connected to server"),
1189                                         CA_PROP_APPLICATION_NAME, g_get_application_name (),
1190                                         NULL);
1191                 /* Account connected without error, remove error message if any */
1192                 error_widget = g_hash_table_lookup (window->errors, account);
1193                 if (error_widget) {
1194                         gtk_widget_destroy (error_widget);
1195                         g_hash_table_remove (window->errors, account);
1196                 }
1197         }
1198
1199         g_object_unref (account);
1200 }
1201
1202 static void
1203 main_window_update_status (EmpathyMainWindow *window)
1204 {
1205         GList *accounts, *l;
1206         guint  connected = 0;
1207         guint  connecting = 0;
1208         guint  disconnected = 0;
1209
1210         /* Count number of connected/connecting/disconnected accounts */
1211         accounts = mc_accounts_list (); 
1212         for (l = accounts; l; l = l->next) {
1213                 McAccount *account;
1214                 guint      status;
1215
1216                 account = l->data;
1217
1218                 status = mission_control_get_connection_status (window->mc,
1219                                                                 account,
1220                                                                 NULL);
1221
1222                 if (status == 0) {
1223                         connected++;
1224                 } else if (status == 1) {
1225                         connecting++;
1226                 } else if (status == 2) {
1227                         disconnected++;
1228                 }
1229
1230                 g_object_unref (account);
1231         }
1232         g_list_free (accounts);
1233
1234         /* Update the spinner state */
1235         if (connecting > 0) {
1236                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
1237         } else {
1238                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
1239         }
1240
1241         /* Update widgets sensibility */
1242         for (l = window->widgets_connected; l; l = l->next) {
1243                 gtk_widget_set_sensitive (l->data, (connected > 0));
1244         }
1245
1246         for (l = window->widgets_disconnected; l; l = l->next) {
1247                 gtk_widget_set_sensitive (l->data, (disconnected > 0));
1248         }
1249 }
1250
1251 /*
1252  * Accels
1253  */
1254 static void
1255 main_window_accels_load (void)
1256 {
1257         gchar *filename;
1258
1259         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1260         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1261                 DEBUG ("Loading from:'%s'", filename);
1262                 gtk_accel_map_load (filename);
1263         }
1264
1265         g_free (filename);
1266 }
1267
1268 static void
1269 main_window_accels_save (void)
1270 {
1271         gchar *dir;
1272         gchar *file_with_path;
1273
1274         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1275         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1276         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1277         g_free (dir);
1278
1279         DEBUG ("Saving to:'%s'", file_with_path);
1280         gtk_accel_map_save (file_with_path);
1281
1282         g_free (file_with_path);
1283 }
1284
1285 static void
1286 main_window_connection_items_setup (EmpathyMainWindow *window,
1287                                     GladeXML          *glade)
1288 {
1289         GList         *list;
1290         GtkWidget     *w;
1291         gint           i;
1292         const gchar *widgets_connected[] = {
1293                 "room",
1294                 "chat_new_message",
1295                 "chat_add_contact",
1296                 "edit_personal_information"
1297         };
1298         const gchar *widgets_disconnected[] = {
1299         };
1300
1301         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1302                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1303                 list = g_list_prepend (list, w);
1304         }
1305
1306         window->widgets_connected = list;
1307
1308         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
1309                 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
1310                 list = g_list_prepend (list, w);
1311         }
1312
1313         window->widgets_disconnected = list;
1314 }
1315
1316 static gboolean
1317 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1318 {
1319         gint x, y, w, h;
1320
1321         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1322         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1323
1324         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1325
1326         window->size_timeout_id = 0;
1327
1328         return FALSE;
1329 }
1330
1331 static gboolean
1332 main_window_configure_event_cb (GtkWidget         *widget,
1333                                 GdkEventConfigure *event,
1334                                 EmpathyMainWindow *window)
1335 {
1336         if (window->size_timeout_id) {
1337                 g_source_remove (window->size_timeout_id);
1338         }
1339
1340         window->size_timeout_id = g_timeout_add_seconds (1,
1341                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1342                                                          window);
1343
1344         return FALSE;
1345 }
1346
1347 static void
1348 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1349                                     const gchar *key,
1350                                     gpointer     check_menu_item)
1351 {
1352         gboolean show_offline;
1353
1354         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1355                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1356                                                 show_offline);
1357         }
1358 }
1359
1360 static void
1361 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1362                                     const gchar       *key,
1363                                     EmpathyMainWindow *window)
1364 {
1365         gboolean show_avatars;
1366
1367         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1368                 empathy_contact_list_store_set_show_avatars (window->list_store,
1369                                                             show_avatars);
1370         }
1371 }
1372
1373 static void
1374 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1375                                             const gchar       *key,
1376                                             EmpathyMainWindow *window)
1377 {
1378         gboolean compact_contact_list;
1379
1380         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1381                 empathy_contact_list_store_set_is_compact (window->list_store,
1382                                                           compact_contact_list);
1383         }
1384 }
1385
1386 static void
1387 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1388                                       const gchar       *key,
1389                                       EmpathyMainWindow *window)
1390 {
1391         gchar *str = NULL;
1392
1393         if (empathy_conf_get_string (conf, key, &str) && str) {
1394                 GType       type;
1395                 GEnumClass *enum_class;
1396                 GEnumValue *enum_value;
1397
1398                 type = empathy_contact_list_store_sort_get_type ();
1399                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1400                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1401                 g_free (str);
1402
1403                 if (enum_value) {
1404                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1405                                                                        enum_value->value);
1406                 }
1407         }
1408 }
1409