]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Merge remote-tracking branch 'pochu/upgrade-software'
[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-2010 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
23  */
24
25 #include <config.h>
26
27 #include <sys/stat.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <glib/gi18n.h>
31
32 #include <telepathy-glib/account-manager.h>
33 #include <telepathy-glib/util.h>
34 #include <folks/folks.h>
35
36 #include <libempathy/empathy-contact.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-request-util.h>
39 #include <libempathy/empathy-chatroom-manager.h>
40 #include <libempathy/empathy-chatroom.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-contact-manager.h>
43 #include <libempathy/empathy-gsettings.h>
44 #include <libempathy/empathy-individual-manager.h>
45 #include <libempathy/empathy-gsettings.h>
46 #include <libempathy/empathy-status-presets.h>
47 #include <libempathy/empathy-tp-contact-factory.h>
48
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-contact-list-store.h>
51 #include <libempathy-gtk/empathy-contact-list-view.h>
52 #include <libempathy-gtk/empathy-live-search.h>
53 #include <libempathy-gtk/empathy-contact-blocking-dialog.h>
54 #include <libempathy-gtk/empathy-contact-search-dialog.h>
55 #include <libempathy-gtk/empathy-geometry.h>
56 #include <libempathy-gtk/empathy-gtk-enum-types.h>
57 #include <libempathy-gtk/empathy-individual-dialogs.h>
58 #include <libempathy-gtk/empathy-individual-store.h>
59 #include <libempathy-gtk/empathy-individual-view.h>
60 #include <libempathy-gtk/empathy-new-message-dialog.h>
61 #include <libempathy-gtk/empathy-new-call-dialog.h>
62 #include <libempathy-gtk/empathy-log-window.h>
63 #include <libempathy-gtk/empathy-presence-chooser.h>
64 #include <libempathy-gtk/empathy-sound-manager.h>
65 #include <libempathy-gtk/empathy-ui-utils.h>
66
67 #include "empathy-accounts-dialog.h"
68 #include "empathy-call-observer.h"
69 #include "empathy-chat-manager.h"
70 #include "empathy-main-window.h"
71 #include "empathy-preferences.h"
72 #include "empathy-about-dialog.h"
73 #include "empathy-debug-window.h"
74 #include "empathy-new-chatroom-dialog.h"
75 #include "empathy-map-view.h"
76 #include "empathy-chatrooms-window.h"
77 #include "empathy-event-manager.h"
78 #include "empathy-ft-manager.h"
79
80 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
81 #include <libempathy/empathy-debug.h>
82
83 /* Flashing delay for icons (milliseconds). */
84 #define FLASH_TIMEOUT 500
85
86 /* Minimum width of roster window if something goes wrong. */
87 #define MIN_WIDTH 50
88
89 /* Accels (menu shortcuts) can be configured and saved */
90 #define ACCELS_FILENAME "accels.txt"
91
92 /* Name in the geometry file */
93 #define GEOMETRY_NAME "main-window"
94
95 enum {
96         PAGE_CONTACT_LIST = 0,
97         PAGE_NO_MATCH
98 };
99
100 enum {
101         PROP_0,
102         PROP_SHELL_RUNNING
103 };
104
105 G_DEFINE_TYPE (EmpathyMainWindow, empathy_main_window, GTK_TYPE_WINDOW);
106
107 #define GET_PRIV(self) ((EmpathyMainWindowPriv *)((EmpathyMainWindow *) self)->priv)
108
109 struct _EmpathyMainWindowPriv {
110         EmpathyContactList      *contact_manager;
111         EmpathyIndividualStore  *individual_store;
112         EmpathyIndividualView   *individual_view;
113         TpAccountManager        *account_manager;
114         EmpathyChatroomManager  *chatroom_manager;
115         EmpathyEventManager     *event_manager;
116         EmpathySoundManager     *sound_mgr;
117         EmpathyCallObserver     *call_observer;
118         guint                    flash_timeout_id;
119         gboolean                 flash_on;
120         gboolean                 empty;
121
122         GSettings              *gsettings_ui;
123         GSettings              *gsettings_contacts;
124
125         GtkWidget              *preferences;
126         GtkWidget              *main_vbox;
127         GtkWidget              *throbber;
128         GtkWidget              *throbber_tool_item;
129         GtkWidget              *presence_toolbar;
130         GtkWidget              *presence_chooser;
131         GtkWidget              *errors_vbox;
132         GtkWidget              *auth_vbox;
133         GtkWidget              *search_bar;
134         GtkWidget              *notebook;
135         GtkWidget              *no_entry_label;
136
137         GtkToggleAction        *show_protocols;
138         GtkRadioAction         *sort_by_name;
139         GtkRadioAction         *sort_by_status;
140         GtkRadioAction         *normal_with_avatars;
141         GtkRadioAction         *normal_size;
142         GtkRadioAction         *compact_size;
143
144         GtkUIManager           *ui_manager;
145         GtkAction              *view_history;
146         GtkAction              *room_join_favorites;
147         GtkWidget              *room_menu;
148         GtkWidget              *room_separator;
149         GtkWidget              *edit_context;
150         GtkWidget              *edit_context_separator;
151
152         GtkActionGroup         *balance_action_group;
153         GtkAction              *view_balance_show_in_roster;
154         GtkWidget              *balance_vbox;
155
156         guint                   size_timeout_id;
157
158         /* reffed TpAccount* => visible GtkInfoBar* */
159         GHashTable             *errors;
160
161         /* EmpathyEvent* => visible GtkInfoBar* */
162         GHashTable             *auths;
163
164         /* stores a mapping from TpAccount to Handler ID to prevent
165          * to listen more than once to the status-changed signal */
166         GHashTable             *status_changed_handlers;
167
168         /* Actions that are enabled when there are connected accounts */
169         GList                  *actions_connected;
170
171         gboolean               shell_running;
172 };
173
174 static void
175 main_window_flash_stop (EmpathyMainWindow *window)
176 {
177         EmpathyMainWindowPriv *priv = GET_PRIV (window);
178
179         if (priv->flash_timeout_id == 0) {
180                 return;
181         }
182
183         DEBUG ("Stop flashing");
184         g_source_remove (priv->flash_timeout_id);
185         priv->flash_timeout_id = 0;
186         priv->flash_on = FALSE;
187 }
188
189 typedef struct {
190         EmpathyEvent       *event;
191         gboolean            on;
192         EmpathyMainWindow  *window;
193 } FlashForeachData;
194
195 static gboolean
196 main_window_flash_foreach (GtkTreeModel *model,
197                            GtkTreePath  *path,
198                            GtkTreeIter  *iter,
199                            gpointer      user_data)
200 {
201         FlashForeachData *data = (FlashForeachData *) user_data;
202         FolksIndividual *individual;
203         EmpathyContact   *contact;
204         const gchar      *icon_name;
205         GtkTreePath      *parent_path = NULL;
206         GtkTreeIter       parent_iter;
207         GdkPixbuf        *pixbuf = NULL;
208
209         gtk_tree_model_get (model, iter,
210                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
211                             -1);
212
213         if (individual == NULL)
214                 return FALSE;
215
216         contact = empathy_contact_dup_from_folks_individual (individual);
217         if (contact != data->event->contact)
218                 goto out;
219
220         if (data->on) {
221                 icon_name = data->event->icon_name;
222                 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
223         } else {
224                 pixbuf = empathy_individual_store_get_individual_status_icon (
225                                                 GET_PRIV (data->window)->individual_store,
226                                                 individual);
227                 if (pixbuf != NULL)
228                         g_object_ref (pixbuf);
229         }
230
231         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
232                             EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf,
233                             -1);
234
235         /* To make sure the parent is shown correctly, we emit
236          * the row-changed signal on the parent so it prompts
237          * it to be refreshed by the filter func.
238          */
239         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
240                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
241         }
242         if (parent_path) {
243                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
244                 gtk_tree_path_free (parent_path);
245         }
246
247 out:
248         g_object_unref (individual);
249         tp_clear_object (&contact);
250         tp_clear_object (&pixbuf);
251
252         return FALSE;
253 }
254
255 static gboolean
256 main_window_flash_cb (EmpathyMainWindow *window)
257 {
258         EmpathyMainWindowPriv *priv = GET_PRIV (window);
259         GtkTreeModel     *model;
260         GSList           *events, *l;
261         gboolean          found_event = FALSE;
262         FlashForeachData  data;
263
264         priv->flash_on = !priv->flash_on;
265         data.on = priv->flash_on;
266         model = GTK_TREE_MODEL (priv->individual_store);
267
268         events = empathy_event_manager_get_events (priv->event_manager);
269         for (l = events; l; l = l->next) {
270                 data.event = l->data;
271                 data.window = window;
272                 if (!data.event->contact || !data.event->must_ack) {
273                         continue;
274                 }
275
276                 found_event = TRUE;
277                 gtk_tree_model_foreach (model,
278                                         main_window_flash_foreach,
279                                         &data);
280         }
281
282         if (!found_event) {
283                 main_window_flash_stop (window);
284         }
285
286         return TRUE;
287 }
288
289 static void
290 main_window_flash_start (EmpathyMainWindow *window)
291 {
292         EmpathyMainWindowPriv *priv = GET_PRIV (window);
293
294         if (priv->flash_timeout_id != 0) {
295                 return;
296         }
297
298         DEBUG ("Start flashing");
299         priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
300                                                 (GSourceFunc) main_window_flash_cb,
301                                                 window);
302         main_window_flash_cb (window);
303 }
304
305 static void
306 main_window_remove_auth (EmpathyMainWindow *window,
307                          EmpathyEvent      *event)
308 {
309         EmpathyMainWindowPriv *priv = GET_PRIV (window);
310         GtkWidget *error_widget;
311
312         error_widget = g_hash_table_lookup (priv->auths, event);
313         if (error_widget != NULL) {
314                 gtk_widget_destroy (error_widget);
315                 g_hash_table_remove (priv->auths, event);
316         }
317 }
318
319 static void
320 main_window_auth_add_clicked_cb (GtkButton         *button,
321                                  EmpathyMainWindow *window)
322 {
323         EmpathyEvent *event;
324
325         event = g_object_get_data (G_OBJECT (button), "event");
326
327         empathy_event_approve (event);
328
329         main_window_remove_auth (window, event);
330 }
331
332 static void
333 main_window_auth_close_clicked_cb (GtkButton         *button,
334                                    EmpathyMainWindow *window)
335 {
336         EmpathyEvent *event;
337
338         event = g_object_get_data (G_OBJECT (button), "event");
339
340         empathy_event_decline (event);
341         main_window_remove_auth (window, event);
342 }
343
344 static void
345 main_window_auth_display (EmpathyMainWindow *window,
346                           EmpathyEvent      *event)
347 {
348         EmpathyMainWindowPriv *priv = GET_PRIV (window);
349         TpAccount *account = event->account;
350         GtkWidget *info_bar;
351         GtkWidget *content_area;
352         GtkWidget *image;
353         GtkWidget *label;
354         GtkWidget *add_button;
355         GtkWidget *close_button;
356         GtkWidget *action_area;
357         GtkWidget *action_table;
358         const gchar *icon_name;
359         gchar *str;
360
361         if (g_hash_table_lookup (priv->auths, event) != NULL) {
362                 return;
363         }
364
365         info_bar = gtk_info_bar_new ();
366         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_QUESTION);
367
368         gtk_widget_set_no_show_all (info_bar, TRUE);
369         gtk_box_pack_start (GTK_BOX (priv->auth_vbox), info_bar, FALSE, TRUE, 0);
370         gtk_widget_show (info_bar);
371
372         icon_name = tp_account_get_icon_name (account);
373         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
374         gtk_widget_show (image);
375
376         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
377                                        tp_account_get_display_name (account),
378                                        _("Password required"));
379
380         label = gtk_label_new (str);
381         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
382         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
383         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
384         gtk_widget_show (label);
385
386         g_free (str);
387
388         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
389         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
390         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
391
392         image = gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON);
393         add_button = gtk_button_new ();
394         gtk_button_set_image (GTK_BUTTON (add_button), image);
395         gtk_widget_set_tooltip_text (add_button, _("Provide Password"));
396         gtk_widget_show (add_button);
397
398         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
399         close_button = gtk_button_new ();
400         gtk_button_set_image (GTK_BUTTON (close_button), image);
401         gtk_widget_set_tooltip_text (close_button, _("Disconnect"));
402         gtk_widget_show (close_button);
403
404         action_table = gtk_table_new (1, 2, FALSE);
405         gtk_table_set_col_spacings (GTK_TABLE (action_table), 6);
406         gtk_widget_show (action_table);
407
408         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
409         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
410
411         gtk_table_attach (GTK_TABLE (action_table), add_button, 0, 1, 0, 1,
412                           (GtkAttachOptions) (GTK_SHRINK),
413                           (GtkAttachOptions) (GTK_SHRINK), 0, 0);
414         gtk_table_attach (GTK_TABLE (action_table), close_button, 1, 2, 0, 1,
415                           (GtkAttachOptions) (GTK_SHRINK),
416                           (GtkAttachOptions) (GTK_SHRINK), 0, 0);
417
418         g_object_set_data_full (G_OBJECT (info_bar),
419                                 "event", event, NULL);
420         g_object_set_data_full (G_OBJECT (add_button),
421                                 "event", event, NULL);
422         g_object_set_data_full (G_OBJECT (close_button),
423                                 "event", event, NULL);
424
425         g_signal_connect (add_button, "clicked",
426                           G_CALLBACK (main_window_auth_add_clicked_cb),
427                           window);
428         g_signal_connect (close_button, "clicked",
429                           G_CALLBACK (main_window_auth_close_clicked_cb),
430                           window);
431
432         gtk_widget_show (priv->auth_vbox);
433
434         g_hash_table_insert (priv->auths, event, info_bar);
435 }
436
437 static void
438 modify_event_count (GtkTreeModel *model,
439                     GtkTreeIter *iter,
440                     EmpathyEvent *event,
441                     gboolean increase)
442 {
443         FolksIndividual *individual;
444         EmpathyContact *contact;
445         guint count;
446
447         gtk_tree_model_get (model, iter,
448                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
449                             EMPATHY_INDIVIDUAL_STORE_COL_EVENT_COUNT, &count,
450                             -1);
451
452         if (individual == NULL)
453                 return;
454
455         increase ? count++ : count--;
456
457         contact = empathy_contact_dup_from_folks_individual (individual);
458         if (contact == event->contact) {
459                 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
460                                     EMPATHY_INDIVIDUAL_STORE_COL_EVENT_COUNT, count, -1);
461         }
462
463         tp_clear_object (&contact);
464         g_object_unref (individual);
465 }
466
467 static gboolean
468 increase_event_count_foreach (GtkTreeModel *model,
469                               GtkTreePath *path,
470                               GtkTreeIter *iter,
471                               gpointer user_data)
472 {
473         EmpathyEvent *event = user_data;
474
475         modify_event_count (model, iter, event, TRUE);
476
477         return FALSE;
478 }
479
480 static void
481 increase_event_count (EmpathyMainWindow *self,
482                       EmpathyEvent *event)
483 {
484         EmpathyMainWindowPriv *priv = GET_PRIV (self);
485         GtkTreeModel *model;
486
487         model = GTK_TREE_MODEL (priv->individual_store);
488
489         gtk_tree_model_foreach (model, increase_event_count_foreach, event);
490 }
491
492 static gboolean
493 decrease_event_count_foreach (GtkTreeModel *model,
494                               GtkTreePath *path,
495                               GtkTreeIter *iter,
496                               gpointer user_data)
497 {
498         EmpathyEvent *event = user_data;
499
500         modify_event_count (model, iter, event, FALSE);
501
502         return FALSE;
503 }
504
505 static void
506 decrease_event_count (EmpathyMainWindow *self,
507                       EmpathyEvent *event)
508 {
509         EmpathyMainWindowPriv *priv = GET_PRIV (self);
510         GtkTreeModel *model;
511
512         model = GTK_TREE_MODEL (priv->individual_store);
513
514         gtk_tree_model_foreach (model, decrease_event_count_foreach, event);
515 }
516
517 static void
518 main_window_event_added_cb (EmpathyEventManager *manager,
519                             EmpathyEvent        *event,
520                             EmpathyMainWindow   *window)
521 {
522         if (event->contact) {
523                 increase_event_count (window, event);
524
525                 main_window_flash_start (window);
526         } else if (event->type == EMPATHY_EVENT_TYPE_AUTH) {
527                 main_window_auth_display (window, event);
528         }
529 }
530
531 static void
532 main_window_event_removed_cb (EmpathyEventManager *manager,
533                               EmpathyEvent        *event,
534                               EmpathyMainWindow   *window)
535 {
536         EmpathyMainWindowPriv *priv = GET_PRIV (window);
537         FlashForeachData data;
538
539         if (event->type == EMPATHY_EVENT_TYPE_AUTH) {
540                 main_window_remove_auth (window, event);
541                 return;
542         }
543
544         if (!event->contact) {
545                 return;
546         }
547
548         decrease_event_count (window, event);
549
550         data.on = FALSE;
551         data.event = event;
552         data.window = window;
553         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->individual_store),
554                                 main_window_flash_foreach,
555                                 &data);
556 }
557
558 static gboolean
559 main_window_load_events_idle_cb (gpointer user_data)
560 {
561         EmpathyMainWindow *window = user_data;
562         EmpathyMainWindowPriv *priv = GET_PRIV (window);
563         GSList *l;
564
565         l = empathy_event_manager_get_events (priv->event_manager);
566         while (l) {
567                 main_window_event_added_cb (priv->event_manager, l->data,
568                                 window);
569                 l = l->next;
570         }
571
572         return FALSE;
573 }
574
575 static void
576 main_window_row_activated_cb (EmpathyContactListView *view,
577                               GtkTreePath            *path,
578                               GtkTreeViewColumn      *col,
579                               EmpathyMainWindow      *window)
580 {
581         EmpathyMainWindowPriv *priv = GET_PRIV (window);
582         EmpathyContact *contact = NULL;
583         FolksIndividual *individual;
584         GtkTreeModel   *model;
585         GtkTreeIter     iter;
586         GSList         *events, *l;
587
588         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
589         gtk_tree_model_get_iter (model, &iter, path);
590
591         gtk_tree_model_get (model, &iter,
592                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
593                                 &individual,
594                             -1);
595
596         if (individual != NULL) {
597                 contact = empathy_contact_dup_from_folks_individual (individual);
598         }
599
600         if (!contact) {
601                 goto OUT;
602         }
603
604         /* If the contact has an event activate it, otherwise the
605          * default handler of row-activated will be called. */
606         events = empathy_event_manager_get_events (priv->event_manager);
607         for (l = events; l; l = l->next) {
608                 EmpathyEvent *event = l->data;
609
610                 if (event->contact == contact) {
611                         DEBUG ("Activate event");
612                         empathy_event_activate (event);
613
614                         /* We don't want the default handler of this signal
615                          * (e.g. open a chat) */
616                         g_signal_stop_emission_by_name (view, "row-activated");
617                         break;
618                 }
619         }
620
621         g_object_unref (contact);
622 OUT:
623         tp_clear_object (&individual);
624 }
625
626 static void
627 main_window_row_deleted_cb (GtkTreeModel      *model,
628                             GtkTreePath       *path,
629                             EmpathyMainWindow *window)
630 {
631         EmpathyMainWindowPriv *priv = GET_PRIV (window);
632         GtkTreeIter help_iter;
633
634         if (!gtk_tree_model_get_iter_first (model, &help_iter)) {
635                 priv->empty = TRUE;
636
637                 if (empathy_individual_view_is_searching (
638                                 priv->individual_view)) {
639                         gchar *tmp;
640
641                         tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>",
642                                 _("No match found"));
643
644                         gtk_label_set_markup (GTK_LABEL (priv->no_entry_label), tmp);
645                         g_free (tmp);
646
647                         gtk_label_set_line_wrap (GTK_LABEL (priv->no_entry_label),
648                                 TRUE);
649
650                         gtk_notebook_set_current_page (
651                                         GTK_NOTEBOOK (priv->notebook), PAGE_NO_MATCH);
652                 }
653         }
654 }
655
656 static void
657 main_window_row_inserted_cb (GtkTreeModel      *model,
658                              GtkTreePath       *path,
659                              GtkTreeIter       *iter,
660                              EmpathyMainWindow *window)
661 {
662         EmpathyMainWindowPriv *priv = GET_PRIV (window);
663
664         if (priv->empty) {
665                 priv->empty = FALSE;
666                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
667                                 PAGE_CONTACT_LIST);
668                 gtk_widget_grab_focus (GTK_WIDGET (priv->individual_view));
669
670                 /* The store is being filled, it will be done after an idle cb.
671                  * So we can then get events. If we do that too soon, event's
672                  * contact is not yet in the store and it won't get marked as
673                  * having events. */
674                 g_idle_add (main_window_load_events_idle_cb, window);
675         }
676 }
677
678 static void
679 main_window_remove_error (EmpathyMainWindow *window,
680                           TpAccount         *account)
681 {
682         EmpathyMainWindowPriv *priv = GET_PRIV (window);
683         GtkWidget *error_widget;
684
685         error_widget = g_hash_table_lookup (priv->errors, account);
686         if (error_widget != NULL) {
687                 gtk_widget_destroy (error_widget);
688                 g_hash_table_remove (priv->errors, account);
689         }
690 }
691
692 static void
693 main_window_account_disabled_cb (TpAccountManager  *manager,
694                                  TpAccount         *account,
695                                  EmpathyMainWindow *window)
696 {
697         main_window_remove_error (window, account);
698 }
699
700 static void
701 main_window_error_retry_clicked_cb (GtkButton         *button,
702                                     EmpathyMainWindow *window)
703 {
704         TpAccount *account;
705
706         account = g_object_get_data (G_OBJECT (button), "account");
707         tp_account_reconnect_async (account, NULL, NULL);
708
709         main_window_remove_error (window, account);
710 }
711
712 static void
713 main_window_error_edit_clicked_cb (GtkButton         *button,
714                                    EmpathyMainWindow *window)
715 {
716         TpAccount *account;
717
718         account = g_object_get_data (G_OBJECT (button), "account");
719
720         empathy_accounts_dialog_show_application (
721                         gtk_widget_get_screen (GTK_WIDGET (button)),
722                         account, FALSE, FALSE);
723
724         main_window_remove_error (window, account);
725 }
726
727 static void
728 main_window_error_close_clicked_cb (GtkButton         *button,
729                                     EmpathyMainWindow *window)
730 {
731         TpAccount *account;
732
733         account = g_object_get_data (G_OBJECT (button), "account");
734         main_window_remove_error (window, account);
735 }
736
737 static void
738 main_window_error_upgrade_sw_clicked_cb (GtkButton         *button,
739                                          EmpathyMainWindow *window)
740 {
741         TpAccount *account;
742         GtkWidget *dialog;
743
744         account = g_object_get_data (G_OBJECT (button), "account");
745         main_window_remove_error (window, account);
746
747         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
748                 GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
749                 GTK_BUTTONS_OK,
750                 _("Sorry, %s accounts can’t be used until your %s software is updated."),
751                 tp_account_get_protocol (account),
752                 tp_account_get_protocol (account));
753
754         g_signal_connect_swapped (dialog, "response",
755                 G_CALLBACK (gtk_widget_destroy),
756                 dialog);
757
758         gtk_widget_show (dialog);
759 }
760
761 static void
762 main_window_upgrade_software_error (EmpathyMainWindow *window,
763                                     TpAccount         *account)
764 {
765         EmpathyMainWindowPriv *priv = GET_PRIV (window);
766         GtkWidget *info_bar;
767         GtkWidget *content_area;
768         GtkWidget *label;
769         GtkWidget *image;
770         GtkWidget *upgrade_button;
771         GtkWidget *close_button;
772         GtkWidget *action_area;
773         GtkWidget *action_table;
774         gchar     *str;
775         const gchar     *icon_name;
776         const gchar *error_message;
777         gboolean user_requested;
778
779         error_message =
780                 empathy_account_get_error_message (account, &user_requested);
781
782         if (user_requested) {
783                 return;
784         }
785
786         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
787                                                tp_account_get_display_name (account),
788                                                error_message);
789
790         /* If there are other errors, remove them */
791         main_window_remove_error (window, account);
792
793         info_bar = gtk_info_bar_new ();
794         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
795
796         gtk_widget_set_no_show_all (info_bar, TRUE);
797         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
798         gtk_widget_show (info_bar);
799
800         icon_name = tp_account_get_icon_name (account);
801         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
802         gtk_widget_show (image);
803
804         label = gtk_label_new (str);
805         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
806         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
807         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
808         gtk_widget_show (label);
809         g_free (str);
810
811         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
812         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
813         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
814
815         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
816         upgrade_button = gtk_button_new ();
817         gtk_button_set_image (GTK_BUTTON (upgrade_button), image);
818         gtk_widget_set_tooltip_text (upgrade_button, _("Update software..."));
819         gtk_widget_show (upgrade_button);
820
821         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
822         close_button = gtk_button_new ();
823         gtk_button_set_image (GTK_BUTTON (close_button), image);
824         gtk_widget_set_tooltip_text (close_button, _("Close"));
825         gtk_widget_show (close_button);
826
827         action_table = gtk_table_new (1, 2, FALSE);
828         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
829         gtk_widget_show (action_table);
830
831         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
832         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
833
834         gtk_table_attach (GTK_TABLE (action_table), upgrade_button, 0, 1, 0, 1,
835                                                                                 (GtkAttachOptions) (GTK_SHRINK),
836                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
837         gtk_table_attach (GTK_TABLE (action_table), close_button, 1, 2, 0, 1,
838                                                                                 (GtkAttachOptions) (GTK_SHRINK),
839                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
840
841         g_object_set_data (G_OBJECT (info_bar), "label", label);
842         g_object_set_data_full (G_OBJECT (info_bar),
843                                 "account", g_object_ref (account),
844                                 g_object_unref);
845         g_object_set_data_full (G_OBJECT (upgrade_button),
846                                 "account", g_object_ref (account),
847                                 g_object_unref);
848         g_object_set_data_full (G_OBJECT (close_button),
849                                 "account", g_object_ref (account),
850                                 g_object_unref);
851
852         g_signal_connect (upgrade_button, "clicked",
853                           G_CALLBACK (main_window_error_upgrade_sw_clicked_cb),
854                           window);
855         g_signal_connect (close_button, "clicked",
856                           G_CALLBACK (main_window_error_close_clicked_cb),
857                           window);
858
859         gtk_widget_set_tooltip_text (priv->errors_vbox, error_message);
860         gtk_widget_show (priv->errors_vbox);
861
862         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
863 }
864
865 static void
866 main_window_error_display (EmpathyMainWindow *window,
867                            TpAccount         *account)
868 {
869         EmpathyMainWindowPriv *priv = GET_PRIV (window);
870         GtkWidget *info_bar;
871         GtkWidget *content_area;
872         GtkWidget *label;
873         GtkWidget *image;
874         GtkWidget *retry_button;
875         GtkWidget *edit_button;
876         GtkWidget *close_button;
877         GtkWidget *action_area;
878         GtkWidget *action_table;
879         gchar     *str;
880         const gchar     *icon_name;
881         const gchar *error_message;
882         gboolean user_requested;
883
884         if (!tp_strdiff (TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
885                          tp_account_get_detailed_error (account, NULL))) {
886                 main_window_upgrade_software_error (window, account);
887                 return;
888         }
889
890         error_message =
891                 empathy_account_get_error_message (account, &user_requested);
892
893         if (user_requested) {
894                 return;
895         }
896
897         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
898                                                tp_account_get_display_name (account),
899                                                error_message);
900
901         info_bar = g_hash_table_lookup (priv->errors, account);
902         if (info_bar) {
903                 label = g_object_get_data (G_OBJECT (info_bar), "label");
904
905                 /* Just set the latest error and return */
906                 gtk_label_set_markup (GTK_LABEL (label), str);
907                 g_free (str);
908
909                 return;
910         }
911
912         info_bar = gtk_info_bar_new ();
913         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
914
915         gtk_widget_set_no_show_all (info_bar, TRUE);
916         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
917         gtk_widget_show (info_bar);
918
919         icon_name = tp_account_get_icon_name (account);
920         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
921         gtk_widget_show (image);
922
923         label = gtk_label_new (str);
924         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
925         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
926         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
927         gtk_widget_show (label);
928         g_free (str);
929
930         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
931         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
932         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
933
934         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
935         retry_button = gtk_button_new ();
936         gtk_button_set_image (GTK_BUTTON (retry_button), image);
937         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
938         gtk_widget_show (retry_button);
939
940         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
941         edit_button = gtk_button_new ();
942         gtk_button_set_image (GTK_BUTTON (edit_button), image);
943         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
944         gtk_widget_show (edit_button);
945
946         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
947         close_button = gtk_button_new ();
948         gtk_button_set_image (GTK_BUTTON (close_button), image);
949         gtk_widget_set_tooltip_text (close_button, _("Close"));
950         gtk_widget_show (close_button);
951
952         action_table = gtk_table_new (1, 3, FALSE);
953         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
954         gtk_widget_show (action_table);
955
956         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
957         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
958
959         gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
960                                                                                 (GtkAttachOptions) (GTK_SHRINK),
961                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
962         gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
963                                                                                 (GtkAttachOptions) (GTK_SHRINK),
964                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
965         gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
966                                                                                 (GtkAttachOptions) (GTK_SHRINK),
967                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
968
969         g_object_set_data (G_OBJECT (info_bar), "label", label);
970         g_object_set_data_full (G_OBJECT (info_bar),
971                                 "account", g_object_ref (account),
972                                 g_object_unref);
973         g_object_set_data_full (G_OBJECT (edit_button),
974                                 "account", g_object_ref (account),
975                                 g_object_unref);
976         g_object_set_data_full (G_OBJECT (close_button),
977                                 "account", g_object_ref (account),
978                                 g_object_unref);
979         g_object_set_data_full (G_OBJECT (retry_button),
980                                 "account", g_object_ref (account),
981                                 g_object_unref);
982
983         g_signal_connect (edit_button, "clicked",
984                           G_CALLBACK (main_window_error_edit_clicked_cb),
985                           window);
986         g_signal_connect (close_button, "clicked",
987                           G_CALLBACK (main_window_error_close_clicked_cb),
988                           window);
989         g_signal_connect (retry_button, "clicked",
990                           G_CALLBACK (main_window_error_retry_clicked_cb),
991                           window);
992
993         gtk_widget_set_tooltip_text (priv->errors_vbox, error_message);
994         gtk_widget_show (priv->errors_vbox);
995
996         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
997 }
998
999 static void
1000 main_window_update_status (EmpathyMainWindow *window)
1001 {
1002         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1003         gboolean connected, connecting;
1004         GList *l, *children;
1005
1006         connected = empathy_account_manager_get_accounts_connected (&connecting);
1007
1008         /* Update the spinner state */
1009         if (connecting) {
1010                 gtk_spinner_start (GTK_SPINNER (priv->throbber));
1011                 gtk_widget_show (priv->throbber_tool_item);
1012         } else {
1013                 gtk_spinner_stop (GTK_SPINNER (priv->throbber));
1014                 gtk_widget_hide (priv->throbber_tool_item);
1015         }
1016
1017         /* Update widgets sensibility */
1018         for (l = priv->actions_connected; l; l = l->next) {
1019                 gtk_action_set_sensitive (l->data, connected);
1020         }
1021
1022         /* Update favourite rooms sensitivity */
1023         children = gtk_container_get_children (GTK_CONTAINER (priv->room_menu));
1024         for (l = children; l != NULL; l = l->next) {
1025                 if (g_object_get_data (G_OBJECT (l->data), "is_favorite") != NULL) {
1026                         gtk_widget_set_sensitive (GTK_WIDGET (l->data), connected);
1027                 }
1028         }
1029         g_list_free (children);
1030 }
1031
1032 static char *
1033 main_window_account_to_action_name (TpAccount *account)
1034 {
1035         char *r;
1036
1037         /* action names can't have '/' in them, replace it with '.' */
1038         r = g_strdup (tp_account_get_path_suffix (account));
1039         r = g_strdelimit (r, "/", '.');
1040
1041         return r;
1042 }
1043
1044 static void
1045 main_window_balance_activate_cb (GtkAction         *action,
1046                                  EmpathyMainWindow *window)
1047 {
1048         const char *uri;
1049
1050         uri = g_object_get_data (G_OBJECT (action), "manage-credit-uri");
1051
1052         if (!tp_str_empty (uri)) {
1053                 DEBUG ("Top-up credit URI: %s", uri);
1054                 empathy_url_show (GTK_WIDGET (window), uri);
1055         } else {
1056                 DEBUG ("unknown protocol for top-up");
1057         }
1058 }
1059
1060 static void
1061 main_window_balance_update_balance (GtkAction   *action,
1062                                     TpConnection *conn)
1063 {
1064         TpAccount *account = tp_connection_get_account (conn);
1065         GtkWidget *label;
1066         int amount = 0;
1067         guint scale = G_MAXINT32;
1068         const gchar *currency = "";
1069         char *money, *str;
1070
1071         if (!tp_connection_get_balance (conn, &amount, &scale, &currency))
1072                 return;
1073
1074         if (amount == 0 &&
1075             scale == G_MAXINT32 &&
1076             tp_str_empty (currency)) {
1077                 /* unknown balance */
1078                 money = g_strdup ("--");
1079         } else {
1080                 char *tmp = empathy_format_currency (amount, scale, currency);
1081
1082                 money = g_strdup_printf ("%s %s", currency, tmp);
1083                 g_free (tmp);
1084         }
1085
1086         /* Translators: this string will be something like:
1087          *   Top up My Account ($1.23)..." */
1088         str = g_strdup_printf (_("Top up %s (%s)..."),
1089                 tp_account_get_display_name (account),
1090                 money);
1091
1092         gtk_action_set_label (action, str);
1093         g_free (str);
1094
1095         /* update the money label in the roster */
1096         label = g_object_get_data (G_OBJECT (action), "money-label");
1097
1098         gtk_label_set_text (GTK_LABEL (label), money);
1099         g_free (money);
1100 }
1101
1102 static void
1103 main_window_balance_changed_cb (TpConnection      *conn,
1104                                 guint balance,
1105                                 guint scale,
1106                                 const gchar *currency,
1107                                 GtkAction *action)
1108 {
1109         main_window_balance_update_balance (action, conn);
1110 }
1111
1112 static GtkAction *
1113 main_window_setup_balance_create_action (EmpathyMainWindow *window,
1114                                          TpAccount *account)
1115 {
1116         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1117         GtkAction *action;
1118         char *name, *ui;
1119         guint merge_id;
1120         GError *error = NULL;
1121
1122         /* create the action group if required */
1123         if (priv->balance_action_group == NULL) {
1124                 priv->balance_action_group =
1125                         gtk_action_group_new ("balance-action-group");
1126
1127                 gtk_ui_manager_insert_action_group (priv->ui_manager,
1128                         priv->balance_action_group, -1);
1129         }
1130
1131         /* create the action */
1132         name = main_window_account_to_action_name (account);
1133         action = gtk_action_new (name,
1134                 tp_account_get_display_name (account),
1135                 _("Top up account credit"),
1136                 NULL);
1137         g_object_bind_property (account, "icon-name", action, "icon-name",
1138                 G_BINDING_SYNC_CREATE);
1139
1140         g_signal_connect (action, "activate",
1141                 G_CALLBACK (main_window_balance_activate_cb), window);
1142
1143         gtk_action_group_add_action (priv->balance_action_group, action);
1144         g_object_unref (action);
1145
1146         ui = g_strdup_printf (
1147                 "<ui>"
1148                 " <menubar name='menubar'>"
1149                 "  <menu action='view'>"
1150                 "   <placeholder name='view_balance_placeholder'>"
1151                 "    <menuitem action='%s'/>"
1152                 "   </placeholder>"
1153                 "  </menu>"
1154                 " </menubar>"
1155                 "</ui>",
1156                 name);
1157
1158         merge_id = gtk_ui_manager_add_ui_from_string (priv->ui_manager,
1159                 ui, -1, &error);
1160         if (error != NULL) {
1161                 DEBUG ("Failed to add balance UI for %s: %s",
1162                         tp_account_get_display_name (account),
1163                         error->message);
1164                 g_error_free (error);
1165         }
1166
1167         g_object_set_data (G_OBJECT (action),
1168                 "merge-id", GUINT_TO_POINTER (merge_id));
1169
1170         g_free (name);
1171         g_free (ui);
1172
1173         return action;
1174 }
1175
1176 static GtkWidget *
1177 main_window_setup_balance_create_widget (EmpathyMainWindow *window,
1178                                          GtkAction         *action,
1179                                          TpAccount *account)
1180 {
1181         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1182         GtkWidget *hbox, *image, *label, *button;
1183
1184         hbox = gtk_hbox_new (FALSE, 6);
1185
1186         /* protocol icon */
1187         image = gtk_image_new ();
1188         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, TRUE, 0);
1189         g_object_bind_property (action, "icon-name", image, "icon-name",
1190                 G_BINDING_SYNC_CREATE);
1191
1192         /* account name label */
1193         label = gtk_label_new ("");
1194         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1195         gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
1196         g_object_bind_property (account, "display-name", label, "label",
1197                 G_BINDING_SYNC_CREATE);
1198
1199         /* balance label */
1200         label = gtk_label_new ("");
1201         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1202         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
1203         g_object_set_data (G_OBJECT (action), "money-label", label);
1204
1205         /* top up button */
1206         button = gtk_button_new_with_label (_("Top Up..."));
1207         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
1208         g_signal_connect_swapped (button, "clicked",
1209                 G_CALLBACK (gtk_action_activate), action);
1210
1211         gtk_box_pack_start (GTK_BOX (priv->balance_vbox), hbox, FALSE, TRUE, 0);
1212         gtk_widget_show_all (hbox);
1213
1214         /* tie the lifetime of the widget to the lifetime of the action */
1215         g_object_weak_ref (G_OBJECT (action),
1216                 (GWeakNotify) gtk_widget_destroy, hbox);
1217
1218         return hbox;
1219 }
1220
1221 static void
1222 main_window_setup_balance (EmpathyMainWindow *window,
1223                            TpAccount         *account)
1224 {
1225         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1226         TpConnection *conn = tp_account_get_connection (account);
1227         GtkAction *action;
1228         const gchar *uri;
1229
1230         if (conn == NULL)
1231                 return;
1232
1233         if (!tp_proxy_is_prepared (conn, TP_CONNECTION_FEATURE_BALANCE))
1234                 return;
1235
1236         DEBUG ("Setting up balance for acct: %s",
1237                 tp_account_get_display_name (account));
1238
1239         /* create the action */
1240         action = main_window_setup_balance_create_action (window, account);
1241
1242         if (action == NULL)
1243                 return;
1244
1245         gtk_action_set_visible (priv->view_balance_show_in_roster, TRUE);
1246
1247         /* create the display widget */
1248         main_window_setup_balance_create_widget (window, action, account);
1249
1250         /* check the current balance and monitor for any changes */
1251         uri = tp_connection_get_balance_uri (conn);
1252
1253         g_object_set_data_full (G_OBJECT (action), "manage-credit-uri",
1254                 g_strdup (uri), g_free);
1255         gtk_action_set_sensitive (GTK_ACTION (action), !tp_str_empty (uri));
1256
1257         main_window_balance_update_balance (GTK_ACTION (action), conn);
1258
1259         g_signal_connect (conn, "balance-changed",
1260                 G_CALLBACK (main_window_balance_changed_cb), action);
1261
1262 }
1263
1264 static void
1265 main_window_remove_balance_action (EmpathyMainWindow *window,
1266                                    TpAccount         *account)
1267 {
1268         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1269         GtkAction *action;
1270         char *name;
1271         GList *a;
1272
1273         if (priv->balance_action_group == NULL)
1274                 return;
1275
1276         name = main_window_account_to_action_name (account);
1277
1278         action = gtk_action_group_get_action (
1279                 priv->balance_action_group, name);
1280
1281         if (action != NULL) {
1282                 guint merge_id;
1283
1284                 DEBUG ("Removing action");
1285
1286                 merge_id = GPOINTER_TO_UINT (g_object_get_data (
1287                         G_OBJECT (action),
1288                         "merge-id"));
1289
1290                 gtk_ui_manager_remove_ui (priv->ui_manager,
1291                         merge_id);
1292                 gtk_action_group_remove_action (
1293                         priv->balance_action_group, action);
1294         }
1295
1296         g_free (name);
1297
1298         a = gtk_action_group_list_actions (
1299                 priv->balance_action_group);
1300
1301         gtk_action_set_visible (
1302                 priv->view_balance_show_in_roster,
1303                 g_list_length (a) > 0);
1304
1305         g_list_free (a);
1306 }
1307
1308 static void
1309 main_window_connection_changed_cb (TpAccount  *account,
1310                                    guint       old_status,
1311                                    guint       current,
1312                                    guint       reason,
1313                                    gchar      *dbus_error_name,
1314                                    GHashTable *details,
1315                                    EmpathyMainWindow *window)
1316 {
1317         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1318
1319         main_window_update_status (window);
1320
1321         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
1322             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
1323                 main_window_error_display (window, account);
1324         }
1325
1326         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
1327                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
1328                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
1329
1330                 /* remove balance action if required */
1331                 main_window_remove_balance_action (window, account);
1332         }
1333
1334         if (current == TP_CONNECTION_STATUS_CONNECTED) {
1335                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
1336                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
1337
1338                 /* Account connected without error, remove error message if any */
1339                 main_window_remove_error (window, account);
1340                 main_window_setup_balance (window, account);
1341         }
1342 }
1343
1344 static void
1345 main_window_accels_load (void)
1346 {
1347         gchar *filename;
1348
1349         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
1350         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1351                 DEBUG ("Loading from:'%s'", filename);
1352                 gtk_accel_map_load (filename);
1353         }
1354
1355         g_free (filename);
1356 }
1357
1358 static void
1359 main_window_accels_save (void)
1360 {
1361         gchar *dir;
1362         gchar *file_with_path;
1363
1364         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
1365         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1366         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1367         g_free (dir);
1368
1369         DEBUG ("Saving to:'%s'", file_with_path);
1370         gtk_accel_map_save (file_with_path);
1371
1372         g_free (file_with_path);
1373 }
1374
1375 static void
1376 empathy_main_window_finalize (GObject *window)
1377 {
1378         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1379         GHashTableIter iter;
1380         gpointer key, value;
1381
1382         /* Save user-defined accelerators. */
1383         main_window_accels_save ();
1384
1385         g_list_free (priv->actions_connected);
1386
1387         g_object_unref (priv->account_manager);
1388         g_object_unref (priv->individual_store);
1389         g_object_unref (priv->contact_manager);
1390         g_object_unref (priv->sound_mgr);
1391         g_hash_table_destroy (priv->errors);
1392         g_hash_table_destroy (priv->auths);
1393
1394         /* disconnect all handlers of status-changed signal */
1395         g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1396         while (g_hash_table_iter_next (&iter, &key, &value))
1397                 g_signal_handler_disconnect (TP_ACCOUNT (key),
1398                                              GPOINTER_TO_UINT (value));
1399
1400         g_hash_table_destroy (priv->status_changed_handlers);
1401
1402         g_signal_handlers_disconnect_by_func (priv->event_manager,
1403                                               main_window_event_added_cb,
1404                                               window);
1405         g_signal_handlers_disconnect_by_func (priv->event_manager,
1406                                               main_window_event_removed_cb,
1407                                               window);
1408         g_object_unref (priv->call_observer);
1409         g_object_unref (priv->event_manager);
1410         g_object_unref (priv->ui_manager);
1411         g_object_unref (priv->chatroom_manager);
1412
1413         g_object_unref (priv->gsettings_ui);
1414         g_object_unref (priv->gsettings_contacts);
1415
1416         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
1417 }
1418
1419 static gboolean
1420 main_window_key_press_event_cb  (GtkWidget   *window,
1421                                  GdkEventKey *event,
1422                                  gpointer     user_data)
1423 {
1424         if (event->keyval == GDK_KEY_T
1425             && event->state & GDK_SHIFT_MASK
1426             && event->state & GDK_CONTROL_MASK) {
1427                 empathy_chat_manager_call_undo_closed_chat ();
1428         }
1429         return FALSE;
1430 }
1431
1432 static void
1433 main_window_chat_quit_cb (GtkAction         *action,
1434                           EmpathyMainWindow *window)
1435 {
1436         gtk_main_quit ();
1437 }
1438
1439 static void
1440 main_window_view_history_cb (GtkAction         *action,
1441                              EmpathyMainWindow *window)
1442 {
1443         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
1444 }
1445
1446 static void
1447 main_window_chat_new_message_cb (GtkAction         *action,
1448                                  EmpathyMainWindow *window)
1449 {
1450         empathy_new_message_dialog_show (GTK_WINDOW (window));
1451 }
1452
1453 static void
1454 main_window_chat_new_call_cb (GtkAction         *action,
1455                               EmpathyMainWindow *window)
1456 {
1457         empathy_new_call_dialog_show (GTK_WINDOW (window));
1458 }
1459
1460 static void
1461 main_window_chat_add_contact_cb (GtkAction         *action,
1462                                  EmpathyMainWindow *window)
1463 {
1464         empathy_new_individual_dialog_show (GTK_WINDOW (window));
1465 }
1466
1467 static void
1468 main_window_chat_search_contacts_cb (GtkAction         *action,
1469                                      EmpathyMainWindow *window)
1470 {
1471         GtkWidget *dialog = empathy_contact_search_dialog_new (
1472                         GTK_WINDOW (window));
1473         gtk_widget_show (dialog);
1474 }
1475
1476 static void
1477 main_window_view_show_ft_manager (GtkAction         *action,
1478                                   EmpathyMainWindow *window)
1479 {
1480         empathy_ft_manager_show ();
1481 }
1482
1483 static void
1484 main_window_view_show_offline_cb (GtkToggleAction   *action,
1485                                   EmpathyMainWindow *window)
1486 {
1487         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1488         gboolean current;
1489
1490         current = gtk_toggle_action_get_active (action);
1491         g_settings_set_boolean (priv->gsettings_ui,
1492                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
1493                                 current);
1494
1495         empathy_individual_view_set_show_offline (priv->individual_view,
1496                         current);
1497 }
1498
1499 static void
1500 main_window_notify_sort_contact_cb (GSettings         *gsettings,
1501                                     const gchar       *key,
1502                                     EmpathyMainWindow *window)
1503 {
1504         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1505         gchar *str;
1506
1507         str = g_settings_get_string (gsettings, key);
1508
1509         if (str != NULL) {
1510                 GType       type;
1511                 GEnumClass *enum_class;
1512                 GEnumValue *enum_value;
1513
1514                 type = empathy_individual_store_sort_get_type ();
1515                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1516                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1517                 if (enum_value) {
1518                         /* By changing the value of the GtkRadioAction,
1519                            it emits a signal that calls main_window_view_sort_contacts_cb
1520                            which updates the contacts list */
1521                         gtk_radio_action_set_current_value (priv->sort_by_name,
1522                                                             enum_value->value);
1523                 } else {
1524                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
1525                 }
1526                 g_free (str);
1527         }
1528 }
1529
1530 static void
1531 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
1532                                    GtkRadioAction    *current,
1533                                    EmpathyMainWindow *window)
1534 {
1535         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1536         EmpathyContactListStoreSort value;
1537         GSList      *group;
1538         GType        type;
1539         GEnumClass  *enum_class;
1540         GEnumValue  *enum_value;
1541
1542         value = gtk_radio_action_get_current_value (action);
1543         group = gtk_radio_action_get_group (action);
1544
1545         /* Get string from index */
1546         type = empathy_individual_store_sort_get_type ();
1547         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1548         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
1549
1550         if (!enum_value) {
1551                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
1552                            g_slist_index (group, action));
1553         } else {
1554                 g_settings_set_string (priv->gsettings_contacts,
1555                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1556                                        enum_value->value_nick);
1557         }
1558         empathy_individual_store_set_sort_criterium (priv->individual_store,
1559                         value);
1560 }
1561
1562 static void
1563 main_window_view_show_protocols_cb (GtkToggleAction   *action,
1564                                     EmpathyMainWindow *window)
1565 {
1566         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1567         gboolean value;
1568
1569         value = gtk_toggle_action_get_active (action);
1570
1571         g_settings_set_boolean (priv->gsettings_ui,
1572                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1573                                 value);
1574         empathy_individual_store_set_show_protocols (priv->individual_store,
1575                                                      value);
1576 }
1577
1578 /* Matches GtkRadioAction values set in empathy-main-window.ui */
1579 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
1580 #define CONTACT_LIST_NORMAL_SIZE                        1
1581 #define CONTACT_LIST_COMPACT_SIZE                       2
1582
1583 static void
1584 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
1585                                         GtkRadioAction    *current,
1586                                         EmpathyMainWindow *window)
1587 {
1588         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1589         GSettings *gsettings_ui;
1590         gint value;
1591
1592         value = gtk_radio_action_get_current_value (action);
1593         /* create a new GSettings, so we can delay the setting until both
1594          * values are set */
1595         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1596
1597         DEBUG ("radio button toggled, value = %i", value);
1598
1599         g_settings_delay (gsettings_ui);
1600         g_settings_set_boolean (gsettings_ui,
1601                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
1602                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1603
1604         g_settings_set_boolean (gsettings_ui,
1605                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1606                                 value == CONTACT_LIST_COMPACT_SIZE);
1607         g_settings_apply (gsettings_ui);
1608
1609         /* FIXME: these enums probably have the wrong namespace */
1610         empathy_individual_store_set_show_avatars (priv->individual_store,
1611                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1612         empathy_individual_store_set_is_compact (priv->individual_store,
1613                         value == CONTACT_LIST_COMPACT_SIZE);
1614
1615         g_object_unref (gsettings_ui);
1616 }
1617
1618 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
1619                                                   const gchar       *key,
1620                                                   EmpathyMainWindow *window)
1621 {
1622         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1623
1624         gtk_toggle_action_set_active (priv->show_protocols,
1625                         g_settings_get_boolean (gsettings,
1626                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
1627 }
1628
1629
1630 static void
1631 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
1632                                          const gchar       *key,
1633                                          EmpathyMainWindow *window)
1634 {
1635         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1636         gint value;
1637
1638         if (g_settings_get_boolean (gsettings,
1639                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
1640                 value = CONTACT_LIST_COMPACT_SIZE;
1641         } else if (g_settings_get_boolean (gsettings,
1642                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
1643                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
1644         } else {
1645                 value = CONTACT_LIST_NORMAL_SIZE;
1646         }
1647
1648         DEBUG ("setting changed, value = %i", value);
1649
1650         /* By changing the value of the GtkRadioAction,
1651            it emits a signal that calls main_window_view_contacts_list_size_cb
1652            which updates the contacts list */
1653         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
1654 }
1655
1656 static void
1657 main_window_edit_search_contacts_cb (GtkCheckMenuItem  *item,
1658                                      EmpathyMainWindow *window)
1659 {
1660         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1661
1662         empathy_individual_view_start_search (priv->individual_view);
1663 }
1664
1665 static void
1666 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
1667                               EmpathyMainWindow *window)
1668 {
1669 #ifdef HAVE_LIBCHAMPLAIN
1670         empathy_map_view_show ();
1671 #endif
1672 }
1673
1674 static void
1675 join_chatroom (EmpathyChatroom *chatroom,
1676                gint64 timestamp)
1677 {
1678         TpAccount      *account;
1679         const gchar    *room;
1680
1681         account = empathy_chatroom_get_account (chatroom);
1682         room = empathy_chatroom_get_room (chatroom);
1683
1684         DEBUG ("Requesting channel for '%s'", room);
1685         empathy_join_muc (account, room, timestamp);
1686 }
1687
1688 typedef struct
1689 {
1690         TpAccount *account;
1691         EmpathyChatroom *chatroom;
1692         gint64 timestamp;
1693         glong sig_id;
1694         guint timeout;
1695 } join_fav_account_sig_ctx;
1696
1697 static join_fav_account_sig_ctx *
1698 join_fav_account_sig_ctx_new (TpAccount *account,
1699                              EmpathyChatroom *chatroom,
1700                               gint64 timestamp)
1701 {
1702         join_fav_account_sig_ctx *ctx = g_slice_new0 (
1703                 join_fav_account_sig_ctx);
1704
1705         ctx->account = g_object_ref (account);
1706         ctx->chatroom = g_object_ref (chatroom);
1707         ctx->timestamp = timestamp;
1708         return ctx;
1709 }
1710
1711 static void
1712 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1713 {
1714         g_object_unref (ctx->account);
1715         g_object_unref (ctx->chatroom);
1716         g_slice_free (join_fav_account_sig_ctx, ctx);
1717 }
1718
1719 static void
1720 account_status_changed_cb (TpAccount  *account,
1721                            TpConnectionStatus old_status,
1722                            TpConnectionStatus new_status,
1723                            guint reason,
1724                            gchar *dbus_error_name,
1725                            GHashTable *details,
1726                            gpointer user_data)
1727 {
1728         join_fav_account_sig_ctx *ctx = user_data;
1729
1730         switch (new_status) {
1731                 case TP_CONNECTION_STATUS_DISCONNECTED:
1732                         /* Don't wait any longer */
1733                         goto finally;
1734                         break;
1735
1736                 case TP_CONNECTION_STATUS_CONNECTING:
1737                         /* Wait a bit */
1738                         return;
1739
1740                 case TP_CONNECTION_STATUS_CONNECTED:
1741                         /* We can join the room */
1742                         break;
1743
1744                 default:
1745                         g_assert_not_reached ();
1746         }
1747
1748         join_chatroom (ctx->chatroom, ctx->timestamp);
1749
1750 finally:
1751         g_source_remove (ctx->timeout);
1752         g_signal_handler_disconnect (account, ctx->sig_id);
1753 }
1754
1755 #define JOIN_FAVORITE_TIMEOUT 5
1756
1757 static gboolean
1758 join_favorite_timeout_cb (gpointer data)
1759 {
1760         join_fav_account_sig_ctx *ctx = data;
1761
1762         /* stop waiting for joining the favorite room */
1763         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1764         return FALSE;
1765 }
1766
1767 static void
1768 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1769 {
1770         TpAccount      *account;
1771
1772         account = empathy_chatroom_get_account (chatroom);
1773         if (tp_account_get_connection_status (account, NULL) !=
1774                                              TP_CONNECTION_STATUS_CONNECTED) {
1775                 join_fav_account_sig_ctx *ctx;
1776
1777                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1778                         empathy_get_current_action_time ());
1779
1780                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1781                         G_CALLBACK (account_status_changed_cb), ctx,
1782                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1783
1784                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1785                         join_favorite_timeout_cb, ctx);
1786                 return;
1787         }
1788
1789         join_chatroom (chatroom, empathy_get_current_action_time ());
1790 }
1791
1792 static void
1793 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1794                                                 EmpathyChatroom *chatroom)
1795 {
1796         main_window_favorite_chatroom_join (chatroom);
1797 }
1798
1799 static void
1800 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1801                                         EmpathyChatroom   *chatroom)
1802 {
1803         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1804         GtkWidget   *menu_item;
1805         const gchar *name, *account_name;
1806         gchar *label;
1807
1808
1809         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1810                 return;
1811         }
1812
1813         name = empathy_chatroom_get_name (chatroom);
1814         account_name = tp_account_get_display_name (
1815                         empathy_chatroom_get_account (chatroom));
1816         label = g_strdup_printf ("%s (%s)", name, account_name);
1817         menu_item = gtk_menu_item_new_with_label (label);
1818         g_free (label);
1819         g_object_set_data (G_OBJECT (menu_item), "is_favorite",
1820                         GUINT_TO_POINTER (TRUE));
1821
1822         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1823         g_signal_connect (menu_item, "activate",
1824                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1825                           chatroom);
1826
1827         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1828                                menu_item, 4);
1829
1830         gtk_widget_show (menu_item);
1831 }
1832
1833 static void
1834 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1835                                              EmpathyChatroom        *chatroom,
1836                                              EmpathyMainWindow      *window)
1837 {
1838         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1839
1840         main_window_favorite_chatroom_menu_add (window, chatroom);
1841         gtk_widget_show (priv->room_separator);
1842         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1843 }
1844
1845 static void
1846 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1847                                                EmpathyChatroom        *chatroom,
1848                                                EmpathyMainWindow      *window)
1849 {
1850         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1851         GtkWidget *menu_item;
1852         GList *chatrooms;
1853
1854         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1855         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1856         gtk_widget_destroy (menu_item);
1857
1858         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1859         if (chatrooms) {
1860                 gtk_widget_show (priv->room_separator);
1861         } else {
1862                 gtk_widget_hide (priv->room_separator);
1863         }
1864
1865         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1866         g_list_free (chatrooms);
1867 }
1868
1869 static void
1870 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1871 {
1872         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1873         GList *chatrooms, *l;
1874         GtkWidget *room;
1875
1876         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1877         chatrooms = empathy_chatroom_manager_get_chatrooms (
1878                 priv->chatroom_manager, NULL);
1879         room = gtk_ui_manager_get_widget (priv->ui_manager,
1880                 "/menubar/room");
1881         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1882         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1883                 "/menubar/room/room_separator");
1884
1885         for (l = chatrooms; l; l = l->next) {
1886                 main_window_favorite_chatroom_menu_add (window, l->data);
1887         }
1888
1889         if (!chatrooms) {
1890                 gtk_widget_hide (priv->room_separator);
1891         }
1892
1893         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1894
1895         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1896                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1897                           window);
1898         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1899                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1900                           window);
1901
1902         g_list_free (chatrooms);
1903 }
1904
1905 static void
1906 main_window_room_join_new_cb (GtkAction         *action,
1907                               EmpathyMainWindow *window)
1908 {
1909         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1910 }
1911
1912 static void
1913 main_window_room_join_favorites_cb (GtkAction         *action,
1914                                     EmpathyMainWindow *window)
1915 {
1916         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1917         GList *chatrooms, *l;
1918
1919         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1920         for (l = chatrooms; l; l = l->next) {
1921                 main_window_favorite_chatroom_join (l->data);
1922         }
1923         g_list_free (chatrooms);
1924 }
1925
1926 static void
1927 main_window_room_manage_favorites_cb (GtkAction         *action,
1928                                       EmpathyMainWindow *window)
1929 {
1930         empathy_chatrooms_window_show (GTK_WINDOW (window));
1931 }
1932
1933 static void
1934 main_window_edit_cb (GtkAction         *action,
1935                      EmpathyMainWindow *window)
1936 {
1937         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1938         GtkWidget *submenu;
1939
1940         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1941         submenu = empathy_individual_view_get_individual_menu (
1942                         priv->individual_view);
1943         if (submenu) {
1944                 GtkMenuItem *item;
1945                 GtkWidget   *label;
1946
1947                 item = GTK_MENU_ITEM (priv->edit_context);
1948                 label = gtk_bin_get_child (GTK_BIN (item));
1949                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1950
1951                 gtk_widget_show (priv->edit_context);
1952                 gtk_widget_show (priv->edit_context_separator);
1953
1954                 gtk_menu_item_set_submenu (item, submenu);
1955
1956                 return;
1957         }
1958
1959         submenu = empathy_individual_view_get_group_menu (
1960                         priv->individual_view);
1961         if (submenu) {
1962                 GtkMenuItem *item;
1963                 GtkWidget   *label;
1964
1965                 item = GTK_MENU_ITEM (priv->edit_context);
1966                 label = gtk_bin_get_child (GTK_BIN (item));
1967                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1968
1969                 gtk_widget_show (priv->edit_context);
1970                 gtk_widget_show (priv->edit_context_separator);
1971
1972                 gtk_menu_item_set_submenu (item, submenu);
1973
1974                 return;
1975         }
1976
1977         gtk_widget_hide (priv->edit_context);
1978         gtk_widget_hide (priv->edit_context_separator);
1979
1980         return;
1981 }
1982
1983 static void
1984 main_window_edit_accounts_cb (GtkAction         *action,
1985                               EmpathyMainWindow *window)
1986 {
1987         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1988                         NULL, FALSE, FALSE);
1989 }
1990
1991 static void
1992 main_window_edit_personal_information_cb (GtkAction         *action,
1993                                           EmpathyMainWindow *window)
1994 {
1995         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1996 }
1997
1998 static void
1999 main_window_edit_blocked_contacts_cb (GtkAction         *action,
2000                                       EmpathyMainWindow *window)
2001 {
2002         GtkWidget *dialog;
2003
2004         dialog = empathy_contact_blocking_dialog_new (GTK_WINDOW (window));
2005         gtk_widget_show (dialog);
2006         g_signal_connect (dialog, "response",
2007                         G_CALLBACK (gtk_widget_destroy), NULL);
2008 }
2009
2010 void
2011 empathy_main_window_show_preferences (EmpathyMainWindow *window,
2012     const gchar *tab)
2013 {
2014         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2015
2016         if (priv->preferences == NULL) {
2017                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window),
2018                                                              priv->shell_running);
2019                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
2020                                            (gpointer) &priv->preferences);
2021
2022                 gtk_widget_show (priv->preferences);
2023         } else {
2024                 gtk_window_present (GTK_WINDOW (priv->preferences));
2025         }
2026
2027         if (tab != NULL)
2028                 empathy_preferences_show_tab (
2029                         EMPATHY_PREFERENCES (priv->preferences), tab);
2030 }
2031
2032 static void
2033 main_window_edit_preferences_cb (GtkAction         *action,
2034                                  EmpathyMainWindow *window)
2035 {
2036         empathy_main_window_show_preferences (window, NULL);
2037 }
2038
2039 static void
2040 main_window_help_about_cb (GtkAction         *action,
2041                            EmpathyMainWindow *window)
2042 {
2043         empathy_about_dialog_new (GTK_WINDOW (window));
2044 }
2045
2046 static void
2047 main_window_help_debug_cb (GtkAction         *action,
2048                            EmpathyMainWindow *window)
2049 {
2050         empathy_launch_program (BIN_DIR, "empathy-debugger", NULL);
2051 }
2052
2053 static void
2054 main_window_help_contents_cb (GtkAction         *action,
2055                               EmpathyMainWindow *window)
2056 {
2057         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
2058 }
2059
2060 static gboolean
2061 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
2062                                             GdkEventButton    *event,
2063                                             EmpathyMainWindow *window)
2064 {
2065         if (event->type != GDK_BUTTON_PRESS ||
2066             event->button != 1) {
2067                 return FALSE;
2068         }
2069
2070         empathy_accounts_dialog_show_application (
2071                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
2072                         NULL, FALSE, FALSE);
2073
2074         return FALSE;
2075 }
2076
2077 static void
2078 main_window_account_removed_cb (TpAccountManager  *manager,
2079                                 TpAccount         *account,
2080                                 EmpathyMainWindow *window)
2081 {
2082         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2083         GList *a;
2084
2085         a = tp_account_manager_get_valid_accounts (manager);
2086
2087         gtk_action_set_sensitive (priv->view_history,
2088                 g_list_length (a) > 0);
2089
2090         g_list_free (a);
2091
2092         /* remove errors if any */
2093         main_window_remove_error (window, account);
2094
2095         /* remove the balance action if required */
2096         main_window_remove_balance_action (window, account);
2097 }
2098
2099 static void
2100 main_window_account_validity_changed_cb (TpAccountManager  *manager,
2101                                          TpAccount         *account,
2102                                          gboolean           valid,
2103                                          EmpathyMainWindow *window)
2104 {
2105         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2106
2107         if (valid) {
2108                 gulong handler_id;
2109                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
2110                         priv->status_changed_handlers, account));
2111
2112                 /* connect signal only if it was not connected yet */
2113                 if (handler_id == 0) {
2114                         handler_id = g_signal_connect (account,
2115                                 "status-changed",
2116                                 G_CALLBACK (main_window_connection_changed_cb),
2117                                 window);
2118                         g_hash_table_insert (priv->status_changed_handlers,
2119                                 account, GUINT_TO_POINTER (handler_id));
2120                 }
2121         }
2122
2123         main_window_account_removed_cb (manager, account, window);
2124 }
2125
2126 static void
2127 main_window_notify_show_offline_cb (GSettings   *gsettings,
2128                                     const gchar *key,
2129                                     gpointer     toggle_action)
2130 {
2131         gtk_toggle_action_set_active (toggle_action,
2132                         g_settings_get_boolean (gsettings, key));
2133 }
2134
2135 static void
2136 main_window_connection_items_setup (EmpathyMainWindow *window,
2137                                     GtkBuilder        *gui)
2138 {
2139         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2140         GList         *list;
2141         GObject       *action;
2142         guint          i;
2143         const gchar *actions_connected[] = {
2144                 "room_join_new",
2145                 "room_join_favorites",
2146                 "chat_new_message",
2147                 "chat_new_call",
2148                 "chat_search_contacts",
2149                 "chat_add_contact",
2150                 "edit_personal_information",
2151                 "edit_blocked_contacts",
2152                 "edit_search_contacts"
2153         };
2154
2155         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
2156                 action = gtk_builder_get_object (gui, actions_connected[i]);
2157                 list = g_list_prepend (list, action);
2158         }
2159
2160         priv->actions_connected = list;
2161 }
2162
2163 static void
2164 account_manager_prepared_cb (GObject      *source_object,
2165                              GAsyncResult *result,
2166                              gpointer      user_data)
2167 {
2168         GList *accounts, *j;
2169         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2170         EmpathyMainWindow *window = user_data;
2171         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2172         GError *error = NULL;
2173
2174         if (!tp_proxy_prepare_finish (manager, result, &error)) {
2175                 DEBUG ("Failed to prepare account manager: %s", error->message);
2176                 g_error_free (error);
2177                 return;
2178         }
2179
2180         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
2181         for (j = accounts; j != NULL; j = j->next) {
2182                 TpAccount *account = TP_ACCOUNT (j->data);
2183                 gulong handler_id;
2184
2185                 handler_id = g_signal_connect (account, "status-changed",
2186                                   G_CALLBACK (main_window_connection_changed_cb),
2187                                   window);
2188                 g_hash_table_insert (priv->status_changed_handlers,
2189                                      account, GUINT_TO_POINTER (handler_id));
2190
2191                 main_window_setup_balance (window, account);
2192         }
2193
2194         g_signal_connect (manager, "account-validity-changed",
2195                           G_CALLBACK (main_window_account_validity_changed_cb),
2196                           window);
2197
2198         main_window_update_status (window);
2199
2200         /* Disable the "Previous Conversations" menu entry if there is no account */
2201         gtk_action_set_sensitive (priv->view_history,
2202                 g_list_length (accounts) > 0);
2203
2204         g_list_free (accounts);
2205 }
2206
2207 void
2208 empathy_main_window_set_shell_running (EmpathyMainWindow *window,
2209                                        gboolean          shell_running)
2210 {
2211         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2212
2213         if (priv->shell_running == shell_running)
2214                 return;
2215
2216         priv->shell_running = shell_running;
2217         g_object_notify (G_OBJECT (window), "shell-running");
2218 }
2219
2220 static GObject *
2221 empathy_main_window_constructor (GType type,
2222                                  guint n_construct_params,
2223                                  GObjectConstructParam *construct_params)
2224 {
2225         static GObject *window = NULL;
2226
2227         if (window != NULL)
2228                 return g_object_ref (window);
2229
2230         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
2231                 type, n_construct_params, construct_params);
2232
2233         g_object_add_weak_pointer (window, (gpointer) &window);
2234
2235         return window;
2236 }
2237
2238 static void
2239 empathy_main_window_set_property (GObject       *object,
2240                                   guint         property_id,
2241                                   const GValue *value,
2242                                   GParamSpec    *pspec)
2243 {
2244         EmpathyMainWindow *self = EMPATHY_MAIN_WINDOW (object);
2245         EmpathyMainWindowPriv *priv = GET_PRIV (self);
2246
2247         switch (property_id)
2248         {
2249                 case PROP_SHELL_RUNNING:
2250                         priv->shell_running = g_value_get_boolean (value);
2251                         break;
2252                 default:
2253                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2254                         break;
2255         }
2256 }
2257
2258 static void
2259 empathy_main_window_get_property (GObject    *object,
2260                                   guint      property_id,
2261                                   GValue     *value,
2262                                   GParamSpec *pspec)
2263 {
2264         EmpathyMainWindow *self = EMPATHY_MAIN_WINDOW (object);
2265         EmpathyMainWindowPriv *priv = GET_PRIV (self);
2266
2267         switch (property_id)
2268         {
2269                 case PROP_SHELL_RUNNING:
2270                         g_value_set_boolean (value, priv->shell_running);
2271                         break;
2272                 default:
2273                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2274                         break;
2275         }
2276 }
2277
2278 static void
2279 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
2280 {
2281         GObjectClass *object_class = G_OBJECT_CLASS (klass);
2282         GParamSpec *pspec;
2283
2284         object_class->finalize = empathy_main_window_finalize;
2285         object_class->constructor = empathy_main_window_constructor;
2286
2287         object_class->set_property = empathy_main_window_set_property;
2288         object_class->get_property = empathy_main_window_get_property;
2289
2290         pspec = g_param_spec_boolean ("shell-running",
2291                                       "Shell running",
2292                                       "Whether the Shell is running or not",
2293                                       FALSE,
2294                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2295         g_object_class_install_property (object_class, PROP_SHELL_RUNNING, pspec);
2296
2297         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
2298 }
2299
2300 static void
2301 empathy_main_window_init (EmpathyMainWindow *window)
2302 {
2303         EmpathyMainWindowPriv    *priv;
2304         EmpathyIndividualManager *individual_manager;
2305         GtkBuilder               *gui, *gui_mgr;
2306         GtkWidget                *sw;
2307         GtkToggleAction          *show_offline_widget;
2308         GtkAction                *show_map_widget;
2309         GtkToolItem              *item;
2310         gboolean                  show_offline;
2311         gchar                    *filename;
2312         GtkTreeModel             *model;
2313         GtkWidget                *search_vbox;
2314         GtkWidget                *menubar;
2315
2316         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
2317                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
2318
2319         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2320         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
2321
2322         priv->sound_mgr = empathy_sound_manager_dup_singleton ();
2323
2324         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
2325         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
2326         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
2327
2328         /* don't finalize the widget on delete-event, just hide it */
2329         g_signal_connect (window, "delete-event",
2330                 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
2331
2332         /* Set up interface */
2333         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
2334         gui = empathy_builder_get_file (filename,
2335                                        "main_vbox", &priv->main_vbox,
2336                                        "balance_vbox", &priv->balance_vbox,
2337                                        "errors_vbox", &priv->errors_vbox,
2338                                        "auth_vbox", &priv->auth_vbox,
2339                                        "search_vbox", &search_vbox,
2340                                        "presence_toolbar", &priv->presence_toolbar,
2341                                        "notebook", &priv->notebook,
2342                                        "no_entry_label", &priv->no_entry_label,
2343                                        "roster_scrolledwindow", &sw,
2344                                        NULL);
2345         g_free (filename);
2346
2347         /* Set UI manager */
2348         filename = empathy_file_lookup ("empathy-main-window-menubar.ui", "src");
2349         gui_mgr = empathy_builder_get_file (filename,
2350                                        "ui_manager", &priv->ui_manager,
2351                                        "view_show_offline", &show_offline_widget,
2352                                        "view_show_protocols", &priv->show_protocols,
2353                                        "view_sort_by_name", &priv->sort_by_name,
2354                                        "view_sort_by_status", &priv->sort_by_status,
2355                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
2356                                        "view_normal_size", &priv->normal_size,
2357                                        "view_compact_size", &priv->compact_size,
2358                                        "view_history", &priv->view_history,
2359                                        "view_show_map", &show_map_widget,
2360                                        "room_join_favorites", &priv->room_join_favorites,
2361                                        "view_balance_show_in_roster", &priv->view_balance_show_in_roster,
2362                                        "menubar", &menubar,
2363                                        NULL);
2364         g_free (filename);
2365
2366         /* The UI manager is living in its own .ui file as Glade doesn't support
2367          * those. The GtkMenubar has to be in this file as well to we manually add
2368          * it to the first position of the vbox. */
2369         gtk_box_pack_start (GTK_BOX (priv->main_vbox), menubar, FALSE, FALSE, 0);
2370         gtk_box_reorder_child (GTK_BOX (priv->main_vbox), menubar, 0);
2371
2372         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
2373         gtk_widget_show (priv->main_vbox);
2374
2375         g_signal_connect (window, "key-press-event",
2376                           G_CALLBACK (main_window_key_press_event_cb), NULL);
2377
2378         empathy_builder_connect (gui_mgr, window,
2379                               "chat_quit", "activate", main_window_chat_quit_cb,
2380                               "chat_new_message", "activate", main_window_chat_new_message_cb,
2381                               "chat_new_call", "activate", main_window_chat_new_call_cb,
2382                               "view_history", "activate", main_window_view_history_cb,
2383                               "room_join_new", "activate", main_window_room_join_new_cb,
2384                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
2385                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
2386                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
2387                               "chat_search_contacts", "activate", main_window_chat_search_contacts_cb,
2388                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
2389                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
2390                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
2391                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
2392                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
2393                               "view_show_map", "activate", main_window_view_show_map_cb,
2394                               "edit", "activate", main_window_edit_cb,
2395                               "edit_accounts", "activate", main_window_edit_accounts_cb,
2396                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
2397                               "edit_blocked_contacts", "activate", main_window_edit_blocked_contacts_cb,
2398                               "edit_preferences", "activate", main_window_edit_preferences_cb,
2399                               "edit_search_contacts", "activate", main_window_edit_search_contacts_cb,
2400                               "help_about", "activate", main_window_help_about_cb,
2401                               "help_debug", "activate", main_window_help_debug_cb,
2402                               "help_contents", "activate", main_window_help_contents_cb,
2403                               NULL);
2404
2405         /* Set up connection related widgets. */
2406         main_window_connection_items_setup (window, gui_mgr);
2407
2408         g_object_ref (priv->ui_manager);
2409         g_object_unref (gui);
2410         g_object_unref (gui_mgr);
2411
2412 #ifndef HAVE_LIBCHAMPLAIN
2413         gtk_action_set_visible (show_map_widget, FALSE);
2414 #endif
2415
2416         priv->account_manager = tp_account_manager_dup ();
2417
2418         tp_proxy_prepare_async (priv->account_manager, NULL,
2419                                           account_manager_prepared_cb, window);
2420
2421         priv->errors = g_hash_table_new_full (g_direct_hash,
2422                                               g_direct_equal,
2423                                               g_object_unref,
2424                                               NULL);
2425
2426         priv->auths = g_hash_table_new (NULL, NULL);
2427
2428         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
2429                                                                g_direct_equal,
2430                                                                NULL,
2431                                                                NULL);
2432
2433         /* Set up menu */
2434         main_window_favorite_chatroom_menu_setup (window);
2435
2436         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
2437                 "/menubar/edit/edit_context");
2438         priv->edit_context_separator = gtk_ui_manager_get_widget (
2439                 priv->ui_manager,
2440                 "/menubar/edit/edit_context_separator");
2441         gtk_widget_hide (priv->edit_context);
2442         gtk_widget_hide (priv->edit_context_separator);
2443
2444         /* Set up contact list. */
2445         empathy_status_presets_get_all ();
2446
2447         /* Set up presence chooser */
2448         priv->presence_chooser = empathy_presence_chooser_new ();
2449         gtk_widget_show (priv->presence_chooser);
2450         item = gtk_tool_item_new ();
2451         gtk_widget_show (GTK_WIDGET (item));
2452         gtk_widget_set_size_request (priv->presence_chooser, 10, -1);
2453         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
2454         gtk_tool_item_set_is_important (item, TRUE);
2455         gtk_tool_item_set_expand (item, TRUE);
2456         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
2457
2458         /* Set up the throbber */
2459         priv->throbber = gtk_spinner_new ();
2460         gtk_widget_set_size_request (priv->throbber, 16, -1);
2461         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
2462         g_signal_connect (priv->throbber, "button-press-event",
2463                 G_CALLBACK (main_window_throbber_button_press_event_cb),
2464                 window);
2465         gtk_widget_show (priv->throbber);
2466
2467         item = gtk_tool_item_new ();
2468         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
2469         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
2470         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
2471         priv->throbber_tool_item = GTK_WIDGET (item);
2472
2473         /* XXX: this class is designed to live for the duration of the program,
2474          * so it's got a race condition between its signal handlers and its
2475          * finalization. The class is planned to be removed, so we won't fix
2476          * this before then. */
2477         priv->contact_manager = EMPATHY_CONTACT_LIST (
2478                         empathy_contact_manager_dup_singleton ());
2479         individual_manager = empathy_individual_manager_dup_singleton ();
2480         priv->individual_store = empathy_individual_store_new (
2481                         individual_manager);
2482         g_object_unref (individual_manager);
2483
2484         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
2485          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
2486          * drop, so allowing them would achieve nothing except confusion. */
2487         priv->individual_view = empathy_individual_view_new (
2488                         priv->individual_store,
2489                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
2490                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
2491
2492         gtk_widget_show (GTK_WIDGET (priv->individual_view));
2493         gtk_container_add (GTK_CONTAINER (sw),
2494                            GTK_WIDGET (priv->individual_view));
2495         g_signal_connect (priv->individual_view, "row-activated",
2496                           G_CALLBACK (main_window_row_activated_cb),
2497                           window);
2498
2499         /* Set up search bar */
2500         priv->search_bar = empathy_live_search_new (
2501                 GTK_WIDGET (priv->individual_view));
2502         empathy_individual_view_set_live_search (priv->individual_view,
2503                 EMPATHY_LIVE_SEARCH (priv->search_bar));
2504         gtk_box_pack_start (GTK_BOX (search_vbox), priv->search_bar,
2505                 FALSE, TRUE, 0);
2506
2507         g_signal_connect_swapped (window, "map",
2508                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
2509
2510         /* Connect to proper signals to check if contact list is empty or not */
2511         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
2512         priv->empty = TRUE;
2513         g_signal_connect (model, "row-inserted",
2514                           G_CALLBACK (main_window_row_inserted_cb),
2515                           window);
2516         g_signal_connect (model, "row-deleted",
2517                           G_CALLBACK (main_window_row_deleted_cb),
2518                           window);
2519
2520         /* Load user-defined accelerators. */
2521         main_window_accels_load ();
2522
2523         /* Set window size. */
2524         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
2525
2526         /* bind view_balance_show_in_roster */
2527         g_settings_bind (priv->gsettings_ui, "show-balance-in-roster",
2528                 priv->view_balance_show_in_roster, "active",
2529                 G_SETTINGS_BIND_DEFAULT);
2530         g_object_bind_property (priv->view_balance_show_in_roster, "active",
2531                 priv->balance_vbox, "visible",
2532                 G_BINDING_SYNC_CREATE);
2533
2534         /* Enable event handling */
2535         priv->call_observer = empathy_call_observer_dup_singleton ();
2536         priv->event_manager = empathy_event_manager_dup_singleton ();
2537
2538         g_signal_connect (priv->event_manager, "event-added",
2539                           G_CALLBACK (main_window_event_added_cb), window);
2540         g_signal_connect (priv->event_manager, "event-removed",
2541                           G_CALLBACK (main_window_event_removed_cb), window);
2542         g_signal_connect (priv->account_manager, "account-validity-changed",
2543                           G_CALLBACK (main_window_account_validity_changed_cb),
2544                           window);
2545         g_signal_connect (priv->account_manager, "account-removed",
2546                           G_CALLBACK (main_window_account_removed_cb),
2547                           window);
2548         g_signal_connect (priv->account_manager, "account-disabled",
2549                           G_CALLBACK (main_window_account_disabled_cb),
2550                           window);
2551
2552         /* Show offline ? */
2553         show_offline = g_settings_get_boolean (priv->gsettings_ui,
2554                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
2555         g_signal_connect (priv->gsettings_ui,
2556                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
2557                           G_CALLBACK (main_window_notify_show_offline_cb),
2558                           show_offline_widget);
2559
2560         gtk_toggle_action_set_active (show_offline_widget, show_offline);
2561
2562         /* Show protocol ? */
2563         g_signal_connect (priv->gsettings_ui,
2564                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2565                           G_CALLBACK (main_window_notify_show_protocols_cb),
2566                           window);
2567
2568         main_window_notify_show_protocols_cb (priv->gsettings_ui,
2569                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2570                                               window);
2571
2572         /* Sort by name / by status ? */
2573         g_signal_connect (priv->gsettings_contacts,
2574                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2575                           G_CALLBACK (main_window_notify_sort_contact_cb),
2576                           window);
2577
2578         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
2579                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2580                                             window);
2581
2582         /* Contacts list size */
2583         g_signal_connect (priv->gsettings_ui,
2584                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
2585                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2586                           window);
2587         g_signal_connect (priv->gsettings_ui,
2588                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
2589                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2590                           window);
2591
2592         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
2593                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
2594                                                  window);
2595 }
2596
2597 GtkWidget *
2598 empathy_main_window_dup (void)
2599 {
2600         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
2601 }