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