]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-main-window.c
a34ad5bffb082c0f12d0db808cbee671469cbcc1
[empathy.git] / libempathy-gtk / empathy-main-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <sys/stat.h>
26 #include <gtk/gtk.h>
27 #include <glade/glade.h>
28 #include <glib/gi18n.h>
29
30 #include <libtelepathy/tp-helpers.h>
31 #include <libmissioncontrol/mission-control.h>
32
33 #include <libempathy/gossip-conf.h>
34 #include <libempathy/gossip-contact.h>
35 #include <libempathy/gossip-debug.h>
36
37 #include "empathy-main-window.h"
38 #include "ephy-spinner.h"
39 #include "gossip-contact-list.h"
40 #include "gossip-presence-chooser.h"
41 #include "gossip-ui-utils.h"
42 #include "gossip-status-presets.h"
43 #include "gossip-geometry.h"
44 #include "gossip-preferences.h"
45 #include "gossip-accounts-dialog.h"
46
47 #define DEBUG_DOMAIN "MainWindow"
48
49 /* Minimum width of roster window if something goes wrong. */
50 #define MIN_WIDTH 50
51
52 /* Accels (menu shortcuts) can be configured and saved */
53 #define ACCELS_FILENAME "accels.txt"
54
55 /* Flashing delay for icons (milliseconds). */
56 #define FLASH_TIMEOUT 500
57
58 /* Name in the geometry file */
59 #define GEOMETRY_NAME "main-window"
60
61 typedef struct {
62         GossipContactList *contact_list;
63         MissionControl    *mc;
64
65         /* Main widgets */
66         GtkWidget         *window;
67         GtkWidget         *main_vbox;
68
69         /* Tooltips for all widgets */
70         GtkTooltips       *tooltips;
71
72         /* Menu widgets */
73         GtkWidget         *room;
74         GtkWidget         *room_menu;
75         GtkWidget         *room_sep;
76         GtkWidget         *room_join_favorites;
77         GtkWidget         *edit_context;
78         GtkWidget         *edit_context_separator;
79
80         /* Throbber */
81         GtkWidget         *throbber;
82
83         /* Widgets that are enabled when we're connected/disconnected */
84         GList             *widgets_connected;
85         GList             *widgets_disconnected;
86
87         /* Status popup */
88         GtkWidget         *presence_toolbar;
89         GtkWidget         *presence_chooser;
90
91         /* Misc */
92         guint              size_timeout_id;
93 } EmpathyMainWindow;
94
95 static void     main_window_destroy_cb                     (GtkWidget           *widget,
96                                                             EmpathyMainWindow   *window);
97 static void     main_window_favorite_chatroom_menu_setup   (void);
98 static void     main_window_chat_quit_cb                   (GtkWidget           *widget,
99                                                             EmpathyMainWindow   *window);
100 static void     main_window_chat_new_message_cb            (GtkWidget           *widget,
101                                                             EmpathyMainWindow   *window);
102 static void     main_window_chat_history_cb                (GtkWidget           *widget,
103                                                             EmpathyMainWindow   *window);
104 static void     main_window_room_join_new_cb               (GtkWidget           *widget,
105                                                             EmpathyMainWindow   *window);
106 static void     main_window_room_join_favorites_cb         (GtkWidget           *widget,
107                                                             EmpathyMainWindow   *window);
108 static void     main_window_room_manage_favorites_cb       (GtkWidget           *widget,
109                                                             EmpathyMainWindow   *window);
110 static void     main_window_chat_add_contact_cb            (GtkWidget           *widget,
111                                                             EmpathyMainWindow   *window);
112 static void     main_window_chat_show_offline_cb           (GtkCheckMenuItem    *item,
113                                                             EmpathyMainWindow   *window);
114 static gboolean main_window_edit_button_press_event_cb     (GtkWidget           *widget,
115                                                             GdkEventButton      *event,
116                                                             EmpathyMainWindow   *window);
117 static void     main_window_edit_accounts_cb               (GtkWidget           *widget,
118                                                             EmpathyMainWindow   *window);
119 static void     main_window_edit_personal_information_cb   (GtkWidget           *widget,
120                                                             EmpathyMainWindow   *window);
121 static void     main_window_edit_preferences_cb            (GtkWidget           *widget,
122                                                             EmpathyMainWindow   *window);
123 static void     main_window_help_about_cb                  (GtkWidget           *widget,
124                                                             EmpathyMainWindow   *window);
125 static void     main_window_help_contents_cb               (GtkWidget           *widget,
126                                                             EmpathyMainWindow   *window);
127 static gboolean main_window_throbber_button_press_event_cb (GtkWidget           *throbber_ebox,
128                                                             GdkEventButton      *event,
129                                                             gpointer             user_data);
130 static void     main_window_accels_load                    (void);
131 static void     main_window_accels_save                    (void);
132 static void     main_window_connection_items_setup         (EmpathyMainWindow   *window,
133                                                             GladeXML            *glade);
134 //static void     main_window_connection_items_update        (void);
135 static void     main_window_presence_changed_cb            (DBusGProxy          *proxy,
136                                                             McPresence           state,
137                                                             EmpathyMainWindow   *window);
138 static void     main_window_presence_chooser_changed_cb    (GtkWidget           *chooser,
139                                                             McPresence           state,
140                                                             const gchar         *status,
141                                                             EmpathyMainWindow   *window);
142 static gboolean main_window_configure_event_timeout_cb     (EmpathyMainWindow   *window);
143 static gboolean main_window_configure_event_cb             (GtkWidget           *widget,
144                                                             GdkEventConfigure   *event,
145                                                             EmpathyMainWindow   *window);
146 static void     main_window_notify_show_offline_cb         (GossipConf          *conf,
147                                                             const gchar         *key,
148                                                             gpointer             check_menu_item);
149 static void     main_window_notify_show_avatars_cb         (GossipConf          *conf,
150                                                             const gchar         *key,
151                                                             EmpathyMainWindow   *window);
152 static void     main_window_notify_compact_contact_list_cb (GossipConf          *conf,
153                                                             const gchar         *key,
154                                                             EmpathyMainWindow   *window);
155 static void     main_window_notify_sort_criterium_cb       (GossipConf          *conf,
156                                                             const gchar         *key,
157                                                             EmpathyMainWindow   *window);
158
159 GtkWidget *
160 empathy_main_window_show (void)
161 {
162         static EmpathyMainWindow *window = NULL;
163         GladeXML                 *glade;
164         GossipConf               *conf;
165         GtkWidget                *sw;
166         GtkWidget                *show_offline_widget;
167         GtkWidget                *ebox;
168         GtkToolItem              *item;
169         gchar                    *str;
170         McPresence                state;
171         gboolean                  show_offline;
172         gboolean                  show_avatars;
173         gboolean                  compact_contact_list;
174         gint                      x, y, w, h;
175
176         if (window) {
177                 gtk_window_present (GTK_WINDOW (window->window));
178                 return window->window;
179         }
180
181         window = g_new0 (EmpathyMainWindow, 1);
182
183         /* Set up interface */
184         glade = gossip_glade_get_file ("empathy-main-window.glade",
185                                        "main_window",
186                                        NULL,
187                                        "main_window", &window->window,
188                                        "main_vbox", &window->main_vbox,
189                                        "chat_show_offline", &show_offline_widget,
190                                        "room", &window->room,
191                                        "room_sep", &window->room_sep,
192                                        "room_join_favorites", &window->room_join_favorites,
193                                        "edit_context", &window->edit_context,
194                                        "edit_context_separator", &window->edit_context_separator,
195                                        "presence_toolbar", &window->presence_toolbar,
196                                        "roster_scrolledwindow", &sw,
197                                        NULL);
198
199         gossip_glade_connect (glade,
200                               window,
201                               "main_window", "destroy", main_window_destroy_cb,
202                               "main_window", "configure_event", main_window_configure_event_cb,
203                               "chat_quit", "activate", main_window_chat_quit_cb,
204                               "chat_new_message", "activate", main_window_chat_new_message_cb,
205                               "chat_history", "activate", main_window_chat_history_cb,
206                               "room_join_new", "activate", main_window_room_join_new_cb,
207                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
208                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
209                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
210                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
211                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
212                               "edit_accounts", "activate", main_window_edit_accounts_cb,
213                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
214                               "edit_preferences", "activate", main_window_edit_preferences_cb,
215                               "help_about", "activate", main_window_help_about_cb,
216                               "help_contents", "activate", main_window_help_contents_cb,
217                               NULL);
218
219         /* Set up connection related widgets. */
220         main_window_connection_items_setup (window, glade);
221         g_object_unref (glade);
222
223         window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
224
225         /* Set up menu */
226         main_window_favorite_chatroom_menu_setup ();
227
228         gtk_widget_hide (window->edit_context);
229         gtk_widget_hide (window->edit_context_separator);
230
231         /* Set up presence chooser
232          * FIXME: Update status message not yet supported by MC 
233          */
234         window->mc = mission_control_new (tp_get_bus ());
235         window->presence_chooser = gossip_presence_chooser_new ();
236         gtk_widget_show (window->presence_chooser);
237         gossip_presence_chooser_set_flash_interval (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
238                                                     FLASH_TIMEOUT);
239         state = mission_control_get_presence (window->mc, NULL);
240         gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
241                                            state);
242         dbus_g_proxy_connect_signal (DBUS_G_PROXY (window->mc),
243                                      "PresenceStatusRequested",
244                                      G_CALLBACK (main_window_presence_changed_cb),
245                                      window, NULL);
246         g_signal_connect (window->presence_chooser,
247                           "changed",
248                           G_CALLBACK (main_window_presence_chooser_changed_cb),
249                           window);
250
251
252         item = gtk_tool_item_new ();
253         gtk_widget_show (GTK_WIDGET (item));
254         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
255         gtk_tool_item_set_is_important (item, TRUE);
256         gtk_tool_item_set_expand (item, TRUE);
257         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
258
259         window->widgets_connected = g_list_prepend (window->widgets_connected,
260                                                   window->presence_chooser);
261
262         /* Set up the throbber */
263         ebox = gtk_event_box_new ();
264         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
265
266         window->throbber = ephy_spinner_new ();
267         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
268         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
269
270         item = gtk_tool_item_new ();
271         gtk_container_add (GTK_CONTAINER (item), ebox);
272         gtk_widget_show_all (GTK_WIDGET (item));
273
274         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
275
276         str = _("Show and edit accounts");
277         gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
278                               ebox, str, str);
279
280         g_signal_connect (ebox,
281                           "button-press-event",
282                           G_CALLBACK (main_window_throbber_button_press_event_cb),
283                           NULL);
284
285         /* Set up contact list. */
286         gossip_status_presets_get_all ();
287         window->contact_list = gossip_contact_list_new ();
288         gtk_widget_show (GTK_WIDGET (window->contact_list));
289         gtk_container_add (GTK_CONTAINER (sw),
290                            GTK_WIDGET (window->contact_list));
291
292         /* Load user-defined accelerators. */
293         main_window_accels_load ();
294
295         /* Set window size. */
296         gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
297
298         if (w >= 1 && h >= 1) {
299                 /* Use the defaults from the glade file if we
300                  * don't have good w, h geometry.
301                  */
302                 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
303                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
304         }
305
306         if (x >= 0 && y >= 0) {
307                 /* Let the window manager position it if we
308                  * don't have good x, y coordinates.
309                  */
310                 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
311                 gtk_window_move (GTK_WINDOW (window->window), x, y);
312         }
313
314         conf = gossip_conf_get ();
315         
316         /* Show offline ? */
317         gossip_conf_get_bool (conf,
318                               GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
319                               &show_offline);
320         gossip_conf_notify_add (conf,
321                                 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
322                                 main_window_notify_show_offline_cb,
323                                 show_offline_widget);
324
325         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
326                                         show_offline);
327
328         /* Show avatars ? */
329         gossip_conf_get_bool (conf,
330                               GOSSIP_PREFS_UI_SHOW_AVATARS,
331                               &show_avatars);
332         gossip_conf_notify_add (conf,
333                                 GOSSIP_PREFS_UI_SHOW_AVATARS,
334                                 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
335                                 window);
336         gossip_contact_list_set_show_avatars (window->contact_list, show_avatars);
337
338         /* Is compact ? */
339         gossip_conf_get_bool (conf,
340                               GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
341                               &compact_contact_list);
342         gossip_conf_notify_add (conf,
343                                 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
344                                 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
345                                 window);
346         gossip_contact_list_set_is_compact (window->contact_list, compact_contact_list);
347
348         /* Sort criterium */
349         gossip_conf_notify_add (conf,
350                                 GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
351                                 (GossipConfNotifyFunc) main_window_notify_sort_criterium_cb,
352                                 window);
353         main_window_notify_sort_criterium_cb (conf,
354                                               GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
355                                               window);
356
357         gtk_widget_show (window->window);
358
359         return window->window;
360 }
361
362 static void
363 main_window_destroy_cb (GtkWidget         *widget,
364                         EmpathyMainWindow *window)
365 {
366         /* Save user-defined accelerators. */
367         main_window_accels_save ();
368
369         if (window->size_timeout_id) {
370                 g_source_remove (window->size_timeout_id);
371         }
372
373         g_list_free (window->widgets_connected);
374         g_list_free (window->widgets_disconnected);
375
376         g_object_unref (window->tooltips);
377         g_object_unref (window->mc);
378
379         g_free (window);
380 }
381
382 static void
383 main_window_favorite_chatroom_menu_setup (void)
384 {
385 }
386
387 static void
388 main_window_chat_quit_cb (GtkWidget         *widget,
389                           EmpathyMainWindow *window)
390 {
391         gtk_widget_destroy (window->window);
392 }
393
394 static void
395 main_window_chat_new_message_cb (GtkWidget         *widget,
396                                  EmpathyMainWindow *window)
397 {
398         //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
399 }
400
401 static void
402 main_window_chat_history_cb (GtkWidget         *widget,
403                              EmpathyMainWindow *window)
404 {
405         //gossip_log_window_show (NULL, NULL);
406 }
407
408 static void
409 main_window_room_join_new_cb (GtkWidget         *widget,
410                               EmpathyMainWindow *window)
411 {
412         //gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
413 }
414
415 static void
416 main_window_room_join_favorites_cb (GtkWidget         *widget,
417                                     EmpathyMainWindow *window)
418 {
419         //gossip_session_chatroom_join_favorites (window->session);
420 }
421
422 static void
423 main_window_room_manage_favorites_cb (GtkWidget         *widget,
424                                       EmpathyMainWindow *window)
425 {
426         //gossip_chatrooms_window_show (NULL, FALSE);
427 }
428
429 static void
430 main_window_chat_add_contact_cb (GtkWidget         *widget,
431                                  EmpathyMainWindow *window)
432 {
433         //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
434 }
435
436 static void
437 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
438                                   EmpathyMainWindow *window)
439 {
440         gboolean current;
441
442         current = gtk_check_menu_item_get_active (item);
443
444         gossip_conf_set_bool (gossip_conf_get (),
445                               GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
446                               current);
447
448         /* Turn off sound just while we alter the contact list. */
449         // FIXME: gossip_sound_set_enabled (FALSE);
450         g_object_set (window->contact_list, "show_offline", current, NULL);
451         //gossip_sound_set_enabled (TRUE);
452 }
453
454 static gboolean
455 main_window_edit_button_press_event_cb (GtkWidget         *widget,
456                                         GdkEventButton    *event,
457                                         EmpathyMainWindow *window)
458 {
459         GossipContact *contact;
460         gchar         *group;
461
462         if (!event->button == 1) {
463                 return FALSE;
464         }
465
466         group = gossip_contact_list_get_selected_group (window->contact_list);
467         if (group) {
468                 GtkMenuItem *item;
469                 GtkWidget   *label;
470                 GtkWidget   *submenu;
471
472                 item = GTK_MENU_ITEM (window->edit_context);
473                 label = gtk_bin_get_child (GTK_BIN (item));
474                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
475
476                 gtk_widget_show (window->edit_context);
477                 gtk_widget_show (window->edit_context_separator);
478
479                 submenu = gossip_contact_list_get_group_menu (window->contact_list);
480                 gtk_menu_item_set_submenu (item, submenu);
481
482                 g_free (group);
483
484                 return FALSE;
485         }
486
487         contact = gossip_contact_list_get_selected (window->contact_list);
488         if (contact) {
489                 GtkMenuItem *item;
490                 GtkWidget   *label;
491                 GtkWidget   *submenu;
492
493                 item = GTK_MENU_ITEM (window->edit_context);
494                 label = gtk_bin_get_child (GTK_BIN (item));
495                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
496
497                 gtk_widget_show (window->edit_context);
498                 gtk_widget_show (window->edit_context_separator);
499
500                 submenu = gossip_contact_list_get_contact_menu (window->contact_list,
501                                                                 contact);
502                 gtk_menu_item_set_submenu (item, submenu);
503
504                 g_object_unref (contact);
505
506                 return FALSE;
507         }
508
509         gtk_widget_hide (window->edit_context);
510         gtk_widget_hide (window->edit_context_separator);
511
512         return FALSE;
513 }
514
515 static void
516 main_window_edit_accounts_cb (GtkWidget         *widget,
517                               EmpathyMainWindow *window)
518 {
519         gossip_accounts_dialog_show ();
520 }
521
522 static void
523 main_window_edit_personal_information_cb (GtkWidget         *widget,
524                                           EmpathyMainWindow *window)
525 {
526         //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
527 }
528
529 static void
530 main_window_edit_preferences_cb (GtkWidget         *widget,
531                                  EmpathyMainWindow *window)
532 {
533         gossip_preferences_show ();
534 }
535
536 static void
537 main_window_help_about_cb (GtkWidget         *widget,
538                            EmpathyMainWindow *window)
539 {
540         //gossip_about_dialog_new (GTK_WINDOW (window->window));
541 }
542
543 static void
544 main_window_help_contents_cb (GtkWidget         *widget,
545                               EmpathyMainWindow *window)
546 {
547         //gossip_help_show ();
548 }
549
550 static gboolean
551 main_window_throbber_button_press_event_cb (GtkWidget      *throbber_ebox,
552                                             GdkEventButton *event,
553                                             gpointer        user_data)
554 {
555         if (event->type != GDK_BUTTON_PRESS ||
556             event->button != 1) {
557                 return FALSE;
558         }
559
560         gossip_accounts_dialog_show ();
561
562         return FALSE;
563 }
564 #if 0
565 static void
566 main_window_session_protocol_connecting_cb (GossipSession  *session,
567                                     GossipAccount  *account,
568                                     GossipProtocol *protocol,
569                                     gpointer        user_data)
570 {
571         GossipAppPriv *priv;
572         const gchar   *name;
573
574         priv = GET_PRIV (app);
575
576         name = gossip_account_get_name (account);
577         gossip_debug (DEBUG_DOMAIN, "Connecting account:'%s'", name);
578
579         ephy_spinner_start (EPHY_SPINNER (window->throbber));
580 }
581
582 static void
583 main_window_session_protocol_connected_cb (GossipSession  *session,
584                                    GossipAccount  *account,
585                                    GossipProtocol *protocol,
586                                    gpointer        user_data)
587 {
588         GossipAppPriv *priv;
589         gboolean       connecting;
590         const gchar   *name;
591
592         priv = GET_PRIV (app);
593
594         name = gossip_account_get_name (account);
595         gossip_debug (DEBUG_DOMAIN, "Connected account:'%s'", name);
596
597         gossip_session_count_accounts (window->session,
598                                        NULL,
599                                        &connecting,
600                                        NULL);
601
602         if (connecting < 1) {
603                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
604         }
605
606         g_hash_table_remove (window->errors, account);
607         g_hash_table_remove (window->reconnects, account);
608
609         app_connection_items_update ();
610         app_favorite_chatroom_menu_update ();
611
612         /* Use saved presence */
613         gossip_app_set_presence (gossip_status_presets_get_default_state (),
614                                  gossip_status_presets_get_default_status());
615
616         app_presence_updated ();
617 }
618
619 static void
620 main_window_session_protocol_disconnected_cb (GossipSession  *session,
621                                       GossipAccount  *account,
622                                       GossipProtocol *protocol,
623                                       gint            reason,
624                                       gpointer        user_data)
625 {
626         GossipAppPriv *priv;
627         gboolean       connecting;
628         gboolean       should_reconnect;
629         const gchar   *name;
630
631         priv = GET_PRIV (app);
632
633         name = gossip_account_get_name (account);
634         gossip_debug (DEBUG_DOMAIN, "Disconnected account:'%s'", name);
635
636         gossip_session_count_accounts (window->session,
637                                        NULL,
638                                        &connecting,
639                                        NULL);
640
641         if (connecting < 1) {
642                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
643         }
644
645         app_connection_items_update ();
646         app_favorite_chatroom_menu_update ();
647         app_presence_updated ();
648
649         should_reconnect = reason != GOSSIP_PROTOCOL_DISCONNECT_ASKED;
650
651
652         should_reconnect &= !g_hash_table_lookup (window->reconnects, account);
653
654         if (should_reconnect) {
655                 guint id;
656
657                 /* Unexpected disconnection, try to reconnect */
658                 id = g_timeout_add (RETRY_CONNECT_TIMEOUT * 1000,
659                                     (GSourceFunc) app_reconnect_cb,
660                                     account);
661                 g_hash_table_insert (window->reconnects,
662                                      g_object_ref (account),
663                                      &id);
664         }
665 }
666 #endif
667
668 /*
669  * Accels
670  */
671 static void
672 main_window_accels_load (void)
673 {
674         gchar *filename;
675
676         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
677         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
678                 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
679                 gtk_accel_map_load (filename);
680         }
681
682         g_free (filename);
683 }
684
685 static void
686 main_window_accels_save (void)
687 {
688         gchar *dir;
689         gchar *file_with_path;
690
691         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
692         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
693         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
694         g_free (dir);
695
696         gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
697         gtk_accel_map_save (file_with_path);
698
699         g_free (file_with_path);
700 }
701
702 static void
703 main_window_connection_items_setup (EmpathyMainWindow *window,
704                                     GladeXML          *glade)
705 {
706         GList         *list;
707         GtkWidget     *w;
708         gint           i;
709         const gchar *widgets_connected[] = {
710                 "chat_disconnect",
711                 "room",
712                 "chat_new_message",
713                 "chat_add_contact",
714                 "edit_personal_information"
715         };
716         const gchar *widgets_disconnected[] = {
717                 "chat_connect"
718         };
719
720         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
721                 w = glade_xml_get_widget (glade, widgets_connected[i]);
722                 list = g_list_prepend (list, w);
723         }
724
725         window->widgets_connected = list;
726
727         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
728                 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
729                 list = g_list_prepend (list, w);
730         }
731
732         window->widgets_disconnected = list;
733 }
734
735 #if 0
736 FIXME:
737 static void
738 main_window_connection_items_update (void)
739 {
740         GList         *l;
741         guint          connected = 0;
742         guint          disconnected = 0;
743
744         /* Get account count for:
745          *  - connected and disabled,
746          *  - connected and enabled
747          *  - disabled and enabled
748          */
749         gossip_session_count_accounts (window->session,
750                                        &connected,
751                                        NULL,
752                                        &disconnected);
753
754         for (l = window->widgets_connected; l; l = l->next) {
755                 gtk_widget_set_sensitive (l->data, (connected > 0));
756         }
757
758         for (l = window->widgets_disconnected; l; l = l->next) {
759                 gtk_widget_set_sensitive (l->data, (disconnected > 0));
760         }
761 }
762 #endif
763
764 static void
765 main_window_presence_changed_cb (DBusGProxy        *proxy,
766                                  McPresence         state,
767                                  EmpathyMainWindow *window)
768 {
769         gossip_debug (DEBUG_DOMAIN, "presence changed to %d", state);
770         gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
771                                            state);
772 }
773
774 static void
775 main_window_presence_chooser_changed_cb (GtkWidget         *chooser,
776                                          McPresence         state,
777                                          const gchar       *status,
778                                          EmpathyMainWindow *window)
779 {
780         gossip_status_presets_set_default (state, status);
781         mission_control_set_presence (window->mc, state, status, NULL, NULL);
782 }
783
784 static gboolean
785 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
786 {
787         gint x, y, w, h;
788
789         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
790         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
791
792         gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
793
794         window->size_timeout_id = 0;
795
796         return FALSE;
797 }
798
799 static gboolean
800 main_window_configure_event_cb (GtkWidget         *widget,
801                                 GdkEventConfigure *event,
802                                 EmpathyMainWindow *window)
803 {
804         if (window->size_timeout_id) {
805                 g_source_remove (window->size_timeout_id);
806         }
807
808         window->size_timeout_id = g_timeout_add (500,
809                                                (GSourceFunc) main_window_configure_event_timeout_cb,
810                                                window);
811
812         return FALSE;
813 }
814
815 static void
816 main_window_notify_show_offline_cb (GossipConf  *conf,
817                                     const gchar *key,
818                                     gpointer     check_menu_item)
819 {
820         gboolean show_offline;
821
822         if (gossip_conf_get_bool (conf, key, &show_offline)) {
823                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
824                                                 show_offline);
825         }
826 }
827
828 static void
829 main_window_notify_show_avatars_cb (GossipConf        *conf,
830                                     const gchar       *key,
831                                     EmpathyMainWindow *window)
832 {
833         gboolean show_avatars;
834
835         if (gossip_conf_get_bool (conf, key, &show_avatars)) {
836                 gossip_contact_list_set_show_avatars (window->contact_list,
837                                                       show_avatars);
838         }
839 }
840
841 static void
842 main_window_notify_compact_contact_list_cb (GossipConf        *conf,
843                                             const gchar       *key,
844                                             EmpathyMainWindow *window)
845 {
846         gboolean compact_contact_list;
847
848         if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
849                 gossip_contact_list_set_is_compact (window->contact_list,
850                                                     compact_contact_list);
851         }
852 }
853
854 static void
855 main_window_notify_sort_criterium_cb (GossipConf        *conf,
856                                       const gchar       *key,
857                                       EmpathyMainWindow *window)
858 {
859         gchar *str = NULL;
860
861         if (gossip_conf_get_string (conf, key, &str)) {
862                 GType       type;
863                 GEnumClass *enum_class;
864                 GEnumValue *enum_value;
865
866                 type = gossip_contact_list_sort_get_type ();
867                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
868                 enum_value = g_enum_get_value_by_nick (enum_class, str);
869
870                 if (enum_value) {
871                         gossip_contact_list_set_sort_criterium (window->contact_list, 
872                                                                 enum_value->value);
873                 }
874         }
875 }
876