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