1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-logger/log-manager.h>
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-request-util.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-chatroom-manager.h>
34 #include <libempathy/empathy-contact-manager.h>
36 #include "empathy-contact-menu.h"
37 #include "empathy-images.h"
38 #include "empathy-log-window.h"
39 #include "empathy-contact-dialogs.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-share-my-desktop.h"
42 #include "empathy-call-utils.h"
44 static GtkWidget *empathy_contact_block_menu_item_new (EmpathyContact *);
47 empathy_contact_menu_new (EmpathyContact *contact,
48 EmpathyContactFeatureFlags features)
54 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
56 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
60 menu = gtk_menu_new ();
61 shell = GTK_MENU_SHELL (menu);
64 item = empathy_contact_add_menu_item_new (contact);
66 gtk_menu_shell_append (shell, item);
67 gtk_widget_show (item);
71 if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
72 item = empathy_contact_chat_menu_item_new (contact);
73 gtk_menu_shell_append (shell, item);
74 gtk_widget_show (item);
77 if (features & EMPATHY_CONTACT_FEATURE_CALL) {
79 item = empathy_contact_audio_call_menu_item_new (contact);
80 gtk_menu_shell_append (shell, item);
81 gtk_widget_show (item);
84 item = empathy_contact_video_call_menu_item_new (contact);
85 gtk_menu_shell_append (shell, item);
86 gtk_widget_show (item);
90 item = empathy_contact_invite_menu_item_new (contact);
91 gtk_menu_shell_append (shell, item);
92 gtk_widget_show (item);
95 if (features & EMPATHY_CONTACT_FEATURE_FT) {
96 item = empathy_contact_file_transfer_menu_item_new (contact);
97 gtk_menu_shell_append (shell, item);
98 gtk_widget_show (item);
101 /* Share my desktop */
102 /* FIXME we should add the "Share my desktop" menu item if Vino is
103 a registered handler in MC5 */
104 item = empathy_contact_share_my_desktop_menu_item_new (contact);
105 gtk_menu_shell_append (shell, item);
106 gtk_widget_show (item);
109 if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
110 EMPATHY_CONTACT_FEATURE_INFO |
111 EMPATHY_CONTACT_FEATURE_FAVOURITE)) {
112 item = gtk_separator_menu_item_new ();
113 gtk_menu_shell_append (shell, item);
114 gtk_widget_show (item);
118 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
119 item = empathy_contact_edit_menu_item_new (contact);
120 gtk_menu_shell_append (shell, item);
121 gtk_widget_show (item);
125 if (features & EMPATHY_CONTACT_FEATURE_LOG) {
126 item = empathy_contact_log_menu_item_new (contact);
127 gtk_menu_shell_append (shell, item);
128 gtk_widget_show (item);
132 if (features & EMPATHY_CONTACT_FEATURE_INFO) {
133 item = empathy_contact_info_menu_item_new (contact);
134 gtk_menu_shell_append (shell, item);
135 gtk_widget_show (item);
138 /* Favorite checkbox */
139 if (features & EMPATHY_CONTACT_FEATURE_FAVOURITE) {
140 item = empathy_contact_favourite_menu_item_new (contact);
141 gtk_menu_shell_append (shell, item);
142 gtk_widget_show (item);
145 /* Separator & Block */
146 if (features & EMPATHY_CONTACT_FEATURE_BLOCK &&
147 (item = empathy_contact_block_menu_item_new (contact)) != NULL) {
150 sep = gtk_separator_menu_item_new ();
151 gtk_menu_shell_append (shell, sep);
152 gtk_widget_show (sep);
154 gtk_menu_shell_append (shell, item);
155 gtk_widget_show (item);
162 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
163 EmpathyContact *contact)
167 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
168 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
172 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
177 empathy_contact_add_menu_item_new (EmpathyContact *contact)
181 EmpathyContactManager *manager;
182 TpConnection *connection;
184 gboolean found = FALSE;
185 EmpathyContactListFlags flags;
187 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
189 if (!empathy_contact_manager_initialized ()) {
193 manager = empathy_contact_manager_dup_singleton ();
194 connection = empathy_contact_get_connection (contact);
196 flags = empathy_contact_manager_get_flags_for_connection (manager,
199 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
203 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
204 for (l = members; l; l = l->next) {
205 if (!found && empathy_contact_equal (l->data, contact)) {
207 /* we keep iterating so that we don't leak contact
211 g_object_unref (l->data);
213 g_list_free (members);
214 g_object_unref (manager);
220 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
221 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
223 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
225 g_signal_connect (item, "activate",
226 G_CALLBACK (empathy_contact_add_menu_item_activated),
233 empathy_contact_block_menu_item_toggled (GtkCheckMenuItem *item,
234 EmpathyContact *contact)
236 static guint block_signal = 0;
237 EmpathyContactManager *manager;
238 gboolean blocked, abusive;
240 if (block_signal > 0)
243 blocked = gtk_check_menu_item_get_active (item);
246 /* confirm the user really wishes to block the contact */
250 /* gtk_menu_get_attach_widget () doesn't behave properly here
252 parent = g_object_get_data (
253 G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (item))),
256 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48);
258 if (!empathy_block_contact_dialog_show (GTK_WINDOW (parent),
259 contact, avatar, &abusive))
263 manager = empathy_contact_manager_dup_singleton ();
264 empathy_contact_list_set_blocked (EMPATHY_CONTACT_LIST (manager),
265 contact, blocked, abusive);
266 g_object_unref (manager);
268 /* update the toggle with the blocked status */
270 gtk_check_menu_item_set_active (item, blocked);
275 empathy_contact_block_menu_item_new (EmpathyContact *contact)
278 EmpathyContactManager *manager;
279 TpConnection *connection;
280 EmpathyContactListFlags flags;
283 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
285 manager = empathy_contact_manager_dup_singleton ();
287 if (!empathy_contact_manager_initialized ()) {
291 connection = empathy_contact_get_connection (contact);
293 flags = empathy_contact_manager_get_flags_for_connection (manager,
296 if (!(flags & EMPATHY_CONTACT_LIST_CAN_BLOCK)) {
300 item = gtk_check_menu_item_new_with_mnemonic (_("_Block Contact"));
301 blocked = empathy_contact_list_get_blocked (
302 EMPATHY_CONTACT_LIST (manager),
305 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), blocked);
307 g_signal_connect (item, "toggled",
308 G_CALLBACK (empathy_contact_block_menu_item_toggled),
315 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
316 EmpathyContact *contact)
318 empathy_chat_with_contact (contact, empathy_get_current_action_time ());
322 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
327 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
329 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
330 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
332 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
333 gtk_widget_set_sensitive (item, !empathy_contact_is_user (contact));
334 gtk_widget_show (image);
336 g_signal_connect (item, "activate",
337 G_CALLBACK (empathy_contact_chat_menu_item_activated),
344 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
345 EmpathyContact *contact)
347 empathy_call_new_with_streams (empathy_contact_get_id (contact),
348 empathy_contact_get_account (contact),
350 empathy_get_current_action_time ());
354 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
359 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
361 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
362 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
364 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
365 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact) &&
366 !empathy_contact_is_user (contact));
367 gtk_widget_show (image);
369 g_signal_connect (item, "activate",
370 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
377 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
378 EmpathyContact *contact)
380 empathy_call_new_with_streams (empathy_contact_get_id (contact),
381 empathy_contact_get_account (contact),
383 empathy_get_current_action_time ());
387 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
392 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
394 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
395 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
397 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
398 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact) &&
399 !empathy_contact_is_user (contact));
400 gtk_widget_show (image);
402 g_signal_connect (item, "activate",
403 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
410 contact_log_menu_item_activate_cb (EmpathyContact *contact)
412 empathy_log_window_show (empathy_contact_get_account (contact),
413 empathy_contact_get_id (contact),
418 empathy_contact_log_menu_item_new (EmpathyContact *contact)
420 TplLogManager *manager;
426 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
428 manager = tpl_log_manager_dup_singleton ();
429 entity = tpl_entity_new_from_tp_contact (empathy_contact_get_tp_contact (contact),
432 have_log = tpl_log_manager_exists (manager,
433 empathy_contact_get_account (contact),
435 TPL_EVENT_MASK_TEXT);
437 g_object_unref (entity);
438 g_object_unref (manager);
440 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
441 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
443 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
444 gtk_widget_set_sensitive (item, have_log);
445 gtk_widget_show (image);
447 g_signal_connect_swapped (item, "activate",
448 G_CALLBACK (contact_log_menu_item_activate_cb),
455 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
460 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
462 item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
463 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
465 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact) &&
466 !empathy_contact_is_user (contact));
467 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
468 gtk_widget_show (image);
470 g_signal_connect_swapped (item, "activate",
471 G_CALLBACK (empathy_send_file_with_file_chooser),
478 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
483 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
485 item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
486 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
488 gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
490 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
491 gtk_widget_show (image);
493 g_signal_connect_swapped (item, "activate",
494 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
501 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
502 EmpathyContact *contact)
504 EmpathyContactManager *manager;
505 EmpathyContactList *list;
507 manager = empathy_contact_manager_dup_singleton ();
508 list = EMPATHY_CONTACT_LIST (manager);
510 if (gtk_check_menu_item_get_active (item)) {
511 empathy_contact_list_add_to_favourites (list, contact);
513 empathy_contact_list_remove_from_favourites (list, contact);
516 g_object_unref (manager);
520 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
523 EmpathyContactManager *manager;
525 item = gtk_check_menu_item_new_with_label (_("Favorite"));
527 manager = empathy_contact_manager_dup_singleton ();
528 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
529 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
532 g_signal_connect (item, "toggled",
533 G_CALLBACK (favourite_menu_item_toggled_cb),
536 g_object_unref (manager);
541 contact_info_menu_item_activate_cb (EmpathyContact *contact)
543 empathy_contact_information_dialog_show (contact, NULL);
547 empathy_contact_info_menu_item_new (EmpathyContact *contact)
552 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
554 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
555 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
557 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
558 gtk_widget_show (image);
560 g_signal_connect_swapped (item, "activate",
561 G_CALLBACK (contact_info_menu_item_activate_cb),
568 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
570 empathy_contact_edit_dialog_show (contact, NULL);
574 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
576 EmpathyContactManager *manager;
579 gboolean enable = FALSE;
581 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
583 if (empathy_contact_manager_initialized ()) {
584 TpConnection *connection;
585 EmpathyContactListFlags flags;
587 manager = empathy_contact_manager_dup_singleton ();
588 connection = empathy_contact_get_connection (contact);
589 flags = empathy_contact_manager_get_flags_for_connection (
590 manager, connection);
592 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
593 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
595 g_object_unref (manager);
598 item = gtk_image_menu_item_new_with_mnemonic (
599 C_("Edit contact (contextual menu)",
601 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
603 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
604 gtk_widget_show (image);
606 gtk_widget_set_sensitive (item, enable);
608 g_signal_connect_swapped (item, "activate",
609 G_CALLBACK (contact_edit_menu_item_activate_cb),
616 EmpathyContact *contact;
617 EmpathyChatroom *chatroom;
620 static RoomSubMenuData *
621 room_sub_menu_data_new (EmpathyContact *contact,
622 EmpathyChatroom *chatroom)
624 RoomSubMenuData *data;
626 data = g_slice_new (RoomSubMenuData);
627 data->contact = g_object_ref (contact);
628 data->chatroom = g_object_ref (chatroom);
633 room_sub_menu_data_free (RoomSubMenuData *data)
635 g_object_unref (data->contact);
636 g_object_unref (data->chatroom);
637 g_slice_free (RoomSubMenuData, data);
641 room_sub_menu_activate_cb (GtkWidget *item,
642 RoomSubMenuData *data)
646 chat = empathy_chatroom_get_tp_chat (data->chatroom);
648 /* channel was invalidated. Ignoring */
652 /* send invitation */
653 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
654 _("Inviting you to this room"));
658 create_room_sub_menu (EmpathyContact *contact,
659 EmpathyChatroom *chatroom)
662 RoomSubMenuData *data;
664 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
665 data = room_sub_menu_data_new (contact, chatroom);
666 g_signal_connect_data (item, "activate",
667 G_CALLBACK (room_sub_menu_activate_cb), data,
668 (GClosureNotify) room_sub_menu_data_free, 0);
674 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
678 GtkWidget *room_item;
679 EmpathyChatroomManager *mgr;
681 GtkWidget *submenu = NULL;
683 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
685 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
686 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
688 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
690 if (empathy_contact_is_user (contact)) {
691 gtk_widget_set_sensitive (item, FALSE);
692 gtk_widget_show (image);
696 mgr = empathy_chatroom_manager_dup_singleton (NULL);
697 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
698 empathy_contact_get_account (contact));
700 for (l = rooms; l != NULL; l = g_list_next (l)) {
701 EmpathyChatroom *chatroom = l->data;
703 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
704 if (G_UNLIKELY (submenu == NULL))
705 submenu = gtk_menu_new ();
707 room_item = create_room_sub_menu (contact, chatroom);
708 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
709 gtk_widget_show (room_item);
714 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
716 gtk_widget_set_sensitive (item, FALSE);
719 gtk_widget_show (image);
721 g_object_unref (mgr);