]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
use gtk_box_new() instead of gtk_[h,v]box_new()
[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-utils.h>
38 #include <libempathy/empathy-request-util.h>
39 #include <libempathy/empathy-chatroom-manager.h>
40 #include <libempathy/empathy-chatroom.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-contact-manager.h>
43 #include <libempathy/empathy-gsettings.h>
44 #include <libempathy/empathy-individual-manager.h>
45 #include <libempathy/empathy-gsettings.h>
46 #include <libempathy/empathy-status-presets.h>
47 #include <libempathy/empathy-tp-contact-factory.h>
48
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-live-search.h>
51 #include <libempathy-gtk/empathy-contact-blocking-dialog.h>
52 #include <libempathy-gtk/empathy-contact-search-dialog.h>
53 #include <libempathy-gtk/empathy-geometry.h>
54 #include <libempathy-gtk/empathy-gtk-enum-types.h>
55 #include <libempathy-gtk/empathy-individual-dialogs.h>
56 #include <libempathy-gtk/empathy-individual-store.h>
57 #include <libempathy-gtk/empathy-individual-view.h>
58 #include <libempathy-gtk/empathy-new-message-dialog.h>
59 #include <libempathy-gtk/empathy-new-call-dialog.h>
60 #include <libempathy-gtk/empathy-log-window.h>
61 #include <libempathy-gtk/empathy-presence-chooser.h>
62 #include <libempathy-gtk/empathy-sound-manager.h>
63 #include <libempathy-gtk/empathy-ui-utils.h>
64
65 #include "empathy-accounts-dialog.h"
66 #include "empathy-call-observer.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
78 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
79 #include <libempathy/empathy-debug.h>
80
81 /* Flashing delay for icons (milliseconds). */
82 #define FLASH_TIMEOUT 500
83
84 /* Minimum width of roster window if something goes wrong. */
85 #define MIN_WIDTH 50
86
87 /* Accels (menu shortcuts) can be configured and saved */
88 #define ACCELS_FILENAME "accels.txt"
89
90 /* Name in the geometry file */
91 #define GEOMETRY_NAME "main-window"
92
93 enum {
94         PAGE_CONTACT_LIST = 0,
95         PAGE_NO_MATCH
96 };
97
98 enum {
99         PROP_0,
100         PROP_SHELL_RUNNING
101 };
102
103 G_DEFINE_TYPE (EmpathyMainWindow, empathy_main_window, GTK_TYPE_WINDOW);
104
105 #define GET_PRIV(self) ((EmpathyMainWindowPriv *)((EmpathyMainWindow *) self)->priv)
106
107 struct _EmpathyMainWindowPriv {
108         EmpathyContactList      *contact_manager;
109         EmpathyIndividualStore  *individual_store;
110         EmpathyIndividualView   *individual_view;
111         TpAccountManager        *account_manager;
112         EmpathyChatroomManager  *chatroom_manager;
113         EmpathyEventManager     *event_manager;
114         EmpathySoundManager     *sound_mgr;
115         EmpathyCallObserver     *call_observer;
116         guint                    flash_timeout_id;
117         gboolean                 flash_on;
118         gboolean                 empty;
119
120         GSettings              *gsettings_ui;
121         GSettings              *gsettings_contacts;
122
123         GtkWidget              *preferences;
124         GtkWidget              *main_vbox;
125         GtkWidget              *throbber;
126         GtkWidget              *throbber_tool_item;
127         GtkWidget              *presence_toolbar;
128         GtkWidget              *presence_chooser;
129         GtkWidget              *errors_vbox;
130         GtkWidget              *auth_vbox;
131         GtkWidget              *search_bar;
132         GtkWidget              *notebook;
133         GtkWidget              *no_entry_label;
134
135         GtkToggleAction        *show_protocols;
136         GtkRadioAction         *sort_by_name;
137         GtkRadioAction         *sort_by_status;
138         GtkRadioAction         *normal_with_avatars;
139         GtkRadioAction         *normal_size;
140         GtkRadioAction         *compact_size;
141
142         GtkUIManager           *ui_manager;
143         GtkAction              *view_history;
144         GtkAction              *room_join_favorites;
145         GtkWidget              *room_menu;
146         GtkWidget              *room_separator;
147         GtkWidget              *edit_context;
148         GtkWidget              *edit_context_separator;
149
150         GtkActionGroup         *balance_action_group;
151         GtkAction              *view_balance_show_in_roster;
152         GtkWidget              *balance_vbox;
153
154         guint                   size_timeout_id;
155
156         /* reffed TpAccount* => visible GtkInfoBar* */
157         GHashTable             *errors;
158
159         /* EmpathyEvent* => visible GtkInfoBar* */
160         GHashTable             *auths;
161
162         /* stores a mapping from TpAccount to Handler ID to prevent
163          * to listen more than once to the status-changed signal */
164         GHashTable             *status_changed_handlers;
165
166         /* Actions that are enabled when there are connected accounts */
167         GList                  *actions_connected;
168
169         gboolean               shell_running;
170 };
171
172 static void
173 main_window_flash_stop (EmpathyMainWindow *window)
174 {
175         EmpathyMainWindowPriv *priv = GET_PRIV (window);
176
177         if (priv->flash_timeout_id == 0) {
178                 return;
179         }
180
181         DEBUG ("Stop flashing");
182         g_source_remove (priv->flash_timeout_id);
183         priv->flash_timeout_id = 0;
184         priv->flash_on = FALSE;
185 }
186
187 typedef struct {
188         EmpathyEvent       *event;
189         gboolean            on;
190         EmpathyMainWindow  *window;
191 } FlashForeachData;
192
193 static gboolean
194 main_window_flash_foreach (GtkTreeModel *model,
195                            GtkTreePath  *path,
196                            GtkTreeIter  *iter,
197                            gpointer      user_data)
198 {
199         FlashForeachData *data = (FlashForeachData *) user_data;
200         FolksIndividual *individual;
201         EmpathyContact   *contact;
202         const gchar      *icon_name;
203         GtkTreePath      *parent_path = NULL;
204         GtkTreeIter       parent_iter;
205         GdkPixbuf        *pixbuf = NULL;
206
207         gtk_tree_model_get (model, iter,
208                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
209                             -1);
210
211         if (individual == NULL)
212                 return FALSE;
213
214         contact = empathy_contact_dup_from_folks_individual (individual);
215         if (contact != data->event->contact)
216                 goto out;
217
218         if (data->on) {
219                 icon_name = data->event->icon_name;
220                 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
221         } else {
222                 pixbuf = empathy_individual_store_get_individual_status_icon (
223                                                 GET_PRIV (data->window)->individual_store,
224                                                 individual);
225                 if (pixbuf != NULL)
226                         g_object_ref (pixbuf);
227         }
228
229         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
230                             EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf,
231                             -1);
232
233         /* To make sure the parent is shown correctly, we emit
234          * the row-changed signal on the parent so it prompts
235          * it to be refreshed by the filter func.
236          */
237         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
238                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
239         }
240         if (parent_path) {
241                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
242                 gtk_tree_path_free (parent_path);
243         }
244
245 out:
246         g_object_unref (individual);
247         tp_clear_object (&contact);
248         tp_clear_object (&pixbuf);
249
250         return FALSE;
251 }
252
253 static gboolean
254 main_window_flash_cb (EmpathyMainWindow *window)
255 {
256         EmpathyMainWindowPriv *priv = GET_PRIV (window);
257         GtkTreeModel     *model;
258         GSList           *events, *l;
259         gboolean          found_event = FALSE;
260         FlashForeachData  data;
261
262         priv->flash_on = !priv->flash_on;
263         data.on = priv->flash_on;
264         model = GTK_TREE_MODEL (priv->individual_store);
265
266         events = empathy_event_manager_get_events (priv->event_manager);
267         for (l = events; l; l = l->next) {
268                 data.event = l->data;
269                 data.window = window;
270                 if (!data.event->contact || !data.event->must_ack) {
271                         continue;
272                 }
273
274                 found_event = TRUE;
275                 gtk_tree_model_foreach (model,
276                                         main_window_flash_foreach,
277                                         &data);
278         }
279
280         if (!found_event) {
281                 main_window_flash_stop (window);
282         }
283
284         return TRUE;
285 }
286
287 static void
288 main_window_flash_start (EmpathyMainWindow *window)
289 {
290         EmpathyMainWindowPriv *priv = GET_PRIV (window);
291
292         if (priv->flash_timeout_id != 0) {
293                 return;
294         }
295
296         DEBUG ("Start flashing");
297         priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
298                                                 (GSourceFunc) main_window_flash_cb,
299                                                 window);
300         main_window_flash_cb (window);
301 }
302
303 static void
304 main_window_remove_auth (EmpathyMainWindow *window,
305                          EmpathyEvent      *event)
306 {
307         EmpathyMainWindowPriv *priv = GET_PRIV (window);
308         GtkWidget *error_widget;
309
310         error_widget = g_hash_table_lookup (priv->auths, event);
311         if (error_widget != NULL) {
312                 gtk_widget_destroy (error_widget);
313                 g_hash_table_remove (priv->auths, event);
314         }
315 }
316
317 static void
318 main_window_auth_add_clicked_cb (GtkButton         *button,
319                                  EmpathyMainWindow *window)
320 {
321         EmpathyEvent *event;
322
323         event = g_object_get_data (G_OBJECT (button), "event");
324
325         empathy_event_approve (event);
326
327         main_window_remove_auth (window, event);
328 }
329
330 static void
331 main_window_auth_close_clicked_cb (GtkButton         *button,
332                                    EmpathyMainWindow *window)
333 {
334         EmpathyEvent *event;
335
336         event = g_object_get_data (G_OBJECT (button), "event");
337
338         empathy_event_decline (event);
339         main_window_remove_auth (window, event);
340 }
341
342 static void
343 main_window_auth_display (EmpathyMainWindow *window,
344                           EmpathyEvent      *event)
345 {
346         EmpathyMainWindowPriv *priv = GET_PRIV (window);
347         TpAccount *account = event->account;
348         GtkWidget *info_bar;
349         GtkWidget *content_area;
350         GtkWidget *image;
351         GtkWidget *label;
352         GtkWidget *add_button;
353         GtkWidget *close_button;
354         GtkWidget *action_area;
355         GtkWidget *action_grid;
356         const gchar *icon_name;
357         gchar *str;
358
359         if (g_hash_table_lookup (priv->auths, event) != NULL) {
360                 return;
361         }
362
363         info_bar = gtk_info_bar_new ();
364         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_QUESTION);
365
366         gtk_widget_set_no_show_all (info_bar, TRUE);
367         gtk_box_pack_start (GTK_BOX (priv->auth_vbox), info_bar, FALSE, TRUE, 0);
368         gtk_widget_show (info_bar);
369
370         icon_name = tp_account_get_icon_name (account);
371         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
372         gtk_widget_show (image);
373
374         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
375                                        tp_account_get_display_name (account),
376                                        _("Password required"));
377
378         label = gtk_label_new (str);
379         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
380         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
381         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
382         gtk_widget_show (label);
383
384         g_free (str);
385
386         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
387         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
388         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
389
390         image = gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON);
391         add_button = gtk_button_new ();
392         gtk_button_set_image (GTK_BUTTON (add_button), image);
393         gtk_widget_set_tooltip_text (add_button, _("Provide Password"));
394         gtk_widget_show (add_button);
395
396         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
397         close_button = gtk_button_new ();
398         gtk_button_set_image (GTK_BUTTON (close_button), image);
399         gtk_widget_set_tooltip_text (close_button, _("Disconnect"));
400         gtk_widget_show (close_button);
401
402         action_grid = gtk_grid_new ();
403         gtk_grid_set_column_spacing (GTK_GRID (action_grid), 6);
404         gtk_widget_show (action_grid);
405
406         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
407         gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
408
409         gtk_grid_attach (GTK_GRID (action_grid), add_button, 0, 0, 1, 1);
410         gtk_grid_attach (GTK_GRID (action_grid), close_button, 1, 0, 1, 1);
411
412         g_object_set_data_full (G_OBJECT (info_bar),
413                                 "event", event, NULL);
414         g_object_set_data_full (G_OBJECT (add_button),
415                                 "event", event, NULL);
416         g_object_set_data_full (G_OBJECT (close_button),
417                                 "event", event, NULL);
418
419         g_signal_connect (add_button, "clicked",
420                           G_CALLBACK (main_window_auth_add_clicked_cb),
421                           window);
422         g_signal_connect (close_button, "clicked",
423                           G_CALLBACK (main_window_auth_close_clicked_cb),
424                           window);
425
426         gtk_widget_show (priv->auth_vbox);
427
428         g_hash_table_insert (priv->auths, event, info_bar);
429 }
430
431 static void
432 modify_event_count (GtkTreeModel *model,
433                     GtkTreeIter *iter,
434                     EmpathyEvent *event,
435                     gboolean increase)
436 {
437         FolksIndividual *individual;
438         EmpathyContact *contact;
439         guint count;
440
441         gtk_tree_model_get (model, iter,
442                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
443                             EMPATHY_INDIVIDUAL_STORE_COL_EVENT_COUNT, &count,
444                             -1);
445
446         if (individual == NULL)
447                 return;
448
449         increase ? count++ : count--;
450
451         contact = empathy_contact_dup_from_folks_individual (individual);
452         if (contact == event->contact) {
453                 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
454                                     EMPATHY_INDIVIDUAL_STORE_COL_EVENT_COUNT, count, -1);
455         }
456
457         tp_clear_object (&contact);
458         g_object_unref (individual);
459 }
460
461 static gboolean
462 increase_event_count_foreach (GtkTreeModel *model,
463                               GtkTreePath *path,
464                               GtkTreeIter *iter,
465                               gpointer user_data)
466 {
467         EmpathyEvent *event = user_data;
468
469         modify_event_count (model, iter, event, TRUE);
470
471         return FALSE;
472 }
473
474 static void
475 increase_event_count (EmpathyMainWindow *self,
476                       EmpathyEvent *event)
477 {
478         EmpathyMainWindowPriv *priv = GET_PRIV (self);
479         GtkTreeModel *model;
480
481         model = GTK_TREE_MODEL (priv->individual_store);
482
483         gtk_tree_model_foreach (model, increase_event_count_foreach, event);
484 }
485
486 static gboolean
487 decrease_event_count_foreach (GtkTreeModel *model,
488                               GtkTreePath *path,
489                               GtkTreeIter *iter,
490                               gpointer user_data)
491 {
492         EmpathyEvent *event = user_data;
493
494         modify_event_count (model, iter, event, FALSE);
495
496         return FALSE;
497 }
498
499 static void
500 decrease_event_count (EmpathyMainWindow *self,
501                       EmpathyEvent *event)
502 {
503         EmpathyMainWindowPriv *priv = GET_PRIV (self);
504         GtkTreeModel *model;
505
506         model = GTK_TREE_MODEL (priv->individual_store);
507
508         gtk_tree_model_foreach (model, decrease_event_count_foreach, event);
509 }
510
511 static void
512 main_window_event_added_cb (EmpathyEventManager *manager,
513                             EmpathyEvent        *event,
514                             EmpathyMainWindow   *window)
515 {
516         if (event->contact) {
517                 increase_event_count (window, event);
518
519                 main_window_flash_start (window);
520         } else if (event->type == EMPATHY_EVENT_TYPE_AUTH) {
521                 main_window_auth_display (window, event);
522         }
523 }
524
525 static void
526 main_window_event_removed_cb (EmpathyEventManager *manager,
527                               EmpathyEvent        *event,
528                               EmpathyMainWindow   *window)
529 {
530         EmpathyMainWindowPriv *priv = GET_PRIV (window);
531         FlashForeachData data;
532
533         if (event->type == EMPATHY_EVENT_TYPE_AUTH) {
534                 main_window_remove_auth (window, event);
535                 return;
536         }
537
538         if (!event->contact) {
539                 return;
540         }
541
542         decrease_event_count (window, event);
543
544         data.on = FALSE;
545         data.event = event;
546         data.window = window;
547         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->individual_store),
548                                 main_window_flash_foreach,
549                                 &data);
550 }
551
552 static gboolean
553 main_window_load_events_idle_cb (gpointer user_data)
554 {
555         EmpathyMainWindow *window = user_data;
556         EmpathyMainWindowPriv *priv = GET_PRIV (window);
557         GSList *l;
558
559         l = empathy_event_manager_get_events (priv->event_manager);
560         while (l) {
561                 main_window_event_added_cb (priv->event_manager, l->data,
562                                 window);
563                 l = l->next;
564         }
565
566         return FALSE;
567 }
568
569 static void
570 main_window_row_activated_cb (EmpathyIndividualView *view,
571                               GtkTreePath            *path,
572                               GtkTreeViewColumn      *col,
573                               EmpathyMainWindow      *window)
574 {
575         EmpathyMainWindowPriv *priv = GET_PRIV (window);
576         EmpathyContact *contact = NULL;
577         FolksIndividual *individual;
578         GtkTreeModel   *model;
579         GtkTreeIter     iter;
580         GSList         *events, *l;
581
582         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
583         gtk_tree_model_get_iter (model, &iter, path);
584
585         gtk_tree_model_get (model, &iter,
586                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
587                                 &individual,
588                             -1);
589
590         if (individual != NULL) {
591                 contact = empathy_contact_dup_from_folks_individual (individual);
592         }
593
594         if (!contact) {
595                 goto OUT;
596         }
597
598         /* If the contact has an event activate it, otherwise the
599          * default handler of row-activated will be called. */
600         events = empathy_event_manager_get_events (priv->event_manager);
601         for (l = events; l; l = l->next) {
602                 EmpathyEvent *event = l->data;
603
604                 if (event->contact == contact) {
605                         DEBUG ("Activate event");
606                         empathy_event_activate (event);
607
608                         /* We don't want the default handler of this signal
609                          * (e.g. open a chat) */
610                         g_signal_stop_emission_by_name (view, "row-activated");
611                         break;
612                 }
613         }
614
615         g_object_unref (contact);
616 OUT:
617         tp_clear_object (&individual);
618 }
619
620 static void
621 main_window_row_deleted_cb (GtkTreeModel      *model,
622                             GtkTreePath       *path,
623                             EmpathyMainWindow *window)
624 {
625         EmpathyMainWindowPriv *priv = GET_PRIV (window);
626         GtkTreeIter help_iter;
627
628         if (!gtk_tree_model_get_iter_first (model, &help_iter)) {
629                 priv->empty = TRUE;
630
631                 if (empathy_individual_view_is_searching (
632                                 priv->individual_view)) {
633                         gchar *tmp;
634
635                         tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>",
636                                 _("No match found"));
637
638                         gtk_label_set_markup (GTK_LABEL (priv->no_entry_label), tmp);
639                         g_free (tmp);
640
641                         gtk_label_set_line_wrap (GTK_LABEL (priv->no_entry_label),
642                                 TRUE);
643
644                         gtk_notebook_set_current_page (
645                                         GTK_NOTEBOOK (priv->notebook), PAGE_NO_MATCH);
646                 }
647         }
648 }
649
650 static void
651 main_window_row_inserted_cb (GtkTreeModel      *model,
652                              GtkTreePath       *path,
653                              GtkTreeIter       *iter,
654                              EmpathyMainWindow *window)
655 {
656         EmpathyMainWindowPriv *priv = GET_PRIV (window);
657
658         if (priv->empty) {
659                 priv->empty = FALSE;
660                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
661                                 PAGE_CONTACT_LIST);
662                 gtk_widget_grab_focus (GTK_WIDGET (priv->individual_view));
663
664                 /* The store is being filled, it will be done after an idle cb.
665                  * So we can then get events. If we do that too soon, event's
666                  * contact is not yet in the store and it won't get marked as
667                  * having events. */
668                 g_idle_add (main_window_load_events_idle_cb, window);
669         }
670 }
671
672 static void
673 main_window_remove_error (EmpathyMainWindow *window,
674                           TpAccount         *account)
675 {
676         EmpathyMainWindowPriv *priv = GET_PRIV (window);
677         GtkWidget *error_widget;
678
679         error_widget = g_hash_table_lookup (priv->errors, account);
680         if (error_widget != NULL) {
681                 gtk_widget_destroy (error_widget);
682                 g_hash_table_remove (priv->errors, account);
683         }
684 }
685
686 static void
687 main_window_account_disabled_cb (TpAccountManager  *manager,
688                                  TpAccount         *account,
689                                  EmpathyMainWindow *window)
690 {
691         main_window_remove_error (window, account);
692 }
693
694 static void
695 main_window_error_retry_clicked_cb (GtkButton         *button,
696                                     EmpathyMainWindow *window)
697 {
698         TpAccount *account;
699
700         account = g_object_get_data (G_OBJECT (button), "account");
701         tp_account_reconnect_async (account, NULL, NULL);
702
703         main_window_remove_error (window, account);
704 }
705
706 static void
707 main_window_error_edit_clicked_cb (GtkButton         *button,
708                                    EmpathyMainWindow *window)
709 {
710         TpAccount *account;
711
712         account = g_object_get_data (G_OBJECT (button), "account");
713
714         empathy_accounts_dialog_show_application (
715                         gtk_widget_get_screen (GTK_WIDGET (button)),
716                         account, FALSE, FALSE);
717
718         main_window_remove_error (window, account);
719 }
720
721 static void
722 main_window_error_close_clicked_cb (GtkButton         *button,
723                                     EmpathyMainWindow *window)
724 {
725         TpAccount *account;
726
727         account = g_object_get_data (G_OBJECT (button), "account");
728         main_window_remove_error (window, account);
729 }
730
731 static void
732 main_window_error_upgrade_sw_clicked_cb (GtkButton         *button,
733                                          EmpathyMainWindow *window)
734 {
735         TpAccount *account;
736         GtkWidget *dialog;
737
738         account = g_object_get_data (G_OBJECT (button), "account");
739         main_window_remove_error (window, account);
740
741         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
742                 GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
743                 GTK_BUTTONS_OK,
744                 _("Sorry, %s accounts can’t be used until your %s software is updated."),
745                 tp_account_get_protocol (account),
746                 tp_account_get_protocol (account));
747
748         g_signal_connect_swapped (dialog, "response",
749                 G_CALLBACK (gtk_widget_destroy),
750                 dialog);
751
752         gtk_widget_show (dialog);
753 }
754
755 static void
756 main_window_upgrade_software_error (EmpathyMainWindow *window,
757                                     TpAccount         *account)
758 {
759         EmpathyMainWindowPriv *priv = GET_PRIV (window);
760         GtkWidget *info_bar;
761         GtkWidget *content_area;
762         GtkWidget *label;
763         GtkWidget *image;
764         GtkWidget *upgrade_button;
765         GtkWidget *close_button;
766         GtkWidget *action_area;
767         GtkWidget *action_grid;
768         gchar     *str;
769         const gchar     *icon_name;
770         const gchar *error_message;
771         gboolean user_requested;
772
773         error_message =
774                 empathy_account_get_error_message (account, &user_requested);
775
776         if (user_requested) {
777                 return;
778         }
779
780         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
781                                                tp_account_get_display_name (account),
782                                                error_message);
783
784         /* If there are other errors, remove them */
785         main_window_remove_error (window, account);
786
787         info_bar = gtk_info_bar_new ();
788         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
789
790         gtk_widget_set_no_show_all (info_bar, TRUE);
791         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
792         gtk_widget_show (info_bar);
793
794         icon_name = tp_account_get_icon_name (account);
795         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
796         gtk_widget_show (image);
797
798         label = gtk_label_new (str);
799         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
800         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
801         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
802         gtk_widget_show (label);
803         g_free (str);
804
805         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
806         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
807         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
808
809         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
810         upgrade_button = gtk_button_new ();
811         gtk_button_set_image (GTK_BUTTON (upgrade_button), image);
812         gtk_widget_set_tooltip_text (upgrade_button, _("Update software..."));
813         gtk_widget_show (upgrade_button);
814
815         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
816         close_button = gtk_button_new ();
817         gtk_button_set_image (GTK_BUTTON (close_button), image);
818         gtk_widget_set_tooltip_text (close_button, _("Close"));
819         gtk_widget_show (close_button);
820
821         action_grid = gtk_grid_new ();
822         gtk_grid_set_column_spacing (GTK_GRID (action_grid), 2);
823         gtk_widget_show (action_grid);
824
825         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
826         gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
827
828         gtk_grid_attach (GTK_GRID (action_grid), upgrade_button, 0, 0, 1, 1);
829         gtk_grid_attach (GTK_GRID (action_grid), close_button, 1, 0, 1, 1);
830
831         g_object_set_data (G_OBJECT (info_bar), "label", label);
832         g_object_set_data_full (G_OBJECT (info_bar),
833                                 "account", g_object_ref (account),
834                                 g_object_unref);
835         g_object_set_data_full (G_OBJECT (upgrade_button),
836                                 "account", g_object_ref (account),
837                                 g_object_unref);
838         g_object_set_data_full (G_OBJECT (close_button),
839                                 "account", g_object_ref (account),
840                                 g_object_unref);
841
842         g_signal_connect (upgrade_button, "clicked",
843                           G_CALLBACK (main_window_error_upgrade_sw_clicked_cb),
844                           window);
845         g_signal_connect (close_button, "clicked",
846                           G_CALLBACK (main_window_error_close_clicked_cb),
847                           window);
848
849         gtk_widget_set_tooltip_text (priv->errors_vbox, error_message);
850         gtk_widget_show (priv->errors_vbox);
851
852         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
853 }
854
855 static void
856 main_window_error_display (EmpathyMainWindow *window,
857                            TpAccount         *account)
858 {
859         EmpathyMainWindowPriv *priv = GET_PRIV (window);
860         GtkWidget *info_bar;
861         GtkWidget *content_area;
862         GtkWidget *label;
863         GtkWidget *image;
864         GtkWidget *retry_button;
865         GtkWidget *edit_button;
866         GtkWidget *close_button;
867         GtkWidget *action_area;
868         GtkWidget *action_grid;
869         gchar     *str;
870         const gchar     *icon_name;
871         const gchar *error_message;
872         gboolean user_requested;
873
874         if (!tp_strdiff (TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
875                          tp_account_get_detailed_error (account, NULL))) {
876                 main_window_upgrade_software_error (window, account);
877                 return;
878         }
879
880         error_message =
881                 empathy_account_get_error_message (account, &user_requested);
882
883         if (user_requested) {
884                 return;
885         }
886
887         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
888                                                tp_account_get_display_name (account),
889                                                error_message);
890
891         info_bar = g_hash_table_lookup (priv->errors, account);
892         if (info_bar) {
893                 label = g_object_get_data (G_OBJECT (info_bar), "label");
894
895                 /* Just set the latest error and return */
896                 gtk_label_set_markup (GTK_LABEL (label), str);
897                 g_free (str);
898
899                 return;
900         }
901
902         info_bar = gtk_info_bar_new ();
903         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
904
905         gtk_widget_set_no_show_all (info_bar, TRUE);
906         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
907         gtk_widget_show (info_bar);
908
909         icon_name = tp_account_get_icon_name (account);
910         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
911         gtk_widget_show (image);
912
913         label = gtk_label_new (str);
914         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
915         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
916         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
917         gtk_widget_show (label);
918         g_free (str);
919
920         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
921         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
922         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
923
924         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
925         retry_button = gtk_button_new ();
926         gtk_button_set_image (GTK_BUTTON (retry_button), image);
927         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
928         gtk_widget_show (retry_button);
929
930         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
931         edit_button = gtk_button_new ();
932         gtk_button_set_image (GTK_BUTTON (edit_button), image);
933         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
934         gtk_widget_show (edit_button);
935
936         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
937         close_button = gtk_button_new ();
938         gtk_button_set_image (GTK_BUTTON (close_button), image);
939         gtk_widget_set_tooltip_text (close_button, _("Close"));
940         gtk_widget_show (close_button);
941
942         action_grid = gtk_grid_new ();
943         gtk_grid_set_column_spacing (GTK_GRID (action_grid), 2);
944         gtk_widget_show (action_grid);
945
946         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
947         gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
948
949         gtk_grid_attach (GTK_GRID (action_grid), retry_button, 0, 0, 1, 1);
950         gtk_grid_attach (GTK_GRID (action_grid), edit_button, 1, 0, 1, 1);
951         gtk_grid_attach (GTK_GRID (action_grid), close_button, 2, 0, 1, 1);
952
953         g_object_set_data (G_OBJECT (info_bar), "label", label);
954         g_object_set_data_full (G_OBJECT (info_bar),
955                                 "account", g_object_ref (account),
956                                 g_object_unref);
957         g_object_set_data_full (G_OBJECT (edit_button),
958                                 "account", g_object_ref (account),
959                                 g_object_unref);
960         g_object_set_data_full (G_OBJECT (close_button),
961                                 "account", g_object_ref (account),
962                                 g_object_unref);
963         g_object_set_data_full (G_OBJECT (retry_button),
964                                 "account", g_object_ref (account),
965                                 g_object_unref);
966
967         g_signal_connect (edit_button, "clicked",
968                           G_CALLBACK (main_window_error_edit_clicked_cb),
969                           window);
970         g_signal_connect (close_button, "clicked",
971                           G_CALLBACK (main_window_error_close_clicked_cb),
972                           window);
973         g_signal_connect (retry_button, "clicked",
974                           G_CALLBACK (main_window_error_retry_clicked_cb),
975                           window);
976
977         gtk_widget_set_tooltip_text (priv->errors_vbox, error_message);
978         gtk_widget_show (priv->errors_vbox);
979
980         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
981 }
982
983 static void
984 main_window_update_status (EmpathyMainWindow *window)
985 {
986         EmpathyMainWindowPriv *priv = GET_PRIV (window);
987         gboolean connected, connecting;
988         GList *l, *children;
989
990         connected = empathy_account_manager_get_accounts_connected (&connecting);
991
992         /* Update the spinner state */
993         if (connecting) {
994                 gtk_spinner_start (GTK_SPINNER (priv->throbber));
995                 gtk_widget_show (priv->throbber_tool_item);
996         } else {
997                 gtk_spinner_stop (GTK_SPINNER (priv->throbber));
998                 gtk_widget_hide (priv->throbber_tool_item);
999         }
1000
1001         /* Update widgets sensibility */
1002         for (l = priv->actions_connected; l; l = l->next) {
1003                 gtk_action_set_sensitive (l->data, connected);
1004         }
1005
1006         /* Update favourite rooms sensitivity */
1007         children = gtk_container_get_children (GTK_CONTAINER (priv->room_menu));
1008         for (l = children; l != NULL; l = l->next) {
1009                 if (g_object_get_data (G_OBJECT (l->data), "is_favorite") != NULL) {
1010                         gtk_widget_set_sensitive (GTK_WIDGET (l->data), connected);
1011                 }
1012         }
1013         g_list_free (children);
1014 }
1015
1016 static char *
1017 main_window_account_to_action_name (TpAccount *account)
1018 {
1019         char *r;
1020
1021         /* action names can't have '/' in them, replace it with '.' */
1022         r = g_strdup (tp_account_get_path_suffix (account));
1023         r = g_strdelimit (r, "/", '.');
1024
1025         return r;
1026 }
1027
1028 static void
1029 main_window_balance_activate_cb (GtkAction         *action,
1030                                  EmpathyMainWindow *window)
1031 {
1032         const char *uri;
1033
1034         uri = g_object_get_data (G_OBJECT (action), "manage-credit-uri");
1035
1036         if (!tp_str_empty (uri)) {
1037                 DEBUG ("Top-up credit URI: %s", uri);
1038                 empathy_url_show (GTK_WIDGET (window), uri);
1039         } else {
1040                 DEBUG ("unknown protocol for top-up");
1041         }
1042 }
1043
1044 static void
1045 main_window_balance_update_balance (GtkAction   *action,
1046                                     TpConnection *conn)
1047 {
1048         TpAccount *account = tp_connection_get_account (conn);
1049         GtkWidget *label;
1050         int amount = 0;
1051         guint scale = G_MAXINT32;
1052         const gchar *currency = "";
1053         char *money, *str;
1054
1055         if (!tp_connection_get_balance (conn, &amount, &scale, &currency))
1056                 return;
1057
1058         if (amount == 0 &&
1059             scale == G_MAXINT32 &&
1060             tp_str_empty (currency)) {
1061                 /* unknown balance */
1062                 money = g_strdup ("--");
1063         } else {
1064                 char *tmp = empathy_format_currency (amount, scale, currency);
1065
1066                 money = g_strdup_printf ("%s %s", currency, tmp);
1067                 g_free (tmp);
1068         }
1069
1070         /* Translators: this string will be something like:
1071          *   Top up My Account ($1.23)..." */
1072         str = g_strdup_printf (_("Top up %s (%s)..."),
1073                 tp_account_get_display_name (account),
1074                 money);
1075
1076         gtk_action_set_label (action, str);
1077         g_free (str);
1078
1079         /* update the money label in the roster */
1080         label = g_object_get_data (G_OBJECT (action), "money-label");
1081
1082         gtk_label_set_text (GTK_LABEL (label), money);
1083         g_free (money);
1084 }
1085
1086 static void
1087 main_window_balance_changed_cb (TpConnection      *conn,
1088                                 guint balance,
1089                                 guint scale,
1090                                 const gchar *currency,
1091                                 GtkAction *action)
1092 {
1093         main_window_balance_update_balance (action, conn);
1094 }
1095
1096 static GtkAction *
1097 main_window_setup_balance_create_action (EmpathyMainWindow *window,
1098                                          TpAccount *account)
1099 {
1100         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1101         GtkAction *action;
1102         char *name, *ui;
1103         guint merge_id;
1104         GError *error = NULL;
1105
1106         /* create the action group if required */
1107         if (priv->balance_action_group == NULL) {
1108                 priv->balance_action_group =
1109                         gtk_action_group_new ("balance-action-group");
1110
1111                 gtk_ui_manager_insert_action_group (priv->ui_manager,
1112                         priv->balance_action_group, -1);
1113         }
1114
1115         /* create the action */
1116         name = main_window_account_to_action_name (account);
1117         action = gtk_action_new (name,
1118                 tp_account_get_display_name (account),
1119                 _("Top up account credit"),
1120                 NULL);
1121         g_object_bind_property (account, "icon-name", action, "icon-name",
1122                 G_BINDING_SYNC_CREATE);
1123
1124         g_signal_connect (action, "activate",
1125                 G_CALLBACK (main_window_balance_activate_cb), window);
1126
1127         gtk_action_group_add_action (priv->balance_action_group, action);
1128         g_object_unref (action);
1129
1130         ui = g_strdup_printf (
1131                 "<ui>"
1132                 " <menubar name='menubar'>"
1133                 "  <menu action='view'>"
1134                 "   <placeholder name='view_balance_placeholder'>"
1135                 "    <menuitem action='%s'/>"
1136                 "   </placeholder>"
1137                 "  </menu>"
1138                 " </menubar>"
1139                 "</ui>",
1140                 name);
1141
1142         merge_id = gtk_ui_manager_add_ui_from_string (priv->ui_manager,
1143                 ui, -1, &error);
1144         if (error != NULL) {
1145                 DEBUG ("Failed to add balance UI for %s: %s",
1146                         tp_account_get_display_name (account),
1147                         error->message);
1148                 g_error_free (error);
1149         }
1150
1151         g_object_set_data (G_OBJECT (action),
1152                 "merge-id", GUINT_TO_POINTER (merge_id));
1153
1154         g_free (name);
1155         g_free (ui);
1156
1157         return action;
1158 }
1159
1160 static GtkWidget *
1161 main_window_setup_balance_create_widget (EmpathyMainWindow *window,
1162                                          GtkAction         *action,
1163                                          TpAccount *account)
1164 {
1165         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1166         GtkWidget *hbox, *image, *label, *button;
1167
1168         hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
1169
1170         /* protocol icon */
1171         image = gtk_image_new ();
1172         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, TRUE, 0);
1173         g_object_bind_property (action, "icon-name", image, "icon-name",
1174                 G_BINDING_SYNC_CREATE);
1175
1176         /* account name label */
1177         label = gtk_label_new ("");
1178         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1179         gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
1180         g_object_bind_property (account, "display-name", label, "label",
1181                 G_BINDING_SYNC_CREATE);
1182
1183         /* balance label */
1184         label = gtk_label_new ("");
1185         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1186         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
1187         g_object_set_data (G_OBJECT (action), "money-label", label);
1188
1189         /* top up button */
1190         button = gtk_button_new_with_label (_("Top Up..."));
1191         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
1192         g_signal_connect_swapped (button, "clicked",
1193                 G_CALLBACK (gtk_action_activate), action);
1194
1195         gtk_box_pack_start (GTK_BOX (priv->balance_vbox), hbox, FALSE, TRUE, 0);
1196         gtk_widget_show_all (hbox);
1197
1198         /* tie the lifetime of the widget to the lifetime of the action */
1199         g_object_weak_ref (G_OBJECT (action),
1200                 (GWeakNotify) gtk_widget_destroy, hbox);
1201
1202         return hbox;
1203 }
1204
1205 static void
1206 main_window_setup_balance (EmpathyMainWindow *window,
1207                            TpAccount         *account)
1208 {
1209         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1210         TpConnection *conn = tp_account_get_connection (account);
1211         GtkAction *action;
1212         const gchar *uri;
1213
1214         if (conn == NULL)
1215                 return;
1216
1217         if (!tp_proxy_is_prepared (conn, TP_CONNECTION_FEATURE_BALANCE))
1218                 return;
1219
1220         DEBUG ("Setting up balance for acct: %s",
1221                 tp_account_get_display_name (account));
1222
1223         /* create the action */
1224         action = main_window_setup_balance_create_action (window, account);
1225
1226         if (action == NULL)
1227                 return;
1228
1229         gtk_action_set_visible (priv->view_balance_show_in_roster, TRUE);
1230
1231         /* create the display widget */
1232         main_window_setup_balance_create_widget (window, action, account);
1233
1234         /* check the current balance and monitor for any changes */
1235         uri = tp_connection_get_balance_uri (conn);
1236
1237         g_object_set_data_full (G_OBJECT (action), "manage-credit-uri",
1238                 g_strdup (uri), g_free);
1239         gtk_action_set_sensitive (GTK_ACTION (action), !tp_str_empty (uri));
1240
1241         main_window_balance_update_balance (GTK_ACTION (action), conn);
1242
1243         g_signal_connect (conn, "balance-changed",
1244                 G_CALLBACK (main_window_balance_changed_cb), action);
1245
1246 }
1247
1248 static void
1249 main_window_remove_balance_action (EmpathyMainWindow *window,
1250                                    TpAccount         *account)
1251 {
1252         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1253         GtkAction *action;
1254         char *name;
1255         GList *a;
1256
1257         if (priv->balance_action_group == NULL)
1258                 return;
1259
1260         name = main_window_account_to_action_name (account);
1261
1262         action = gtk_action_group_get_action (
1263                 priv->balance_action_group, name);
1264
1265         if (action != NULL) {
1266                 guint merge_id;
1267
1268                 DEBUG ("Removing action");
1269
1270                 merge_id = GPOINTER_TO_UINT (g_object_get_data (
1271                         G_OBJECT (action),
1272                         "merge-id"));
1273
1274                 gtk_ui_manager_remove_ui (priv->ui_manager,
1275                         merge_id);
1276                 gtk_action_group_remove_action (
1277                         priv->balance_action_group, action);
1278         }
1279
1280         g_free (name);
1281
1282         a = gtk_action_group_list_actions (
1283                 priv->balance_action_group);
1284
1285         gtk_action_set_visible (
1286                 priv->view_balance_show_in_roster,
1287                 g_list_length (a) > 0);
1288
1289         g_list_free (a);
1290 }
1291
1292 static void
1293 main_window_connection_changed_cb (TpAccount  *account,
1294                                    guint       old_status,
1295                                    guint       current,
1296                                    guint       reason,
1297                                    gchar      *dbus_error_name,
1298                                    GHashTable *details,
1299                                    EmpathyMainWindow *window)
1300 {
1301         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1302
1303         main_window_update_status (window);
1304
1305         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
1306             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
1307                 main_window_error_display (window, account);
1308         }
1309
1310         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
1311                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
1312                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
1313
1314                 /* remove balance action if required */
1315                 main_window_remove_balance_action (window, account);
1316         }
1317
1318         if (current == TP_CONNECTION_STATUS_CONNECTED) {
1319                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
1320                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
1321
1322                 /* Account connected without error, remove error message if any */
1323                 main_window_remove_error (window, account);
1324                 main_window_setup_balance (window, account);
1325         }
1326 }
1327
1328 static void
1329 main_window_accels_load (void)
1330 {
1331         gchar *filename;
1332
1333         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
1334         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1335                 DEBUG ("Loading from:'%s'", filename);
1336                 gtk_accel_map_load (filename);
1337         }
1338
1339         g_free (filename);
1340 }
1341
1342 static void
1343 main_window_accels_save (void)
1344 {
1345         gchar *dir;
1346         gchar *file_with_path;
1347
1348         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
1349         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1350         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1351         g_free (dir);
1352
1353         DEBUG ("Saving to:'%s'", file_with_path);
1354         gtk_accel_map_save (file_with_path);
1355
1356         g_free (file_with_path);
1357 }
1358
1359 static void
1360 empathy_main_window_finalize (GObject *window)
1361 {
1362         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1363         GHashTableIter iter;
1364         gpointer key, value;
1365
1366         /* Save user-defined accelerators. */
1367         main_window_accels_save ();
1368
1369         g_list_free (priv->actions_connected);
1370
1371         g_object_unref (priv->account_manager);
1372         g_object_unref (priv->individual_store);
1373         g_object_unref (priv->contact_manager);
1374         g_object_unref (priv->sound_mgr);
1375         g_hash_table_unref (priv->errors);
1376         g_hash_table_unref (priv->auths);
1377
1378         /* disconnect all handlers of status-changed signal */
1379         g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1380         while (g_hash_table_iter_next (&iter, &key, &value))
1381                 g_signal_handler_disconnect (TP_ACCOUNT (key),
1382                                              GPOINTER_TO_UINT (value));
1383
1384         g_hash_table_unref (priv->status_changed_handlers);
1385
1386         g_signal_handlers_disconnect_by_func (priv->event_manager,
1387                                               main_window_event_added_cb,
1388                                               window);
1389         g_signal_handlers_disconnect_by_func (priv->event_manager,
1390                                               main_window_event_removed_cb,
1391                                               window);
1392         g_object_unref (priv->call_observer);
1393         g_object_unref (priv->event_manager);
1394         g_object_unref (priv->ui_manager);
1395         g_object_unref (priv->chatroom_manager);
1396
1397         g_object_unref (priv->gsettings_ui);
1398         g_object_unref (priv->gsettings_contacts);
1399
1400         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
1401 }
1402
1403 static gboolean
1404 main_window_key_press_event_cb  (GtkWidget   *window,
1405                                  GdkEventKey *event,
1406                                  gpointer     user_data)
1407 {
1408         if (event->keyval == GDK_KEY_T
1409             && event->state & GDK_SHIFT_MASK
1410             && event->state & GDK_CONTROL_MASK) {
1411                 empathy_chat_manager_call_undo_closed_chat ();
1412         }
1413         return FALSE;
1414 }
1415
1416 static void
1417 main_window_chat_quit_cb (GtkAction         *action,
1418                           EmpathyMainWindow *window)
1419 {
1420         gtk_widget_destroy (GTK_WIDGET (window));
1421 }
1422
1423 static void
1424 main_window_view_history_cb (GtkAction         *action,
1425                              EmpathyMainWindow *window)
1426 {
1427         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
1428 }
1429
1430 static void
1431 main_window_chat_new_message_cb (GtkAction         *action,
1432                                  EmpathyMainWindow *window)
1433 {
1434         empathy_new_message_dialog_show (GTK_WINDOW (window));
1435 }
1436
1437 static void
1438 main_window_chat_new_call_cb (GtkAction         *action,
1439                               EmpathyMainWindow *window)
1440 {
1441         empathy_new_call_dialog_show (GTK_WINDOW (window));
1442 }
1443
1444 static void
1445 main_window_chat_add_contact_cb (GtkAction         *action,
1446                                  EmpathyMainWindow *window)
1447 {
1448         empathy_new_individual_dialog_show (GTK_WINDOW (window));
1449 }
1450
1451 static void
1452 main_window_chat_search_contacts_cb (GtkAction         *action,
1453                                      EmpathyMainWindow *window)
1454 {
1455         GtkWidget *dialog = empathy_contact_search_dialog_new (
1456                         GTK_WINDOW (window));
1457         gtk_widget_show (dialog);
1458 }
1459
1460 static void
1461 main_window_view_show_ft_manager (GtkAction         *action,
1462                                   EmpathyMainWindow *window)
1463 {
1464         empathy_ft_manager_show ();
1465 }
1466
1467 static void
1468 main_window_view_show_offline_cb (GtkToggleAction   *action,
1469                                   EmpathyMainWindow *window)
1470 {
1471         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1472         gboolean current;
1473
1474         current = gtk_toggle_action_get_active (action);
1475         g_settings_set_boolean (priv->gsettings_ui,
1476                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
1477                                 current);
1478
1479         empathy_individual_view_set_show_offline (priv->individual_view,
1480                         current);
1481 }
1482
1483 static void
1484 main_window_notify_sort_contact_cb (GSettings         *gsettings,
1485                                     const gchar       *key,
1486                                     EmpathyMainWindow *window)
1487 {
1488         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1489         gchar *str;
1490
1491         str = g_settings_get_string (gsettings, key);
1492
1493         if (str != NULL) {
1494                 GType       type;
1495                 GEnumClass *enum_class;
1496                 GEnumValue *enum_value;
1497
1498                 type = empathy_individual_store_sort_get_type ();
1499                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1500                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1501                 if (enum_value) {
1502                         /* By changing the value of the GtkRadioAction,
1503                            it emits a signal that calls main_window_view_sort_contacts_cb
1504                            which updates the contacts list */
1505                         gtk_radio_action_set_current_value (priv->sort_by_name,
1506                                                             enum_value->value);
1507                 } else {
1508                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
1509                 }
1510                 g_free (str);
1511         }
1512 }
1513
1514 static void
1515 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
1516                                    GtkRadioAction    *current,
1517                                    EmpathyMainWindow *window)
1518 {
1519         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1520         EmpathyIndividualStoreSort value;
1521         GSList      *group;
1522         GType        type;
1523         GEnumClass  *enum_class;
1524         GEnumValue  *enum_value;
1525
1526         value = gtk_radio_action_get_current_value (action);
1527         group = gtk_radio_action_get_group (action);
1528
1529         /* Get string from index */
1530         type = empathy_individual_store_sort_get_type ();
1531         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1532         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
1533
1534         if (!enum_value) {
1535                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
1536                            g_slist_index (group, action));
1537         } else {
1538                 g_settings_set_string (priv->gsettings_contacts,
1539                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1540                                        enum_value->value_nick);
1541         }
1542         empathy_individual_store_set_sort_criterium (priv->individual_store,
1543                         value);
1544 }
1545
1546 static void
1547 main_window_view_show_protocols_cb (GtkToggleAction   *action,
1548                                     EmpathyMainWindow *window)
1549 {
1550         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1551         gboolean value;
1552
1553         value = gtk_toggle_action_get_active (action);
1554
1555         g_settings_set_boolean (priv->gsettings_ui,
1556                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1557                                 value);
1558         empathy_individual_store_set_show_protocols (priv->individual_store,
1559                                                      value);
1560 }
1561
1562 /* Matches GtkRadioAction values set in empathy-main-window.ui */
1563 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
1564 #define CONTACT_LIST_NORMAL_SIZE                        1
1565 #define CONTACT_LIST_COMPACT_SIZE                       2
1566
1567 static void
1568 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
1569                                         GtkRadioAction    *current,
1570                                         EmpathyMainWindow *window)
1571 {
1572         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1573         GSettings *gsettings_ui;
1574         gint value;
1575
1576         value = gtk_radio_action_get_current_value (action);
1577         /* create a new GSettings, so we can delay the setting until both
1578          * values are set */
1579         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1580
1581         DEBUG ("radio button toggled, value = %i", value);
1582
1583         g_settings_delay (gsettings_ui);
1584         g_settings_set_boolean (gsettings_ui,
1585                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
1586                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1587
1588         g_settings_set_boolean (gsettings_ui,
1589                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1590                                 value == CONTACT_LIST_COMPACT_SIZE);
1591         g_settings_apply (gsettings_ui);
1592
1593         /* FIXME: these enums probably have the wrong namespace */
1594         empathy_individual_store_set_show_avatars (priv->individual_store,
1595                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1596         empathy_individual_store_set_is_compact (priv->individual_store,
1597                         value == CONTACT_LIST_COMPACT_SIZE);
1598
1599         g_object_unref (gsettings_ui);
1600 }
1601
1602 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
1603                                                   const gchar       *key,
1604                                                   EmpathyMainWindow *window)
1605 {
1606         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1607
1608         gtk_toggle_action_set_active (priv->show_protocols,
1609                         g_settings_get_boolean (gsettings,
1610                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
1611 }
1612
1613
1614 static void
1615 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
1616                                          const gchar       *key,
1617                                          EmpathyMainWindow *window)
1618 {
1619         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1620         gint value;
1621
1622         if (g_settings_get_boolean (gsettings,
1623                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
1624                 value = CONTACT_LIST_COMPACT_SIZE;
1625         } else if (g_settings_get_boolean (gsettings,
1626                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
1627                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
1628         } else {
1629                 value = CONTACT_LIST_NORMAL_SIZE;
1630         }
1631
1632         DEBUG ("setting changed, value = %i", value);
1633
1634         /* By changing the value of the GtkRadioAction,
1635            it emits a signal that calls main_window_view_contacts_list_size_cb
1636            which updates the contacts list */
1637         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
1638 }
1639
1640 static void
1641 main_window_edit_search_contacts_cb (GtkCheckMenuItem  *item,
1642                                      EmpathyMainWindow *window)
1643 {
1644         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1645
1646         empathy_individual_view_start_search (priv->individual_view);
1647 }
1648
1649 static void
1650 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
1651                               EmpathyMainWindow *window)
1652 {
1653 #ifdef HAVE_LIBCHAMPLAIN
1654         empathy_map_view_show ();
1655 #endif
1656 }
1657
1658 static void
1659 join_chatroom (EmpathyChatroom *chatroom,
1660                gint64 timestamp)
1661 {
1662         TpAccount      *account;
1663         const gchar    *room;
1664
1665         account = empathy_chatroom_get_account (chatroom);
1666         room = empathy_chatroom_get_room (chatroom);
1667
1668         DEBUG ("Requesting channel for '%s'", room);
1669         empathy_join_muc (account, room, timestamp);
1670 }
1671
1672 typedef struct
1673 {
1674         TpAccount *account;
1675         EmpathyChatroom *chatroom;
1676         gint64 timestamp;
1677         glong sig_id;
1678         guint timeout;
1679 } join_fav_account_sig_ctx;
1680
1681 static join_fav_account_sig_ctx *
1682 join_fav_account_sig_ctx_new (TpAccount *account,
1683                              EmpathyChatroom *chatroom,
1684                               gint64 timestamp)
1685 {
1686         join_fav_account_sig_ctx *ctx = g_slice_new0 (
1687                 join_fav_account_sig_ctx);
1688
1689         ctx->account = g_object_ref (account);
1690         ctx->chatroom = g_object_ref (chatroom);
1691         ctx->timestamp = timestamp;
1692         return ctx;
1693 }
1694
1695 static void
1696 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1697 {
1698         g_object_unref (ctx->account);
1699         g_object_unref (ctx->chatroom);
1700         g_slice_free (join_fav_account_sig_ctx, ctx);
1701 }
1702
1703 static void
1704 account_status_changed_cb (TpAccount  *account,
1705                            TpConnectionStatus old_status,
1706                            TpConnectionStatus new_status,
1707                            guint reason,
1708                            gchar *dbus_error_name,
1709                            GHashTable *details,
1710                            gpointer user_data)
1711 {
1712         join_fav_account_sig_ctx *ctx = user_data;
1713
1714         switch (new_status) {
1715                 case TP_CONNECTION_STATUS_DISCONNECTED:
1716                         /* Don't wait any longer */
1717                         goto finally;
1718                         break;
1719
1720                 case TP_CONNECTION_STATUS_CONNECTING:
1721                         /* Wait a bit */
1722                         return;
1723
1724                 case TP_CONNECTION_STATUS_CONNECTED:
1725                         /* We can join the room */
1726                         break;
1727
1728                 default:
1729                         g_assert_not_reached ();
1730         }
1731
1732         join_chatroom (ctx->chatroom, ctx->timestamp);
1733
1734 finally:
1735         g_source_remove (ctx->timeout);
1736         g_signal_handler_disconnect (account, ctx->sig_id);
1737 }
1738
1739 #define JOIN_FAVORITE_TIMEOUT 5
1740
1741 static gboolean
1742 join_favorite_timeout_cb (gpointer data)
1743 {
1744         join_fav_account_sig_ctx *ctx = data;
1745
1746         /* stop waiting for joining the favorite room */
1747         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1748         return FALSE;
1749 }
1750
1751 static void
1752 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1753 {
1754         TpAccount      *account;
1755
1756         account = empathy_chatroom_get_account (chatroom);
1757         if (tp_account_get_connection_status (account, NULL) !=
1758                                              TP_CONNECTION_STATUS_CONNECTED) {
1759                 join_fav_account_sig_ctx *ctx;
1760
1761                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1762                         empathy_get_current_action_time ());
1763
1764                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1765                         G_CALLBACK (account_status_changed_cb), ctx,
1766                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1767
1768                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1769                         join_favorite_timeout_cb, ctx);
1770                 return;
1771         }
1772
1773         join_chatroom (chatroom, empathy_get_current_action_time ());
1774 }
1775
1776 static void
1777 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1778                                                 EmpathyChatroom *chatroom)
1779 {
1780         main_window_favorite_chatroom_join (chatroom);
1781 }
1782
1783 static void
1784 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1785                                         EmpathyChatroom   *chatroom)
1786 {
1787         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1788         GtkWidget   *menu_item;
1789         const gchar *name, *account_name;
1790         gchar *label;
1791
1792
1793         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1794                 return;
1795         }
1796
1797         name = empathy_chatroom_get_name (chatroom);
1798         account_name = tp_account_get_display_name (
1799                         empathy_chatroom_get_account (chatroom));
1800         label = g_strdup_printf ("%s (%s)", name, account_name);
1801         menu_item = gtk_menu_item_new_with_label (label);
1802         g_free (label);
1803         g_object_set_data (G_OBJECT (menu_item), "is_favorite",
1804                         GUINT_TO_POINTER (TRUE));
1805
1806         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1807         g_signal_connect (menu_item, "activate",
1808                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1809                           chatroom);
1810
1811         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1812                                menu_item, 4);
1813
1814         gtk_widget_show (menu_item);
1815 }
1816
1817 static void
1818 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1819                                              EmpathyChatroom        *chatroom,
1820                                              EmpathyMainWindow      *window)
1821 {
1822         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1823
1824         main_window_favorite_chatroom_menu_add (window, chatroom);
1825         gtk_widget_show (priv->room_separator);
1826         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1827 }
1828
1829 static void
1830 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1831                                                EmpathyChatroom        *chatroom,
1832                                                EmpathyMainWindow      *window)
1833 {
1834         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1835         GtkWidget *menu_item;
1836         GList *chatrooms;
1837
1838         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1839         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1840         gtk_widget_destroy (menu_item);
1841
1842         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1843         if (chatrooms) {
1844                 gtk_widget_show (priv->room_separator);
1845         } else {
1846                 gtk_widget_hide (priv->room_separator);
1847         }
1848
1849         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1850         g_list_free (chatrooms);
1851 }
1852
1853 static void
1854 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1855 {
1856         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1857         GList *chatrooms, *l;
1858         GtkWidget *room;
1859
1860         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1861         chatrooms = empathy_chatroom_manager_get_chatrooms (
1862                 priv->chatroom_manager, NULL);
1863         room = gtk_ui_manager_get_widget (priv->ui_manager,
1864                 "/menubar/room");
1865         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1866         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1867                 "/menubar/room/room_separator");
1868
1869         for (l = chatrooms; l; l = l->next) {
1870                 main_window_favorite_chatroom_menu_add (window, l->data);
1871         }
1872
1873         if (!chatrooms) {
1874                 gtk_widget_hide (priv->room_separator);
1875         }
1876
1877         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1878
1879         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1880                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1881                           window);
1882         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1883                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1884                           window);
1885
1886         g_list_free (chatrooms);
1887 }
1888
1889 static void
1890 main_window_room_join_new_cb (GtkAction         *action,
1891                               EmpathyMainWindow *window)
1892 {
1893         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1894 }
1895
1896 static void
1897 main_window_room_join_favorites_cb (GtkAction         *action,
1898                                     EmpathyMainWindow *window)
1899 {
1900         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1901         GList *chatrooms, *l;
1902
1903         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1904         for (l = chatrooms; l; l = l->next) {
1905                 main_window_favorite_chatroom_join (l->data);
1906         }
1907         g_list_free (chatrooms);
1908 }
1909
1910 static void
1911 main_window_room_manage_favorites_cb (GtkAction         *action,
1912                                       EmpathyMainWindow *window)
1913 {
1914         empathy_chatrooms_window_show (GTK_WINDOW (window));
1915 }
1916
1917 static void
1918 main_window_edit_cb (GtkAction         *action,
1919                      EmpathyMainWindow *window)
1920 {
1921         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1922         GtkWidget *submenu;
1923
1924         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1925         submenu = empathy_individual_view_get_individual_menu (
1926                         priv->individual_view);
1927         if (submenu) {
1928                 GtkMenuItem *item;
1929                 GtkWidget   *label;
1930
1931                 item = GTK_MENU_ITEM (priv->edit_context);
1932                 label = gtk_bin_get_child (GTK_BIN (item));
1933                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1934
1935                 gtk_widget_show (priv->edit_context);
1936                 gtk_widget_show (priv->edit_context_separator);
1937
1938                 gtk_menu_item_set_submenu (item, submenu);
1939
1940                 return;
1941         }
1942
1943         submenu = empathy_individual_view_get_group_menu (
1944                         priv->individual_view);
1945         if (submenu) {
1946                 GtkMenuItem *item;
1947                 GtkWidget   *label;
1948
1949                 item = GTK_MENU_ITEM (priv->edit_context);
1950                 label = gtk_bin_get_child (GTK_BIN (item));
1951                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1952
1953                 gtk_widget_show (priv->edit_context);
1954                 gtk_widget_show (priv->edit_context_separator);
1955
1956                 gtk_menu_item_set_submenu (item, submenu);
1957
1958                 return;
1959         }
1960
1961         gtk_widget_hide (priv->edit_context);
1962         gtk_widget_hide (priv->edit_context_separator);
1963
1964         return;
1965 }
1966
1967 static void
1968 main_window_edit_accounts_cb (GtkAction         *action,
1969                               EmpathyMainWindow *window)
1970 {
1971         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1972                         NULL, FALSE, FALSE);
1973 }
1974
1975 static void
1976 main_window_edit_personal_information_cb (GtkAction         *action,
1977                                           EmpathyMainWindow *window)
1978 {
1979         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1980 }
1981
1982 static void
1983 main_window_edit_blocked_contacts_cb (GtkAction         *action,
1984                                       EmpathyMainWindow *window)
1985 {
1986         GtkWidget *dialog;
1987
1988         dialog = empathy_contact_blocking_dialog_new (GTK_WINDOW (window));
1989         gtk_widget_show (dialog);
1990         g_signal_connect (dialog, "response",
1991                         G_CALLBACK (gtk_widget_destroy), NULL);
1992 }
1993
1994 void
1995 empathy_main_window_show_preferences (EmpathyMainWindow *window,
1996     const gchar *tab)
1997 {
1998         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1999
2000         if (priv->preferences == NULL) {
2001                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window),
2002                                                              priv->shell_running);
2003                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
2004                                            (gpointer) &priv->preferences);
2005
2006                 gtk_widget_show (priv->preferences);
2007         } else {
2008                 gtk_window_present (GTK_WINDOW (priv->preferences));
2009         }
2010
2011         if (tab != NULL)
2012                 empathy_preferences_show_tab (
2013                         EMPATHY_PREFERENCES (priv->preferences), tab);
2014 }
2015
2016 static void
2017 main_window_edit_preferences_cb (GtkAction         *action,
2018                                  EmpathyMainWindow *window)
2019 {
2020         empathy_main_window_show_preferences (window, NULL);
2021 }
2022
2023 static void
2024 main_window_help_about_cb (GtkAction         *action,
2025                            EmpathyMainWindow *window)
2026 {
2027         empathy_about_dialog_new (GTK_WINDOW (window));
2028 }
2029
2030 static void
2031 main_window_help_debug_cb (GtkAction         *action,
2032                            EmpathyMainWindow *window)
2033 {
2034         empathy_launch_program (BIN_DIR, "empathy-debugger", NULL);
2035 }
2036
2037 static void
2038 main_window_help_contents_cb (GtkAction         *action,
2039                               EmpathyMainWindow *window)
2040 {
2041         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
2042 }
2043
2044 static gboolean
2045 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
2046                                             GdkEventButton    *event,
2047                                             EmpathyMainWindow *window)
2048 {
2049         if (event->type != GDK_BUTTON_PRESS ||
2050             event->button != 1) {
2051                 return FALSE;
2052         }
2053
2054         empathy_accounts_dialog_show_application (
2055                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
2056                         NULL, FALSE, FALSE);
2057
2058         return FALSE;
2059 }
2060
2061 static void
2062 main_window_account_removed_cb (TpAccountManager  *manager,
2063                                 TpAccount         *account,
2064                                 EmpathyMainWindow *window)
2065 {
2066         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2067         GList *a;
2068
2069         a = tp_account_manager_get_valid_accounts (manager);
2070
2071         gtk_action_set_sensitive (priv->view_history,
2072                 g_list_length (a) > 0);
2073
2074         g_list_free (a);
2075
2076         /* remove errors if any */
2077         main_window_remove_error (window, account);
2078
2079         /* remove the balance action if required */
2080         main_window_remove_balance_action (window, account);
2081 }
2082
2083 static void
2084 main_window_account_validity_changed_cb (TpAccountManager  *manager,
2085                                          TpAccount         *account,
2086                                          gboolean           valid,
2087                                          EmpathyMainWindow *window)
2088 {
2089         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2090
2091         if (valid) {
2092                 gulong handler_id;
2093                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
2094                         priv->status_changed_handlers, account));
2095
2096                 /* connect signal only if it was not connected yet */
2097                 if (handler_id == 0) {
2098                         handler_id = g_signal_connect (account,
2099                                 "status-changed",
2100                                 G_CALLBACK (main_window_connection_changed_cb),
2101                                 window);
2102                         g_hash_table_insert (priv->status_changed_handlers,
2103                                 account, GUINT_TO_POINTER (handler_id));
2104                 }
2105         }
2106
2107         main_window_account_removed_cb (manager, account, window);
2108 }
2109
2110 static void
2111 main_window_notify_show_offline_cb (GSettings   *gsettings,
2112                                     const gchar *key,
2113                                     gpointer     toggle_action)
2114 {
2115         gtk_toggle_action_set_active (toggle_action,
2116                         g_settings_get_boolean (gsettings, key));
2117 }
2118
2119 static void
2120 main_window_connection_items_setup (EmpathyMainWindow *window,
2121                                     GtkBuilder        *gui)
2122 {
2123         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2124         GList         *list;
2125         GObject       *action;
2126         guint          i;
2127         const gchar *actions_connected[] = {
2128                 "room_join_new",
2129                 "room_join_favorites",
2130                 "chat_new_message",
2131                 "chat_new_call",
2132                 "chat_search_contacts",
2133                 "chat_add_contact",
2134                 "edit_personal_information",
2135                 "edit_blocked_contacts",
2136                 "edit_search_contacts"
2137         };
2138
2139         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
2140                 action = gtk_builder_get_object (gui, actions_connected[i]);
2141                 list = g_list_prepend (list, action);
2142         }
2143
2144         priv->actions_connected = list;
2145 }
2146
2147 static void
2148 account_manager_prepared_cb (GObject      *source_object,
2149                              GAsyncResult *result,
2150                              gpointer      user_data)
2151 {
2152         GList *accounts, *j;
2153         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2154         EmpathyMainWindow *window = user_data;
2155         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2156         GError *error = NULL;
2157
2158         if (!tp_proxy_prepare_finish (manager, result, &error)) {
2159                 DEBUG ("Failed to prepare account manager: %s", error->message);
2160                 g_error_free (error);
2161                 return;
2162         }
2163
2164         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
2165         for (j = accounts; j != NULL; j = j->next) {
2166                 TpAccount *account = TP_ACCOUNT (j->data);
2167                 gulong handler_id;
2168
2169                 handler_id = g_signal_connect (account, "status-changed",
2170                                   G_CALLBACK (main_window_connection_changed_cb),
2171                                   window);
2172                 g_hash_table_insert (priv->status_changed_handlers,
2173                                      account, GUINT_TO_POINTER (handler_id));
2174
2175                 main_window_setup_balance (window, account);
2176         }
2177
2178         g_signal_connect (manager, "account-validity-changed",
2179                           G_CALLBACK (main_window_account_validity_changed_cb),
2180                           window);
2181
2182         main_window_update_status (window);
2183
2184         /* Disable the "Previous Conversations" menu entry if there is no account */
2185         gtk_action_set_sensitive (priv->view_history,
2186                 g_list_length (accounts) > 0);
2187
2188         g_list_free (accounts);
2189 }
2190
2191 void
2192 empathy_main_window_set_shell_running (EmpathyMainWindow *window,
2193                                        gboolean          shell_running)
2194 {
2195         EmpathyMainWindowPriv *priv = GET_PRIV (window);
2196
2197         if (priv->shell_running == shell_running)
2198                 return;
2199
2200         priv->shell_running = shell_running;
2201         g_object_notify (G_OBJECT (window), "shell-running");
2202 }
2203
2204 static GObject *
2205 empathy_main_window_constructor (GType type,
2206                                  guint n_construct_params,
2207                                  GObjectConstructParam *construct_params)
2208 {
2209         static GObject *window = NULL;
2210
2211         if (window != NULL)
2212                 return g_object_ref (window);
2213
2214         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
2215                 type, n_construct_params, construct_params);
2216
2217         g_object_add_weak_pointer (window, (gpointer) &window);
2218
2219         return window;
2220 }
2221
2222 static void
2223 empathy_main_window_set_property (GObject       *object,
2224                                   guint         property_id,
2225                                   const GValue *value,
2226                                   GParamSpec    *pspec)
2227 {
2228         EmpathyMainWindow *self = EMPATHY_MAIN_WINDOW (object);
2229         EmpathyMainWindowPriv *priv = GET_PRIV (self);
2230
2231         switch (property_id)
2232         {
2233                 case PROP_SHELL_RUNNING:
2234                         priv->shell_running = g_value_get_boolean (value);
2235                         break;
2236                 default:
2237                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2238                         break;
2239         }
2240 }
2241
2242 static void
2243 empathy_main_window_get_property (GObject    *object,
2244                                   guint      property_id,
2245                                   GValue     *value,
2246                                   GParamSpec *pspec)
2247 {
2248         EmpathyMainWindow *self = EMPATHY_MAIN_WINDOW (object);
2249         EmpathyMainWindowPriv *priv = GET_PRIV (self);
2250
2251         switch (property_id)
2252         {
2253                 case PROP_SHELL_RUNNING:
2254                         g_value_set_boolean (value, priv->shell_running);
2255                         break;
2256                 default:
2257                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2258                         break;
2259         }
2260 }
2261
2262 static void
2263 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
2264 {
2265         GObjectClass *object_class = G_OBJECT_CLASS (klass);
2266         GParamSpec *pspec;
2267
2268         object_class->finalize = empathy_main_window_finalize;
2269         object_class->constructor = empathy_main_window_constructor;
2270
2271         object_class->set_property = empathy_main_window_set_property;
2272         object_class->get_property = empathy_main_window_get_property;
2273
2274         pspec = g_param_spec_boolean ("shell-running",
2275                                       "Shell running",
2276                                       "Whether the Shell is running or not",
2277                                       FALSE,
2278                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2279         g_object_class_install_property (object_class, PROP_SHELL_RUNNING, pspec);
2280
2281         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
2282 }
2283
2284 static void
2285 empathy_main_window_init (EmpathyMainWindow *window)
2286 {
2287         EmpathyMainWindowPriv    *priv;
2288         EmpathyIndividualManager *individual_manager;
2289         GtkBuilder               *gui, *gui_mgr;
2290         GtkWidget                *sw;
2291         GtkToggleAction          *show_offline_widget;
2292         GtkAction                *show_map_widget;
2293         GtkToolItem              *item;
2294         gboolean                  show_offline;
2295         gchar                    *filename;
2296         GtkTreeModel             *model;
2297         GtkWidget                *search_vbox;
2298         GtkWidget                *menubar;
2299
2300         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
2301                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
2302
2303         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2304         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
2305
2306         priv->sound_mgr = empathy_sound_manager_dup_singleton ();
2307
2308         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
2309         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
2310         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
2311
2312         /* don't finalize the widget on delete-event, just hide it */
2313         g_signal_connect (window, "delete-event",
2314                 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
2315
2316         /* Set up interface */
2317         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
2318         gui = empathy_builder_get_file (filename,
2319                                        "main_vbox", &priv->main_vbox,
2320                                        "balance_vbox", &priv->balance_vbox,
2321                                        "errors_vbox", &priv->errors_vbox,
2322                                        "auth_vbox", &priv->auth_vbox,
2323                                        "search_vbox", &search_vbox,
2324                                        "presence_toolbar", &priv->presence_toolbar,
2325                                        "notebook", &priv->notebook,
2326                                        "no_entry_label", &priv->no_entry_label,
2327                                        "roster_scrolledwindow", &sw,
2328                                        NULL);
2329         g_free (filename);
2330
2331         /* Set UI manager */
2332         filename = empathy_file_lookup ("empathy-main-window-menubar.ui", "src");
2333         gui_mgr = empathy_builder_get_file (filename,
2334                                        "ui_manager", &priv->ui_manager,
2335                                        "view_show_offline", &show_offline_widget,
2336                                        "view_show_protocols", &priv->show_protocols,
2337                                        "view_sort_by_name", &priv->sort_by_name,
2338                                        "view_sort_by_status", &priv->sort_by_status,
2339                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
2340                                        "view_normal_size", &priv->normal_size,
2341                                        "view_compact_size", &priv->compact_size,
2342                                        "view_history", &priv->view_history,
2343                                        "view_show_map", &show_map_widget,
2344                                        "room_join_favorites", &priv->room_join_favorites,
2345                                        "view_balance_show_in_roster", &priv->view_balance_show_in_roster,
2346                                        "menubar", &menubar,
2347                                        NULL);
2348         g_free (filename);
2349
2350         /* The UI manager is living in its own .ui file as Glade doesn't support
2351          * those. The GtkMenubar has to be in this file as well to we manually add
2352          * it to the first position of the vbox. */
2353         gtk_box_pack_start (GTK_BOX (priv->main_vbox), menubar, FALSE, FALSE, 0);
2354         gtk_box_reorder_child (GTK_BOX (priv->main_vbox), menubar, 0);
2355
2356         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
2357         gtk_widget_show (priv->main_vbox);
2358
2359         g_signal_connect (window, "key-press-event",
2360                           G_CALLBACK (main_window_key_press_event_cb), NULL);
2361
2362         empathy_builder_connect (gui_mgr, window,
2363                               "chat_quit", "activate", main_window_chat_quit_cb,
2364                               "chat_new_message", "activate", main_window_chat_new_message_cb,
2365                               "chat_new_call", "activate", main_window_chat_new_call_cb,
2366                               "view_history", "activate", main_window_view_history_cb,
2367                               "room_join_new", "activate", main_window_room_join_new_cb,
2368                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
2369                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
2370                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
2371                               "chat_search_contacts", "activate", main_window_chat_search_contacts_cb,
2372                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
2373                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
2374                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
2375                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
2376                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
2377                               "view_show_map", "activate", main_window_view_show_map_cb,
2378                               "edit", "activate", main_window_edit_cb,
2379                               "edit_accounts", "activate", main_window_edit_accounts_cb,
2380                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
2381                               "edit_blocked_contacts", "activate", main_window_edit_blocked_contacts_cb,
2382                               "edit_preferences", "activate", main_window_edit_preferences_cb,
2383                               "edit_search_contacts", "activate", main_window_edit_search_contacts_cb,
2384                               "help_about", "activate", main_window_help_about_cb,
2385                               "help_debug", "activate", main_window_help_debug_cb,
2386                               "help_contents", "activate", main_window_help_contents_cb,
2387                               NULL);
2388
2389         /* Set up connection related widgets. */
2390         main_window_connection_items_setup (window, gui_mgr);
2391
2392         g_object_ref (priv->ui_manager);
2393         g_object_unref (gui);
2394         g_object_unref (gui_mgr);
2395
2396 #ifndef HAVE_LIBCHAMPLAIN
2397         gtk_action_set_visible (show_map_widget, FALSE);
2398 #endif
2399
2400         priv->account_manager = tp_account_manager_dup ();
2401
2402         tp_proxy_prepare_async (priv->account_manager, NULL,
2403                                           account_manager_prepared_cb, window);
2404
2405         priv->errors = g_hash_table_new_full (g_direct_hash,
2406                                               g_direct_equal,
2407                                               g_object_unref,
2408                                               NULL);
2409
2410         priv->auths = g_hash_table_new (NULL, NULL);
2411
2412         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
2413                                                                g_direct_equal,
2414                                                                NULL,
2415                                                                NULL);
2416
2417         /* Set up menu */
2418         main_window_favorite_chatroom_menu_setup (window);
2419
2420         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
2421                 "/menubar/edit/edit_context");
2422         priv->edit_context_separator = gtk_ui_manager_get_widget (
2423                 priv->ui_manager,
2424                 "/menubar/edit/edit_context_separator");
2425         gtk_widget_hide (priv->edit_context);
2426         gtk_widget_hide (priv->edit_context_separator);
2427
2428         /* Set up contact list. */
2429         empathy_status_presets_get_all ();
2430
2431         /* Set up presence chooser */
2432         priv->presence_chooser = empathy_presence_chooser_new ();
2433         gtk_widget_show (priv->presence_chooser);
2434         item = gtk_tool_item_new ();
2435         gtk_widget_show (GTK_WIDGET (item));
2436         gtk_widget_set_size_request (priv->presence_chooser, 10, -1);
2437         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
2438         gtk_tool_item_set_is_important (item, TRUE);
2439         gtk_tool_item_set_expand (item, TRUE);
2440         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
2441
2442         /* Set up the throbber */
2443         priv->throbber = gtk_spinner_new ();
2444         gtk_widget_set_size_request (priv->throbber, 16, -1);
2445         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
2446         g_signal_connect (priv->throbber, "button-press-event",
2447                 G_CALLBACK (main_window_throbber_button_press_event_cb),
2448                 window);
2449         gtk_widget_show (priv->throbber);
2450
2451         item = gtk_tool_item_new ();
2452         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
2453         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
2454         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
2455         priv->throbber_tool_item = GTK_WIDGET (item);
2456
2457         /* XXX: this class is designed to live for the duration of the program,
2458          * so it's got a race condition between its signal handlers and its
2459          * finalization. The class is planned to be removed, so we won't fix
2460          * this before then. */
2461         priv->contact_manager = EMPATHY_CONTACT_LIST (
2462                         empathy_contact_manager_dup_singleton ());
2463         individual_manager = empathy_individual_manager_dup_singleton ();
2464         priv->individual_store = empathy_individual_store_new (
2465                         individual_manager);
2466         g_object_unref (individual_manager);
2467
2468         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
2469          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
2470          * drop, so allowing them would achieve nothing except confusion. */
2471         priv->individual_view = empathy_individual_view_new (
2472                         priv->individual_store,
2473                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
2474                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
2475
2476         gtk_widget_show (GTK_WIDGET (priv->individual_view));
2477         gtk_container_add (GTK_CONTAINER (sw),
2478                            GTK_WIDGET (priv->individual_view));
2479         g_signal_connect (priv->individual_view, "row-activated",
2480                           G_CALLBACK (main_window_row_activated_cb),
2481                           window);
2482
2483         /* Set up search bar */
2484         priv->search_bar = empathy_live_search_new (
2485                 GTK_WIDGET (priv->individual_view));
2486         empathy_individual_view_set_live_search (priv->individual_view,
2487                 EMPATHY_LIVE_SEARCH (priv->search_bar));
2488         gtk_box_pack_start (GTK_BOX (search_vbox), priv->search_bar,
2489                 FALSE, TRUE, 0);
2490
2491         g_signal_connect_swapped (window, "map",
2492                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
2493
2494         /* Connect to proper signals to check if contact list is empty or not */
2495         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
2496         priv->empty = TRUE;
2497         g_signal_connect (model, "row-inserted",
2498                           G_CALLBACK (main_window_row_inserted_cb),
2499                           window);
2500         g_signal_connect (model, "row-deleted",
2501                           G_CALLBACK (main_window_row_deleted_cb),
2502                           window);
2503
2504         /* Load user-defined accelerators. */
2505         main_window_accels_load ();
2506
2507         /* Set window size. */
2508         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
2509
2510         /* bind view_balance_show_in_roster */
2511         g_settings_bind (priv->gsettings_ui, "show-balance-in-roster",
2512                 priv->view_balance_show_in_roster, "active",
2513                 G_SETTINGS_BIND_DEFAULT);
2514         g_object_bind_property (priv->view_balance_show_in_roster, "active",
2515                 priv->balance_vbox, "visible",
2516                 G_BINDING_SYNC_CREATE);
2517
2518         /* Enable event handling */
2519         priv->call_observer = empathy_call_observer_dup_singleton ();
2520         priv->event_manager = empathy_event_manager_dup_singleton ();
2521
2522         g_signal_connect (priv->event_manager, "event-added",
2523                           G_CALLBACK (main_window_event_added_cb), window);
2524         g_signal_connect (priv->event_manager, "event-removed",
2525                           G_CALLBACK (main_window_event_removed_cb), window);
2526         g_signal_connect (priv->account_manager, "account-validity-changed",
2527                           G_CALLBACK (main_window_account_validity_changed_cb),
2528                           window);
2529         g_signal_connect (priv->account_manager, "account-removed",
2530                           G_CALLBACK (main_window_account_removed_cb),
2531                           window);
2532         g_signal_connect (priv->account_manager, "account-disabled",
2533                           G_CALLBACK (main_window_account_disabled_cb),
2534                           window);
2535
2536         /* Show offline ? */
2537         show_offline = g_settings_get_boolean (priv->gsettings_ui,
2538                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
2539         g_signal_connect (priv->gsettings_ui,
2540                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
2541                           G_CALLBACK (main_window_notify_show_offline_cb),
2542                           show_offline_widget);
2543
2544         gtk_toggle_action_set_active (show_offline_widget, show_offline);
2545
2546         /* Show protocol ? */
2547         g_signal_connect (priv->gsettings_ui,
2548                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2549                           G_CALLBACK (main_window_notify_show_protocols_cb),
2550                           window);
2551
2552         main_window_notify_show_protocols_cb (priv->gsettings_ui,
2553                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2554                                               window);
2555
2556         /* Sort by name / by status ? */
2557         g_signal_connect (priv->gsettings_contacts,
2558                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2559                           G_CALLBACK (main_window_notify_sort_contact_cb),
2560                           window);
2561
2562         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
2563                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2564                                             window);
2565
2566         /* Contacts list size */
2567         g_signal_connect (priv->gsettings_ui,
2568                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
2569                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2570                           window);
2571         g_signal_connect (priv->gsettings_ui,
2572                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
2573                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2574                           window);
2575
2576         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
2577                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
2578                                                  window);
2579 }
2580
2581 GtkWidget *
2582 empathy_main_window_dup (void)
2583 {
2584         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
2585 }