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