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