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