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