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