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