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