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