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