make the infobar smaller
[empathy-mirror.git] / src / empathy-main-window.c
blob04142ad0820750f565797bf634da1c2dbc28dd61
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.
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.
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.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include <config.h>
26 #include <sys/stat.h>
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
30 #include <telepathy-glib/account-manager.h>
32 #include <libempathy/empathy-contact.h>
33 #include <libempathy/empathy-idle.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-dispatcher.h>
36 #include <libempathy/empathy-chatroom-manager.h>
37 #include <libempathy/empathy-chatroom.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/empathy-contact-manager.h>
40 #include <libempathy/empathy-status-presets.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-contact-dialogs.h>
44 #include <libempathy-gtk/empathy-contact-list-store.h>
45 #include <libempathy-gtk/empathy-contact-list-view.h>
46 #include <libempathy-gtk/empathy-geometry.h>
47 #include <libempathy-gtk/empathy-gtk-enum-types.h>
48 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-presence-chooser.h>
51 #include <libempathy-gtk/empathy-sound.h>
52 #include <libempathy-gtk/empathy-ui-utils.h>
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-debug-window.h"
60 #include "empathy-new-chatroom-dialog.h"
61 #include "empathy-map-view.h"
62 #include "empathy-chatrooms-window.h"
63 #include "empathy-event-manager.h"
64 #include "empathy-ft-manager.h"
66 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
67 #include <libempathy/empathy-debug.h>
69 /* Flashing delay for icons (milliseconds). */
70 #define FLASH_TIMEOUT 500
72 /* Minimum width of roster window if something goes wrong. */
73 #define MIN_WIDTH 50
75 /* Accels (menu shortcuts) can be configured and saved */
76 #define ACCELS_FILENAME "accels.txt"
78 /* Name in the geometry file */
79 #define GEOMETRY_NAME "main-window"
81 typedef struct {
82 EmpathyContactListView *list_view;
83 EmpathyContactListStore *list_store;
84 TpAccountManager *account_manager;
85 EmpathyChatroomManager *chatroom_manager;
86 EmpathyEventManager *event_manager;
87 guint flash_timeout_id;
88 gboolean flash_on;
90 GtkWidget *window;
91 GtkWidget *main_vbox;
92 GtkWidget *throbber;
93 GtkWidget *presence_toolbar;
94 GtkWidget *presence_chooser;
95 GtkWidget *errors_vbox;
97 GtkRadioAction *sort_by_name;
98 GtkRadioAction *sort_by_status;
99 GtkRadioAction *normal_with_avatars;
100 GtkRadioAction *normal_size;
101 GtkRadioAction *compact_size;
103 GtkUIManager *ui_manager;
104 GtkAction *view_history;
105 GtkAction *room_join_favorites;
106 GtkWidget *room_menu;
107 GtkWidget *room_separator;
108 GtkWidget *edit_context;
109 GtkWidget *edit_context_separator;
111 guint size_timeout_id;
112 GHashTable *errors;
114 /* Actions that are enabled when there are connected accounts */
115 GList *actions_connected;
116 } EmpathyMainWindow;
118 static EmpathyMainWindow *main_window = NULL;
120 static gboolean main_window_configure_event_timeout_cb (
121 EmpathyMainWindow *window);
123 static void
124 main_window_flash_stop (EmpathyMainWindow *window)
126 if (window->flash_timeout_id == 0) {
127 return;
130 DEBUG ("Stop flashing");
131 g_source_remove (window->flash_timeout_id);
132 window->flash_timeout_id = 0;
133 window->flash_on = FALSE;
136 typedef struct {
137 EmpathyEvent *event;
138 gboolean on;
139 } FlashForeachData;
141 static gboolean
142 main_window_flash_foreach (GtkTreeModel *model,
143 GtkTreePath *path,
144 GtkTreeIter *iter,
145 gpointer user_data)
147 FlashForeachData *data = (FlashForeachData *) user_data;
148 EmpathyContact *contact;
149 const gchar *icon_name;
150 GtkTreePath *parent_path = NULL;
151 GtkTreeIter parent_iter;
153 /* To be used with gtk_tree_model_foreach, update the status icon
154 * of the contact to show the event icon (on=TRUE) or the presence
155 * (on=FALSE) */
156 gtk_tree_model_get (model, iter,
157 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
158 -1);
160 if (contact != data->event->contact) {
161 if (contact) {
162 g_object_unref (contact);
164 return FALSE;
167 if (data->on) {
168 icon_name = data->event->icon_name;
169 } else {
170 icon_name = empathy_icon_name_for_contact (contact);
173 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
174 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
175 -1);
177 /* To make sure the parent is shown correctly, we emit
178 * the row-changed signal on the parent so it prompts
179 * it to be refreshed by the filter func.
181 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
182 parent_path = gtk_tree_model_get_path (model, &parent_iter);
184 if (parent_path) {
185 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
186 gtk_tree_path_free (parent_path);
189 g_object_unref (contact);
191 return FALSE;
194 static gboolean
195 main_window_flash_cb (EmpathyMainWindow *window)
197 GtkTreeModel *model;
198 GSList *events, *l;
199 gboolean found_event = FALSE;
200 FlashForeachData data;
202 window->flash_on = !window->flash_on;
203 data.on = window->flash_on;
204 model = GTK_TREE_MODEL (window->list_store);
206 events = empathy_event_manager_get_events (window->event_manager);
207 for (l = events; l; l = l->next) {
208 data.event = l->data;
209 if (!data.event->contact || !data.event->must_ack) {
210 continue;
213 found_event = TRUE;
214 gtk_tree_model_foreach (model,
215 main_window_flash_foreach,
216 &data);
219 if (!found_event) {
220 main_window_flash_stop (window);
223 return TRUE;
226 static void
227 main_window_flash_start (EmpathyMainWindow *window)
229 if (window->flash_timeout_id != 0) {
230 return;
233 DEBUG ("Start flashing");
234 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
235 (GSourceFunc) main_window_flash_cb,
236 window);
237 main_window_flash_cb (window);
240 static void
241 main_window_event_added_cb (EmpathyEventManager *manager,
242 EmpathyEvent *event,
243 EmpathyMainWindow *window)
245 if (event->contact) {
246 main_window_flash_start (window);
250 static void
251 main_window_event_removed_cb (EmpathyEventManager *manager,
252 EmpathyEvent *event,
253 EmpathyMainWindow *window)
255 FlashForeachData data;
257 if (!event->contact) {
258 return;
261 data.on = FALSE;
262 data.event = event;
263 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
264 main_window_flash_foreach,
265 &data);
268 static void
269 main_window_row_activated_cb (EmpathyContactListView *view,
270 GtkTreePath *path,
271 GtkTreeViewColumn *col,
272 EmpathyMainWindow *window)
274 EmpathyContact *contact;
275 GtkTreeModel *model;
276 GtkTreeIter iter;
277 GSList *events, *l;
279 model = GTK_TREE_MODEL (window->list_store);
280 gtk_tree_model_get_iter (model, &iter, path);
281 gtk_tree_model_get (model, &iter,
282 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
283 -1);
285 if (!contact) {
286 return;
289 /* If the contact has an event activate it, otherwise the
290 * default handler of row-activated will be called. */
291 events = empathy_event_manager_get_events (window->event_manager);
292 for (l = events; l; l = l->next) {
293 EmpathyEvent *event = l->data;
295 if (event->contact == contact) {
296 DEBUG ("Activate event");
297 empathy_event_activate (event);
299 /* We don't want the default handler of this signal
300 * (e.g. open a chat) */
301 g_signal_stop_emission_by_name (view, "row-activated");
302 break;
306 g_object_unref (contact);
309 static void
310 main_window_error_infobar_button_clicked_cb (GtkInfoBar *info_bar,
311 gint response_id,
312 EmpathyMainWindow *window)
314 TpAccount *account;
315 GtkWidget *error_widget;
317 account = g_object_get_data (G_OBJECT (info_bar), "account");
318 switch (response_id) {
319 case GTK_RESPONSE_CLOSE:
320 break;
321 default:
322 break;
325 error_widget = g_hash_table_lookup (window->errors, account);
326 gtk_widget_destroy (error_widget);
327 g_hash_table_remove (window->errors, account);
330 static void
331 main_window_error_display (EmpathyMainWindow *window,
332 TpAccount *account,
333 const gchar *message)
335 GtkWidget *info_bar;
336 GtkWidget *content_area;
337 GtkWidget *label;
338 GtkWidget *image;
339 gchar *str;
340 const gchar *icon_name;
342 info_bar = g_hash_table_lookup (window->errors, account);
343 if (info_bar) {
344 label = g_object_get_data (G_OBJECT (info_bar), "label");
346 /* Just set the latest error and return */
347 str = g_markup_printf_escaped ("<b>%s</b> - %s",
348 tp_account_get_display_name (account),
349 message);
350 gtk_label_set_markup (GTK_LABEL (label), str);
351 g_free (str);
353 return;
356 info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
357 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
358 g_signal_connect (info_bar, "response",
359 G_CALLBACK (main_window_error_infobar_button_clicked_cb),
360 window);
362 gtk_widget_set_no_show_all (info_bar, TRUE);
363 gtk_box_pack_start (GTK_BOX (window->errors_vbox), info_bar, FALSE, TRUE, 0);
364 gtk_widget_show (info_bar);
366 icon_name = empathy_account_get_icon_name (account);
367 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
368 gtk_widget_show (image);
370 str = g_markup_printf_escaped ("<b>%s</b> - %s",
371 tp_account_get_display_name (account),
372 message);
373 label = gtk_label_new (str);
374 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
375 gtk_widget_show (label);
376 g_free (str);
378 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
379 gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
380 gtk_box_pack_start (GTK_BOX (content_area), label, FALSE, FALSE, 0);
382 g_object_set_data (G_OBJECT (info_bar), "label", label);
383 g_object_set_data_full (G_OBJECT (info_bar),
384 "account", g_object_ref (account),
385 g_object_unref);
387 gtk_widget_show (window->errors_vbox);
389 g_hash_table_insert (window->errors, g_object_ref (account), info_bar);
392 static void
393 main_window_update_status (EmpathyMainWindow *window)
395 gboolean connected, connecting;
396 GList *l;
398 connected = empathy_account_manager_get_accounts_connected (&connecting);
400 /* Update the spinner state */
401 if (connecting) {
402 ephy_spinner_start (EPHY_SPINNER (window->throbber));
403 } else {
404 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
407 /* Update widgets sensibility */
408 for (l = window->actions_connected; l; l = l->next) {
409 gtk_action_set_sensitive (l->data, connected);
413 static void
414 main_window_connection_changed_cb (TpAccount *account,
415 guint old_status,
416 guint current,
417 guint reason,
418 gchar *dbus_error_name,
419 GHashTable *details,
420 EmpathyMainWindow *window)
422 main_window_update_status (window);
424 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
425 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
426 const gchar *message;
428 switch (reason) {
429 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
430 message = _("No error specified");
431 break;
432 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
433 message = _("Network error");
434 break;
435 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
436 message = _("Authentication failed");
437 break;
438 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
439 message = _("Encryption error");
440 break;
441 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
442 message = _("Name in use");
443 break;
444 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
445 message = _("Certificate not provided");
446 break;
447 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
448 message = _("Certificate untrusted");
449 break;
450 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
451 message = _("Certificate expired");
452 break;
453 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
454 message = _("Certificate not activated");
455 break;
456 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
457 message = _("Certificate hostname mismatch");
458 break;
459 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
460 message = _("Certificate fingerprint mismatch");
461 break;
462 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
463 message = _("Certificate self-signed");
464 break;
465 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
466 message = _("Certificate error");
467 break;
468 default:
469 message = _("Unknown error");
470 break;
473 main_window_error_display (window, account, message);
476 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
477 empathy_sound_play (GTK_WIDGET (window->window),
478 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
481 if (current == TP_CONNECTION_STATUS_CONNECTED) {
482 GtkWidget *error_widget;
484 empathy_sound_play (GTK_WIDGET (window->window),
485 EMPATHY_SOUND_ACCOUNT_CONNECTED);
487 /* Account connected without error, remove error message if any */
488 error_widget = g_hash_table_lookup (window->errors, account);
489 if (error_widget) {
490 gtk_widget_destroy (error_widget);
491 g_hash_table_remove (window->errors, account);
496 static void
497 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
498 EmpathyContact *contact,
499 TpConnectionPresenceType current,
500 TpConnectionPresenceType previous,
501 EmpathyMainWindow *window)
503 TpAccount *account;
504 gboolean should_play = FALSE;
505 EmpathyIdle *idle;
507 account = empathy_contact_get_account (contact);
508 idle = empathy_idle_dup_singleton ();
510 should_play = !empathy_idle_account_is_just_connected (idle, account);
512 if (!should_play)
513 goto out;
515 if (tp_connection_presence_type_cmp_availability (previous,
516 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
518 /* contact was online */
519 if (tp_connection_presence_type_cmp_availability (current,
520 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
521 /* someone is logging off */
522 empathy_sound_play (GTK_WIDGET (window->window),
523 EMPATHY_SOUND_CONTACT_DISCONNECTED);
525 else
527 /* contact was offline */
528 if (tp_connection_presence_type_cmp_availability (current,
529 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
530 /* someone is logging in */
531 empathy_sound_play (GTK_WIDGET (window->window),
532 EMPATHY_SOUND_CONTACT_CONNECTED);
535 out:
536 g_object_unref (idle);
539 static void
540 main_window_accels_load (void)
542 gchar *filename;
544 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
545 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
546 DEBUG ("Loading from:'%s'", filename);
547 gtk_accel_map_load (filename);
550 g_free (filename);
553 static void
554 main_window_accels_save (void)
556 gchar *dir;
557 gchar *file_with_path;
559 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
560 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
561 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
562 g_free (dir);
564 DEBUG ("Saving to:'%s'", file_with_path);
565 gtk_accel_map_save (file_with_path);
567 g_free (file_with_path);
570 static void
571 main_window_destroy_cb (GtkWidget *widget,
572 EmpathyMainWindow *window)
574 /* Save user-defined accelerators. */
575 main_window_accels_save ();
577 if (window->size_timeout_id) {
578 g_source_remove (window->size_timeout_id);
579 main_window_configure_event_timeout_cb (window);
582 g_list_free (window->actions_connected);
584 g_object_unref (window->account_manager);
585 g_object_unref (window->list_store);
586 g_hash_table_destroy (window->errors);
588 g_signal_handlers_disconnect_by_func (window->event_manager,
589 main_window_event_added_cb,
590 window);
591 g_signal_handlers_disconnect_by_func (window->event_manager,
592 main_window_event_removed_cb,
593 window);
594 g_object_unref (window->event_manager);
595 g_object_unref (window->ui_manager);
597 g_free (window);
600 static void
601 main_window_chat_quit_cb (GtkAction *action,
602 EmpathyMainWindow *window)
604 gtk_main_quit ();
607 static void
608 main_window_view_history_cb (GtkAction *action,
609 EmpathyMainWindow *window)
611 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
614 static void
615 main_window_chat_new_message_cb (GtkAction *action,
616 EmpathyMainWindow *window)
618 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
621 static void
622 main_window_chat_add_contact_cb (GtkAction *action,
623 EmpathyMainWindow *window)
625 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
628 static void
629 main_window_view_show_ft_manager (GtkAction *action,
630 EmpathyMainWindow *window)
632 empathy_ft_manager_show ();
635 static void
636 main_window_view_show_offline_cb (GtkToggleAction *action,
637 EmpathyMainWindow *window)
639 gboolean current;
641 current = gtk_toggle_action_get_active (action);
642 empathy_conf_set_bool (empathy_conf_get (),
643 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
644 current);
646 /* Turn off sound just while we alter the contact list. */
647 // FIXME: empathy_sound_set_enabled (FALSE);
648 empathy_contact_list_store_set_show_offline (window->list_store, current);
649 //empathy_sound_set_enabled (TRUE);
652 static void
653 main_window_notify_sort_contact_cb (EmpathyConf *conf,
654 const gchar *key,
655 EmpathyMainWindow *window)
657 gchar *str = NULL;
659 if (empathy_conf_get_string (conf, key, &str) && str) {
660 GType type;
661 GEnumClass *enum_class;
662 GEnumValue *enum_value;
664 type = empathy_contact_list_store_sort_get_type ();
665 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
666 enum_value = g_enum_get_value_by_nick (enum_class, str);
667 if (enum_value) {
668 /* By changing the value of the GtkRadioAction,
669 it emits a signal that calls main_window_view_sort_contacts_cb
670 which updates the contacts list */
671 gtk_radio_action_set_current_value (window->sort_by_name,
672 enum_value->value);
673 } else {
674 g_warning ("Wrong value for sort_criterium configuration : %s", str);
676 g_free (str);
680 static void
681 main_window_view_sort_contacts_cb (GtkRadioAction *action,
682 GtkRadioAction *current,
683 EmpathyMainWindow *window)
685 EmpathyContactListStoreSort value;
686 GSList *group;
687 GType type;
688 GEnumClass *enum_class;
689 GEnumValue *enum_value;
691 value = gtk_radio_action_get_current_value (action);
692 group = gtk_radio_action_get_group (action);
694 /* Get string from index */
695 type = empathy_contact_list_store_sort_get_type ();
696 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
697 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
699 if (!enum_value) {
700 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
701 g_slist_index (group, action));
702 } else {
703 empathy_conf_set_string (empathy_conf_get (),
704 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
705 enum_value->value_nick);
707 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
710 /* Matches GtkRadioAction values set in empathy-main-window.ui */
711 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
712 #define CONTACT_LIST_NORMAL_SIZE 1
713 #define CONTACT_LIST_COMPACT_SIZE 2
715 static void
716 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
717 GtkRadioAction *current,
718 EmpathyMainWindow *window)
720 gint value;
722 value = gtk_radio_action_get_current_value (action);
724 empathy_conf_set_bool (empathy_conf_get (),
725 EMPATHY_PREFS_UI_SHOW_AVATARS,
726 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
727 empathy_conf_set_bool (empathy_conf_get (),
728 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
729 value == CONTACT_LIST_COMPACT_SIZE);
731 empathy_contact_list_store_set_show_avatars (window->list_store,
732 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
733 empathy_contact_list_store_set_is_compact (window->list_store,
734 value == CONTACT_LIST_COMPACT_SIZE);
737 static void
738 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
739 const gchar *key,
740 EmpathyMainWindow *window)
742 gboolean show_avatars;
743 gboolean compact_contact_list;
744 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
746 if (empathy_conf_get_bool (conf,
747 EMPATHY_PREFS_UI_SHOW_AVATARS,
748 &show_avatars)
749 && empathy_conf_get_bool (conf,
750 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
751 &compact_contact_list)) {
752 if (compact_contact_list) {
753 value = CONTACT_LIST_COMPACT_SIZE;
754 } else if (show_avatars) {
755 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
756 } else {
757 value = CONTACT_LIST_NORMAL_SIZE;
760 /* By changing the value of the GtkRadioAction,
761 it emits a signal that calls main_window_view_contacts_list_size_cb
762 which updates the contacts list */
763 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
766 static void
767 main_window_view_show_map_cb (GtkCheckMenuItem *item,
768 EmpathyMainWindow *window)
770 #if HAVE_LIBCHAMPLAIN
771 empathy_map_view_show ();
772 #endif
775 static void
776 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
778 TpAccount *account;
779 TpConnection *connection;
780 const gchar *room;
782 account = empathy_chatroom_get_account (chatroom);
783 connection = tp_account_get_connection (account);
784 room = empathy_chatroom_get_room (chatroom);
786 if (connection != NULL) {
787 DEBUG ("Requesting channel for '%s'", room);
788 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
792 static void
793 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
794 EmpathyChatroom *chatroom)
796 main_window_favorite_chatroom_join (chatroom);
799 static void
800 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
801 EmpathyChatroom *chatroom)
803 GtkWidget *menu_item;
804 const gchar *name;
806 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
807 return;
810 name = empathy_chatroom_get_name (chatroom);
811 menu_item = gtk_menu_item_new_with_label (name);
813 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
814 g_signal_connect (menu_item, "activate",
815 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
816 chatroom);
818 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
819 menu_item, 4);
821 gtk_widget_show (menu_item);
824 static void
825 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
826 EmpathyChatroom *chatroom,
827 EmpathyMainWindow *window)
829 main_window_favorite_chatroom_menu_add (window, chatroom);
830 gtk_widget_show (window->room_separator);
831 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
834 static void
835 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
836 EmpathyChatroom *chatroom,
837 EmpathyMainWindow *window)
839 GtkWidget *menu_item;
840 GList *chatrooms;
842 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
843 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
844 gtk_widget_destroy (menu_item);
846 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
847 if (chatrooms) {
848 gtk_widget_show (window->room_separator);
849 } else {
850 gtk_widget_hide (window->room_separator);
853 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
854 g_list_free (chatrooms);
857 static void
858 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
860 GList *chatrooms, *l;
861 GtkWidget *room;
863 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
864 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
865 room = gtk_ui_manager_get_widget (window->ui_manager,
866 "/menubar/room");
867 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
868 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
869 "/menubar/room/room_separator");
871 for (l = chatrooms; l; l = l->next) {
872 main_window_favorite_chatroom_menu_add (window, l->data);
875 if (!chatrooms) {
876 gtk_widget_hide (window->room_separator);
879 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
881 g_signal_connect (window->chatroom_manager, "chatroom-added",
882 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
883 window);
884 g_signal_connect (window->chatroom_manager, "chatroom-removed",
885 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
886 window);
888 g_list_free (chatrooms);
891 static void
892 main_window_room_join_new_cb (GtkAction *action,
893 EmpathyMainWindow *window)
895 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
898 static void
899 main_window_room_join_favorites_cb (GtkAction *action,
900 EmpathyMainWindow *window)
902 GList *chatrooms, *l;
904 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
905 for (l = chatrooms; l; l = l->next) {
906 main_window_favorite_chatroom_join (l->data);
908 g_list_free (chatrooms);
911 static void
912 main_window_room_manage_favorites_cb (GtkAction *action,
913 EmpathyMainWindow *window)
915 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
918 static void
919 main_window_edit_cb (GtkAction *action,
920 EmpathyMainWindow *window)
922 GtkWidget *submenu;
924 /* FIXME: It should use the UIManager to merge the contact/group submenu */
925 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
926 if (submenu) {
927 GtkMenuItem *item;
928 GtkWidget *label;
930 item = GTK_MENU_ITEM (window->edit_context);
931 label = gtk_bin_get_child (GTK_BIN (item));
932 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
934 gtk_widget_show (window->edit_context);
935 gtk_widget_show (window->edit_context_separator);
937 gtk_menu_item_set_submenu (item, submenu);
939 return;
942 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
943 if (submenu) {
944 GtkMenuItem *item;
945 GtkWidget *label;
947 item = GTK_MENU_ITEM (window->edit_context);
948 label = gtk_bin_get_child (GTK_BIN (item));
949 gtk_label_set_text (GTK_LABEL (label), _("Group"));
951 gtk_widget_show (window->edit_context);
952 gtk_widget_show (window->edit_context_separator);
954 gtk_menu_item_set_submenu (item, submenu);
956 return;
959 gtk_widget_hide (window->edit_context);
960 gtk_widget_hide (window->edit_context_separator);
962 return;
965 static void
966 main_window_edit_accounts_cb (GtkAction *action,
967 EmpathyMainWindow *window)
969 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
972 static void
973 main_window_edit_personal_information_cb (GtkAction *action,
974 EmpathyMainWindow *window)
976 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
979 static void
980 main_window_edit_preferences_cb (GtkAction *action,
981 EmpathyMainWindow *window)
983 empathy_preferences_show (GTK_WINDOW (window->window));
986 static void
987 main_window_help_about_cb (GtkAction *action,
988 EmpathyMainWindow *window)
990 empathy_about_dialog_new (GTK_WINDOW (window->window));
993 static void
994 main_window_help_debug_cb (GtkAction *action,
995 EmpathyMainWindow *window)
997 empathy_debug_window_new (NULL);
1000 static void
1001 main_window_help_contents_cb (GtkAction *action,
1002 EmpathyMainWindow *window)
1004 empathy_url_show (window->window, "ghelp:empathy");
1007 static gboolean
1008 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1009 GdkEventButton *event,
1010 EmpathyMainWindow *window)
1012 if (event->type != GDK_BUTTON_PRESS ||
1013 event->button != 1) {
1014 return FALSE;
1017 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1019 return FALSE;
1022 static gboolean
1023 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1025 gint x, y, w, h;
1027 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1028 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1030 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1032 window->size_timeout_id = 0;
1034 return FALSE;
1037 static gboolean
1038 main_window_configure_event_cb (GtkWidget *widget,
1039 GdkEventConfigure *event,
1040 EmpathyMainWindow *window)
1042 if (window->size_timeout_id) {
1043 g_source_remove (window->size_timeout_id);
1046 window->size_timeout_id = g_timeout_add_seconds (1,
1047 (GSourceFunc) main_window_configure_event_timeout_cb,
1048 window);
1050 return FALSE;
1053 static void
1054 main_window_account_removed_cb (TpAccountManager *manager,
1055 TpAccount *account,
1056 EmpathyMainWindow *window)
1058 GList *a;
1060 a = tp_account_manager_get_valid_accounts (manager);
1062 gtk_action_set_sensitive (window->view_history,
1063 g_list_length (a) > 0);
1065 g_list_free (a);
1068 static void
1069 main_window_account_validity_changed_cb (TpAccountManager *manager,
1070 TpAccount *account,
1071 gboolean valid,
1072 EmpathyMainWindow *window)
1074 main_window_account_removed_cb (manager, account, window);
1077 static void
1078 main_window_notify_show_offline_cb (EmpathyConf *conf,
1079 const gchar *key,
1080 gpointer toggle_action)
1082 gboolean show_offline;
1084 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1085 gtk_toggle_action_set_active (toggle_action, show_offline);
1089 static void
1090 main_window_connection_items_setup (EmpathyMainWindow *window,
1091 GtkBuilder *gui)
1093 GList *list;
1094 GObject *action;
1095 guint i;
1096 const gchar *actions_connected[] = {
1097 "room",
1098 "chat_new_message",
1099 "chat_add_contact",
1100 "edit_personal_information"
1103 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1104 action = gtk_builder_get_object (gui, actions_connected[i]);
1105 list = g_list_prepend (list, action);
1108 window->actions_connected = list;
1111 GtkWidget *
1112 empathy_main_window_get (void)
1114 return main_window != NULL ? main_window->window : NULL;
1117 static void
1118 account_manager_prepared_cb (GObject *source_object,
1119 GAsyncResult *result,
1120 gpointer user_data)
1122 GList *accounts, *j;
1123 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1124 EmpathyMainWindow *window = user_data;
1125 GError *error = NULL;
1127 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1128 DEBUG ("Failed to prepare account manager: %s", error->message);
1129 g_error_free (error);
1130 return;
1133 accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1134 for (j = accounts; j != NULL; j = j->next) {
1135 TpAccount *account = TP_ACCOUNT (j->data);
1137 g_signal_connect (account, "status-changed",
1138 G_CALLBACK (main_window_connection_changed_cb),
1139 window);
1142 main_window_update_status (window);
1144 /* Disable the "Previous Conversations" menu entry if there is no account */
1145 gtk_action_set_sensitive (window->view_history,
1146 g_list_length (accounts) > 0);
1148 g_list_free (accounts);
1151 GtkWidget *
1152 empathy_main_window_show (void)
1154 EmpathyMainWindow *window;
1155 EmpathyContactList *list_iface;
1156 EmpathyContactMonitor *monitor;
1157 GtkBuilder *gui;
1158 EmpathyConf *conf;
1159 GtkWidget *sw;
1160 GtkToggleAction *show_offline_widget;
1161 GtkWidget *ebox;
1162 GtkAction *show_map_widget;
1163 GtkToolItem *item;
1164 gboolean show_offline;
1165 gint x, y, w, h;
1166 gchar *filename;
1167 GSList *l;
1169 if (main_window) {
1170 empathy_window_present (GTK_WINDOW (main_window->window), TRUE);
1171 return main_window->window;
1174 main_window = g_new0 (EmpathyMainWindow, 1);
1175 window = main_window;
1177 /* Set up interface */
1178 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1179 gui = empathy_builder_get_file (filename,
1180 "main_window", &window->window,
1181 "main_vbox", &window->main_vbox,
1182 "errors_vbox", &window->errors_vbox,
1183 "ui_manager", &window->ui_manager,
1184 "view_show_offline", &show_offline_widget,
1185 "view_sort_by_name", &window->sort_by_name,
1186 "view_sort_by_status", &window->sort_by_status,
1187 "view_normal_size_with_avatars", &window->normal_with_avatars,
1188 "view_normal_size", &window->normal_size,
1189 "view_compact_size", &window->compact_size,
1190 "view_history", &window->view_history,
1191 "view_show_map", &show_map_widget,
1192 "room_join_favorites", &window->room_join_favorites,
1193 "presence_toolbar", &window->presence_toolbar,
1194 "roster_scrolledwindow", &sw,
1195 NULL);
1196 g_free (filename);
1198 empathy_builder_connect (gui, window,
1199 "main_window", "destroy", main_window_destroy_cb,
1200 "main_window", "configure_event", main_window_configure_event_cb,
1201 "chat_quit", "activate", main_window_chat_quit_cb,
1202 "chat_new_message", "activate", main_window_chat_new_message_cb,
1203 "view_history", "activate", main_window_view_history_cb,
1204 "room_join_new", "activate", main_window_room_join_new_cb,
1205 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1206 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1207 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1208 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1209 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1210 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1211 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1212 "view_show_map", "activate", main_window_view_show_map_cb,
1213 "edit", "activate", main_window_edit_cb,
1214 "edit_accounts", "activate", main_window_edit_accounts_cb,
1215 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1216 "edit_preferences", "activate", main_window_edit_preferences_cb,
1217 "help_about", "activate", main_window_help_about_cb,
1218 "help_debug", "activate", main_window_help_debug_cb,
1219 "help_contents", "activate", main_window_help_contents_cb,
1220 NULL);
1222 /* Set up connection related widgets. */
1223 main_window_connection_items_setup (window, gui);
1225 g_object_ref (window->ui_manager);
1226 g_object_unref (gui);
1228 #if !HAVE_LIBCHAMPLAIN
1229 gtk_action_set_visible (show_map_widget, FALSE);
1230 #endif
1232 window->account_manager = tp_account_manager_dup ();
1234 tp_account_manager_prepare_async (window->account_manager, NULL,
1235 account_manager_prepared_cb, window);
1237 window->errors = g_hash_table_new_full (g_direct_hash,
1238 g_direct_equal,
1239 g_object_unref,
1240 NULL);
1242 /* Set up menu */
1243 main_window_favorite_chatroom_menu_setup (window);
1245 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1246 "/menubar/edit/edit_context");
1247 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1248 "/menubar/edit/edit_context_separator");
1249 gtk_widget_hide (window->edit_context);
1250 gtk_widget_hide (window->edit_context_separator);
1252 /* Set up contact list. */
1253 empathy_status_presets_get_all ();
1255 /* Set up presence chooser */
1256 window->presence_chooser = empathy_presence_chooser_new ();
1257 gtk_widget_show (window->presence_chooser);
1258 item = gtk_tool_item_new ();
1259 gtk_widget_show (GTK_WIDGET (item));
1260 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1261 gtk_tool_item_set_is_important (item, TRUE);
1262 gtk_tool_item_set_expand (item, TRUE);
1263 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1265 /* Set up the throbber */
1266 ebox = gtk_event_box_new ();
1267 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1268 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1269 g_signal_connect (ebox,
1270 "button-press-event",
1271 G_CALLBACK (main_window_throbber_button_press_event_cb),
1272 window);
1273 gtk_widget_show (ebox);
1275 window->throbber = ephy_spinner_new ();
1276 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1277 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1278 gtk_widget_show (window->throbber);
1280 item = gtk_tool_item_new ();
1281 gtk_container_add (GTK_CONTAINER (item), ebox);
1282 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1283 gtk_widget_show (GTK_WIDGET (item));
1285 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1286 monitor = empathy_contact_list_get_monitor (list_iface);
1287 window->list_store = empathy_contact_list_store_new (list_iface);
1288 window->list_view = empathy_contact_list_view_new (window->list_store,
1289 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1290 EMPATHY_CONTACT_FEATURE_ALL);
1291 g_signal_connect (monitor, "contact-presence-changed",
1292 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1293 g_object_unref (list_iface);
1295 gtk_widget_show (GTK_WIDGET (window->list_view));
1296 gtk_container_add (GTK_CONTAINER (sw),
1297 GTK_WIDGET (window->list_view));
1298 g_signal_connect (window->list_view, "row-activated",
1299 G_CALLBACK (main_window_row_activated_cb),
1300 window);
1302 /* Load user-defined accelerators. */
1303 main_window_accels_load ();
1305 /* Set window size. */
1306 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1308 if (w >= 1 && h >= 1) {
1309 /* Use the defaults from the ui file if we
1310 * don't have good w, h geometry.
1312 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1313 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1316 if (x >= 0 && y >= 0) {
1317 /* Let the window manager position it if we
1318 * don't have good x, y coordinates.
1320 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1321 gtk_window_move (GTK_WINDOW (window->window), x, y);
1324 /* Enable event handling */
1325 window->event_manager = empathy_event_manager_dup_singleton ();
1326 g_signal_connect (window->event_manager, "event-added",
1327 G_CALLBACK (main_window_event_added_cb),
1328 window);
1329 g_signal_connect (window->event_manager, "event-removed",
1330 G_CALLBACK (main_window_event_removed_cb),
1331 window);
1333 g_signal_connect (window->account_manager, "account-validity-changed",
1334 G_CALLBACK (main_window_account_validity_changed_cb),
1335 window);
1336 g_signal_connect (window->account_manager, "account-removed",
1337 G_CALLBACK (main_window_account_removed_cb),
1338 window);
1340 l = empathy_event_manager_get_events (window->event_manager);
1341 while (l) {
1342 main_window_event_added_cb (window->event_manager,
1343 l->data, window);
1344 l = l->next;
1347 conf = empathy_conf_get ();
1349 /* Show offline ? */
1350 empathy_conf_get_bool (conf,
1351 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1352 &show_offline);
1353 empathy_conf_notify_add (conf,
1354 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1355 main_window_notify_show_offline_cb,
1356 show_offline_widget);
1358 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1360 /* Sort by name / by status ? */
1361 empathy_conf_notify_add (conf,
1362 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1363 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1364 window);
1366 main_window_notify_sort_contact_cb (conf,
1367 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1368 window);
1370 /* Contacts list size */
1371 empathy_conf_notify_add (conf,
1372 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1373 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1374 window);
1375 empathy_conf_notify_add (conf,
1376 EMPATHY_PREFS_UI_SHOW_AVATARS,
1377 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1378 window);
1380 main_window_notify_contact_list_size_cb (conf,
1381 EMPATHY_PREFS_UI_SHOW_AVATARS,
1382 window);
1384 return window->window;