Port EmpathyMainWindow to new API
[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-2008 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <config.h>
25
26 #include <sys/stat.h>
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29
30 #include <libempathy/empathy-contact.h>
31 #include <libempathy/empathy-utils.h>
32 #include <libempathy/empathy-account-manager.h>
33 #include <libempathy/empathy-dispatcher.h>
34 #include <libempathy/empathy-chatroom-manager.h>
35 #include <libempathy/empathy-chatroom.h>
36 #include <libempathy/empathy-contact-list.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-contact-factory.h>
39 #include <libempathy/empathy-status-presets.h>
40
41 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include <libempathy-gtk/empathy-contact-list-store.h>
43 #include <libempathy-gtk/empathy-contact-list-view.h>
44 #include <libempathy-gtk/empathy-presence-chooser.h>
45 #include <libempathy-gtk/empathy-ui-utils.h>
46 #include <libempathy-gtk/empathy-geometry.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-log-window.h>
49 #include <libempathy-gtk/empathy-new-message-dialog.h>
50 #include <libempathy-gtk/empathy-gtk-enum-types.h>
51
52 #include <libmissioncontrol/mission-control.h>
53
54 #include "empathy-accounts-dialog.h"
55 #include "empathy-main-window.h"
56 #include "ephy-spinner.h"
57 #include "empathy-preferences.h"
58 #include "empathy-about-dialog.h"
59 #include "empathy-new-chatroom-dialog.h"
60 #include "empathy-chatrooms-window.h"
61 #include "empathy-event-manager.h"
62
63 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
64 #include <libempathy/empathy-debug.h>
65
66 /* Flashing delay for icons (milliseconds). */
67 #define FLASH_TIMEOUT 500
68
69 /* Minimum width of roster window if something goes wrong. */
70 #define MIN_WIDTH 50
71
72 /* Accels (menu shortcuts) can be configured and saved */
73 #define ACCELS_FILENAME "accels.txt"
74
75 /* Name in the geometry file */
76 #define GEOMETRY_NAME "main-window"
77
78 typedef struct {
79         EmpathyContactListView  *list_view;
80         EmpathyContactListStore *list_store;
81         MissionControl          *mc;
82         EmpathyAccountManager   *account_manager;
83         EmpathyChatroomManager  *chatroom_manager;
84         EmpathyEventManager     *event_manager;
85         guint                    flash_timeout_id;
86         gboolean                 flash_on;
87
88         GtkWidget              *window;
89         GtkWidget              *main_vbox;
90         GtkWidget              *throbber;
91         GtkWidget              *presence_toolbar;
92         GtkWidget              *presence_chooser;
93         GtkWidget              *errors_vbox;
94
95         GtkUIManager           *ui_manager;
96         GtkAction              *chat_history;
97         GtkAction              *room_join_favorites;
98         GtkWidget              *room_menu;
99         GtkWidget              *room_separator;
100         GtkWidget              *edit_context;
101         GtkWidget              *edit_context_separator;
102
103         guint                   size_timeout_id;
104         GHashTable             *errors;
105
106         /* Actions that are enabled when there are connected accounts */
107         GList                  *actions_connected;
108 } EmpathyMainWindow;
109
110 static EmpathyMainWindow *window = NULL;
111
112 static void
113 main_window_flash_stop (EmpathyMainWindow *window)
114 {
115         if (window->flash_timeout_id == 0) {
116                 return;
117         }
118
119         DEBUG ("Stop flashing");
120         g_source_remove (window->flash_timeout_id);
121         window->flash_timeout_id = 0;
122         window->flash_on = FALSE;
123 }
124
125 typedef struct {
126         EmpathyEvent *event;
127         gboolean      on;
128 } FlashForeachData;
129
130 static gboolean
131 main_window_flash_foreach (GtkTreeModel *model,
132                            GtkTreePath  *path,
133                            GtkTreeIter  *iter,
134                            gpointer      user_data)
135 {
136         FlashForeachData *data = (FlashForeachData*) user_data;
137         EmpathyContact   *contact;
138         const gchar      *icon_name;
139         GtkTreePath      *parent_path = NULL;
140         GtkTreeIter       parent_iter;
141
142         /* To be used with gtk_tree_model_foreach, update the status icon
143          * of the contact to show the event icon (on=TRUE) or the presence
144          * (on=FALSE) */
145         gtk_tree_model_get (model, iter,
146                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
147                             -1);
148
149         if (contact != data->event->contact) {
150                 if (contact) {
151                         g_object_unref (contact);
152                 }
153                 return FALSE;
154         }
155
156         if (data->on) {
157                 icon_name = data->event->icon_name;
158         } else {
159                 icon_name = empathy_icon_name_for_contact (contact);
160         }
161
162         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
163                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
164                             -1);
165
166         /* To make sure the parent is shown correctly, we emit
167          * the row-changed signal on the parent so it prompts
168          * it to be refreshed by the filter func. 
169          */
170         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
171                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
172         }
173         if (parent_path) {
174                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
175                 gtk_tree_path_free (parent_path);
176         }
177
178         g_object_unref (contact);
179
180         return FALSE;
181 }
182
183 static gboolean
184 main_window_flash_cb (EmpathyMainWindow *window)
185 {
186         GtkTreeModel     *model;
187         GSList           *events, *l;
188         gboolean          found_event = FALSE;
189         FlashForeachData  data;
190
191         window->flash_on = !window->flash_on;
192         data.on = window->flash_on;
193         model = GTK_TREE_MODEL (window->list_store);
194
195         events = empathy_event_manager_get_events (window->event_manager);
196         for (l = events; l; l = l->next) {
197                 data.event = l->data;
198                 if (!data.event->contact) {
199                         continue;
200                 }
201
202                 found_event = TRUE;
203                 gtk_tree_model_foreach (model,
204                                         main_window_flash_foreach,
205                                         &data);
206         }
207
208         if (!found_event) {
209                 main_window_flash_stop (window);
210         }
211
212         return TRUE;
213 }
214
215 static void
216 main_window_flash_start (EmpathyMainWindow *window)
217 {
218         if (window->flash_timeout_id != 0) {
219                 return;
220         }
221
222         DEBUG ("Start flashing");
223         window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
224                                                   (GSourceFunc) main_window_flash_cb,
225                                                   window);
226         main_window_flash_cb (window);
227 }
228
229 static void
230 main_window_event_added_cb (EmpathyEventManager *manager,
231                             EmpathyEvent        *event,
232                             EmpathyMainWindow   *window)
233 {
234         if (event->contact) {
235                 main_window_flash_start (window);
236         }
237 }
238
239 static void
240 main_window_event_removed_cb (EmpathyEventManager *manager,
241                               EmpathyEvent        *event,
242                               EmpathyMainWindow   *window)
243 {
244         FlashForeachData data;
245
246         if (!event->contact) {
247                 return;
248         }
249
250         data.on = FALSE;
251         data.event = event;
252         gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
253                                 main_window_flash_foreach,
254                                 &data);
255 }
256
257 static void
258 main_window_row_activated_cb (EmpathyContactListView *view,
259                               GtkTreePath            *path,
260                               GtkTreeViewColumn      *col,
261                               EmpathyMainWindow      *window)
262 {
263         EmpathyContact *contact;
264         GtkTreeModel   *model;
265         GtkTreeIter     iter;
266         GSList         *events, *l;
267
268         model = GTK_TREE_MODEL (window->list_store);
269         gtk_tree_model_get_iter (model, &iter, path);
270         gtk_tree_model_get (model, &iter,
271                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
272                             -1);
273
274         if (!contact) {
275                 return;
276         }
277
278         /* If the contact has an event activate it, otherwise the
279          * default handler of row-activated will be called. */
280         events = empathy_event_manager_get_events (window->event_manager);
281         for (l = events; l; l = l->next) {
282                 EmpathyEvent *event = l->data;
283
284                 if (event->contact == contact) {
285                         DEBUG ("Activate event");
286                         empathy_event_activate (event);
287
288                         /* We don't want the default handler of this signal
289                          * (e.g. open a chat) */
290                         g_signal_stop_emission_by_name (view, "row-activated");
291                         break;
292                 }
293         }
294
295         g_object_unref (contact);
296 }
297
298 static void
299 main_window_error_edit_clicked_cb (GtkButton         *button,
300                                    EmpathyMainWindow *window)
301 {
302         McAccount *account;
303         GtkWidget *error_widget;
304
305         account = g_object_get_data (G_OBJECT (button), "account");
306         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
307
308         error_widget = g_hash_table_lookup (window->errors, account);
309         gtk_widget_destroy (error_widget);
310         g_hash_table_remove (window->errors, account);
311 }
312
313 static void
314 main_window_error_clear_clicked_cb (GtkButton         *button,
315                                     EmpathyMainWindow *window)
316 {
317         McAccount *account;
318         GtkWidget *error_widget;
319
320         account = g_object_get_data (G_OBJECT (button), "account");
321         error_widget = g_hash_table_lookup (window->errors, account);
322         gtk_widget_destroy (error_widget);
323         g_hash_table_remove (window->errors, account);
324 }
325
326 static void
327 main_window_error_display (EmpathyMainWindow *window,
328                            McAccount         *account,
329                            const gchar       *message)
330 {
331         GtkWidget *child;
332         GtkWidget *table;
333         GtkWidget *image;
334         GtkWidget *button_edit;
335         GtkWidget *alignment;
336         GtkWidget *hbox;
337         GtkWidget *label;
338         GtkWidget *fixed;
339         GtkWidget *vbox;
340         GtkWidget *button_close;
341         gchar     *str;
342
343         child = g_hash_table_lookup (window->errors, account);
344         if (child) {
345                 label = g_object_get_data (G_OBJECT (child), "label");
346
347                 /* Just set the latest error and return */
348                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
349                                                mc_account_get_display_name (account),
350                                                message);
351                 gtk_label_set_markup (GTK_LABEL (label), str);
352                 g_free (str);
353
354                 return;
355         }
356
357         child = gtk_vbox_new (FALSE, 0);
358         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
359         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
360         gtk_widget_show (child);
361
362         table = gtk_table_new (2, 4, FALSE);
363         gtk_widget_show (table);
364         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
365         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
366         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
367
368         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
369         gtk_widget_show (image);
370         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
371                           (GtkAttachOptions) (GTK_FILL),
372                           (GtkAttachOptions) (GTK_FILL), 0, 0);
373         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
374
375         button_edit = gtk_button_new ();
376         gtk_widget_show (button_edit);
377         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
378                           (GtkAttachOptions) (GTK_FILL),
379                           (GtkAttachOptions) (0), 0, 0);
380
381         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
382         gtk_widget_show (alignment);
383         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
384
385         hbox = gtk_hbox_new (FALSE, 2);
386         gtk_widget_show (hbox);
387         gtk_container_add (GTK_CONTAINER (alignment), hbox);
388
389         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
390         gtk_widget_show (image);
391         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
392
393         label = gtk_label_new_with_mnemonic (_("_Edit account"));
394         gtk_widget_show (label);
395         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
396
397         fixed = gtk_fixed_new ();
398         gtk_widget_show (fixed);
399         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
400                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
401                           (GtkAttachOptions) (GTK_FILL), 0, 0);
402
403         vbox = gtk_vbox_new (FALSE, 6);
404         gtk_widget_show (vbox);
405         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
406                           (GtkAttachOptions) (GTK_FILL),
407                           (GtkAttachOptions) (GTK_FILL), 0, 0);
408
409         button_close = gtk_button_new ();
410         gtk_widget_show (button_close);
411         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
412         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
413
414
415         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
416         gtk_widget_show (image);
417         gtk_container_add (GTK_CONTAINER (button_close), image);
418
419         label = gtk_label_new ("");
420         gtk_widget_show (label);
421         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
422                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
423                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
424         gtk_widget_set_size_request (label, 175, -1);
425         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
426         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
427         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
428
429         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
430                                        mc_account_get_display_name (account),
431                                        message);
432         gtk_label_set_markup (GTK_LABEL (label), str);
433         g_free (str);
434
435         g_object_set_data (G_OBJECT (child), "label", label);
436         g_object_set_data_full (G_OBJECT (button_edit),
437                                 "account", g_object_ref (account),
438                                 g_object_unref);
439         g_object_set_data_full (G_OBJECT (button_close),
440                                 "account", g_object_ref (account),
441                                 g_object_unref);
442
443         g_signal_connect (button_edit, "clicked",
444                           G_CALLBACK (main_window_error_edit_clicked_cb),
445                           window);
446
447         g_signal_connect (button_close, "clicked",
448                           G_CALLBACK (main_window_error_clear_clicked_cb),
449                           window);
450
451         gtk_widget_show (window->errors_vbox);
452
453         g_hash_table_insert (window->errors, g_object_ref (account), child);
454 }
455
456 static void
457 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
458 {
459         int  connected;
460         int  connecting;
461         GList *l;
462
463         /* Count number of connected/connecting/disconnected accounts */
464         connected = empathy_account_manager_get_connected_accounts (manager);
465         connecting = empathy_account_manager_get_connecting_accounts (manager);
466
467         /* Update the spinner state */
468         if (connecting > 0) {
469                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
470         } else {
471                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
472         }
473
474         /* Update widgets sensibility */
475         for (l = window->actions_connected; l; l = l->next) {
476                 gtk_action_set_sensitive (l->data, (connected > 0));
477         }
478 }
479
480 static void
481 main_window_connection_changed_cb (EmpathyAccountManager *manager,
482                                    McAccount *account,
483                                    TpConnectionStatusReason reason,
484                                    TpConnectionStatus current,
485                                    TpConnectionStatus previous,
486                                    EmpathyMainWindow *window)
487 {
488         main_window_update_status (window, manager);
489
490         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
491             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
492                 const gchar *message;
493
494                 switch (reason) {
495                 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
496                         message = _("No error specified");
497                         break;
498                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
499                         message = _("Network error");
500                         break;
501                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
502                         message = _("Authentication failed");
503                         break;
504                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
505                         message = _("Encryption error");
506                         break;
507                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
508                         message = _("Name in use");
509                         break;
510                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
511                         message = _("Certificate not provided");
512                         break;
513                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
514                         message = _("Certificate untrusted");
515                         break;
516                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
517                         message = _("Certificate expired");
518                         break;
519                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
520                         message = _("Certificate not activated");
521                         break;
522                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
523                         message = _("Certificate hostname mismatch");
524                         break;
525                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
526                         message = _("Certificate fingerprint mismatch");
527                         break;
528                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
529                         message = _("Certificate self-signed");
530                         break;
531                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
532                         message = _("Certificate error");
533                         break;
534                 default:
535                         message = _("Unknown error");
536                         break;
537                 }
538
539                 main_window_error_display (window, account, message);
540         }
541
542         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
543                 empathy_sound_play (GTK_WIDGET (window->window),
544                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
545         }
546
547         if (current == TP_CONNECTION_STATUS_CONNECTED) {
548                 GtkWidget *error_widget;
549
550                 empathy_sound_play (GTK_WIDGET (window->window),
551                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
552
553                 /* Account connected without error, remove error message if any */
554                 error_widget = g_hash_table_lookup (window->errors, account);
555                 if (error_widget) {
556                         gtk_widget_destroy (error_widget);
557                         g_hash_table_remove (window->errors, account);
558                 }
559         }
560 }
561
562 static void
563 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
564                                          EmpathyContact *contact,
565                                          McPresence current,
566                                          McPresence previous,
567                                          EmpathyMainWindow *window)
568 {
569         McAccount *account;
570         gboolean should_play;
571
572         account = empathy_contact_get_account (contact);
573         should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
574
575         if (!should_play) {
576                 return;
577         }
578
579         if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
580                 /* someone is logging in */
581                 empathy_sound_play (GTK_WIDGET (window->window),
582                                     EMPATHY_SOUND_CONTACT_CONNECTED);
583                 return;
584         }
585
586         if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
587                 /* someone is logging off */
588                 empathy_sound_play (GTK_WIDGET (window->window),
589                                     EMPATHY_SOUND_CONTACT_DISCONNECTED);
590         }
591 }
592
593 static void
594 main_window_accels_load (void)
595 {
596         gchar *filename;
597
598         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
599         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
600                 DEBUG ("Loading from:'%s'", filename);
601                 gtk_accel_map_load (filename);
602         }
603
604         g_free (filename);
605 }
606
607 static void
608 main_window_accels_save (void)
609 {
610         gchar *dir;
611         gchar *file_with_path;
612
613         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
614         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
615         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
616         g_free (dir);
617
618         DEBUG ("Saving to:'%s'", file_with_path);
619         gtk_accel_map_save (file_with_path);
620
621         g_free (file_with_path);
622 }
623
624 static void
625 main_window_destroy_cb (GtkWidget         *widget,
626                         EmpathyMainWindow *window)
627 {
628         /* Save user-defined accelerators. */
629         main_window_accels_save ();
630
631         g_signal_handlers_disconnect_by_func (window->account_manager,
632                                               main_window_connection_changed_cb,
633                                               window);
634
635         if (window->size_timeout_id) {
636                 g_source_remove (window->size_timeout_id);
637         }
638
639         g_list_free (window->actions_connected);
640
641         g_object_unref (window->mc);
642         g_object_unref (window->account_manager);
643         g_object_unref (window->list_store);
644         g_hash_table_destroy (window->errors);
645
646         g_signal_handlers_disconnect_by_func (window->event_manager,
647                                               main_window_event_added_cb,
648                                               window);
649         g_signal_handlers_disconnect_by_func (window->event_manager,
650                                               main_window_event_removed_cb,
651                                               window);
652         g_object_unref (window->event_manager);
653         g_object_unref (window->ui_manager);
654
655         g_free (window);
656 }
657
658 static void
659 main_window_chat_quit_cb (GtkAction         *action,
660                           EmpathyMainWindow *window)
661 {
662         gtk_main_quit ();
663 }
664
665 static void
666 main_window_chat_history_cb (GtkAction         *action,
667                              EmpathyMainWindow *window)
668 {
669         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
670 }
671
672 static void
673 main_window_chat_new_message_cb (GtkAction         *action,
674                                  EmpathyMainWindow *window)
675 {
676         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
677 }
678
679 static void
680 main_window_chat_add_contact_cb (GtkAction         *action,
681                                  EmpathyMainWindow *window)
682 {
683         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
684 }
685
686 static void
687 main_window_chat_show_offline_cb (GtkToggleAction   *action,
688                                   EmpathyMainWindow *window)
689 {
690         gboolean current;
691
692         current = gtk_toggle_action_get_active (action);
693         empathy_conf_set_bool (empathy_conf_get (),
694                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
695                               current);
696
697         /* Turn off sound just while we alter the contact list. */
698         // FIXME: empathy_sound_set_enabled (FALSE);
699         empathy_contact_list_store_set_show_offline (window->list_store, current);
700         //empathy_sound_set_enabled (TRUE);
701 }
702
703 static void
704 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
705 {
706         EmpathyAccountManager *manager;
707         McAccount      *account;
708         TpConnection   *connection;
709         const gchar    *room;
710
711         manager = empathy_account_manager_dup_singleton ();
712         account = empathy_chatroom_get_account (chatroom);
713         connection = empathy_account_manager_get_connection (manager, account);
714         room = empathy_chatroom_get_room (chatroom);
715         g_object_unref (manager);
716
717         if (connection != NULL) {
718                 DEBUG ("Requesting channel for '%s'", room);
719                 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
720         }
721 }
722
723 static void
724 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
725                                                 EmpathyChatroom *chatroom)
726 {
727         main_window_favorite_chatroom_join (chatroom);
728 }
729
730 static void
731 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
732                                         EmpathyChatroom    *chatroom)
733 {
734         GtkWidget   *menu_item;
735         const gchar *name;
736
737         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
738                 return;
739         }
740
741         name = empathy_chatroom_get_name (chatroom);
742         menu_item = gtk_menu_item_new_with_label (name);
743
744         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
745         g_signal_connect (menu_item, "activate",
746                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
747                           chatroom);
748
749         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
750                                menu_item, 4);
751
752         gtk_widget_show (menu_item);
753 }
754
755 static void
756 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
757                                              EmpathyChatroom        *chatroom,
758                                              EmpathyMainWindow     *window)
759 {
760         main_window_favorite_chatroom_menu_add (window, chatroom);
761         gtk_widget_show (window->room_separator);
762         gtk_action_set_sensitive (window->room_join_favorites, TRUE);
763 }
764
765 static void
766 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
767                                                EmpathyChatroom        *chatroom,
768                                                EmpathyMainWindow     *window)
769 {
770         GtkWidget *menu_item;
771         GList *chatrooms;
772
773         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
774         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
775         gtk_widget_destroy (menu_item);
776
777         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
778         if (chatrooms) {
779                 gtk_widget_show (window->room_separator);
780         } else {
781                 gtk_widget_hide (window->room_separator);
782         }
783
784         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
785         g_list_free (chatrooms);
786 }
787
788 static void
789 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
790 {
791         GList *chatrooms, *l;
792         GtkWidget *room;
793
794         window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
795         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
796         room = gtk_ui_manager_get_widget (window->ui_manager,
797                 "/menubar/room");
798         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
799         window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
800                 "/menubar/room/room_separator");
801
802         for (l = chatrooms; l; l = l->next) {
803                 main_window_favorite_chatroom_menu_add (window, l->data);
804         }
805
806         if (!chatrooms) {
807                 gtk_widget_hide (window->room_separator);
808         }
809
810         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
811
812         g_signal_connect (window->chatroom_manager, "chatroom-added",
813                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
814                           window);
815         g_signal_connect (window->chatroom_manager, "chatroom-removed",
816                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
817                           window);
818
819         g_list_free (chatrooms);
820 }
821
822 static void
823 main_window_room_join_new_cb (GtkAction         *action,
824                               EmpathyMainWindow *window)
825 {
826         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
827 }
828
829 static void
830 main_window_room_join_favorites_cb (GtkAction         *action,
831                                     EmpathyMainWindow *window)
832 {
833         GList *chatrooms, *l;
834
835         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
836         for (l = chatrooms; l; l = l->next) {
837                 main_window_favorite_chatroom_join (l->data);
838         }
839         g_list_free (chatrooms);
840 }
841
842 static void
843 main_window_room_manage_favorites_cb (GtkAction         *action,
844                                       EmpathyMainWindow *window)
845 {
846         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
847 }
848
849 static void
850 main_window_edit_cb (GtkAction *action,
851                      EmpathyMainWindow *window)
852 {
853         GtkWidget *submenu;
854
855         /* FIXME: It should use the UIManager to merge the contact/group submenu */
856         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
857         if (submenu) {
858                 GtkMenuItem *item;
859                 GtkWidget   *label;
860
861                 item = GTK_MENU_ITEM (window->edit_context);
862                 label = gtk_bin_get_child (GTK_BIN (item));
863                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
864
865                 gtk_widget_show (window->edit_context);
866                 gtk_widget_show (window->edit_context_separator);
867
868                 gtk_menu_item_set_submenu (item, submenu);
869
870                 return;
871         }
872
873         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
874         if (submenu) {
875                 GtkMenuItem *item;
876                 GtkWidget   *label;
877
878                 item = GTK_MENU_ITEM (window->edit_context);
879                 label = gtk_bin_get_child (GTK_BIN (item));
880                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
881
882                 gtk_widget_show (window->edit_context);
883                 gtk_widget_show (window->edit_context_separator);
884
885                 gtk_menu_item_set_submenu (item, submenu);
886
887                 return;
888         }
889
890         gtk_widget_hide (window->edit_context);
891         gtk_widget_hide (window->edit_context_separator);
892
893         return;
894 }
895
896 static void
897 main_window_edit_accounts_cb (GtkAction         *action,
898                               EmpathyMainWindow *window)
899 {
900         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
901 }
902
903 static void
904 main_window_edit_personal_information_cb (GtkAction         *action,
905                                           EmpathyMainWindow *window)
906 {
907         empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
908 }
909
910 static void
911 main_window_edit_preferences_cb (GtkAction         *action,
912                                  EmpathyMainWindow *window)
913 {
914         empathy_preferences_show (GTK_WINDOW (window->window));
915 }
916
917 static void
918 main_window_help_about_cb (GtkAction         *action,
919                            EmpathyMainWindow *window)
920 {
921         empathy_about_dialog_new (GTK_WINDOW (window->window));
922 }
923
924 static void
925 main_window_help_contents_cb (GtkAction         *action,
926                               EmpathyMainWindow *window)
927 {
928         empathy_url_show (window->window, "ghelp:empathy");
929 }
930
931 static gboolean
932 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
933                                             GdkEventButton    *event,
934                                             EmpathyMainWindow *window)
935 {
936         if (event->type != GDK_BUTTON_PRESS ||
937             event->button != 1) {
938                 return FALSE;
939         }
940
941         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
942
943         return FALSE;
944 }
945
946 static gboolean
947 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
948 {
949         gint x, y, w, h;
950
951         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
952         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
953
954         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
955
956         window->size_timeout_id = 0;
957
958         return FALSE;
959 }
960
961 static gboolean
962 main_window_configure_event_cb (GtkWidget         *widget,
963                                 GdkEventConfigure *event,
964                                 EmpathyMainWindow *window)
965 {
966         if (window->size_timeout_id) {
967                 g_source_remove (window->size_timeout_id);
968         }
969
970         window->size_timeout_id = g_timeout_add_seconds (1,
971                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
972                                                          window);
973
974         return FALSE;
975 }
976
977 static void
978 main_window_account_created_or_deleted_cb (EmpathyAccountManager  *manager,
979                                            McAccount              *account,
980                                            EmpathyMainWindow      *window)
981 {
982         gtk_action_set_sensitive (window->chat_history,
983                 empathy_account_manager_get_count (manager) > 0);
984 }
985
986 static void
987 main_window_notify_show_offline_cb (EmpathyConf *conf,
988                                     const gchar *key,
989                                     gpointer     toggle_action)
990 {
991         gboolean show_offline;
992
993         if (empathy_conf_get_bool (conf, key, &show_offline)) {
994                 gtk_toggle_action_set_active (toggle_action, show_offline);
995         }
996 }
997
998 static void
999 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1000                                     const gchar       *key,
1001                                     EmpathyMainWindow *window)
1002 {
1003         gboolean show_avatars;
1004
1005         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1006                 empathy_contact_list_store_set_show_avatars (window->list_store,
1007                                                             show_avatars);
1008         }
1009 }
1010
1011 static void
1012 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1013                                             const gchar       *key,
1014                                             EmpathyMainWindow *window)
1015 {
1016         gboolean compact_contact_list;
1017
1018         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1019                 empathy_contact_list_store_set_is_compact (window->list_store,
1020                                                           compact_contact_list);
1021         }
1022 }
1023
1024 static void
1025 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1026                                       const gchar       *key,
1027                                       EmpathyMainWindow *window)
1028 {
1029         gchar *str = NULL;
1030
1031         if (empathy_conf_get_string (conf, key, &str) && str) {
1032                 GType       type;
1033                 GEnumClass *enum_class;
1034                 GEnumValue *enum_value;
1035
1036                 type = empathy_contact_list_store_sort_get_type ();
1037                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1038                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1039                 g_free (str);
1040
1041                 if (enum_value) {
1042                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1043                                                                        enum_value->value);
1044                 }
1045         }
1046 }
1047
1048 static void
1049 main_window_connection_items_setup (EmpathyMainWindow *window,
1050                                     GtkBuilder        *gui)
1051 {
1052         GList         *list;
1053         GObject       *action;
1054         gint           i;
1055         const gchar *actions_connected[] = {
1056                 "room",
1057                 "chat_new_message",
1058                 "chat_add_contact",
1059                 "edit_personal_information"
1060         };
1061
1062         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1063                 action = gtk_builder_get_object (gui, actions_connected[i]);
1064                 list = g_list_prepend (list, action);
1065         }
1066
1067         window->actions_connected = list;
1068 }
1069
1070 GtkWidget *
1071 empathy_main_window_get (void)
1072 {
1073   return window != NULL ? window->window : NULL;
1074 }
1075
1076 GtkWidget *
1077 empathy_main_window_show (void)
1078 {
1079         EmpathyContactList       *list_iface;
1080         EmpathyContactMonitor    *monitor;
1081         GtkBuilder               *gui;
1082         EmpathyConf              *conf;
1083         GtkWidget                *sw;
1084         GtkToggleAction          *show_offline_widget;
1085         GtkWidget                *ebox;
1086         GtkToolItem              *item;
1087         gboolean                  show_offline;
1088         gboolean                  show_avatars;
1089         gboolean                  compact_contact_list;
1090         gint                      x, y, w, h;
1091         gchar                    *filename;
1092         GSList                   *l;
1093
1094         if (window) {
1095                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1096                 return window->window;
1097         }
1098
1099         window = g_new0 (EmpathyMainWindow, 1);
1100
1101         /* Set up interface */
1102         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1103         gui = empathy_builder_get_file (filename,
1104                                        "main_window", &window->window,
1105                                        "main_vbox", &window->main_vbox,
1106                                        "errors_vbox", &window->errors_vbox,
1107                                        "ui_manager", &window->ui_manager,
1108                                        "chat_show_offline", &show_offline_widget,
1109                                        "chat_history", &window->chat_history,
1110                                        "room_join_favorites", &window->room_join_favorites,
1111                                        "presence_toolbar", &window->presence_toolbar,
1112                                        "roster_scrolledwindow", &sw,
1113                                        NULL);
1114         g_free (filename);
1115
1116         empathy_builder_connect (gui, window,
1117                               "main_window", "destroy", main_window_destroy_cb,
1118                               "main_window", "configure_event", main_window_configure_event_cb,
1119                               "chat_quit", "activate", main_window_chat_quit_cb,
1120                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1121                               "chat_history", "activate", main_window_chat_history_cb,
1122                               "room_join_new", "activate", main_window_room_join_new_cb,
1123                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1124                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1125                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1126                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
1127                               "edit", "activate", main_window_edit_cb,
1128                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1129                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1130                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1131                               "help_about", "activate", main_window_help_about_cb,
1132                               "help_contents", "activate", main_window_help_contents_cb,
1133                               NULL);
1134
1135         /* Set up connection related widgets. */
1136         main_window_connection_items_setup (window, gui);
1137
1138         g_object_ref (window->ui_manager);
1139         g_object_unref (gui);
1140
1141         window->mc = empathy_mission_control_dup_singleton ();
1142         window->account_manager = empathy_account_manager_dup_singleton ();
1143
1144         g_signal_connect (window->account_manager,
1145                           "account-connection-changed",
1146                           G_CALLBACK (main_window_connection_changed_cb), window);
1147
1148         window->errors = g_hash_table_new_full (empathy_account_hash,
1149                                                 empathy_account_equal,
1150                                                 g_object_unref,
1151                                                 NULL);
1152
1153         /* Set up menu */
1154         main_window_favorite_chatroom_menu_setup (window);
1155
1156         window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1157                 "/menubar/edit/edit_context");
1158         window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1159                 "/menubar/edit/edit_context_separator");
1160         gtk_widget_hide (window->edit_context);
1161         gtk_widget_hide (window->edit_context_separator);
1162
1163         /* Set up contact list. */
1164         empathy_status_presets_get_all ();
1165
1166         /* Set up presence chooser */
1167         window->presence_chooser = empathy_presence_chooser_new ();
1168         gtk_widget_show (window->presence_chooser);
1169         item = gtk_tool_item_new ();
1170         gtk_widget_show (GTK_WIDGET (item));
1171         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1172         gtk_tool_item_set_is_important (item, TRUE);
1173         gtk_tool_item_set_expand (item, TRUE);
1174         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1175
1176         /* Set up the throbber */
1177         ebox = gtk_event_box_new ();
1178         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1179         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1180         g_signal_connect (ebox,
1181                           "button-press-event",
1182                           G_CALLBACK (main_window_throbber_button_press_event_cb),
1183                           window);
1184         gtk_widget_show (ebox);
1185
1186         window->throbber = ephy_spinner_new ();
1187         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1188         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1189         gtk_widget_show (window->throbber);
1190
1191         item = gtk_tool_item_new ();
1192         gtk_container_add (GTK_CONTAINER (item), ebox);
1193         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1194         gtk_widget_show (GTK_WIDGET (item));
1195
1196         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1197         monitor = empathy_contact_list_get_monitor (list_iface);
1198         window->list_store = empathy_contact_list_store_new (list_iface);
1199         window->list_view = empathy_contact_list_view_new (window->list_store,
1200                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
1201                                                            EMPATHY_CONTACT_FEATURE_ALL);
1202         g_signal_connect (monitor, "contact-presence-changed",
1203                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
1204         g_object_unref (list_iface);
1205
1206         gtk_widget_show (GTK_WIDGET (window->list_view));
1207         gtk_container_add (GTK_CONTAINER (sw),
1208                            GTK_WIDGET (window->list_view));
1209         g_signal_connect (window->list_view, "row-activated",
1210                           G_CALLBACK (main_window_row_activated_cb),
1211                           window);
1212
1213         /* Load user-defined accelerators. */
1214         main_window_accels_load ();
1215
1216         /* Set window size. */
1217         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1218
1219         if (w >= 1 && h >= 1) {
1220                 /* Use the defaults from the ui file if we
1221                  * don't have good w, h geometry.
1222                  */
1223                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1224                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1225         }
1226
1227         if (x >= 0 && y >= 0) {
1228                 /* Let the window manager position it if we
1229                  * don't have good x, y coordinates.
1230                  */
1231                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1232                 gtk_window_move (GTK_WINDOW (window->window), x, y);
1233         }
1234
1235         /* Enable event handling */
1236         window->event_manager = empathy_event_manager_dup_singleton ();
1237         g_signal_connect (window->event_manager, "event-added",
1238                           G_CALLBACK (main_window_event_added_cb),
1239                           window);
1240         g_signal_connect (window->event_manager, "event-removed",
1241                           G_CALLBACK (main_window_event_removed_cb),
1242                           window);
1243
1244         g_signal_connect (window->account_manager, "account-created",
1245                           G_CALLBACK (main_window_account_created_or_deleted_cb),
1246                           window);
1247         g_signal_connect (window->account_manager, "account-deleted",
1248                           G_CALLBACK (main_window_account_created_or_deleted_cb),
1249                           window);
1250         main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1251
1252         l = empathy_event_manager_get_events (window->event_manager);
1253         while (l) {
1254                 main_window_event_added_cb (window->event_manager,
1255                                             l->data, window);
1256                 l = l->next;
1257         }
1258
1259         conf = empathy_conf_get ();
1260
1261         /* Show offline ? */
1262         empathy_conf_get_bool (conf,
1263                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1264                               &show_offline);
1265         empathy_conf_notify_add (conf,
1266                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1267                                 main_window_notify_show_offline_cb,
1268                                 show_offline_widget);
1269
1270         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1271
1272         /* Show avatars ? */
1273         empathy_conf_get_bool (conf,
1274                               EMPATHY_PREFS_UI_SHOW_AVATARS,
1275                               &show_avatars);
1276         empathy_conf_notify_add (conf,
1277                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
1278                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1279                                 window);
1280         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1281
1282         /* Is compact ? */
1283         empathy_conf_get_bool (conf,
1284                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1285                               &compact_contact_list);
1286         empathy_conf_notify_add (conf,
1287                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1288                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1289                                 window);
1290         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1291
1292         /* Sort criterium */
1293         empathy_conf_notify_add (conf,
1294                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1295                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1296                                 window);
1297         main_window_notify_sort_criterium_cb (conf,
1298                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1299                                               window);
1300
1301         main_window_update_status (window, window->account_manager);
1302
1303         return window->window;
1304 }
1305