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