]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
only show label during search
[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
479         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
480                                                tp_account_get_display_name (account),
481                                                empathy_account_get_error_message (account));
482
483         info_bar = g_hash_table_lookup (priv->errors, account);
484         if (info_bar) {
485                 label = g_object_get_data (G_OBJECT (info_bar), "label");
486
487                 /* Just set the latest error and return */
488                 gtk_label_set_markup (GTK_LABEL (label), str);
489                 g_free (str);
490
491                 return;
492         }
493
494         info_bar = gtk_info_bar_new ();
495         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
496
497         gtk_widget_set_no_show_all (info_bar, TRUE);
498         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
499         gtk_widget_show (info_bar);
500
501         icon_name = tp_account_get_icon_name (account);
502         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
503         gtk_widget_show (image);
504
505         label = gtk_label_new (str);
506         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
507         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
508         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
509         gtk_widget_show (label);
510         g_free (str);
511
512         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
513         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
514         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
515
516         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
517         retry_button = gtk_button_new ();
518         gtk_button_set_image (GTK_BUTTON (retry_button), image);
519         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
520         gtk_widget_show (retry_button);
521
522         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
523         edit_button = gtk_button_new ();
524         gtk_button_set_image (GTK_BUTTON (edit_button), image);
525         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
526         gtk_widget_show (edit_button);
527
528         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
529         close_button = gtk_button_new ();
530         gtk_button_set_image (GTK_BUTTON (close_button), image);
531         gtk_widget_set_tooltip_text (close_button, _("Close"));
532         gtk_widget_show (close_button);
533
534         action_table = gtk_table_new (1, 3, FALSE);
535         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
536         gtk_widget_show (action_table);
537
538         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
539         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
540
541         gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
542                                                                                 (GtkAttachOptions) (GTK_SHRINK),
543                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
544         gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
545                                                                                 (GtkAttachOptions) (GTK_SHRINK),
546                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
547         gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
548                                                                                 (GtkAttachOptions) (GTK_SHRINK),
549                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
550
551         g_object_set_data (G_OBJECT (info_bar), "label", label);
552         g_object_set_data_full (G_OBJECT (info_bar),
553                                 "account", g_object_ref (account),
554                                 g_object_unref);
555         g_object_set_data_full (G_OBJECT (edit_button),
556                                 "account", g_object_ref (account),
557                                 g_object_unref);
558         g_object_set_data_full (G_OBJECT (close_button),
559                                 "account", g_object_ref (account),
560                                 g_object_unref);
561         g_object_set_data_full (G_OBJECT (retry_button),
562                                 "account", g_object_ref (account),
563                                 g_object_unref);
564
565         g_signal_connect (edit_button, "clicked",
566                           G_CALLBACK (main_window_error_edit_clicked_cb),
567                           window);
568         g_signal_connect (close_button, "clicked",
569                           G_CALLBACK (main_window_error_close_clicked_cb),
570                           window);
571         g_signal_connect (retry_button, "clicked",
572                           G_CALLBACK (main_window_error_retry_clicked_cb),
573                           window);
574
575         gtk_widget_show (priv->errors_vbox);
576
577         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
578 }
579
580 static void
581 main_window_update_status (EmpathyMainWindow *window)
582 {
583         EmpathyMainWindowPriv *priv = GET_PRIV (window);
584         gboolean connected, connecting;
585         GList *l;
586
587         connected = empathy_account_manager_get_accounts_connected (&connecting);
588
589         /* Update the spinner state */
590         if (connecting) {
591                 gtk_spinner_start (GTK_SPINNER (priv->throbber));
592                 gtk_widget_show (priv->throbber_tool_item);
593         } else {
594                 gtk_spinner_stop (GTK_SPINNER (priv->throbber));
595                 gtk_widget_hide (priv->throbber_tool_item);
596         }
597
598         /* Update widgets sensibility */
599         for (l = priv->actions_connected; l; l = l->next) {
600                 gtk_action_set_sensitive (l->data, connected);
601         }
602 }
603
604 static void
605 main_window_connection_changed_cb (TpAccount  *account,
606                                    guint       old_status,
607                                    guint       current,
608                                    guint       reason,
609                                    gchar      *dbus_error_name,
610                                    GHashTable *details,
611                                    EmpathyMainWindow *window)
612 {
613         main_window_update_status (window);
614
615         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
616             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
617                 main_window_error_display (window, account);
618         }
619
620         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
621                 empathy_sound_play (GTK_WIDGET (window),
622                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
623         }
624
625         if (current == TP_CONNECTION_STATUS_CONNECTED) {
626                 empathy_sound_play (GTK_WIDGET (window),
627                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
628
629                 /* Account connected without error, remove error message if any */
630                 main_window_remove_error (window, account);
631         }
632 }
633
634 static void
635 main_window_accels_load (void)
636 {
637         gchar *filename;
638
639         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
640         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
641                 DEBUG ("Loading from:'%s'", filename);
642                 gtk_accel_map_load (filename);
643         }
644
645         g_free (filename);
646 }
647
648 static void
649 main_window_accels_save (void)
650 {
651         gchar *dir;
652         gchar *file_with_path;
653
654         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
655         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
656         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
657         g_free (dir);
658
659         DEBUG ("Saving to:'%s'", file_with_path);
660         gtk_accel_map_save (file_with_path);
661
662         g_free (file_with_path);
663 }
664
665 static void
666 empathy_main_window_finalize (GObject *window)
667 {
668         EmpathyMainWindowPriv *priv = GET_PRIV (window);
669         GHashTableIter iter;
670         gpointer key, value;
671
672         /* Save user-defined accelerators. */
673         main_window_accels_save ();
674
675         g_list_free (priv->actions_connected);
676
677         g_object_unref (priv->account_manager);
678         g_object_unref (priv->individual_store);
679         g_hash_table_destroy (priv->errors);
680
681         /* disconnect all handlers of status-changed signal */
682         g_hash_table_iter_init (&iter, priv->status_changed_handlers);
683         while (g_hash_table_iter_next (&iter, &key, &value))
684                 g_signal_handler_disconnect (TP_ACCOUNT (key),
685                                              GPOINTER_TO_UINT (value));
686
687         g_hash_table_destroy (priv->status_changed_handlers);
688
689         g_signal_handlers_disconnect_by_func (priv->event_manager,
690                                               main_window_event_added_cb,
691                                               window);
692         g_signal_handlers_disconnect_by_func (priv->event_manager,
693                                               main_window_event_removed_cb,
694                                               window);
695         g_object_unref (priv->event_manager);
696         g_object_unref (priv->ui_manager);
697         g_object_unref (priv->chatroom_manager);
698
699         g_object_unref (priv->gsettings_ui);
700         g_object_unref (priv->gsettings_contacts);
701
702         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
703 }
704
705 static gboolean
706 main_window_key_press_event_cb  (GtkWidget   *window,
707                                  GdkEventKey *event,
708                                  gpointer     user_data)
709 {
710         EmpathyChatManager *chat_manager;
711
712         if (event->keyval == GDK_T
713             && event->state & GDK_SHIFT_MASK
714             && event->state & GDK_CONTROL_MASK) {
715                 chat_manager = empathy_chat_manager_dup_singleton ();
716                 empathy_chat_manager_undo_closed_chat (chat_manager);
717                 g_object_unref (chat_manager);
718         }
719         return FALSE;
720 }
721
722 static void
723 main_window_chat_quit_cb (GtkAction         *action,
724                           EmpathyMainWindow *window)
725 {
726         gtk_main_quit ();
727 }
728
729 static void
730 main_window_view_history_cb (GtkAction         *action,
731                              EmpathyMainWindow *window)
732 {
733         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
734 }
735
736 static void
737 main_window_chat_new_message_cb (GtkAction         *action,
738                                  EmpathyMainWindow *window)
739 {
740         empathy_new_message_dialog_show (GTK_WINDOW (window));
741 }
742
743 static void
744 main_window_chat_new_call_cb (GtkAction         *action,
745                               EmpathyMainWindow *window)
746 {
747         empathy_new_call_dialog_show (GTK_WINDOW (window));
748 }
749
750 static void
751 main_window_chat_add_contact_cb (GtkAction         *action,
752                                  EmpathyMainWindow *window)
753 {
754         empathy_new_individual_dialog_show (GTK_WINDOW (window));
755 }
756
757 static void
758 main_window_view_show_ft_manager (GtkAction         *action,
759                                   EmpathyMainWindow *window)
760 {
761         empathy_ft_manager_show ();
762 }
763
764 static void
765 main_window_view_show_offline_cb (GtkToggleAction   *action,
766                                   EmpathyMainWindow *window)
767 {
768         EmpathyMainWindowPriv *priv = GET_PRIV (window);
769         gboolean current;
770
771         current = gtk_toggle_action_get_active (action);
772         g_settings_set_boolean (priv->gsettings_ui,
773                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
774                                 current);
775
776         empathy_individual_view_set_show_offline (priv->individual_view,
777                         current);
778 }
779
780 static void
781 main_window_notify_sort_contact_cb (GSettings         *gsettings,
782                                     const gchar       *key,
783                                     EmpathyMainWindow *window)
784 {
785         EmpathyMainWindowPriv *priv = GET_PRIV (window);
786         gchar *str;
787
788         str = g_settings_get_string (gsettings, key);
789
790         if (str != NULL) {
791                 GType       type;
792                 GEnumClass *enum_class;
793                 GEnumValue *enum_value;
794
795                 type = empathy_individual_store_sort_get_type ();
796                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
797                 enum_value = g_enum_get_value_by_nick (enum_class, str);
798                 if (enum_value) {
799                         /* By changing the value of the GtkRadioAction,
800                            it emits a signal that calls main_window_view_sort_contacts_cb
801                            which updates the contacts list */
802                         gtk_radio_action_set_current_value (priv->sort_by_name,
803                                                             enum_value->value);
804                 } else {
805                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
806                 }
807                 g_free (str);
808         }
809 }
810
811 static void
812 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
813                                    GtkRadioAction    *current,
814                                    EmpathyMainWindow *window)
815 {
816         EmpathyMainWindowPriv *priv = GET_PRIV (window);
817         EmpathyContactListStoreSort value;
818         GSList      *group;
819         GType        type;
820         GEnumClass  *enum_class;
821         GEnumValue  *enum_value;
822
823         value = gtk_radio_action_get_current_value (action);
824         group = gtk_radio_action_get_group (action);
825
826         /* Get string from index */
827         type = empathy_individual_store_sort_get_type ();
828         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
829         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
830
831         if (!enum_value) {
832                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
833                            g_slist_index (group, action));
834         } else {
835                 g_settings_set_string (priv->gsettings_contacts,
836                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
837                                        enum_value->value_nick);
838         }
839         empathy_individual_store_set_sort_criterium (priv->individual_store,
840                         value);
841 }
842
843 static void
844 main_window_view_show_protocols_cb (GtkToggleAction   *action,
845                                     EmpathyMainWindow *window)
846 {
847         EmpathyMainWindowPriv *priv = GET_PRIV (window);
848         gboolean value;
849
850         value = gtk_toggle_action_get_active (action);
851
852         g_settings_set_boolean (priv->gsettings_ui,
853                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
854                                 value);
855         empathy_individual_store_set_show_protocols (priv->individual_store,
856                                                      value);
857 }
858
859 /* Matches GtkRadioAction values set in empathy-main-window.ui */
860 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
861 #define CONTACT_LIST_NORMAL_SIZE                        1
862 #define CONTACT_LIST_COMPACT_SIZE                       2
863
864 static void
865 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
866                                         GtkRadioAction    *current,
867                                         EmpathyMainWindow *window)
868 {
869         EmpathyMainWindowPriv *priv = GET_PRIV (window);
870         GSettings *gsettings_ui;
871         gint value;
872
873         value = gtk_radio_action_get_current_value (action);
874         /* create a new GSettings, so we can delay the setting until both
875          * values are set */
876         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
877
878         DEBUG ("radio button toggled, value = %i", value);
879
880         g_settings_delay (gsettings_ui);
881         g_settings_set_boolean (gsettings_ui,
882                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
883                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
884
885         g_settings_set_boolean (gsettings_ui,
886                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
887                                 value == CONTACT_LIST_COMPACT_SIZE);
888         g_settings_apply (gsettings_ui);
889
890         /* FIXME: these enums probably have the wrong namespace */
891         empathy_individual_store_set_show_avatars (priv->individual_store,
892                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
893         empathy_individual_store_set_is_compact (priv->individual_store,
894                         value == CONTACT_LIST_COMPACT_SIZE);
895
896         g_object_unref (gsettings_ui);
897 }
898
899 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
900                                                   const gchar       *key,
901                                                   EmpathyMainWindow *window)
902 {
903         EmpathyMainWindowPriv *priv = GET_PRIV (window);
904
905         gtk_toggle_action_set_active (priv->show_protocols,
906                         g_settings_get_boolean (gsettings,
907                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
908 }
909
910
911 static void
912 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
913                                          const gchar       *key,
914                                          EmpathyMainWindow *window)
915 {
916         EmpathyMainWindowPriv *priv = GET_PRIV (window);
917         gint value;
918
919         if (g_settings_get_boolean (gsettings,
920                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
921                 value = CONTACT_LIST_COMPACT_SIZE;
922         } else if (g_settings_get_boolean (gsettings,
923                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
924                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
925         } else {
926                 value = CONTACT_LIST_NORMAL_SIZE;
927         }
928
929         DEBUG ("setting changed, value = %i", value);
930
931         /* By changing the value of the GtkRadioAction,
932            it emits a signal that calls main_window_view_contacts_list_size_cb
933            which updates the contacts list */
934         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
935 }
936
937 static void
938 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
939                               EmpathyMainWindow *window)
940 {
941 #ifdef HAVE_LIBCHAMPLAIN
942         empathy_map_view_show ();
943 #endif
944 }
945
946 static void
947 join_chatroom (EmpathyChatroom *chatroom,
948                gint64 timestamp)
949 {
950         TpAccount      *account;
951         TpConnection   *connection;
952         const gchar    *room;
953
954         account = empathy_chatroom_get_account (chatroom);
955         connection = tp_account_get_connection (account);
956         g_assert (connection != NULL);
957         room = empathy_chatroom_get_room (chatroom);
958
959         DEBUG ("Requesting channel for '%s'", room);
960         empathy_dispatcher_join_muc (connection, room, timestamp);
961 }
962
963 typedef struct
964 {
965         TpAccount *account;
966         EmpathyChatroom *chatroom;
967         gint64 timestamp;
968         glong sig_id;
969         guint timeout;
970 } join_fav_account_sig_ctx;
971
972 static join_fav_account_sig_ctx *
973 join_fav_account_sig_ctx_new (TpAccount *account,
974                              EmpathyChatroom *chatroom,
975                               gint64 timestamp)
976 {
977         join_fav_account_sig_ctx *ctx = g_slice_new0 (
978                 join_fav_account_sig_ctx);
979
980         ctx->account = g_object_ref (account);
981         ctx->chatroom = g_object_ref (chatroom);
982         ctx->timestamp = timestamp;
983         return ctx;
984 }
985
986 static void
987 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
988 {
989         g_object_unref (ctx->account);
990         g_object_unref (ctx->chatroom);
991         g_slice_free (join_fav_account_sig_ctx, ctx);
992 }
993
994 static void
995 account_status_changed_cb (TpAccount  *account,
996                            TpConnectionStatus old_status,
997                            TpConnectionStatus new_status,
998                            guint reason,
999                            gchar *dbus_error_name,
1000                            GHashTable *details,
1001                            gpointer user_data)
1002 {
1003         join_fav_account_sig_ctx *ctx = user_data;
1004
1005         switch (new_status) {
1006                 case TP_CONNECTION_STATUS_DISCONNECTED:
1007                         /* Don't wait any longer */
1008                         goto finally;
1009                         break;
1010
1011                 case TP_CONNECTION_STATUS_CONNECTING:
1012                         /* Wait a bit */
1013                         return;
1014
1015                 case TP_CONNECTION_STATUS_CONNECTED:
1016                         /* We can join the room */
1017                         break;
1018
1019                 default:
1020                         g_assert_not_reached ();
1021         }
1022
1023         join_chatroom (ctx->chatroom, ctx->timestamp);
1024
1025 finally:
1026         g_source_remove (ctx->timeout);
1027         g_signal_handler_disconnect (account, ctx->sig_id);
1028 }
1029
1030 #define JOIN_FAVORITE_TIMEOUT 5
1031
1032 static gboolean
1033 join_favorite_timeout_cb (gpointer data)
1034 {
1035         join_fav_account_sig_ctx *ctx = data;
1036
1037         /* stop waiting for joining the favorite room */
1038         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1039         return FALSE;
1040 }
1041
1042 static void
1043 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1044 {
1045         TpAccount      *account;
1046
1047         account = empathy_chatroom_get_account (chatroom);
1048         if (tp_account_get_connection_status (account, NULL) !=
1049                                              TP_CONNECTION_STATUS_CONNECTED) {
1050                 join_fav_account_sig_ctx *ctx;
1051
1052                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1053                         gtk_get_current_event_time ());
1054
1055                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1056                         G_CALLBACK (account_status_changed_cb), ctx,
1057                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1058
1059                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1060                         join_favorite_timeout_cb, ctx);
1061                 return;
1062         }
1063
1064         join_chatroom (chatroom, gtk_get_current_event_time ());
1065 }
1066
1067 static void
1068 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1069                                                 EmpathyChatroom *chatroom)
1070 {
1071         main_window_favorite_chatroom_join (chatroom);
1072 }
1073
1074 static void
1075 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1076                                         EmpathyChatroom   *chatroom)
1077 {
1078         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1079         GtkWidget   *menu_item;
1080         const gchar *name;
1081
1082         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1083                 return;
1084         }
1085
1086         name = empathy_chatroom_get_name (chatroom);
1087         menu_item = gtk_menu_item_new_with_label (name);
1088
1089         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1090         g_signal_connect (menu_item, "activate",
1091                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1092                           chatroom);
1093
1094         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1095                                menu_item, 4);
1096
1097         gtk_widget_show (menu_item);
1098 }
1099
1100 static void
1101 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1102                                              EmpathyChatroom        *chatroom,
1103                                              EmpathyMainWindow      *window)
1104 {
1105         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1106
1107         main_window_favorite_chatroom_menu_add (window, chatroom);
1108         gtk_widget_show (priv->room_separator);
1109         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1110 }
1111
1112 static void
1113 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1114                                                EmpathyChatroom        *chatroom,
1115                                                EmpathyMainWindow      *window)
1116 {
1117         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1118         GtkWidget *menu_item;
1119         GList *chatrooms;
1120
1121         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1122         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1123         gtk_widget_destroy (menu_item);
1124
1125         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1126         if (chatrooms) {
1127                 gtk_widget_show (priv->room_separator);
1128         } else {
1129                 gtk_widget_hide (priv->room_separator);
1130         }
1131
1132         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1133         g_list_free (chatrooms);
1134 }
1135
1136 static void
1137 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1138 {
1139         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1140         GList *chatrooms, *l;
1141         GtkWidget *room;
1142
1143         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1144         chatrooms = empathy_chatroom_manager_get_chatrooms (
1145                 priv->chatroom_manager, NULL);
1146         room = gtk_ui_manager_get_widget (priv->ui_manager,
1147                 "/menubar/room");
1148         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1149         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1150                 "/menubar/room/room_separator");
1151
1152         for (l = chatrooms; l; l = l->next) {
1153                 main_window_favorite_chatroom_menu_add (window, l->data);
1154         }
1155
1156         if (!chatrooms) {
1157                 gtk_widget_hide (priv->room_separator);
1158         }
1159
1160         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1161
1162         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1163                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1164                           window);
1165         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1166                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1167                           window);
1168
1169         g_list_free (chatrooms);
1170 }
1171
1172 static void
1173 main_window_room_join_new_cb (GtkAction         *action,
1174                               EmpathyMainWindow *window)
1175 {
1176         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1177 }
1178
1179 static void
1180 main_window_room_join_favorites_cb (GtkAction         *action,
1181                                     EmpathyMainWindow *window)
1182 {
1183         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1184         GList *chatrooms, *l;
1185
1186         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1187         for (l = chatrooms; l; l = l->next) {
1188                 main_window_favorite_chatroom_join (l->data);
1189         }
1190         g_list_free (chatrooms);
1191 }
1192
1193 static void
1194 main_window_room_manage_favorites_cb (GtkAction         *action,
1195                                       EmpathyMainWindow *window)
1196 {
1197         empathy_chatrooms_window_show (GTK_WINDOW (window));
1198 }
1199
1200 static void
1201 main_window_edit_cb (GtkAction         *action,
1202                      EmpathyMainWindow *window)
1203 {
1204         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1205         GtkWidget *submenu;
1206
1207         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1208         submenu = empathy_individual_view_get_individual_menu (
1209                         priv->individual_view);
1210         if (submenu) {
1211                 GtkMenuItem *item;
1212                 GtkWidget   *label;
1213
1214                 item = GTK_MENU_ITEM (priv->edit_context);
1215                 label = gtk_bin_get_child (GTK_BIN (item));
1216                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1217
1218                 gtk_widget_show (priv->edit_context);
1219                 gtk_widget_show (priv->edit_context_separator);
1220
1221                 gtk_menu_item_set_submenu (item, submenu);
1222
1223                 return;
1224         }
1225
1226         submenu = empathy_individual_view_get_group_menu (
1227                         priv->individual_view);
1228         if (submenu) {
1229                 GtkMenuItem *item;
1230                 GtkWidget   *label;
1231
1232                 item = GTK_MENU_ITEM (priv->edit_context);
1233                 label = gtk_bin_get_child (GTK_BIN (item));
1234                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1235
1236                 gtk_widget_show (priv->edit_context);
1237                 gtk_widget_show (priv->edit_context_separator);
1238
1239                 gtk_menu_item_set_submenu (item, submenu);
1240
1241                 return;
1242         }
1243
1244         gtk_widget_hide (priv->edit_context);
1245         gtk_widget_hide (priv->edit_context_separator);
1246
1247         return;
1248 }
1249
1250 static void
1251 main_window_edit_accounts_cb (GtkAction         *action,
1252                               EmpathyMainWindow *window)
1253 {
1254         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1255                         NULL, FALSE, FALSE);
1256 }
1257
1258 static void
1259 main_window_edit_personal_information_cb (GtkAction         *action,
1260                                           EmpathyMainWindow *window)
1261 {
1262         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1263 }
1264
1265 static void
1266 main_window_edit_preferences_cb (GtkAction         *action,
1267                                  EmpathyMainWindow *window)
1268 {
1269         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1270
1271         if (priv->preferences == NULL) {
1272                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window));
1273                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
1274                                            (gpointer) &priv->preferences);
1275
1276                 gtk_widget_show (priv->preferences);
1277         } else {
1278                 gtk_window_present (GTK_WINDOW (priv->preferences));
1279         }
1280 }
1281
1282 static void
1283 main_window_help_about_cb (GtkAction         *action,
1284                            EmpathyMainWindow *window)
1285 {
1286         empathy_about_dialog_new (GTK_WINDOW (window));
1287 }
1288
1289 static void
1290 main_window_help_debug_cb (GtkAction         *action,
1291                            EmpathyMainWindow *window)
1292 {
1293         GdkScreen *screen = gdk_screen_get_default ();
1294         GError *error = NULL;
1295         gchar *argv[2] = { NULL, };
1296         gint i = 0;
1297         gchar *path;
1298
1299         g_return_if_fail (GDK_IS_SCREEN (screen));
1300
1301         /* Try to run from source directory if possible */
1302         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1303                         "empathy-debugger", NULL);
1304
1305         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1306                 g_free (path);
1307                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1308         }
1309
1310         argv[i++] = path;
1311
1312         gdk_spawn_on_screen (screen, NULL, argv, NULL,
1313                         G_SPAWN_SEARCH_PATH,
1314                         NULL, NULL, NULL, &error);
1315
1316         if (error) {
1317                 g_warning ("Failed to open debug window: %s", error->message);
1318                 g_error_free (error);
1319         }
1320
1321         g_free (path);
1322 }
1323
1324 static void
1325 main_window_help_contents_cb (GtkAction         *action,
1326                               EmpathyMainWindow *window)
1327 {
1328         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
1329 }
1330
1331 static gboolean
1332 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
1333                                             GdkEventButton    *event,
1334                                             EmpathyMainWindow *window)
1335 {
1336         if (event->type != GDK_BUTTON_PRESS ||
1337             event->button != 1) {
1338                 return FALSE;
1339         }
1340
1341         empathy_accounts_dialog_show_application (
1342                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
1343                         NULL, FALSE, FALSE);
1344
1345         return FALSE;
1346 }
1347
1348 static void
1349 main_window_account_removed_cb (TpAccountManager  *manager,
1350                                 TpAccount         *account,
1351                                 EmpathyMainWindow *window)
1352 {
1353         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1354         GList *a;
1355
1356         a = tp_account_manager_get_valid_accounts (manager);
1357
1358         gtk_action_set_sensitive (priv->view_history,
1359                 g_list_length (a) > 0);
1360
1361         g_list_free (a);
1362
1363         /* remove errors if any */
1364         main_window_remove_error (window, account);
1365 }
1366
1367 static void
1368 main_window_account_validity_changed_cb (TpAccountManager  *manager,
1369                                          TpAccount         *account,
1370                                          gboolean           valid,
1371                                          EmpathyMainWindow *window)
1372 {
1373         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1374
1375         if (valid) {
1376                 gulong handler_id;
1377                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1378                         priv->status_changed_handlers, account));
1379
1380                 /* connect signal only if it was not connected yet */
1381                 if (handler_id == 0) {
1382                         handler_id = g_signal_connect (account,
1383                                 "status-changed",
1384                                 G_CALLBACK (main_window_connection_changed_cb),
1385                                 window);
1386                         g_hash_table_insert (priv->status_changed_handlers,
1387                                 account, GUINT_TO_POINTER (handler_id));
1388                 }
1389         }
1390
1391         main_window_account_removed_cb (manager, account, window);
1392 }
1393
1394 static void
1395 main_window_notify_show_offline_cb (GSettings   *gsettings,
1396                                     const gchar *key,
1397                                     gpointer     toggle_action)
1398 {
1399         gtk_toggle_action_set_active (toggle_action,
1400                         g_settings_get_boolean (gsettings, key));
1401 }
1402
1403 static void
1404 main_window_connection_items_setup (EmpathyMainWindow *window,
1405                                     GtkBuilder        *gui)
1406 {
1407         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1408         GList         *list;
1409         GObject       *action;
1410         guint          i;
1411         const gchar *actions_connected[] = {
1412                 "room",
1413                 "chat_new_message",
1414                 "chat_new_call",
1415                 "chat_add_contact",
1416                 "edit_personal_information"
1417         };
1418
1419         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1420                 action = gtk_builder_get_object (gui, actions_connected[i]);
1421                 list = g_list_prepend (list, action);
1422         }
1423
1424         priv->actions_connected = list;
1425 }
1426
1427 static void
1428 account_manager_prepared_cb (GObject      *source_object,
1429                              GAsyncResult *result,
1430                              gpointer      user_data)
1431 {
1432         GList *accounts, *j;
1433         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1434         EmpathyMainWindow *window = user_data;
1435         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1436         GError *error = NULL;
1437
1438         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1439                 DEBUG ("Failed to prepare account manager: %s", error->message);
1440                 g_error_free (error);
1441                 return;
1442         }
1443
1444         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1445         for (j = accounts; j != NULL; j = j->next) {
1446                 TpAccount *account = TP_ACCOUNT (j->data);
1447                 gulong handler_id;
1448
1449                 handler_id = g_signal_connect (account, "status-changed",
1450                                   G_CALLBACK (main_window_connection_changed_cb),
1451                                   window);
1452                 g_hash_table_insert (priv->status_changed_handlers,
1453                                      account, GUINT_TO_POINTER (handler_id));
1454         }
1455
1456         g_signal_connect (manager, "account-validity-changed",
1457                           G_CALLBACK (main_window_account_validity_changed_cb),
1458                           window);
1459
1460         main_window_update_status (window);
1461
1462         /* Disable the "Previous Conversations" menu entry if there is no account */
1463         gtk_action_set_sensitive (priv->view_history,
1464                 g_list_length (accounts) > 0);
1465
1466         g_list_free (accounts);
1467 }
1468
1469 static void
1470 main_window_members_changed_cb (EmpathyContactList *list,
1471                                 EmpathyContact     *contact,
1472                                 EmpathyContact     *actor,
1473                                 guint               reason,
1474                                 gchar              *message,
1475                                 gboolean            is_member,
1476                                 EmpathyMainWindow  *window)
1477 {
1478         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1479
1480         if (!is_member)
1481                 return;
1482
1483         if (!empathy_migrate_butterfly_logs (contact)) {
1484                 g_signal_handler_disconnect (list,
1485                         priv->butterfly_log_migration_members_changed_id);
1486                 priv->butterfly_log_migration_members_changed_id = 0;
1487         }
1488 }
1489
1490 static GObject *
1491 empathy_main_window_constructor (GType type,
1492                                  guint n_construct_params,
1493                                  GObjectConstructParam *construct_params)
1494 {
1495         static GObject *window = NULL;
1496
1497         if (window != NULL)
1498                 return g_object_ref (window);
1499
1500         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
1501                 type, n_construct_params, construct_params);
1502
1503         g_object_add_weak_pointer (window, (gpointer) &window);
1504
1505         return window;
1506 }
1507
1508 static void
1509 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
1510 {
1511         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1512
1513         object_class->finalize = empathy_main_window_finalize;
1514         object_class->constructor = empathy_main_window_constructor;
1515
1516         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
1517 }
1518
1519 static void
1520 empathy_main_window_init (EmpathyMainWindow *window)
1521 {
1522         EmpathyMainWindowPriv    *priv;
1523         EmpathyContactList       *list_iface;
1524         EmpathyIndividualManager *individual_manager;
1525         GtkBuilder               *gui;
1526         GtkWidget                *sw;
1527         GtkToggleAction          *show_offline_widget;
1528         GtkAction                *show_map_widget;
1529         GtkToolItem              *item;
1530         gboolean                  show_offline;
1531         gchar                    *filename;
1532         GSList                   *l;
1533         GtkTreeModel             *model;
1534
1535         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1536                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
1537
1538         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1539         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
1540
1541         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
1542         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
1543         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
1544
1545         /* Set up interface */
1546         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1547         gui = empathy_builder_get_file (filename,
1548                                        "main_vbox", &priv->main_vbox,
1549                                        "errors_vbox", &priv->errors_vbox,
1550                                        "ui_manager", &priv->ui_manager,
1551                                        "view_show_offline", &show_offline_widget,
1552                                        "view_show_protocols", &priv->show_protocols,
1553                                        "view_sort_by_name", &priv->sort_by_name,
1554                                        "view_sort_by_status", &priv->sort_by_status,
1555                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
1556                                        "view_normal_size", &priv->normal_size,
1557                                        "view_compact_size", &priv->compact_size,
1558                                        "view_history", &priv->view_history,
1559                                        "view_show_map", &show_map_widget,
1560                                        "room_join_favorites", &priv->room_join_favorites,
1561                                        "presence_toolbar", &priv->presence_toolbar,
1562                                        "notebook", &priv->notebook,
1563                                        "no_entry_label", &priv->no_entry_label,
1564                                        "roster_scrolledwindow", &sw,
1565                                        NULL);
1566         g_free (filename);
1567
1568         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
1569         gtk_widget_show (priv->main_vbox);
1570
1571         g_signal_connect (window, "key-press-event",
1572                           G_CALLBACK (main_window_key_press_event_cb), NULL);
1573
1574         empathy_builder_connect (gui, window,
1575                               "chat_quit", "activate", main_window_chat_quit_cb,
1576                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1577                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1578                               "view_history", "activate", main_window_view_history_cb,
1579                               "room_join_new", "activate", main_window_room_join_new_cb,
1580                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1581                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1582                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1583                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1584                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1585                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1586                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1587                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1588                               "view_show_map", "activate", main_window_view_show_map_cb,
1589                               "edit", "activate", main_window_edit_cb,
1590                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1591                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1592                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1593                               "help_about", "activate", main_window_help_about_cb,
1594                               "help_debug", "activate", main_window_help_debug_cb,
1595                               "help_contents", "activate", main_window_help_contents_cb,
1596                               NULL);
1597
1598         /* Set up connection related widgets. */
1599         main_window_connection_items_setup (window, gui);
1600
1601         g_object_ref (priv->ui_manager);
1602         g_object_unref (gui);
1603
1604 #ifndef HAVE_LIBCHAMPLAIN
1605         gtk_action_set_visible (show_map_widget, FALSE);
1606 #endif
1607
1608         priv->account_manager = tp_account_manager_dup ();
1609
1610         tp_account_manager_prepare_async (priv->account_manager, NULL,
1611                                           account_manager_prepared_cb, window);
1612
1613         priv->errors = g_hash_table_new_full (g_direct_hash,
1614                                               g_direct_equal,
1615                                               g_object_unref,
1616                                               NULL);
1617
1618         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1619                                                                g_direct_equal,
1620                                                                NULL,
1621                                                                NULL);
1622
1623         /* Set up menu */
1624         main_window_favorite_chatroom_menu_setup (window);
1625
1626         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
1627                 "/menubar/edit/edit_context");
1628         priv->edit_context_separator = gtk_ui_manager_get_widget (
1629                 priv->ui_manager,
1630                 "/menubar/edit/edit_context_separator");
1631         gtk_widget_hide (priv->edit_context);
1632         gtk_widget_hide (priv->edit_context_separator);
1633
1634         /* Set up contact list. */
1635         empathy_status_presets_get_all ();
1636
1637         /* Set up presence chooser */
1638         priv->presence_chooser = empathy_presence_chooser_new ();
1639         gtk_widget_show (priv->presence_chooser);
1640         item = gtk_tool_item_new ();
1641         gtk_widget_show (GTK_WIDGET (item));
1642         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
1643         gtk_tool_item_set_is_important (item, TRUE);
1644         gtk_tool_item_set_expand (item, TRUE);
1645         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1646
1647         /* Set up the throbber */
1648         priv->throbber = gtk_spinner_new ();
1649         gtk_widget_set_size_request (priv->throbber, 16, -1);
1650         gtk_widget_set_tooltip_text (priv->throbber, _("Show and edit accounts"));
1651         gtk_widget_set_has_window (GTK_WIDGET (priv->throbber), TRUE);
1652         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
1653         g_signal_connect (priv->throbber, "button-press-event",
1654                 G_CALLBACK (main_window_throbber_button_press_event_cb),
1655                 window);
1656         gtk_widget_show (priv->throbber);
1657
1658         item = gtk_tool_item_new ();
1659         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
1660         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1661         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
1662         priv->throbber_tool_item = GTK_WIDGET (item);
1663
1664         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1665         individual_manager = empathy_individual_manager_dup_singleton ();
1666         priv->individual_store = empathy_individual_store_new (
1667                         individual_manager);
1668         g_object_unref (individual_manager);
1669
1670         priv->individual_view = empathy_individual_view_new (
1671                         priv->individual_store,
1672                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL,
1673                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
1674
1675         priv->butterfly_log_migration_members_changed_id = g_signal_connect (
1676                         list_iface, "members-changed",
1677                         G_CALLBACK (main_window_members_changed_cb), window);
1678
1679         g_object_unref (list_iface);
1680
1681         gtk_widget_show (GTK_WIDGET (priv->individual_view));
1682         gtk_container_add (GTK_CONTAINER (sw),
1683                            GTK_WIDGET (priv->individual_view));
1684         g_signal_connect (priv->individual_view, "row-activated",
1685                           G_CALLBACK (main_window_row_activated_cb),
1686                           window);
1687
1688         /* Set up search bar */
1689         priv->search_bar = empathy_live_search_new (
1690                 GTK_WIDGET (priv->individual_view));
1691         empathy_individual_view_set_live_search (priv->individual_view,
1692                 EMPATHY_LIVE_SEARCH (priv->search_bar));
1693         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->search_bar,
1694                 FALSE, TRUE, 0);
1695         g_signal_connect_swapped (window, "map",
1696                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
1697
1698         /* Connect to proper signals to check if contact list is empty or not */
1699         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
1700         priv->empty = TRUE;
1701         g_signal_connect (model, "row-inserted",
1702                           G_CALLBACK (main_window_row_inserted_cb),
1703                           window);
1704         g_signal_connect (model, "row-deleted",
1705                           G_CALLBACK (main_window_row_deleted_cb),
1706                           window);
1707
1708         /* Load user-defined accelerators. */
1709         main_window_accels_load ();
1710
1711         /* Set window size. */
1712         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
1713
1714         /* Enable event handling */
1715         priv->event_manager = empathy_event_manager_dup_singleton ();
1716
1717         g_signal_connect (priv->event_manager, "event-added",
1718                           G_CALLBACK (main_window_event_added_cb), window);
1719         g_signal_connect (priv->event_manager, "event-removed",
1720                           G_CALLBACK (main_window_event_removed_cb), window);
1721         g_signal_connect (priv->account_manager, "account-validity-changed",
1722                           G_CALLBACK (main_window_account_validity_changed_cb),
1723                           window);
1724         g_signal_connect (priv->account_manager, "account-removed",
1725                           G_CALLBACK (main_window_account_removed_cb),
1726                           window);
1727         g_signal_connect (priv->account_manager, "account-disabled",
1728                           G_CALLBACK (main_window_account_disabled_cb),
1729                           window);
1730
1731         l = empathy_event_manager_get_events (priv->event_manager);
1732         while (l) {
1733                 main_window_event_added_cb (priv->event_manager, l->data,
1734                                 window);
1735                 l = l->next;
1736         }
1737
1738         /* Show offline ? */
1739         show_offline = g_settings_get_boolean (priv->gsettings_ui,
1740                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
1741         g_signal_connect (priv->gsettings_ui,
1742                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
1743                           G_CALLBACK (main_window_notify_show_offline_cb),
1744                           show_offline_widget);
1745
1746         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1747
1748         /* Show protocol ? */
1749         g_signal_connect (priv->gsettings_ui,
1750                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1751                           G_CALLBACK (main_window_notify_show_protocols_cb),
1752                           window);
1753
1754         main_window_notify_show_protocols_cb (priv->gsettings_ui,
1755                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1756                                               window);
1757
1758         /* Sort by name / by status ? */
1759         g_signal_connect (priv->gsettings_contacts,
1760                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1761                           G_CALLBACK (main_window_notify_sort_contact_cb),
1762                           window);
1763
1764         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
1765                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1766                                             window);
1767
1768         /* Contacts list size */
1769         g_signal_connect (priv->gsettings_ui,
1770                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1771                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1772                           window);
1773         g_signal_connect (priv->gsettings_ui,
1774                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
1775                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1776                           window);
1777
1778         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
1779                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1780                                                  window);
1781 }
1782
1783 GtkWidget *
1784 empathy_main_window_dup (void)
1785 {
1786         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
1787 }