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