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