]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
empathy_mission_control_new -> dup_singleton.
[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-account-manager.h>
34 #include <libempathy/empathy-dispatcher.h>
35 #include <libempathy/empathy-chatroom-manager.h>
36 #include <libempathy/empathy-chatroom.h>
37 #include <libempathy/empathy-contact-list.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/empathy-contact-factory.h>
40 #include <libempathy/empathy-status-presets.h>
41
42 #include <libempathy-gtk/empathy-contact-dialogs.h>
43 #include <libempathy-gtk/empathy-contact-list-store.h>
44 #include <libempathy-gtk/empathy-contact-list-view.h>
45 #include <libempathy-gtk/empathy-presence-chooser.h>
46 #include <libempathy-gtk/empathy-ui-utils.h>
47 #include <libempathy-gtk/empathy-geometry.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-new-message-dialog.h>
51 #include <libempathy-gtk/empathy-gtk-enum-types.h>
52
53 #include <libmissioncontrol/mission-control.h>
54
55 #include "empathy-accounts-dialog.h"
56 #include "empathy-main-window.h"
57 #include "ephy-spinner.h"
58 #include "empathy-preferences.h"
59 #include "empathy-about-dialog.h"
60 #include "empathy-new-chatroom-dialog.h"
61 #include "empathy-chatrooms-window.h"
62 #include "empathy-event-manager.h"
63
64 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
65 #include <libempathy/empathy-debug.h>
66
67 /* Flashing delay for icons (milliseconds). */
68 #define FLASH_TIMEOUT 500
69
70 /* Minimum width of roster window if something goes wrong. */
71 #define MIN_WIDTH 50
72
73 /* Accels (menu shortcuts) can be configured and saved */
74 #define ACCELS_FILENAME "accels.txt"
75
76 /* Name in the geometry file */
77 #define GEOMETRY_NAME "main-window"
78
79 typedef struct {
80         EmpathyContactListView  *list_view;
81         EmpathyContactListStore *list_store;
82         MissionControl          *mc;
83         EmpathyAccountManager   *account_manager;
84         EmpathyChatroomManager  *chatroom_manager;
85         EmpathyEventManager     *event_manager;
86         guint                    flash_timeout_id;
87         gboolean                 flash_on;
88
89         GtkWidget              *window;
90         GtkWidget              *main_vbox;
91         GtkWidget              *throbber;
92         GtkWidget              *presence_toolbar;
93         GtkWidget              *presence_chooser;
94         GtkWidget              *errors_vbox;
95
96         GtkWidget              *room;
97         GtkWidget              *room_menu;
98         GtkWidget              *room_sep;
99         GtkWidget              *room_join_favorites;
100         GtkWidget              *edit_context;
101         GtkWidget              *edit_context_separator;
102
103         guint                   size_timeout_id;
104         GHashTable             *errors;
105
106         /* Widgets that are enabled when there are connected accounts */
107         GList                  *widgets_connected;
108 } EmpathyMainWindow;
109
110 static void     main_window_destroy_cb                         (GtkWidget                *widget,
111                                                                 EmpathyMainWindow        *window);
112 static void     main_window_favorite_chatroom_menu_setup       (EmpathyMainWindow        *window);
113 static void     main_window_favorite_chatroom_menu_added_cb    (EmpathyChatroomManager   *manager,
114                                                                 EmpathyChatroom          *chatroom,
115                                                                 EmpathyMainWindow        *window);
116 static void     main_window_favorite_chatroom_menu_removed_cb  (EmpathyChatroomManager   *manager,
117                                                                 EmpathyChatroom          *chatroom,
118                                                                 EmpathyMainWindow        *window);
119 static void     main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem              *menu_item,
120                                                                 EmpathyChatroom          *chatroom);
121 static void     main_window_favorite_chatroom_menu_update      (EmpathyMainWindow        *window);
122 static void     main_window_favorite_chatroom_menu_add         (EmpathyMainWindow        *window,
123                                                                 EmpathyChatroom          *chatroom);
124 static void     main_window_favorite_chatroom_join             (EmpathyChatroom          *chatroom);
125 static void     main_window_chat_quit_cb                       (GtkWidget                *widget,
126                                                                 EmpathyMainWindow        *window);
127 static void     main_window_chat_new_message_cb                (GtkWidget                *widget,
128                                                                 EmpathyMainWindow        *window);
129 static void     main_window_chat_history_cb                    (GtkWidget                *widget,
130                                                                 EmpathyMainWindow        *window);
131 static void     main_window_room_join_new_cb                   (GtkWidget                *widget,
132                                                                 EmpathyMainWindow        *window);
133 static void     main_window_room_join_favorites_cb             (GtkWidget                *widget,
134                                                                 EmpathyMainWindow        *window);
135 static void     main_window_room_manage_favorites_cb           (GtkWidget                *widget,
136                                                                 EmpathyMainWindow        *window);
137 static void     main_window_chat_add_contact_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_update_status                      (EmpathyMainWindow        *window,
158                                                                 EmpathyAccountManager    *manager);
159 static void     main_window_error_display                      (EmpathyMainWindow        *window,
160                                                                 McAccount                *account,
161                                                                 const gchar              *message);
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         empathy_sound_play (GTK_WIDGET (window->window),
290                             EMPATHY_SOUND_CONVERSATION_NEW);
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 static void
373 main_window_connection_changed_cb (EmpathyAccountManager *manager,
374                                    McAccount *account,
375                                    TpConnectionStatusReason reason,
376                                    TpConnectionStatus current,
377                                    TpConnectionStatus previous,
378                                    EmpathyMainWindow *window)
379 {
380         main_window_update_status (window, manager);
381
382         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
383             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
384                 const gchar *message;
385
386                 switch (reason) {
387                 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
388                         message = _("No error specified");
389                         break;
390                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
391                         message = _("Network error");
392                         break;
393                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
394                         message = _("Authentication failed");
395                         break;
396                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
397                         message = _("Encryption error");
398                         break;
399                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
400                         message = _("Name in use");
401                         break;
402                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
403                         message = _("Certificate not provided");
404                         break;
405                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
406                         message = _("Certificate untrusted");
407                         break;
408                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
409                         message = _("Certificate expired");
410                         break;
411                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
412                         message = _("Certificate not activated");
413                         break;
414                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
415                         message = _("Certificate hostname mismatch");
416                         break;
417                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
418                         message = _("Certificate fingerprint mismatch");
419                         break;
420                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
421                         message = _("Certificate self-signed");
422                         break;
423                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
424                         message = _("Certificate error");
425                         break;
426                 default:
427                         message = _("Unknown error");
428                         break;
429                 }
430
431                 main_window_error_display (window, account, message);
432         }
433
434         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
435                 empathy_sound_play (GTK_WIDGET (window->window),
436                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
437         }
438
439         if (current == TP_CONNECTION_STATUS_CONNECTED) {
440                 GtkWidget *error_widget;
441
442                 empathy_sound_play (GTK_WIDGET (window->window),
443                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
444
445                 /* Account connected without error, remove error message if any */
446                 error_widget = g_hash_table_lookup (window->errors, account);
447                 if (error_widget) {
448                         gtk_widget_destroy (error_widget);
449                         g_hash_table_remove (window->errors, account);
450                 }
451         }
452 }
453
454 static void
455 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
456                                          EmpathyContact *contact,
457                                          McPresence current,
458                                          McPresence previous,
459                                          EmpathyMainWindow *window)
460 {
461         McAccount *account;
462         gboolean should_play;
463
464         account = empathy_contact_get_account (contact);
465         should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
466
467         if (!should_play) {
468                 return;
469         }
470
471         if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
472                 /* someone is logging in */
473                 empathy_sound_play (GTK_WIDGET (window->window),
474                                     EMPATHY_SOUND_CONTACT_CONNECTED);
475                 return;
476         }
477
478         if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
479                 /* someone is logging off */
480                 empathy_sound_play (GTK_WIDGET (window->window),
481                                     EMPATHY_SOUND_CONTACT_DISCONNECTED);
482         }
483 }
484
485 GtkWidget *
486 empathy_main_window_show (void)
487 {
488         static EmpathyMainWindow *window = NULL;
489         EmpathyContactList       *list_iface;
490         EmpathyContactMonitor    *monitor;
491         GladeXML                 *glade;
492         EmpathyConf               *conf;
493         GtkWidget                *sw;
494         GtkWidget                *show_offline_widget;
495         GtkWidget                *ebox;
496         GtkToolItem              *item;
497         gboolean                  show_offline;
498         gboolean                  show_avatars;
499         gboolean                  compact_contact_list;
500         gint                      x, y, w, h;
501         gchar                    *filename;
502         GSList                   *l;
503
504         if (window) {
505                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
506                 return window->window;
507         }
508
509         window = g_new0 (EmpathyMainWindow, 1);
510
511         /* Set up interface */
512         filename = empathy_file_lookup ("empathy-main-window.glade", "src");
513         glade = empathy_glade_get_file (filename,
514                                        "main_window",
515                                        NULL,
516                                        "main_window", &window->window,
517                                        "main_vbox", &window->main_vbox,
518                                        "errors_vbox", &window->errors_vbox,
519                                        "chat_show_offline", &show_offline_widget,
520                                        "room", &window->room,
521                                        "room_sep", &window->room_sep,
522                                        "room_join_favorites", &window->room_join_favorites,
523                                        "edit_context", &window->edit_context,
524                                        "edit_context_separator", &window->edit_context_separator,
525                                        "presence_toolbar", &window->presence_toolbar,
526                                        "roster_scrolledwindow", &sw,
527                                        NULL);
528         g_free (filename);
529
530         empathy_glade_connect (glade,
531                               window,
532                               "main_window", "destroy", main_window_destroy_cb,
533                               "main_window", "configure_event", main_window_configure_event_cb,
534                               "chat_quit", "activate", main_window_chat_quit_cb,
535                               "chat_new_message", "activate", main_window_chat_new_message_cb,
536                               "chat_history", "activate", main_window_chat_history_cb,
537                               "room_join_new", "activate", main_window_room_join_new_cb,
538                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
539                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
540                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
541                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
542                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
543                               "edit_accounts", "activate", main_window_edit_accounts_cb,
544                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
545                               "edit_preferences", "activate", main_window_edit_preferences_cb,
546                               "help_about", "activate", main_window_help_about_cb,
547                               "help_contents", "activate", main_window_help_contents_cb,
548                               NULL);
549
550         /* Set up connection related widgets. */
551         main_window_connection_items_setup (window, glade);
552         g_object_unref (glade);
553
554         window->mc = empathy_mission_control_dup_singleton ();
555         window->account_manager = empathy_account_manager_dup_singleton ();
556
557         g_signal_connect (window->account_manager,
558                           "account-connection-changed",
559                           G_CALLBACK (main_window_connection_changed_cb), window);
560
561         window->errors = g_hash_table_new_full (empathy_account_hash,
562                                                 empathy_account_equal,
563                                                 g_object_unref,
564                                                 NULL);
565
566         /* Set up menu */
567         main_window_favorite_chatroom_menu_setup (window);
568
569         gtk_widget_hide (window->edit_context);
570         gtk_widget_hide (window->edit_context_separator);
571
572         /* Set up presence chooser */
573         window->presence_chooser = empathy_presence_chooser_new ();
574         gtk_widget_show (window->presence_chooser);
575         item = gtk_tool_item_new ();
576         gtk_widget_show (GTK_WIDGET (item));
577         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
578         gtk_tool_item_set_is_important (item, TRUE);
579         gtk_tool_item_set_expand (item, TRUE);
580         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
581
582         /* Set up the throbber */
583         ebox = gtk_event_box_new ();
584         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
585         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
586         g_signal_connect (ebox,
587                           "button-press-event",
588                           G_CALLBACK (main_window_throbber_button_press_event_cb),
589                           window);
590         gtk_widget_show (ebox);
591
592         window->throbber = ephy_spinner_new ();
593         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
594         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
595         gtk_widget_show (window->throbber);
596
597         item = gtk_tool_item_new ();
598         gtk_container_add (GTK_CONTAINER (item), ebox);
599         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
600         gtk_widget_show (GTK_WIDGET (item));
601
602         /* Set up contact list. */
603         empathy_status_presets_get_all ();
604
605         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
606         monitor = empathy_contact_list_get_monitor (list_iface);
607         window->list_store = empathy_contact_list_store_new (list_iface);
608         window->list_view = empathy_contact_list_view_new (window->list_store,
609                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
610                                                            EMPATHY_CONTACT_FEATURE_ALL);
611         g_signal_connect (monitor, "contact-presence-changed",
612                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
613         g_object_unref (list_iface);
614
615         gtk_widget_show (GTK_WIDGET (window->list_view));
616         gtk_container_add (GTK_CONTAINER (sw),
617                            GTK_WIDGET (window->list_view));
618         g_signal_connect (window->list_view, "row-activated",
619                           G_CALLBACK (main_window_row_activated_cb),
620                           window);
621
622         /* Load user-defined accelerators. */
623         main_window_accels_load ();
624
625         /* Set window size. */
626         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
627
628         if (w >= 1 && h >= 1) {
629                 /* Use the defaults from the glade file if we
630                  * don't have good w, h geometry.
631                  */
632                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
633                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
634         }
635
636         if (x >= 0 && y >= 0) {
637                 /* Let the window manager position it if we
638                  * don't have good x, y coordinates.
639                  */
640                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
641                 gtk_window_move (GTK_WINDOW (window->window), x, y);
642         }
643
644         /* Enable event handling */
645         window->event_manager = empathy_event_manager_dup_singleton ();
646         g_signal_connect (window->event_manager, "event-added",
647                           G_CALLBACK (main_window_event_added_cb),
648                           window);
649         g_signal_connect (window->event_manager, "event-removed",
650                           G_CALLBACK (main_window_event_removed_cb),
651                           window);
652
653         l = empathy_event_manager_get_events (window->event_manager);
654         while (l) {
655                 main_window_event_added_cb (window->event_manager,
656                                             l->data, window);
657                 l = l->next;
658         }
659
660         conf = empathy_conf_get ();
661
662         /* Show offline ? */
663         empathy_conf_get_bool (conf,
664                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
665                               &show_offline);
666         empathy_conf_notify_add (conf,
667                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
668                                 main_window_notify_show_offline_cb,
669                                 show_offline_widget);
670
671         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
672                                         show_offline);
673
674         /* Show avatars ? */
675         empathy_conf_get_bool (conf,
676                               EMPATHY_PREFS_UI_SHOW_AVATARS,
677                               &show_avatars);
678         empathy_conf_notify_add (conf,
679                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
680                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
681                                 window);
682         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
683
684         /* Is compact ? */
685         empathy_conf_get_bool (conf,
686                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
687                               &compact_contact_list);
688         empathy_conf_notify_add (conf,
689                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
690                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
691                                 window);
692         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
693
694         /* Sort criterium */
695         empathy_conf_notify_add (conf,
696                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
697                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
698                                 window);
699         main_window_notify_sort_criterium_cb (conf,
700                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
701                                               window);
702
703         main_window_update_status (window, window->account_manager);
704
705         return window->window;
706 }
707
708 static void
709 main_window_destroy_cb (GtkWidget         *widget,
710                         EmpathyMainWindow *window)
711 {
712         /* Save user-defined accelerators. */
713         main_window_accels_save ();
714
715         g_signal_handlers_disconnect_by_func (window->account_manager,
716                                               main_window_connection_changed_cb,
717                                               window);
718
719         if (window->size_timeout_id) {
720                 g_source_remove (window->size_timeout_id);
721         }
722
723         g_list_free (window->widgets_connected);
724
725         g_object_unref (window->mc);
726         g_object_unref (window->account_manager);
727         g_object_unref (window->list_store);
728         g_hash_table_destroy (window->errors);
729
730         g_signal_handlers_disconnect_by_func (window->event_manager,
731                                               main_window_event_added_cb,
732                                               window);
733         g_signal_handlers_disconnect_by_func (window->event_manager,
734                                               main_window_event_removed_cb,
735                                               window);
736         g_object_unref (window->event_manager);
737
738         g_free (window);
739 }
740
741 static void
742 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
743 {
744         GList *chatrooms, *l;
745
746         window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
747         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
748         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
749
750         for (l = chatrooms; l; l = l->next) {
751                 main_window_favorite_chatroom_menu_add (window, l->data);
752         }
753
754         if (!chatrooms) {
755                 gtk_widget_hide (window->room_sep);
756         }
757
758         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
759
760         g_signal_connect (window->chatroom_manager, "chatroom-added",
761                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
762                           window);
763         g_signal_connect (window->chatroom_manager, "chatroom-removed",
764                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
765                           window);
766
767         g_list_free (chatrooms);
768 }
769
770 static void
771 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
772                                              EmpathyChatroom        *chatroom,
773                                              EmpathyMainWindow     *window)
774 {
775         main_window_favorite_chatroom_menu_add (window, chatroom);
776         gtk_widget_show (window->room_sep);
777         gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
778 }
779
780 static void
781 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
782                                                EmpathyChatroom        *chatroom,
783                                                EmpathyMainWindow     *window)
784 {
785         GtkWidget *menu_item;
786
787         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
788
789         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
790         gtk_widget_destroy (menu_item);
791
792         main_window_favorite_chatroom_menu_update (window);
793 }
794
795 static void
796 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
797                                                 EmpathyChatroom *chatroom)
798 {
799         main_window_favorite_chatroom_join (chatroom);
800 }
801
802 static void
803 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
804 {
805         GList *chatrooms;
806
807         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
808
809         if (chatrooms) {
810                 gtk_widget_show (window->room_sep);
811         } else {
812                 gtk_widget_hide (window->room_sep);
813         }
814
815         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
816         g_list_free (chatrooms);
817 }
818
819 static void
820 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
821                                         EmpathyChatroom    *chatroom)
822 {
823         GtkWidget   *menu_item;
824         const gchar *name;
825
826         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
827                 return;
828         }
829
830         name = empathy_chatroom_get_name (chatroom);
831         menu_item = gtk_menu_item_new_with_label (name);
832
833         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
834         g_signal_connect (menu_item, "activate",
835                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
836                           chatroom);
837
838         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
839                                menu_item, 3);
840
841         gtk_widget_show (menu_item);
842 }
843
844 static void
845 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
846 {
847         MissionControl *mc;
848         McAccount      *account;
849         const gchar    *room;
850
851         mc = empathy_mission_control_dup_singleton ();
852         account = empathy_chatroom_get_account (chatroom);
853         room = empathy_chatroom_get_room (chatroom);
854
855          if (mission_control_get_connection_status (mc, account, NULL) !=
856                         TP_CONNECTION_STATUS_CONNECTED) {
857                 return;
858         }
859
860         DEBUG ("Requesting channel for '%s'", room);
861         empathy_dispatcher_join_muc (account, room, NULL, NULL);
862
863         g_object_unref (mc);
864 }
865
866 static void
867 main_window_chat_quit_cb (GtkWidget         *widget,
868                           EmpathyMainWindow *window)
869 {
870         gtk_main_quit ();
871 }
872
873 static void
874 main_window_chat_new_message_cb (GtkWidget         *widget,
875                                  EmpathyMainWindow *window)
876 {
877         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
878 }
879
880 static void
881 main_window_chat_history_cb (GtkWidget         *widget,
882                              EmpathyMainWindow *window)
883 {
884         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
885 }
886
887 static void
888 main_window_room_join_new_cb (GtkWidget         *widget,
889                               EmpathyMainWindow *window)
890 {
891         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
892 }
893
894 static void
895 main_window_room_join_favorites_cb (GtkWidget         *widget,
896                                     EmpathyMainWindow *window)
897 {
898         GList *chatrooms, *l;
899
900         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
901         for (l = chatrooms; l; l = l->next) {
902                 main_window_favorite_chatroom_join (l->data);
903         }
904         g_list_free (chatrooms);
905 }
906
907 static void
908 main_window_room_manage_favorites_cb (GtkWidget         *widget,
909                                       EmpathyMainWindow *window)
910 {
911         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
912 }
913
914 static void
915 main_window_chat_add_contact_cb (GtkWidget         *widget,
916                                  EmpathyMainWindow *window)
917 {
918         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
919 }
920
921 static void
922 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
923                                   EmpathyMainWindow *window)
924 {
925         gboolean current;
926
927         current = gtk_check_menu_item_get_active (item);
928
929         empathy_conf_set_bool (empathy_conf_get (),
930                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
931                               current);
932
933         /* Turn off sound just while we alter the contact list. */
934         // FIXME: empathy_sound_set_enabled (FALSE);
935         empathy_contact_list_store_set_show_offline (window->list_store, current);
936         //empathy_sound_set_enabled (TRUE);
937 }
938
939 static gboolean
940 main_window_edit_button_press_event_cb (GtkWidget         *widget,
941                                         GdkEventButton    *event,
942                                         EmpathyMainWindow *window)
943 {
944         GtkWidget *submenu;
945
946         if (!event->button == 1) {
947                 return FALSE;
948         }
949
950         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
951         if (submenu) {
952                 GtkMenuItem *item;
953                 GtkWidget   *label;
954
955                 item = GTK_MENU_ITEM (window->edit_context);
956                 label = gtk_bin_get_child (GTK_BIN (item));
957                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
958
959                 gtk_widget_show (window->edit_context);
960                 gtk_widget_show (window->edit_context_separator);
961
962                 gtk_menu_item_set_submenu (item, submenu);
963
964                 return FALSE;
965         }
966
967         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
968         if (submenu) {
969                 GtkMenuItem *item;
970                 GtkWidget   *label;
971
972                 item = GTK_MENU_ITEM (window->edit_context);
973                 label = gtk_bin_get_child (GTK_BIN (item));
974                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
975
976                 gtk_widget_show (window->edit_context);
977                 gtk_widget_show (window->edit_context_separator);
978
979                 gtk_menu_item_set_submenu (item, submenu);
980
981                 return FALSE;
982         }
983
984         gtk_widget_hide (window->edit_context);
985         gtk_widget_hide (window->edit_context_separator);
986
987         return FALSE;
988 }
989
990 static void
991 main_window_edit_accounts_cb (GtkWidget         *widget,
992                               EmpathyMainWindow *window)
993 {
994         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
995 }
996
997 static void
998 main_window_edit_personal_information_cb (GtkWidget         *widget,
999                                           EmpathyMainWindow *window)
1000 {
1001         GSList *accounts;
1002
1003         accounts = mission_control_get_online_connections (window->mc, NULL);
1004         if (accounts) {
1005                 EmpathyContactFactory *factory;
1006                 EmpathyContact        *contact;
1007                 McAccount             *account;
1008
1009                 account = accounts->data;
1010                 factory = empathy_contact_factory_dup_singleton ();
1011                 contact = empathy_contact_factory_get_user (factory, account);
1012                 empathy_contact_run_until_ready (contact,
1013                                                  EMPATHY_CONTACT_READY_HANDLE |
1014                                                  EMPATHY_CONTACT_READY_ID,
1015                                                  NULL);
1016
1017                 empathy_contact_information_dialog_show (contact,
1018                                                          GTK_WINDOW (window->window),
1019                                                          TRUE, TRUE);
1020
1021                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
1022                 g_slist_free (accounts);
1023                 g_object_unref (factory);
1024                 g_object_unref (contact);
1025         }
1026 }
1027
1028 static void
1029 main_window_edit_preferences_cb (GtkWidget         *widget,
1030                                  EmpathyMainWindow *window)
1031 {
1032         empathy_preferences_show (GTK_WINDOW (window->window));
1033 }
1034
1035 static void
1036 main_window_help_about_cb (GtkWidget         *widget,
1037                            EmpathyMainWindow *window)
1038 {
1039         empathy_about_dialog_new (GTK_WINDOW (window->window));
1040 }
1041
1042 static void
1043 main_window_help_contents_cb (GtkWidget         *widget,
1044                               EmpathyMainWindow *window)
1045 {
1046         empathy_url_show (widget, "ghelp:empathy");
1047 }
1048
1049 static gboolean
1050 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
1051                                             GdkEventButton    *event,
1052                                             EmpathyMainWindow *window)
1053 {
1054         if (event->type != GDK_BUTTON_PRESS ||
1055             event->button != 1) {
1056                 return FALSE;
1057         }
1058
1059         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1060
1061         return FALSE;
1062 }
1063
1064 static void
1065 main_window_error_edit_clicked_cb (GtkButton         *button,
1066                                    EmpathyMainWindow *window)
1067 {
1068         McAccount *account;
1069         GtkWidget *error_widget;
1070
1071         account = g_object_get_data (G_OBJECT (button), "account");
1072         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
1073
1074         error_widget = g_hash_table_lookup (window->errors, account);
1075         gtk_widget_destroy (error_widget);
1076         g_hash_table_remove (window->errors, account);
1077 }
1078
1079 static void
1080 main_window_error_clear_clicked_cb (GtkButton         *button,
1081                                     EmpathyMainWindow *window)
1082 {
1083         McAccount *account;
1084         GtkWidget *error_widget;
1085
1086         account = g_object_get_data (G_OBJECT (button), "account");
1087         error_widget = g_hash_table_lookup (window->errors, account);
1088         gtk_widget_destroy (error_widget);
1089         g_hash_table_remove (window->errors, account);
1090 }
1091
1092 static void
1093 main_window_error_display (EmpathyMainWindow *window,
1094                            McAccount         *account,
1095                            const gchar       *message)
1096 {
1097         GtkWidget *child;
1098         GtkWidget *table;
1099         GtkWidget *image;
1100         GtkWidget *button_edit;
1101         GtkWidget *alignment;
1102         GtkWidget *hbox;
1103         GtkWidget *label;
1104         GtkWidget *fixed;
1105         GtkWidget *vbox;
1106         GtkWidget *button_close;
1107         gchar     *str;
1108
1109         child = g_hash_table_lookup (window->errors, account);
1110         if (child) {
1111                 label = g_object_get_data (G_OBJECT (child), "label");
1112
1113                 /* Just set the latest error and return */
1114                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1115                                                mc_account_get_display_name (account),
1116                                                message);
1117                 gtk_label_set_markup (GTK_LABEL (label), str);
1118                 g_free (str);
1119
1120                 return;
1121         }
1122
1123         child = gtk_vbox_new (FALSE, 0);
1124         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
1125         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
1126         gtk_widget_show (child);
1127
1128         table = gtk_table_new (2, 4, FALSE);
1129         gtk_widget_show (table);
1130         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
1131         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1132         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
1133
1134         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
1135         gtk_widget_show (image);
1136         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
1137                           (GtkAttachOptions) (GTK_FILL),
1138                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1139         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
1140
1141         button_edit = gtk_button_new ();
1142         gtk_widget_show (button_edit);
1143         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
1144                           (GtkAttachOptions) (GTK_FILL),
1145                           (GtkAttachOptions) (0), 0, 0);
1146
1147         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1148         gtk_widget_show (alignment);
1149         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
1150
1151         hbox = gtk_hbox_new (FALSE, 2);
1152         gtk_widget_show (hbox);
1153         gtk_container_add (GTK_CONTAINER (alignment), hbox);
1154
1155         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
1156         gtk_widget_show (image);
1157         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1158
1159         label = gtk_label_new_with_mnemonic (_("_Edit account"));
1160         gtk_widget_show (label);
1161         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1162
1163         fixed = gtk_fixed_new ();
1164         gtk_widget_show (fixed);
1165         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
1166                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1167                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1168
1169         vbox = gtk_vbox_new (FALSE, 6);
1170         gtk_widget_show (vbox);
1171         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
1172                           (GtkAttachOptions) (GTK_FILL),
1173                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1174
1175         button_close = gtk_button_new ();
1176         gtk_widget_show (button_close);
1177         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
1178         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
1179
1180
1181         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
1182         gtk_widget_show (image);
1183         gtk_container_add (GTK_CONTAINER (button_close), image);
1184
1185         label = gtk_label_new ("");
1186         gtk_widget_show (label);
1187         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
1188                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
1189                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
1190         gtk_widget_set_size_request (label, 175, -1);
1191         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1192         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1193         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1194
1195         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1196                                        mc_account_get_display_name (account),
1197                                        message);
1198         gtk_label_set_markup (GTK_LABEL (label), str);
1199         g_free (str);
1200
1201         g_object_set_data (G_OBJECT (child), "label", label);
1202         g_object_set_data_full (G_OBJECT (button_edit),
1203                                 "account", g_object_ref (account),
1204                                 g_object_unref);
1205         g_object_set_data_full (G_OBJECT (button_close),
1206                                 "account", g_object_ref (account),
1207                                 g_object_unref);
1208
1209         g_signal_connect (button_edit, "clicked",
1210                           G_CALLBACK (main_window_error_edit_clicked_cb),
1211                           window);
1212
1213         g_signal_connect (button_close, "clicked",
1214                           G_CALLBACK (main_window_error_clear_clicked_cb),
1215                           window);
1216
1217         gtk_widget_show (window->errors_vbox);
1218
1219         g_hash_table_insert (window->errors, g_object_ref (account), child);
1220 }
1221
1222 static void
1223 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
1224 {
1225         int  connected;
1226         int  connecting;
1227         GList *l;
1228
1229         /* Count number of connected/connecting/disconnected accounts */
1230         connected = empathy_account_manager_get_connected_accounts (manager);
1231         connecting = empathy_account_manager_get_connecting_accounts (manager);
1232
1233         /* Update the spinner state */
1234         if (connecting > 0) {
1235                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
1236         } else {
1237                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
1238         }
1239
1240         /* Update widgets sensibility */
1241         for (l = window->widgets_connected; l; l = l->next) {
1242                 gtk_widget_set_sensitive (l->data, (connected > 0));
1243         }
1244 }
1245
1246 /*
1247  * Accels
1248  */
1249 static void
1250 main_window_accels_load (void)
1251 {
1252         gchar *filename;
1253
1254         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1255         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1256                 DEBUG ("Loading from:'%s'", filename);
1257                 gtk_accel_map_load (filename);
1258         }
1259
1260         g_free (filename);
1261 }
1262
1263 static void
1264 main_window_accels_save (void)
1265 {
1266         gchar *dir;
1267         gchar *file_with_path;
1268
1269         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1270         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1271         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1272         g_free (dir);
1273
1274         DEBUG ("Saving to:'%s'", file_with_path);
1275         gtk_accel_map_save (file_with_path);
1276
1277         g_free (file_with_path);
1278 }
1279
1280 static void
1281 main_window_connection_items_setup (EmpathyMainWindow *window,
1282                                     GladeXML          *glade)
1283 {
1284         GList         *list;
1285         GtkWidget     *w;
1286         gint           i;
1287         const gchar *widgets_connected[] = {
1288                 "room",
1289                 "chat_new_message",
1290                 "chat_add_contact",
1291                 "edit_personal_information"
1292         };
1293
1294         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1295                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1296                 list = g_list_prepend (list, w);
1297         }
1298
1299         window->widgets_connected = list;
1300 }
1301
1302 static gboolean
1303 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1304 {
1305         gint x, y, w, h;
1306
1307         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1308         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1309
1310         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1311
1312         window->size_timeout_id = 0;
1313
1314         return FALSE;
1315 }
1316
1317 static gboolean
1318 main_window_configure_event_cb (GtkWidget         *widget,
1319                                 GdkEventConfigure *event,
1320                                 EmpathyMainWindow *window)
1321 {
1322         if (window->size_timeout_id) {
1323                 g_source_remove (window->size_timeout_id);
1324         }
1325
1326         window->size_timeout_id = g_timeout_add_seconds (1,
1327                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1328                                                          window);
1329
1330         return FALSE;
1331 }
1332
1333 static void
1334 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1335                                     const gchar *key,
1336                                     gpointer     check_menu_item)
1337 {
1338         gboolean show_offline;
1339
1340         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1341                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1342                                                 show_offline);
1343         }
1344 }
1345
1346 static void
1347 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1348                                     const gchar       *key,
1349                                     EmpathyMainWindow *window)
1350 {
1351         gboolean show_avatars;
1352
1353         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1354                 empathy_contact_list_store_set_show_avatars (window->list_store,
1355                                                             show_avatars);
1356         }
1357 }
1358
1359 static void
1360 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1361                                             const gchar       *key,
1362                                             EmpathyMainWindow *window)
1363 {
1364         gboolean compact_contact_list;
1365
1366         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1367                 empathy_contact_list_store_set_is_compact (window->list_store,
1368                                                           compact_contact_list);
1369         }
1370 }
1371
1372 static void
1373 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1374                                       const gchar       *key,
1375                                       EmpathyMainWindow *window)
1376 {
1377         gchar *str = NULL;
1378
1379         if (empathy_conf_get_string (conf, key, &str) && str) {
1380                 GType       type;
1381                 GEnumClass *enum_class;
1382                 GEnumValue *enum_value;
1383
1384                 type = empathy_contact_list_store_sort_get_type ();
1385                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1386                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1387                 g_free (str);
1388
1389                 if (enum_value) {
1390                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1391                                                                        enum_value->value);
1392                 }
1393         }
1394 }
1395