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