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