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