Don't repeat a tooltip format string
[empathy-mirror.git] / src / empathy-main-window.c
blob5830d2b71b00c78c64fc5cf0a0b2a9d92f88ba20
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 <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>
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>
51 #include <libmissioncontrol/mission-control.h>
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-debug-dialog.h"
59 #include "empathy-new-chatroom-dialog.h"
60 #include "empathy-map-view.h"
61 #include "empathy-chatrooms-window.h"
62 #include "empathy-event-manager.h"
63 #include "empathy-ft-manager.h"
65 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
66 #include <libempathy/empathy-debug.h>
68 /* Flashing delay for icons (milliseconds). */
69 #define FLASH_TIMEOUT 500
71 /* Minimum width of roster window if something goes wrong. */
72 #define MIN_WIDTH 50
74 /* Accels (menu shortcuts) can be configured and saved */
75 #define ACCELS_FILENAME "accels.txt"
77 /* Name in the geometry file */
78 #define GEOMETRY_NAME "main-window"
80 typedef struct {
81 EmpathyContactListView *list_view;
82 EmpathyContactListStore *list_store;
83 MissionControl *mc;
84 EmpathyAccountManager *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 GtkUIManager *ui_manager;
98 GtkAction *view_history;
99 GtkAction *room_join_favorites;
100 GtkWidget *room_menu;
101 GtkWidget *room_separator;
102 GtkWidget *edit_context;
103 GtkWidget *edit_context_separator;
105 guint size_timeout_id;
106 GHashTable *errors;
108 /* Actions that are enabled when there are connected accounts */
109 GList *actions_connected;
110 } EmpathyMainWindow;
112 static EmpathyMainWindow *window = NULL;
114 static void
115 main_window_flash_stop (EmpathyMainWindow *window)
117 if (window->flash_timeout_id == 0) {
118 return;
121 DEBUG ("Stop flashing");
122 g_source_remove (window->flash_timeout_id);
123 window->flash_timeout_id = 0;
124 window->flash_on = FALSE;
127 typedef struct {
128 EmpathyEvent *event;
129 gboolean on;
130 } FlashForeachData;
132 static gboolean
133 main_window_flash_foreach (GtkTreeModel *model,
134 GtkTreePath *path,
135 GtkTreeIter *iter,
136 gpointer user_data)
138 FlashForeachData *data = (FlashForeachData *) user_data;
139 EmpathyContact *contact;
140 const gchar *icon_name;
141 GtkTreePath *parent_path = NULL;
142 GtkTreeIter parent_iter;
144 /* To be used with gtk_tree_model_foreach, update the status icon
145 * of the contact to show the event icon (on=TRUE) or the presence
146 * (on=FALSE) */
147 gtk_tree_model_get (model, iter,
148 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
149 -1);
151 if (contact != data->event->contact) {
152 if (contact) {
153 g_object_unref (contact);
155 return FALSE;
158 if (data->on) {
159 icon_name = data->event->icon_name;
160 } else {
161 icon_name = empathy_icon_name_for_contact (contact);
164 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
165 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
166 -1);
168 /* To make sure the parent is shown correctly, we emit
169 * the row-changed signal on the parent so it prompts
170 * it to be refreshed by the filter func.
172 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
173 parent_path = gtk_tree_model_get_path (model, &parent_iter);
175 if (parent_path) {
176 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
177 gtk_tree_path_free (parent_path);
180 g_object_unref (contact);
182 return FALSE;
185 static gboolean
186 main_window_flash_cb (EmpathyMainWindow *window)
188 GtkTreeModel *model;
189 GSList *events, *l;
190 gboolean found_event = FALSE;
191 FlashForeachData data;
193 window->flash_on = !window->flash_on;
194 data.on = window->flash_on;
195 model = GTK_TREE_MODEL (window->list_store);
197 events = empathy_event_manager_get_events (window->event_manager);
198 for (l = events; l; l = l->next) {
199 data.event = l->data;
200 if (!data.event->contact) {
201 continue;
204 found_event = TRUE;
205 gtk_tree_model_foreach (model,
206 main_window_flash_foreach,
207 &data);
210 if (!found_event) {
211 main_window_flash_stop (window);
214 return TRUE;
217 static void
218 main_window_flash_start (EmpathyMainWindow *window)
220 if (window->flash_timeout_id != 0) {
221 return;
224 DEBUG ("Start flashing");
225 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
226 (GSourceFunc) main_window_flash_cb,
227 window);
228 main_window_flash_cb (window);
231 static void
232 main_window_event_added_cb (EmpathyEventManager *manager,
233 EmpathyEvent *event,
234 EmpathyMainWindow *window)
236 if (event->contact) {
237 main_window_flash_start (window);
241 static void
242 main_window_event_removed_cb (EmpathyEventManager *manager,
243 EmpathyEvent *event,
244 EmpathyMainWindow *window)
246 FlashForeachData data;
248 if (!event->contact) {
249 return;
252 data.on = FALSE;
253 data.event = event;
254 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
255 main_window_flash_foreach,
256 &data);
259 static void
260 main_window_row_activated_cb (EmpathyContactListView *view,
261 GtkTreePath *path,
262 GtkTreeViewColumn *col,
263 EmpathyMainWindow *window)
265 EmpathyContact *contact;
266 GtkTreeModel *model;
267 GtkTreeIter iter;
268 GSList *events, *l;
270 model = GTK_TREE_MODEL (window->list_store);
271 gtk_tree_model_get_iter (model, &iter, path);
272 gtk_tree_model_get (model, &iter,
273 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
274 -1);
276 if (!contact) {
277 return;
280 /* If the contact has an event activate it, otherwise the
281 * default handler of row-activated will be called. */
282 events = empathy_event_manager_get_events (window->event_manager);
283 for (l = events; l; l = l->next) {
284 EmpathyEvent *event = l->data;
286 if (event->contact == contact) {
287 DEBUG ("Activate event");
288 empathy_event_activate (event);
290 /* We don't want the default handler of this signal
291 * (e.g. open a chat) */
292 g_signal_stop_emission_by_name (view, "row-activated");
293 break;
297 g_object_unref (contact);
300 static void
301 main_window_error_edit_clicked_cb (GtkButton *button,
302 EmpathyMainWindow *window)
304 McAccount *account;
305 GtkWidget *error_widget;
307 account = g_object_get_data (G_OBJECT (button), "account");
308 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
310 error_widget = g_hash_table_lookup (window->errors, account);
311 gtk_widget_destroy (error_widget);
312 g_hash_table_remove (window->errors, account);
315 static void
316 main_window_error_clear_clicked_cb (GtkButton *button,
317 EmpathyMainWindow *window)
319 McAccount *account;
320 GtkWidget *error_widget;
322 account = g_object_get_data (G_OBJECT (button), "account");
323 error_widget = g_hash_table_lookup (window->errors, account);
324 gtk_widget_destroy (error_widget);
325 g_hash_table_remove (window->errors, account);
328 static void
329 main_window_error_display (EmpathyMainWindow *window,
330 McAccount *account,
331 const gchar *message)
333 GtkWidget *child;
334 GtkWidget *table;
335 GtkWidget *image;
336 GtkWidget *button_edit;
337 GtkWidget *alignment;
338 GtkWidget *hbox;
339 GtkWidget *label;
340 GtkWidget *fixed;
341 GtkWidget *vbox;
342 GtkWidget *button_close;
343 gchar *str;
345 child = g_hash_table_lookup (window->errors, account);
346 if (child) {
347 label = g_object_get_data (G_OBJECT (child), "label");
349 /* Just set the latest error and return */
350 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
351 mc_account_get_display_name (account),
352 message);
353 gtk_label_set_markup (GTK_LABEL (label), str);
354 g_free (str);
356 return;
359 child = gtk_vbox_new (FALSE, 0);
360 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
361 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
362 gtk_widget_show (child);
364 table = gtk_table_new (2, 4, FALSE);
365 gtk_widget_show (table);
366 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
367 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
368 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
370 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
371 gtk_widget_show (image);
372 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
373 (GtkAttachOptions) (GTK_FILL),
374 (GtkAttachOptions) (GTK_FILL), 0, 0);
375 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
377 button_edit = gtk_button_new ();
378 gtk_widget_show (button_edit);
379 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
380 (GtkAttachOptions) (GTK_FILL),
381 (GtkAttachOptions) (0), 0, 0);
383 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
384 gtk_widget_show (alignment);
385 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
387 hbox = gtk_hbox_new (FALSE, 2);
388 gtk_widget_show (hbox);
389 gtk_container_add (GTK_CONTAINER (alignment), hbox);
391 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
392 gtk_widget_show (image);
393 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
395 label = gtk_label_new_with_mnemonic (_("_Edit account"));
396 gtk_widget_show (label);
397 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
399 fixed = gtk_fixed_new ();
400 gtk_widget_show (fixed);
401 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
402 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
403 (GtkAttachOptions) (GTK_FILL), 0, 0);
405 vbox = gtk_vbox_new (FALSE, 6);
406 gtk_widget_show (vbox);
407 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
408 (GtkAttachOptions) (GTK_FILL),
409 (GtkAttachOptions) (GTK_FILL), 0, 0);
411 button_close = gtk_button_new ();
412 gtk_widget_show (button_close);
413 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
414 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
417 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
418 gtk_widget_show (image);
419 gtk_container_add (GTK_CONTAINER (button_close), image);
421 label = gtk_label_new ("");
422 gtk_widget_show (label);
423 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
424 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
425 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
426 gtk_widget_set_size_request (label, 175, -1);
427 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
428 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
429 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
431 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
432 mc_account_get_display_name (account),
433 message);
434 gtk_label_set_markup (GTK_LABEL (label), str);
435 g_free (str);
437 g_object_set_data (G_OBJECT (child), "label", label);
438 g_object_set_data_full (G_OBJECT (button_edit),
439 "account", g_object_ref (account),
440 g_object_unref);
441 g_object_set_data_full (G_OBJECT (button_close),
442 "account", g_object_ref (account),
443 g_object_unref);
445 g_signal_connect (button_edit, "clicked",
446 G_CALLBACK (main_window_error_edit_clicked_cb),
447 window);
449 g_signal_connect (button_close, "clicked",
450 G_CALLBACK (main_window_error_clear_clicked_cb),
451 window);
453 gtk_widget_show (window->errors_vbox);
455 g_hash_table_insert (window->errors, g_object_ref (account), child);
458 static void
459 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
461 int connected;
462 int connecting;
463 GList *l;
465 /* Count number of connected/connecting/disconnected accounts */
466 connected = empathy_account_manager_get_connected_accounts (manager);
467 connecting = empathy_account_manager_get_connecting_accounts (manager);
469 /* Update the spinner state */
470 if (connecting > 0) {
471 ephy_spinner_start (EPHY_SPINNER (window->throbber));
472 } else {
473 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
476 /* Update widgets sensibility */
477 for (l = window->actions_connected; l; l = l->next) {
478 gtk_action_set_sensitive (l->data, (connected > 0));
482 static void
483 main_window_connection_changed_cb (EmpathyAccountManager *manager,
484 McAccount *account,
485 TpConnectionStatusReason reason,
486 TpConnectionStatus current,
487 TpConnectionStatus previous,
488 EmpathyMainWindow *window)
490 main_window_update_status (window, manager);
492 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
493 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
494 const gchar *message;
496 switch (reason) {
497 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
498 message = _("No error specified");
499 break;
500 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
501 message = _("Network error");
502 break;
503 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
504 message = _("Authentication failed");
505 break;
506 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
507 message = _("Encryption error");
508 break;
509 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
510 message = _("Name in use");
511 break;
512 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
513 message = _("Certificate not provided");
514 break;
515 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
516 message = _("Certificate untrusted");
517 break;
518 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
519 message = _("Certificate expired");
520 break;
521 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
522 message = _("Certificate not activated");
523 break;
524 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
525 message = _("Certificate hostname mismatch");
526 break;
527 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
528 message = _("Certificate fingerprint mismatch");
529 break;
530 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
531 message = _("Certificate self-signed");
532 break;
533 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
534 message = _("Certificate error");
535 break;
536 default:
537 message = _("Unknown error");
538 break;
541 main_window_error_display (window, account, message);
544 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
545 empathy_sound_play (GTK_WIDGET (window->window),
546 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
549 if (current == TP_CONNECTION_STATUS_CONNECTED) {
550 GtkWidget *error_widget;
552 empathy_sound_play (GTK_WIDGET (window->window),
553 EMPATHY_SOUND_ACCOUNT_CONNECTED);
555 /* Account connected without error, remove error message if any */
556 error_widget = g_hash_table_lookup (window->errors, account);
557 if (error_widget) {
558 gtk_widget_destroy (error_widget);
559 g_hash_table_remove (window->errors, account);
564 static void
565 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
566 EmpathyContact *contact,
567 TpConnectionPresenceType current,
568 TpConnectionPresenceType previous,
569 EmpathyMainWindow *window)
571 McAccount *account;
572 gboolean should_play;
574 account = empathy_contact_get_account (contact);
575 should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
577 if (!should_play) {
578 return;
581 if (tp_connection_presence_type_cmp_availability (previous,
582 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
584 /* contact was online */
585 if (tp_connection_presence_type_cmp_availability (current,
586 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
587 /* someone is logging off */
588 empathy_sound_play (GTK_WIDGET (window->window),
589 EMPATHY_SOUND_CONTACT_DISCONNECTED);
591 else
593 /* contact was offline */
594 if (tp_connection_presence_type_cmp_availability (current,
595 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
596 /* someone is logging in */
597 empathy_sound_play (GTK_WIDGET (window->window),
598 EMPATHY_SOUND_CONTACT_CONNECTED);
602 static void
603 main_window_accels_load (void)
605 gchar *filename;
607 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
608 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
609 DEBUG ("Loading from:'%s'", filename);
610 gtk_accel_map_load (filename);
613 g_free (filename);
616 static void
617 main_window_accels_save (void)
619 gchar *dir;
620 gchar *file_with_path;
622 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
623 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
624 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
625 g_free (dir);
627 DEBUG ("Saving to:'%s'", file_with_path);
628 gtk_accel_map_save (file_with_path);
630 g_free (file_with_path);
633 static void
634 main_window_destroy_cb (GtkWidget *widget,
635 EmpathyMainWindow *window)
637 /* Save user-defined accelerators. */
638 main_window_accels_save ();
640 g_signal_handlers_disconnect_by_func (window->account_manager,
641 main_window_connection_changed_cb,
642 window);
644 if (window->size_timeout_id) {
645 g_source_remove (window->size_timeout_id);
648 g_list_free (window->actions_connected);
650 g_object_unref (window->mc);
651 g_object_unref (window->account_manager);
652 g_object_unref (window->list_store);
653 g_hash_table_destroy (window->errors);
655 g_signal_handlers_disconnect_by_func (window->event_manager,
656 main_window_event_added_cb,
657 window);
658 g_signal_handlers_disconnect_by_func (window->event_manager,
659 main_window_event_removed_cb,
660 window);
661 g_object_unref (window->event_manager);
662 g_object_unref (window->ui_manager);
664 g_free (window);
667 static void
668 main_window_chat_quit_cb (GtkAction *action,
669 EmpathyMainWindow *window)
671 gtk_main_quit ();
674 static void
675 main_window_view_history_cb (GtkAction *action,
676 EmpathyMainWindow *window)
678 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
681 static void
682 main_window_chat_new_message_cb (GtkAction *action,
683 EmpathyMainWindow *window)
685 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
688 static void
689 main_window_chat_add_contact_cb (GtkAction *action,
690 EmpathyMainWindow *window)
692 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
695 static void
696 main_window_view_show_ft_manager (GtkAction *action,
697 EmpathyMainWindow *window)
699 empathy_ft_manager_show ();
702 static void
703 main_window_view_show_offline_cb (GtkToggleAction *action,
704 EmpathyMainWindow *window)
706 gboolean current;
708 current = gtk_toggle_action_get_active (action);
709 empathy_conf_set_bool (empathy_conf_get (),
710 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
711 current);
713 /* Turn off sound just while we alter the contact list. */
714 // FIXME: empathy_sound_set_enabled (FALSE);
715 empathy_contact_list_store_set_show_offline (window->list_store, current);
716 //empathy_sound_set_enabled (TRUE);
719 static void
720 main_window_view_show_map_cb (GtkCheckMenuItem *item,
721 EmpathyMainWindow *window)
723 #if HAVE_LIBCHAMPLAIN
724 empathy_map_view_show ();
725 #endif
728 static void
729 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
731 EmpathyAccountManager *manager;
732 McAccount *account;
733 TpConnection *connection;
734 const gchar *room;
736 manager = empathy_account_manager_dup_singleton ();
737 account = empathy_chatroom_get_account (chatroom);
738 connection = empathy_account_manager_get_connection (manager, account);
739 room = empathy_chatroom_get_room (chatroom);
740 g_object_unref (manager);
742 if (connection != NULL) {
743 DEBUG ("Requesting channel for '%s'", room);
744 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
748 static void
749 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
750 EmpathyChatroom *chatroom)
752 main_window_favorite_chatroom_join (chatroom);
755 static void
756 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
757 EmpathyChatroom *chatroom)
759 GtkWidget *menu_item;
760 const gchar *name;
762 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
763 return;
766 name = empathy_chatroom_get_name (chatroom);
767 menu_item = gtk_menu_item_new_with_label (name);
769 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
770 g_signal_connect (menu_item, "activate",
771 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
772 chatroom);
774 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
775 menu_item, 4);
777 gtk_widget_show (menu_item);
780 static void
781 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
782 EmpathyChatroom *chatroom,
783 EmpathyMainWindow *window)
785 main_window_favorite_chatroom_menu_add (window, chatroom);
786 gtk_widget_show (window->room_separator);
787 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
790 static void
791 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
792 EmpathyChatroom *chatroom,
793 EmpathyMainWindow *window)
795 GtkWidget *menu_item;
796 GList *chatrooms;
798 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
799 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
800 gtk_widget_destroy (menu_item);
802 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
803 if (chatrooms) {
804 gtk_widget_show (window->room_separator);
805 } else {
806 gtk_widget_hide (window->room_separator);
809 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
810 g_list_free (chatrooms);
813 static void
814 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
816 GList *chatrooms, *l;
817 GtkWidget *room;
819 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
820 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
821 room = gtk_ui_manager_get_widget (window->ui_manager,
822 "/menubar/room");
823 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
824 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
825 "/menubar/room/room_separator");
827 for (l = chatrooms; l; l = l->next) {
828 main_window_favorite_chatroom_menu_add (window, l->data);
831 if (!chatrooms) {
832 gtk_widget_hide (window->room_separator);
835 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
837 g_signal_connect (window->chatroom_manager, "chatroom-added",
838 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
839 window);
840 g_signal_connect (window->chatroom_manager, "chatroom-removed",
841 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
842 window);
844 g_list_free (chatrooms);
847 static void
848 main_window_room_join_new_cb (GtkAction *action,
849 EmpathyMainWindow *window)
851 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
854 static void
855 main_window_room_join_favorites_cb (GtkAction *action,
856 EmpathyMainWindow *window)
858 GList *chatrooms, *l;
860 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
861 for (l = chatrooms; l; l = l->next) {
862 main_window_favorite_chatroom_join (l->data);
864 g_list_free (chatrooms);
867 static void
868 main_window_room_manage_favorites_cb (GtkAction *action,
869 EmpathyMainWindow *window)
871 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
874 static void
875 main_window_edit_cb (GtkAction *action,
876 EmpathyMainWindow *window)
878 GtkWidget *submenu;
880 /* FIXME: It should use the UIManager to merge the contact/group submenu */
881 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
882 if (submenu) {
883 GtkMenuItem *item;
884 GtkWidget *label;
886 item = GTK_MENU_ITEM (window->edit_context);
887 label = gtk_bin_get_child (GTK_BIN (item));
888 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
890 gtk_widget_show (window->edit_context);
891 gtk_widget_show (window->edit_context_separator);
893 gtk_menu_item_set_submenu (item, submenu);
895 return;
898 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
899 if (submenu) {
900 GtkMenuItem *item;
901 GtkWidget *label;
903 item = GTK_MENU_ITEM (window->edit_context);
904 label = gtk_bin_get_child (GTK_BIN (item));
905 gtk_label_set_text (GTK_LABEL (label), _("Group"));
907 gtk_widget_show (window->edit_context);
908 gtk_widget_show (window->edit_context_separator);
910 gtk_menu_item_set_submenu (item, submenu);
912 return;
915 gtk_widget_hide (window->edit_context);
916 gtk_widget_hide (window->edit_context_separator);
918 return;
921 static void
922 main_window_edit_accounts_cb (GtkAction *action,
923 EmpathyMainWindow *window)
925 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
928 static void
929 main_window_edit_personal_information_cb (GtkAction *action,
930 EmpathyMainWindow *window)
932 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
935 static void
936 main_window_edit_preferences_cb (GtkAction *action,
937 EmpathyMainWindow *window)
939 empathy_preferences_show (GTK_WINDOW (window->window));
942 static void
943 main_window_help_about_cb (GtkAction *action,
944 EmpathyMainWindow *window)
946 empathy_about_dialog_new (GTK_WINDOW (window->window));
949 static void
950 main_window_help_debug_cb (GtkAction *action,
951 EmpathyMainWindow *window)
953 empathy_debug_dialog_new (GTK_WINDOW (window->window));
956 static void
957 main_window_help_contents_cb (GtkAction *action,
958 EmpathyMainWindow *window)
960 empathy_url_show (window->window, "ghelp:empathy");
963 static gboolean
964 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
965 GdkEventButton *event,
966 EmpathyMainWindow *window)
968 if (event->type != GDK_BUTTON_PRESS ||
969 event->button != 1) {
970 return FALSE;
973 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
975 return FALSE;
978 static gboolean
979 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
981 gint x, y, w, h;
983 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
984 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
986 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
988 window->size_timeout_id = 0;
990 return FALSE;
993 static gboolean
994 main_window_configure_event_cb (GtkWidget *widget,
995 GdkEventConfigure *event,
996 EmpathyMainWindow *window)
998 if (window->size_timeout_id) {
999 g_source_remove (window->size_timeout_id);
1002 window->size_timeout_id = g_timeout_add_seconds (1,
1003 (GSourceFunc) main_window_configure_event_timeout_cb,
1004 window);
1006 return FALSE;
1009 static void
1010 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1011 McAccount *account,
1012 EmpathyMainWindow *window)
1014 gtk_action_set_sensitive (window->view_history,
1015 empathy_account_manager_get_count (manager) > 0);
1018 static void
1019 main_window_notify_show_offline_cb (EmpathyConf *conf,
1020 const gchar *key,
1021 gpointer toggle_action)
1023 gboolean show_offline;
1025 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1026 gtk_toggle_action_set_active (toggle_action, show_offline);
1030 static void
1031 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1032 const gchar *key,
1033 EmpathyMainWindow *window)
1035 gboolean show_avatars;
1037 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1038 empathy_contact_list_store_set_show_avatars (window->list_store,
1039 show_avatars);
1043 static void
1044 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1045 const gchar *key,
1046 EmpathyMainWindow *window)
1048 gboolean compact_contact_list;
1050 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1051 empathy_contact_list_store_set_is_compact (window->list_store,
1052 compact_contact_list);
1056 static void
1057 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1058 const gchar *key,
1059 EmpathyMainWindow *window)
1061 gchar *str = NULL;
1063 if (empathy_conf_get_string (conf, key, &str) && str) {
1064 GType type;
1065 GEnumClass *enum_class;
1066 GEnumValue *enum_value;
1068 type = empathy_contact_list_store_sort_get_type ();
1069 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1070 enum_value = g_enum_get_value_by_nick (enum_class, str);
1071 g_free (str);
1073 if (enum_value) {
1074 empathy_contact_list_store_set_sort_criterium (window->list_store,
1075 enum_value->value);
1080 static void
1081 main_window_connection_items_setup (EmpathyMainWindow *window,
1082 GtkBuilder *gui)
1084 GList *list;
1085 GObject *action;
1086 gint i;
1087 const gchar *actions_connected[] = {
1088 "room",
1089 "chat_new_message",
1090 "chat_add_contact",
1091 "edit_personal_information"
1094 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1095 action = gtk_builder_get_object (gui, actions_connected[i]);
1096 list = g_list_prepend (list, action);
1099 window->actions_connected = list;
1102 GtkWidget *
1103 empathy_main_window_get (void)
1105 return window != NULL ? window->window : NULL;
1108 GtkWidget *
1109 empathy_main_window_show (void)
1111 EmpathyContactList *list_iface;
1112 EmpathyContactMonitor *monitor;
1113 GtkBuilder *gui;
1114 EmpathyConf *conf;
1115 GtkWidget *sw;
1116 GtkToggleAction *show_offline_widget;
1117 GtkWidget *ebox;
1118 GtkAction *show_map_widget;
1119 GtkToolItem *item;
1120 gboolean show_offline;
1121 gboolean show_avatars;
1122 gboolean compact_contact_list;
1123 gint x, y, w, h;
1124 gchar *filename;
1125 GSList *l;
1127 if (window) {
1128 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1129 return window->window;
1132 window = g_new0 (EmpathyMainWindow, 1);
1134 /* Set up interface */
1135 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1136 gui = empathy_builder_get_file (filename,
1137 "main_window", &window->window,
1138 "main_vbox", &window->main_vbox,
1139 "errors_vbox", &window->errors_vbox,
1140 "ui_manager", &window->ui_manager,
1141 "view_show_offline", &show_offline_widget,
1142 "view_history", &window->view_history,
1143 "view_show_map", &show_map_widget,
1144 "room_join_favorites", &window->room_join_favorites,
1145 "presence_toolbar", &window->presence_toolbar,
1146 "roster_scrolledwindow", &sw,
1147 NULL);
1148 g_free (filename);
1150 empathy_builder_connect (gui, window,
1151 "main_window", "destroy", main_window_destroy_cb,
1152 "main_window", "configure_event", main_window_configure_event_cb,
1153 "chat_quit", "activate", main_window_chat_quit_cb,
1154 "chat_new_message", "activate", main_window_chat_new_message_cb,
1155 "view_history", "activate", main_window_view_history_cb,
1156 "room_join_new", "activate", main_window_room_join_new_cb,
1157 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1158 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1159 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1160 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1161 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1162 "view_show_map", "activate", main_window_view_show_map_cb,
1163 "edit", "activate", main_window_edit_cb,
1164 "edit_accounts", "activate", main_window_edit_accounts_cb,
1165 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1166 "edit_preferences", "activate", main_window_edit_preferences_cb,
1167 "help_about", "activate", main_window_help_about_cb,
1168 "help_debug", "activate", main_window_help_debug_cb,
1169 "help_contents", "activate", main_window_help_contents_cb,
1170 NULL);
1172 /* Set up connection related widgets. */
1173 main_window_connection_items_setup (window, gui);
1175 g_object_ref (window->ui_manager);
1176 g_object_unref (gui);
1178 #if !HAVE_LIBCHAMPLAIN
1179 gtk_action_set_visible (show_map_widget, FALSE);
1180 #endif
1182 window->mc = empathy_mission_control_dup_singleton ();
1183 window->account_manager = empathy_account_manager_dup_singleton ();
1185 g_signal_connect (window->account_manager,
1186 "account-connection-changed",
1187 G_CALLBACK (main_window_connection_changed_cb), window);
1189 window->errors = g_hash_table_new_full (empathy_account_hash,
1190 empathy_account_equal,
1191 g_object_unref,
1192 NULL);
1194 /* Set up menu */
1195 main_window_favorite_chatroom_menu_setup (window);
1197 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1198 "/menubar/edit/edit_context");
1199 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1200 "/menubar/edit/edit_context_separator");
1201 gtk_widget_hide (window->edit_context);
1202 gtk_widget_hide (window->edit_context_separator);
1204 /* Set up contact list. */
1205 empathy_status_presets_get_all ();
1207 /* Set up presence chooser */
1208 window->presence_chooser = empathy_presence_chooser_new ();
1209 gtk_widget_show (window->presence_chooser);
1210 item = gtk_tool_item_new ();
1211 gtk_widget_show (GTK_WIDGET (item));
1212 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1213 gtk_tool_item_set_is_important (item, TRUE);
1214 gtk_tool_item_set_expand (item, TRUE);
1215 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1217 /* Set up the throbber */
1218 ebox = gtk_event_box_new ();
1219 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1220 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1221 g_signal_connect (ebox,
1222 "button-press-event",
1223 G_CALLBACK (main_window_throbber_button_press_event_cb),
1224 window);
1225 gtk_widget_show (ebox);
1227 window->throbber = ephy_spinner_new ();
1228 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1229 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1230 gtk_widget_show (window->throbber);
1232 item = gtk_tool_item_new ();
1233 gtk_container_add (GTK_CONTAINER (item), ebox);
1234 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1235 gtk_widget_show (GTK_WIDGET (item));
1237 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1238 monitor = empathy_contact_list_get_monitor (list_iface);
1239 window->list_store = empathy_contact_list_store_new (list_iface);
1240 window->list_view = empathy_contact_list_view_new (window->list_store,
1241 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1242 EMPATHY_CONTACT_FEATURE_ALL);
1243 g_signal_connect (monitor, "contact-presence-changed",
1244 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1245 g_object_unref (list_iface);
1247 gtk_widget_show (GTK_WIDGET (window->list_view));
1248 gtk_container_add (GTK_CONTAINER (sw),
1249 GTK_WIDGET (window->list_view));
1250 g_signal_connect (window->list_view, "row-activated",
1251 G_CALLBACK (main_window_row_activated_cb),
1252 window);
1254 /* Load user-defined accelerators. */
1255 main_window_accels_load ();
1257 /* Set window size. */
1258 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1260 if (w >= 1 && h >= 1) {
1261 /* Use the defaults from the ui file if we
1262 * don't have good w, h geometry.
1264 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1265 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1268 if (x >= 0 && y >= 0) {
1269 /* Let the window manager position it if we
1270 * don't have good x, y coordinates.
1272 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1273 gtk_window_move (GTK_WINDOW (window->window), x, y);
1276 /* Enable event handling */
1277 window->event_manager = empathy_event_manager_dup_singleton ();
1278 g_signal_connect (window->event_manager, "event-added",
1279 G_CALLBACK (main_window_event_added_cb),
1280 window);
1281 g_signal_connect (window->event_manager, "event-removed",
1282 G_CALLBACK (main_window_event_removed_cb),
1283 window);
1285 g_signal_connect (window->account_manager, "account-created",
1286 G_CALLBACK (main_window_account_created_or_deleted_cb),
1287 window);
1288 g_signal_connect (window->account_manager, "account-deleted",
1289 G_CALLBACK (main_window_account_created_or_deleted_cb),
1290 window);
1291 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1293 l = empathy_event_manager_get_events (window->event_manager);
1294 while (l) {
1295 main_window_event_added_cb (window->event_manager,
1296 l->data, window);
1297 l = l->next;
1300 conf = empathy_conf_get ();
1302 /* Show offline ? */
1303 empathy_conf_get_bool (conf,
1304 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1305 &show_offline);
1306 empathy_conf_notify_add (conf,
1307 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1308 main_window_notify_show_offline_cb,
1309 show_offline_widget);
1311 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1313 /* Show avatars ? */
1314 empathy_conf_get_bool (conf,
1315 EMPATHY_PREFS_UI_SHOW_AVATARS,
1316 &show_avatars);
1317 empathy_conf_notify_add (conf,
1318 EMPATHY_PREFS_UI_SHOW_AVATARS,
1319 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1320 window);
1321 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1323 /* Is compact ? */
1324 empathy_conf_get_bool (conf,
1325 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1326 &compact_contact_list);
1327 empathy_conf_notify_add (conf,
1328 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1329 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1330 window);
1331 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1333 /* Sort criterium */
1334 empathy_conf_notify_add (conf,
1335 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1336 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1337 window);
1338 main_window_notify_sort_criterium_cb (conf,
1339 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1340 window);
1342 main_window_update_status (window, window->account_manager);
1344 return window->window;