]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Merge branch 'trivia'
[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 = g_object_get_data (G_OBJECT (action), "account");
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_object_set_data (G_OBJECT (action), "account", account);
981         g_signal_connect (action, "activate",
982                 G_CALLBACK (main_window_balance_activate_cb), window);
983
984         gtk_action_group_add_action (priv->balance_action_group, action);
985         g_object_unref (action);
986
987         ui = g_strdup_printf (
988                 "<ui>"
989                 " <menubar name='menubar'>"
990                 "  <menu action='view'>"
991                 "   <placeholder name='view_balance_placeholder'>"
992                 "    <menuitem action='%s'/>"
993                 "   </placeholder>"
994                 "  </menu>"
995                 " </menubar>"
996                 "</ui>",
997                 name);
998
999         merge_id = gtk_ui_manager_add_ui_from_string (priv->ui_manager,
1000                 ui, -1, &error);
1001         if (error != NULL) {
1002                 DEBUG ("Failed to add balance UI for %s: %s",
1003                         tp_account_get_display_name (account),
1004                         error->message);
1005                 g_error_free (error);
1006         }
1007
1008         g_object_set_data (G_OBJECT (action),
1009                 "merge-id", GUINT_TO_POINTER (merge_id));
1010
1011         g_free (name);
1012         g_free (ui);
1013
1014         return action;
1015 }
1016
1017 static GtkWidget *
1018 main_window_setup_balance_create_widget (EmpathyMainWindow *window,
1019                                          GtkAction         *action)
1020 {
1021         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1022         TpAccount *account;
1023         GtkWidget *hbox, *image, *label, *button;
1024
1025         account = g_object_get_data (G_OBJECT (action), "account");
1026         g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1027
1028         hbox = gtk_hbox_new (FALSE, 6);
1029
1030         /* protocol icon */
1031         image = gtk_image_new ();
1032         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, TRUE, 0);
1033         g_object_bind_property (action, "icon-name", image, "icon-name",
1034                 G_BINDING_SYNC_CREATE);
1035
1036         /* account name label */
1037         label = gtk_label_new ("");
1038         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1039         gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
1040         g_object_bind_property (account, "display-name", label, "label",
1041                 G_BINDING_SYNC_CREATE);
1042
1043         /* balance label */
1044         label = gtk_label_new ("");
1045         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1046         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
1047         g_object_set_data (G_OBJECT (action), "money-label", label);
1048
1049         /* top up button */
1050         button = gtk_button_new_with_label (_("Top Up..."));
1051         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
1052         g_signal_connect_swapped (button, "clicked",
1053                 G_CALLBACK (gtk_action_activate), action);
1054
1055         gtk_box_pack_start (GTK_BOX (priv->balance_vbox), hbox, FALSE, TRUE, 0);
1056         gtk_widget_show_all (hbox);
1057
1058         /* tie the lifetime of the widget to the lifetime of the action */
1059         g_object_weak_ref (G_OBJECT (action),
1060                 (GWeakNotify) gtk_widget_destroy, hbox);
1061
1062         return hbox;
1063 }
1064
1065 static void
1066 main_window_setup_balance (EmpathyMainWindow *window,
1067                            TpAccount         *account)
1068 {
1069         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1070         TpConnection *conn = tp_account_get_connection (account);
1071         GtkAction *action;
1072         const gchar *uri;
1073
1074         if (conn == NULL)
1075                 return;
1076
1077         /* need to prepare the connection:
1078          * store the account on the connection */
1079         g_object_set_data (G_OBJECT (conn), "account", account);
1080
1081         if (!tp_proxy_is_prepared (conn, TP_CONNECTION_FEATURE_BALANCE))
1082                 return;
1083
1084         DEBUG ("Setting up balance for acct: %s",
1085                 tp_account_get_display_name (account));
1086
1087         /* create the action */
1088         action = main_window_setup_balance_create_action (window, account);
1089
1090         if (action == NULL)
1091                 return;
1092
1093         gtk_action_set_visible (priv->view_balance_show_in_roster, TRUE);
1094
1095         /* create the display widget */
1096         main_window_setup_balance_create_widget (window, action);
1097
1098         /* check the current balance and monitor for any changes */
1099         uri = tp_connection_get_balance_uri (conn);
1100
1101         g_object_set_data_full (G_OBJECT (action), "manage-credit-uri",
1102                 g_strdup (uri), g_free);
1103         gtk_action_set_sensitive (GTK_ACTION (action), !tp_str_empty (uri));
1104
1105         main_window_balance_update_balance (GTK_ACTION (action), conn);
1106
1107         g_signal_connect (conn, "balance-changed",
1108                 G_CALLBACK (main_window_balance_changed_cb), action);
1109
1110 }
1111
1112 static void
1113 main_window_remove_balance_action (EmpathyMainWindow *window,
1114                                    TpAccount         *account)
1115 {
1116         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1117         GtkAction *action;
1118         char *name;
1119         GList *a;
1120
1121         if (priv->balance_action_group == NULL)
1122                 return;
1123
1124         name = main_window_account_to_action_name (account);
1125
1126         action = gtk_action_group_get_action (
1127                 priv->balance_action_group, name);
1128
1129         if (action != NULL) {
1130                 guint merge_id;
1131
1132                 DEBUG ("Removing action");
1133
1134                 merge_id = GPOINTER_TO_UINT (g_object_get_data (
1135                         G_OBJECT (action),
1136                         "merge-id"));
1137
1138                 gtk_ui_manager_remove_ui (priv->ui_manager,
1139                         merge_id);
1140                 gtk_action_group_remove_action (
1141                         priv->balance_action_group, action);
1142         }
1143
1144         g_free (name);
1145
1146         a = gtk_action_group_list_actions (
1147                 priv->balance_action_group);
1148
1149         gtk_action_set_visible (
1150                 priv->view_balance_show_in_roster,
1151                 g_list_length (a) > 0);
1152
1153         g_list_free (a);
1154 }
1155
1156 static void
1157 main_window_connection_changed_cb (TpAccount  *account,
1158                                    guint       old_status,
1159                                    guint       current,
1160                                    guint       reason,
1161                                    gchar      *dbus_error_name,
1162                                    GHashTable *details,
1163                                    EmpathyMainWindow *window)
1164 {
1165         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1166
1167         main_window_update_status (window);
1168
1169         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
1170             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
1171                 main_window_error_display (window, account);
1172         }
1173
1174         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
1175                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
1176                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
1177
1178                 /* remove balance action if required */
1179                 main_window_remove_balance_action (window, account);
1180         }
1181
1182         if (current == TP_CONNECTION_STATUS_CONNECTED) {
1183                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
1184                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
1185
1186                 /* Account connected without error, remove error message if any */
1187                 main_window_remove_error (window, account);
1188                 main_window_setup_balance (window, account);
1189         }
1190 }
1191
1192 static void
1193 main_window_accels_load (void)
1194 {
1195         gchar *filename;
1196
1197         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
1198         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1199                 DEBUG ("Loading from:'%s'", filename);
1200                 gtk_accel_map_load (filename);
1201         }
1202
1203         g_free (filename);
1204 }
1205
1206 static void
1207 main_window_accels_save (void)
1208 {
1209         gchar *dir;
1210         gchar *file_with_path;
1211
1212         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
1213         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1214         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1215         g_free (dir);
1216
1217         DEBUG ("Saving to:'%s'", file_with_path);
1218         gtk_accel_map_save (file_with_path);
1219
1220         g_free (file_with_path);
1221 }
1222
1223 static void
1224 empathy_main_window_finalize (GObject *window)
1225 {
1226         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1227         GHashTableIter iter;
1228         gpointer key, value;
1229
1230         /* Save user-defined accelerators. */
1231         main_window_accels_save ();
1232
1233         g_list_free (priv->actions_connected);
1234
1235         g_object_unref (priv->account_manager);
1236         g_object_unref (priv->individual_store);
1237         g_object_unref (priv->contact_manager);
1238         g_object_unref (priv->sound_mgr);
1239         g_hash_table_destroy (priv->errors);
1240         g_hash_table_destroy (priv->auths);
1241
1242         /* disconnect all handlers of status-changed signal */
1243         g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1244         while (g_hash_table_iter_next (&iter, &key, &value))
1245                 g_signal_handler_disconnect (TP_ACCOUNT (key),
1246                                              GPOINTER_TO_UINT (value));
1247
1248         g_hash_table_destroy (priv->status_changed_handlers);
1249
1250         g_signal_handlers_disconnect_by_func (priv->event_manager,
1251                                               main_window_event_added_cb,
1252                                               window);
1253         g_signal_handlers_disconnect_by_func (priv->event_manager,
1254                                               main_window_event_removed_cb,
1255                                               window);
1256         g_object_unref (priv->call_observer);
1257         g_object_unref (priv->event_manager);
1258         g_object_unref (priv->ui_manager);
1259         g_object_unref (priv->chatroom_manager);
1260
1261         g_object_unref (priv->gsettings_ui);
1262         g_object_unref (priv->gsettings_contacts);
1263
1264         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
1265 }
1266
1267 static gboolean
1268 main_window_key_press_event_cb  (GtkWidget   *window,
1269                                  GdkEventKey *event,
1270                                  gpointer     user_data)
1271 {
1272         if (event->keyval == GDK_KEY_T
1273             && event->state & GDK_SHIFT_MASK
1274             && event->state & GDK_CONTROL_MASK) {
1275                 empathy_chat_manager_call_undo_closed_chat ();
1276         }
1277         return FALSE;
1278 }
1279
1280 static void
1281 main_window_chat_quit_cb (GtkAction         *action,
1282                           EmpathyMainWindow *window)
1283 {
1284         gtk_main_quit ();
1285 }
1286
1287 static void
1288 main_window_view_history_cb (GtkAction         *action,
1289                              EmpathyMainWindow *window)
1290 {
1291         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
1292 }
1293
1294 static void
1295 main_window_chat_new_message_cb (GtkAction         *action,
1296                                  EmpathyMainWindow *window)
1297 {
1298         empathy_new_message_dialog_show (GTK_WINDOW (window));
1299 }
1300
1301 static void
1302 main_window_chat_new_call_cb (GtkAction         *action,
1303                               EmpathyMainWindow *window)
1304 {
1305         empathy_new_call_dialog_show (GTK_WINDOW (window));
1306 }
1307
1308 static void
1309 main_window_chat_add_contact_cb (GtkAction         *action,
1310                                  EmpathyMainWindow *window)
1311 {
1312         empathy_new_individual_dialog_show (GTK_WINDOW (window));
1313 }
1314
1315 static void
1316 main_window_chat_search_contacts_cb (GtkAction         *action,
1317                                      EmpathyMainWindow *window)
1318 {
1319         GtkWidget *dialog = empathy_contact_search_dialog_new (
1320                         GTK_WINDOW (window));
1321         gtk_widget_show (dialog);
1322 }
1323
1324 static void
1325 main_window_view_show_ft_manager (GtkAction         *action,
1326                                   EmpathyMainWindow *window)
1327 {
1328         empathy_ft_manager_show ();
1329 }
1330
1331 static void
1332 main_window_view_show_offline_cb (GtkToggleAction   *action,
1333                                   EmpathyMainWindow *window)
1334 {
1335         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1336         gboolean current;
1337
1338         current = gtk_toggle_action_get_active (action);
1339         g_settings_set_boolean (priv->gsettings_ui,
1340                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
1341                                 current);
1342
1343         empathy_individual_view_set_show_offline (priv->individual_view,
1344                         current);
1345 }
1346
1347 static void
1348 main_window_notify_sort_contact_cb (GSettings         *gsettings,
1349                                     const gchar       *key,
1350                                     EmpathyMainWindow *window)
1351 {
1352         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1353         gchar *str;
1354
1355         str = g_settings_get_string (gsettings, key);
1356
1357         if (str != NULL) {
1358                 GType       type;
1359                 GEnumClass *enum_class;
1360                 GEnumValue *enum_value;
1361
1362                 type = empathy_individual_store_sort_get_type ();
1363                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1364                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1365                 if (enum_value) {
1366                         /* By changing the value of the GtkRadioAction,
1367                            it emits a signal that calls main_window_view_sort_contacts_cb
1368                            which updates the contacts list */
1369                         gtk_radio_action_set_current_value (priv->sort_by_name,
1370                                                             enum_value->value);
1371                 } else {
1372                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
1373                 }
1374                 g_free (str);
1375         }
1376 }
1377
1378 static void
1379 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
1380                                    GtkRadioAction    *current,
1381                                    EmpathyMainWindow *window)
1382 {
1383         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1384         EmpathyContactListStoreSort value;
1385         GSList      *group;
1386         GType        type;
1387         GEnumClass  *enum_class;
1388         GEnumValue  *enum_value;
1389
1390         value = gtk_radio_action_get_current_value (action);
1391         group = gtk_radio_action_get_group (action);
1392
1393         /* Get string from index */
1394         type = empathy_individual_store_sort_get_type ();
1395         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1396         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
1397
1398         if (!enum_value) {
1399                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
1400                            g_slist_index (group, action));
1401         } else {
1402                 g_settings_set_string (priv->gsettings_contacts,
1403                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1404                                        enum_value->value_nick);
1405         }
1406         empathy_individual_store_set_sort_criterium (priv->individual_store,
1407                         value);
1408 }
1409
1410 static void
1411 main_window_view_show_protocols_cb (GtkToggleAction   *action,
1412                                     EmpathyMainWindow *window)
1413 {
1414         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1415         gboolean value;
1416
1417         value = gtk_toggle_action_get_active (action);
1418
1419         g_settings_set_boolean (priv->gsettings_ui,
1420                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1421                                 value);
1422         empathy_individual_store_set_show_protocols (priv->individual_store,
1423                                                      value);
1424 }
1425
1426 /* Matches GtkRadioAction values set in empathy-main-window.ui */
1427 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
1428 #define CONTACT_LIST_NORMAL_SIZE                        1
1429 #define CONTACT_LIST_COMPACT_SIZE                       2
1430
1431 static void
1432 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
1433                                         GtkRadioAction    *current,
1434                                         EmpathyMainWindow *window)
1435 {
1436         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1437         GSettings *gsettings_ui;
1438         gint value;
1439
1440         value = gtk_radio_action_get_current_value (action);
1441         /* create a new GSettings, so we can delay the setting until both
1442          * values are set */
1443         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1444
1445         DEBUG ("radio button toggled, value = %i", value);
1446
1447         g_settings_delay (gsettings_ui);
1448         g_settings_set_boolean (gsettings_ui,
1449                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
1450                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1451
1452         g_settings_set_boolean (gsettings_ui,
1453                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1454                                 value == CONTACT_LIST_COMPACT_SIZE);
1455         g_settings_apply (gsettings_ui);
1456
1457         /* FIXME: these enums probably have the wrong namespace */
1458         empathy_individual_store_set_show_avatars (priv->individual_store,
1459                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1460         empathy_individual_store_set_is_compact (priv->individual_store,
1461                         value == CONTACT_LIST_COMPACT_SIZE);
1462
1463         g_object_unref (gsettings_ui);
1464 }
1465
1466 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
1467                                                   const gchar       *key,
1468                                                   EmpathyMainWindow *window)
1469 {
1470         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1471
1472         gtk_toggle_action_set_active (priv->show_protocols,
1473                         g_settings_get_boolean (gsettings,
1474                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
1475 }
1476
1477
1478 static void
1479 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
1480                                          const gchar       *key,
1481                                          EmpathyMainWindow *window)
1482 {
1483         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1484         gint value;
1485
1486         if (g_settings_get_boolean (gsettings,
1487                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
1488                 value = CONTACT_LIST_COMPACT_SIZE;
1489         } else if (g_settings_get_boolean (gsettings,
1490                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
1491                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
1492         } else {
1493                 value = CONTACT_LIST_NORMAL_SIZE;
1494         }
1495
1496         DEBUG ("setting changed, value = %i", value);
1497
1498         /* By changing the value of the GtkRadioAction,
1499            it emits a signal that calls main_window_view_contacts_list_size_cb
1500            which updates the contacts list */
1501         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
1502 }
1503
1504 static void
1505 main_window_edit_search_contacts_cb (GtkCheckMenuItem  *item,
1506                                      EmpathyMainWindow *window)
1507 {
1508         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1509
1510         empathy_individual_view_start_search (priv->individual_view);
1511 }
1512
1513 static void
1514 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
1515                               EmpathyMainWindow *window)
1516 {
1517 #ifdef HAVE_LIBCHAMPLAIN
1518         empathy_map_view_show ();
1519 #endif
1520 }
1521
1522 static void
1523 join_chatroom (EmpathyChatroom *chatroom,
1524                gint64 timestamp)
1525 {
1526         TpAccount      *account;
1527         const gchar    *room;
1528
1529         account = empathy_chatroom_get_account (chatroom);
1530         room = empathy_chatroom_get_room (chatroom);
1531
1532         DEBUG ("Requesting channel for '%s'", room);
1533         empathy_join_muc (account, room, timestamp);
1534 }
1535
1536 typedef struct
1537 {
1538         TpAccount *account;
1539         EmpathyChatroom *chatroom;
1540         gint64 timestamp;
1541         glong sig_id;
1542         guint timeout;
1543 } join_fav_account_sig_ctx;
1544
1545 static join_fav_account_sig_ctx *
1546 join_fav_account_sig_ctx_new (TpAccount *account,
1547                              EmpathyChatroom *chatroom,
1548                               gint64 timestamp)
1549 {
1550         join_fav_account_sig_ctx *ctx = g_slice_new0 (
1551                 join_fav_account_sig_ctx);
1552
1553         ctx->account = g_object_ref (account);
1554         ctx->chatroom = g_object_ref (chatroom);
1555         ctx->timestamp = timestamp;
1556         return ctx;
1557 }
1558
1559 static void
1560 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1561 {
1562         g_object_unref (ctx->account);
1563         g_object_unref (ctx->chatroom);
1564         g_slice_free (join_fav_account_sig_ctx, ctx);
1565 }
1566
1567 static void
1568 account_status_changed_cb (TpAccount  *account,
1569                            TpConnectionStatus old_status,
1570                            TpConnectionStatus new_status,
1571                            guint reason,
1572                            gchar *dbus_error_name,
1573                            GHashTable *details,
1574                            gpointer user_data)
1575 {
1576         join_fav_account_sig_ctx *ctx = user_data;
1577
1578         switch (new_status) {
1579                 case TP_CONNECTION_STATUS_DISCONNECTED:
1580                         /* Don't wait any longer */
1581                         goto finally;
1582                         break;
1583
1584                 case TP_CONNECTION_STATUS_CONNECTING:
1585                         /* Wait a bit */
1586                         return;
1587
1588                 case TP_CONNECTION_STATUS_CONNECTED:
1589                         /* We can join the room */
1590                         break;
1591
1592                 default:
1593                         g_assert_not_reached ();
1594         }
1595
1596         join_chatroom (ctx->chatroom, ctx->timestamp);
1597
1598 finally:
1599         g_source_remove (ctx->timeout);
1600         g_signal_handler_disconnect (account, ctx->sig_id);
1601 }
1602
1603 #define JOIN_FAVORITE_TIMEOUT 5
1604
1605 static gboolean
1606 join_favorite_timeout_cb (gpointer data)
1607 {
1608         join_fav_account_sig_ctx *ctx = data;
1609
1610         /* stop waiting for joining the favorite room */
1611         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1612         return FALSE;
1613 }
1614
1615 static void
1616 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1617 {
1618         TpAccount      *account;
1619
1620         account = empathy_chatroom_get_account (chatroom);
1621         if (tp_account_get_connection_status (account, NULL) !=
1622                                              TP_CONNECTION_STATUS_CONNECTED) {
1623                 join_fav_account_sig_ctx *ctx;
1624
1625                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1626                         empathy_get_current_action_time ());
1627
1628                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1629                         G_CALLBACK (account_status_changed_cb), ctx,
1630                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1631
1632                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1633                         join_favorite_timeout_cb, ctx);
1634                 return;
1635         }
1636
1637         join_chatroom (chatroom, empathy_get_current_action_time ());
1638 }
1639
1640 static void
1641 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1642                                                 EmpathyChatroom *chatroom)
1643 {
1644         main_window_favorite_chatroom_join (chatroom);
1645 }
1646
1647 static void
1648 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1649                                         EmpathyChatroom   *chatroom)
1650 {
1651         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1652         GtkWidget   *menu_item;
1653         const gchar *name, *account_name;
1654         gchar *label;
1655
1656
1657         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1658                 return;
1659         }
1660
1661         name = empathy_chatroom_get_name (chatroom);
1662         account_name = tp_account_get_display_name (
1663                         empathy_chatroom_get_account (chatroom));
1664         label = g_strdup_printf ("%s (%s)", name, account_name);
1665         menu_item = gtk_menu_item_new_with_label (label);
1666         g_free (label);
1667         g_object_set_data (G_OBJECT (menu_item), "is_favorite",
1668                         GUINT_TO_POINTER (TRUE));
1669
1670         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1671         g_signal_connect (menu_item, "activate",
1672                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1673                           chatroom);
1674
1675         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1676                                menu_item, 4);
1677
1678         gtk_widget_show (menu_item);
1679 }
1680
1681 static void
1682 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1683                                              EmpathyChatroom        *chatroom,
1684                                              EmpathyMainWindow      *window)
1685 {
1686         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1687
1688         main_window_favorite_chatroom_menu_add (window, chatroom);
1689         gtk_widget_show (priv->room_separator);
1690         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1691 }
1692
1693 static void
1694 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1695                                                EmpathyChatroom        *chatroom,
1696                                                EmpathyMainWindow      *window)
1697 {
1698         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1699         GtkWidget *menu_item;
1700         GList *chatrooms;
1701
1702         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1703         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1704         gtk_widget_destroy (menu_item);
1705
1706         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1707         if (chatrooms) {
1708                 gtk_widget_show (priv->room_separator);
1709         } else {
1710                 gtk_widget_hide (priv->room_separator);
1711         }
1712
1713         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1714         g_list_free (chatrooms);
1715 }
1716
1717 static void
1718 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1719 {
1720         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1721         GList *chatrooms, *l;
1722         GtkWidget *room;
1723
1724         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1725         chatrooms = empathy_chatroom_manager_get_chatrooms (
1726                 priv->chatroom_manager, NULL);
1727         room = gtk_ui_manager_get_widget (priv->ui_manager,
1728                 "/menubar/room");
1729         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1730         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1731                 "/menubar/room/room_separator");
1732
1733         for (l = chatrooms; l; l = l->next) {
1734                 main_window_favorite_chatroom_menu_add (window, l->data);
1735         }
1736
1737         if (!chatrooms) {
1738                 gtk_widget_hide (priv->room_separator);
1739         }
1740
1741         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1742
1743         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1744                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1745                           window);
1746         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1747                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1748                           window);
1749
1750         g_list_free (chatrooms);
1751 }
1752
1753 static void
1754 main_window_room_join_new_cb (GtkAction         *action,
1755                               EmpathyMainWindow *window)
1756 {
1757         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1758 }
1759
1760 static void
1761 main_window_room_join_favorites_cb (GtkAction         *action,
1762                                     EmpathyMainWindow *window)
1763 {
1764         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1765         GList *chatrooms, *l;
1766
1767         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1768         for (l = chatrooms; l; l = l->next) {
1769                 main_window_favorite_chatroom_join (l->data);
1770         }
1771         g_list_free (chatrooms);
1772 }
1773
1774 static void
1775 main_window_room_manage_favorites_cb (GtkAction         *action,
1776                                       EmpathyMainWindow *window)
1777 {
1778         empathy_chatrooms_window_show (GTK_WINDOW (window));
1779 }
1780
1781 static void
1782 main_window_edit_cb (GtkAction         *action,
1783                      EmpathyMainWindow *window)
1784 {
1785         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1786         GtkWidget *submenu;
1787
1788         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1789         submenu = empathy_individual_view_get_individual_menu (
1790                         priv->individual_view);
1791         if (submenu) {
1792                 GtkMenuItem *item;
1793                 GtkWidget   *label;
1794
1795                 item = GTK_MENU_ITEM (priv->edit_context);
1796                 label = gtk_bin_get_child (GTK_BIN (item));
1797                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1798
1799                 gtk_widget_show (priv->edit_context);
1800                 gtk_widget_show (priv->edit_context_separator);
1801
1802                 gtk_menu_item_set_submenu (item, submenu);
1803
1804                 return;
1805         }
1806
1807         submenu = empathy_individual_view_get_group_menu (
1808                         priv->individual_view);
1809         if (submenu) {
1810                 GtkMenuItem *item;
1811                 GtkWidget   *label;
1812
1813                 item = GTK_MENU_ITEM (priv->edit_context);
1814                 label = gtk_bin_get_child (GTK_BIN (item));
1815                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1816
1817                 gtk_widget_show (priv->edit_context);
1818                 gtk_widget_show (priv->edit_context_separator);
1819
1820                 gtk_menu_item_set_submenu (item, submenu);
1821
1822                 return;
1823         }
1824
1825         gtk_widget_hide (priv->edit_context);
1826         gtk_widget_hide (priv->edit_context_separator);
1827
1828         return;
1829 }
1830
1831 static void
1832 main_window_edit_accounts_cb (GtkAction         *action,
1833                               EmpathyMainWindow *window)
1834 {
1835         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1836                         NULL, FALSE, FALSE);
1837 }
1838
1839 static void
1840 main_window_edit_personal_information_cb (GtkAction         *action,
1841                                           EmpathyMainWindow *window)
1842 {
1843         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1844 }
1845
1846 static void
1847 main_window_edit_blocked_contacts_cb (GtkAction         *action,
1848                                       EmpathyMainWindow *window)
1849 {
1850         GtkWidget *dialog;
1851
1852         dialog = empathy_contact_blocking_dialog_new (GTK_WINDOW (window));
1853         gtk_widget_show (dialog);
1854         g_signal_connect (dialog, "response",
1855                         G_CALLBACK (gtk_widget_destroy), NULL);
1856 }
1857
1858 void
1859 empathy_main_window_show_preferences (EmpathyMainWindow *window,
1860     const gchar *tab)
1861 {
1862         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1863
1864         if (priv->preferences == NULL) {
1865                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window));
1866                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
1867                                            (gpointer) &priv->preferences);
1868
1869                 gtk_widget_show (priv->preferences);
1870         } else {
1871                 gtk_window_present (GTK_WINDOW (priv->preferences));
1872         }
1873
1874         if (tab != NULL)
1875                 empathy_preferences_show_tab (
1876                         EMPATHY_PREFERENCES (priv->preferences), tab);
1877 }
1878
1879 static void
1880 main_window_edit_preferences_cb (GtkAction         *action,
1881                                  EmpathyMainWindow *window)
1882 {
1883         empathy_main_window_show_preferences (window, NULL);
1884 }
1885
1886 static void
1887 main_window_help_about_cb (GtkAction         *action,
1888                            EmpathyMainWindow *window)
1889 {
1890         empathy_about_dialog_new (GTK_WINDOW (window));
1891 }
1892
1893 static void
1894 main_window_help_debug_cb (GtkAction         *action,
1895                            EmpathyMainWindow *window)
1896 {
1897         empathy_launch_program (BIN_DIR, "empathy-debugger", NULL);
1898 }
1899
1900 static void
1901 main_window_help_contents_cb (GtkAction         *action,
1902                               EmpathyMainWindow *window)
1903 {
1904         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
1905 }
1906
1907 static gboolean
1908 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
1909                                             GdkEventButton    *event,
1910                                             EmpathyMainWindow *window)
1911 {
1912         if (event->type != GDK_BUTTON_PRESS ||
1913             event->button != 1) {
1914                 return FALSE;
1915         }
1916
1917         empathy_accounts_dialog_show_application (
1918                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
1919                         NULL, FALSE, FALSE);
1920
1921         return FALSE;
1922 }
1923
1924 static void
1925 main_window_account_removed_cb (TpAccountManager  *manager,
1926                                 TpAccount         *account,
1927                                 EmpathyMainWindow *window)
1928 {
1929         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1930         GList *a;
1931
1932         a = tp_account_manager_get_valid_accounts (manager);
1933
1934         gtk_action_set_sensitive (priv->view_history,
1935                 g_list_length (a) > 0);
1936
1937         g_list_free (a);
1938
1939         /* remove errors if any */
1940         main_window_remove_error (window, account);
1941
1942         /* remove the balance action if required */
1943         main_window_remove_balance_action (window, account);
1944 }
1945
1946 static void
1947 main_window_account_validity_changed_cb (TpAccountManager  *manager,
1948                                          TpAccount         *account,
1949                                          gboolean           valid,
1950                                          EmpathyMainWindow *window)
1951 {
1952         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1953
1954         if (valid) {
1955                 gulong handler_id;
1956                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1957                         priv->status_changed_handlers, account));
1958
1959                 /* connect signal only if it was not connected yet */
1960                 if (handler_id == 0) {
1961                         handler_id = g_signal_connect (account,
1962                                 "status-changed",
1963                                 G_CALLBACK (main_window_connection_changed_cb),
1964                                 window);
1965                         g_hash_table_insert (priv->status_changed_handlers,
1966                                 account, GUINT_TO_POINTER (handler_id));
1967                 }
1968         }
1969
1970         main_window_account_removed_cb (manager, account, window);
1971 }
1972
1973 static void
1974 main_window_notify_show_offline_cb (GSettings   *gsettings,
1975                                     const gchar *key,
1976                                     gpointer     toggle_action)
1977 {
1978         gtk_toggle_action_set_active (toggle_action,
1979                         g_settings_get_boolean (gsettings, key));
1980 }
1981
1982 static void
1983 main_window_connection_items_setup (EmpathyMainWindow *window,
1984                                     GtkBuilder        *gui)
1985 {
1986         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1987         GList         *list;
1988         GObject       *action;
1989         guint          i;
1990         const gchar *actions_connected[] = {
1991                 "room_join_new",
1992                 "room_join_favorites",
1993                 "chat_new_message",
1994                 "chat_new_call",
1995                 "chat_add_contact",
1996                 "edit_personal_information",
1997                 "edit_blocked_contacts"
1998         };
1999
2000         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
2001                 action = gtk_builder_get_object (gui, actions_connected[i]);
2002                 list = g_list_prepend (list, action);
2003         }
2004
2005         priv->actions_connected = list;
2006 }
2007
2008 static void
2009 account_manager_prepared_cb (GObject      *source_object,
2010                              GAsyncResult *result,
2011                              gpointer      user_data)
2012 {
2013         GList *accounts, *j;
2014         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2015         EmpathyMainWindow *window = user_data;
2016         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2017         GError *error = NULL;
2018
2019         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
2020                 DEBUG ("Failed to prepare account manager: %s", error->message);
2021                 g_error_free (error);
2022                 return;
2023         }
2024
2025         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
2026         for (j = accounts; j != NULL; j = j->next) {
2027                 TpAccount *account = TP_ACCOUNT (j->data);
2028                 gulong handler_id;
2029
2030                 handler_id = g_signal_connect (account, "status-changed",
2031                                   G_CALLBACK (main_window_connection_changed_cb),
2032                                   window);
2033                 g_hash_table_insert (priv->status_changed_handlers,
2034                                      account, GUINT_TO_POINTER (handler_id));
2035
2036                 main_window_setup_balance (window, account);
2037         }
2038
2039         g_signal_connect (manager, "account-validity-changed",
2040                           G_CALLBACK (main_window_account_validity_changed_cb),
2041                           window);
2042
2043         main_window_update_status (window);
2044
2045         /* Disable the "Previous Conversations" menu entry if there is no account */
2046         gtk_action_set_sensitive (priv->view_history,
2047                 g_list_length (accounts) > 0);
2048
2049         g_list_free (accounts);
2050 }
2051
2052 static void
2053 main_window_members_changed_cb (EmpathyContactList *list,
2054                                 EmpathyContact     *contact,
2055                                 EmpathyContact     *actor,
2056                                 guint               reason,
2057                                 gchar              *message,
2058                                 gboolean            is_member,
2059                                 EmpathyMainWindow  *window)
2060 {
2061         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2062
2063         if (!is_member)
2064                 return;
2065
2066         if (!empathy_migrate_butterfly_logs (contact)) {
2067                 g_signal_handler_disconnect (list,
2068                         priv->butterfly_log_migration_members_changed_id);
2069                 priv->butterfly_log_migration_members_changed_id = 0;
2070         }
2071 }
2072
2073 static GObject *
2074 empathy_main_window_constructor (GType type,
2075                                  guint n_construct_params,
2076                                  GObjectConstructParam *construct_params)
2077 {
2078         static GObject *window = NULL;
2079
2080         if (window != NULL)
2081                 return g_object_ref (window);
2082
2083         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
2084                 type, n_construct_params, construct_params);
2085
2086         g_object_add_weak_pointer (window, (gpointer) &window);
2087
2088         return window;
2089 }
2090
2091 static void
2092 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
2093 {
2094         GObjectClass *object_class = G_OBJECT_CLASS (klass);
2095
2096         object_class->finalize = empathy_main_window_finalize;
2097         object_class->constructor = empathy_main_window_constructor;
2098
2099         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
2100 }
2101
2102 static void
2103 empathy_main_window_init (EmpathyMainWindow *window)
2104 {
2105         EmpathyMainWindowPriv    *priv;
2106         EmpathyIndividualManager *individual_manager;
2107         GtkBuilder               *gui;
2108         GtkWidget                *sw;
2109         GtkToggleAction          *show_offline_widget;
2110         GtkAction                *show_map_widget;
2111         GtkToolItem              *item;
2112         gboolean                  show_offline;
2113         gchar                    *filename;
2114         GSList                   *l;
2115         GtkTreeModel             *model;
2116
2117         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
2118                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
2119
2120         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2121         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
2122
2123         priv->sound_mgr = empathy_sound_manager_dup_singleton ();
2124
2125         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
2126         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
2127         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
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                                        "ui_manager", &priv->ui_manager,
2137                                        "view_show_offline", &show_offline_widget,
2138                                        "view_show_protocols", &priv->show_protocols,
2139                                        "view_sort_by_name", &priv->sort_by_name,
2140                                        "view_sort_by_status", &priv->sort_by_status,
2141                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
2142                                        "view_normal_size", &priv->normal_size,
2143                                        "view_compact_size", &priv->compact_size,
2144                                        "view_history", &priv->view_history,
2145                                        "view_show_map", &show_map_widget,
2146                                        "room_join_favorites", &priv->room_join_favorites,
2147                                        "presence_toolbar", &priv->presence_toolbar,
2148                                        "notebook", &priv->notebook,
2149                                        "no_entry_label", &priv->no_entry_label,
2150                                        "roster_scrolledwindow", &sw,
2151                                        "view_balance_show_in_roster", &priv->view_balance_show_in_roster,
2152                                        NULL);
2153         g_free (filename);
2154
2155         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
2156         gtk_widget_show (priv->main_vbox);
2157
2158         g_signal_connect (window, "key-press-event",
2159                           G_CALLBACK (main_window_key_press_event_cb), NULL);
2160
2161         empathy_builder_connect (gui, window,
2162                               "chat_quit", "activate", main_window_chat_quit_cb,
2163                               "chat_new_message", "activate", main_window_chat_new_message_cb,
2164                               "chat_new_call", "activate", main_window_chat_new_call_cb,
2165                               "view_history", "activate", main_window_view_history_cb,
2166                               "room_join_new", "activate", main_window_room_join_new_cb,
2167                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
2168                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
2169                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
2170                               "chat_search_contacts", "activate", main_window_chat_search_contacts_cb,
2171                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
2172                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
2173                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
2174                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
2175                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
2176                               "view_show_map", "activate", main_window_view_show_map_cb,
2177                               "edit", "activate", main_window_edit_cb,
2178                               "edit_accounts", "activate", main_window_edit_accounts_cb,
2179                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
2180                               "edit_blocked_contacts", "activate", main_window_edit_blocked_contacts_cb,
2181                               "edit_preferences", "activate", main_window_edit_preferences_cb,
2182                               "edit_search_contacts", "activate", main_window_edit_search_contacts_cb,
2183                               "help_about", "activate", main_window_help_about_cb,
2184                               "help_debug", "activate", main_window_help_debug_cb,
2185                               "help_contents", "activate", main_window_help_contents_cb,
2186                               NULL);
2187
2188         /* Set up connection related widgets. */
2189         main_window_connection_items_setup (window, gui);
2190
2191         g_object_ref (priv->ui_manager);
2192         g_object_unref (gui);
2193
2194 #ifndef HAVE_LIBCHAMPLAIN
2195         gtk_action_set_visible (show_map_widget, FALSE);
2196 #endif
2197
2198         priv->account_manager = tp_account_manager_dup ();
2199
2200         tp_account_manager_prepare_async (priv->account_manager, NULL,
2201                                           account_manager_prepared_cb, window);
2202
2203         priv->errors = g_hash_table_new_full (g_direct_hash,
2204                                               g_direct_equal,
2205                                               g_object_unref,
2206                                               NULL);
2207
2208         priv->auths = g_hash_table_new (NULL, NULL);
2209
2210         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
2211                                                                g_direct_equal,
2212                                                                NULL,
2213                                                                NULL);
2214
2215         /* Set up menu */
2216         main_window_favorite_chatroom_menu_setup (window);
2217
2218         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
2219                 "/menubar/edit/edit_context");
2220         priv->edit_context_separator = gtk_ui_manager_get_widget (
2221                 priv->ui_manager,
2222                 "/menubar/edit/edit_context_separator");
2223         gtk_widget_hide (priv->edit_context);
2224         gtk_widget_hide (priv->edit_context_separator);
2225
2226         /* Set up contact list. */
2227         empathy_status_presets_get_all ();
2228
2229         /* Set up presence chooser */
2230         priv->presence_chooser = empathy_presence_chooser_new ();
2231         gtk_widget_show (priv->presence_chooser);
2232         item = gtk_tool_item_new ();
2233         gtk_widget_show (GTK_WIDGET (item));
2234         gtk_widget_set_size_request (priv->presence_chooser, 10, -1);
2235         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
2236         gtk_tool_item_set_is_important (item, TRUE);
2237         gtk_tool_item_set_expand (item, TRUE);
2238         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
2239
2240         /* Set up the throbber */
2241         priv->throbber = gtk_spinner_new ();
2242         gtk_widget_set_size_request (priv->throbber, 16, -1);
2243         gtk_widget_set_tooltip_text (priv->throbber, _("Show and edit accounts"));
2244         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
2245         g_signal_connect (priv->throbber, "button-press-event",
2246                 G_CALLBACK (main_window_throbber_button_press_event_cb),
2247                 window);
2248         gtk_widget_show (priv->throbber);
2249
2250         item = gtk_tool_item_new ();
2251         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
2252         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
2253         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
2254         priv->throbber_tool_item = GTK_WIDGET (item);
2255
2256         /* XXX: this class is designed to live for the duration of the program,
2257          * so it's got a race condition between its signal handlers and its
2258          * finalization. The class is planned to be removed, so we won't fix
2259          * this before then. */
2260         priv->contact_manager = EMPATHY_CONTACT_LIST (
2261                         empathy_contact_manager_dup_singleton ());
2262         individual_manager = empathy_individual_manager_dup_singleton ();
2263         priv->individual_store = empathy_individual_store_new (
2264                         individual_manager);
2265         g_object_unref (individual_manager);
2266
2267         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
2268          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
2269          * drop, so allowing them would achieve nothing except confusion. */
2270         priv->individual_view = empathy_individual_view_new (
2271                         priv->individual_store,
2272                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
2273                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
2274
2275         priv->butterfly_log_migration_members_changed_id = g_signal_connect (
2276                         priv->contact_manager, "members-changed",
2277                         G_CALLBACK (main_window_members_changed_cb), window);
2278
2279         gtk_widget_show (GTK_WIDGET (priv->individual_view));
2280         gtk_container_add (GTK_CONTAINER (sw),
2281                            GTK_WIDGET (priv->individual_view));
2282         g_signal_connect (priv->individual_view, "row-activated",
2283                           G_CALLBACK (main_window_row_activated_cb),
2284                           window);
2285
2286         /* Set up search bar */
2287         priv->search_bar = empathy_live_search_new (
2288                 GTK_WIDGET (priv->individual_view));
2289         empathy_individual_view_set_live_search (priv->individual_view,
2290                 EMPATHY_LIVE_SEARCH (priv->search_bar));
2291         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->search_bar,
2292                 FALSE, TRUE, 0);
2293         g_signal_connect_swapped (window, "map",
2294                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
2295
2296         /* Connect to proper signals to check if contact list is empty or not */
2297         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
2298         priv->empty = TRUE;
2299         g_signal_connect (model, "row-inserted",
2300                           G_CALLBACK (main_window_row_inserted_cb),
2301                           window);
2302         g_signal_connect (model, "row-deleted",
2303                           G_CALLBACK (main_window_row_deleted_cb),
2304                           window);
2305
2306         /* Load user-defined accelerators. */
2307         main_window_accels_load ();
2308
2309         /* Set window size. */
2310         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
2311
2312         /* bind view_balance_show_in_roster */
2313         g_settings_bind (priv->gsettings_ui, "show-balance-in-roster",
2314                 priv->view_balance_show_in_roster, "active",
2315                 G_SETTINGS_BIND_DEFAULT);
2316         g_object_bind_property (priv->view_balance_show_in_roster, "active",
2317                 priv->balance_vbox, "visible",
2318                 G_BINDING_SYNC_CREATE);
2319
2320         /* Enable event handling */
2321         priv->call_observer = empathy_call_observer_dup_singleton ();
2322         priv->event_manager = empathy_event_manager_dup_singleton ();
2323
2324         g_signal_connect (priv->event_manager, "event-added",
2325                           G_CALLBACK (main_window_event_added_cb), window);
2326         g_signal_connect (priv->event_manager, "event-removed",
2327                           G_CALLBACK (main_window_event_removed_cb), window);
2328         g_signal_connect (priv->account_manager, "account-validity-changed",
2329                           G_CALLBACK (main_window_account_validity_changed_cb),
2330                           window);
2331         g_signal_connect (priv->account_manager, "account-removed",
2332                           G_CALLBACK (main_window_account_removed_cb),
2333                           window);
2334         g_signal_connect (priv->account_manager, "account-disabled",
2335                           G_CALLBACK (main_window_account_disabled_cb),
2336                           window);
2337
2338         l = empathy_event_manager_get_events (priv->event_manager);
2339         while (l) {
2340                 main_window_event_added_cb (priv->event_manager, l->data,
2341                                 window);
2342                 l = l->next;
2343         }
2344
2345         /* Show offline ? */
2346         show_offline = g_settings_get_boolean (priv->gsettings_ui,
2347                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
2348         g_signal_connect (priv->gsettings_ui,
2349                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
2350                           G_CALLBACK (main_window_notify_show_offline_cb),
2351                           show_offline_widget);
2352
2353         gtk_toggle_action_set_active (show_offline_widget, show_offline);
2354
2355         /* Show protocol ? */
2356         g_signal_connect (priv->gsettings_ui,
2357                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2358                           G_CALLBACK (main_window_notify_show_protocols_cb),
2359                           window);
2360
2361         main_window_notify_show_protocols_cb (priv->gsettings_ui,
2362                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2363                                               window);
2364
2365         /* Sort by name / by status ? */
2366         g_signal_connect (priv->gsettings_contacts,
2367                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2368                           G_CALLBACK (main_window_notify_sort_contact_cb),
2369                           window);
2370
2371         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
2372                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2373                                             window);
2374
2375         /* Contacts list size */
2376         g_signal_connect (priv->gsettings_ui,
2377                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
2378                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2379                           window);
2380         g_signal_connect (priv->gsettings_ui,
2381                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
2382                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2383                           window);
2384
2385         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
2386                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
2387                                                  window);
2388 }
2389
2390 GtkWidget *
2391 empathy_main_window_dup (void)
2392 {
2393         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
2394 }