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