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 item = gtk_separator_menu_item_new ();
112 gtk_menu_shell_append (shell, item);
113 gtk_widget_show (item);
117 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
118 item = empathy_contact_edit_menu_item_new (contact);
119 gtk_menu_shell_append (shell, item);
120 gtk_widget_show (item);
124 if (features & EMPATHY_CONTACT_FEATURE_LOG) {
125 item = empathy_contact_log_menu_item_new (contact);
126 gtk_menu_shell_append (shell, item);
127 gtk_widget_show (item);
131 if (features & EMPATHY_CONTACT_FEATURE_INFO) {
132 item = empathy_contact_info_menu_item_new (contact);
133 gtk_menu_shell_append (shell, item);
134 gtk_widget_show (item);
137 /* Separator & Block */
138 if (features & EMPATHY_CONTACT_FEATURE_BLOCK &&
139 (item = empathy_contact_block_menu_item_new (contact)) != NULL) {
142 sep = gtk_separator_menu_item_new ();
143 gtk_menu_shell_append (shell, sep);
144 gtk_widget_show (sep);
146 gtk_menu_shell_append (shell, item);
147 gtk_widget_show (item);
154 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
155 EmpathyContact *contact)
159 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
160 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
164 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
169 empathy_contact_add_menu_item_new (EmpathyContact *contact)
173 EmpathyContactManager *manager;
174 TpConnection *connection;
176 gboolean found = FALSE;
177 EmpathyContactListFlags flags;
179 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
181 if (!empathy_contact_manager_initialized ()) {
185 manager = empathy_contact_manager_dup_singleton ();
186 connection = empathy_contact_get_connection (contact);
188 flags = empathy_contact_manager_get_flags_for_connection (manager,
191 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
195 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
196 for (l = members; l; l = l->next) {
197 if (!found && empathy_contact_equal (l->data, contact)) {
199 /* we keep iterating so that we don't leak contact
203 g_object_unref (l->data);
205 g_list_free (members);
206 g_object_unref (manager);
212 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
213 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
215 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
217 g_signal_connect (item, "activate",
218 G_CALLBACK (empathy_contact_add_menu_item_activated),
225 empathy_contact_block_menu_item_toggled (GtkCheckMenuItem *item,
226 EmpathyContact *contact)
228 static guint block_signal = 0;
229 gboolean blocked, abusive;
230 TpContact *tp_contact;
232 if (block_signal > 0)
235 blocked = gtk_check_menu_item_get_active (item);
238 /* confirm the user really wishes to block the contact */
242 /* gtk_menu_get_attach_widget () doesn't behave properly here
244 parent = g_object_get_data (
245 G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (item))),
248 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48);
250 if (!empathy_block_contact_dialog_show (GTK_WINDOW (parent),
251 contact, avatar, &abusive))
255 tp_contact = empathy_contact_get_tp_contact (contact);
258 tp_contact_block_async (tp_contact, abusive, NULL, NULL);
260 tp_contact_unblock_async (tp_contact, NULL, NULL);
262 /* update the toggle with the blocked status */
264 gtk_check_menu_item_set_active (item, blocked);
269 empathy_contact_block_menu_item_new (EmpathyContact *contact)
272 EmpathyContactManager *manager;
273 TpConnection *connection;
274 TpContact *tp_contact;
276 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
278 manager = empathy_contact_manager_dup_singleton ();
280 if (!empathy_contact_manager_initialized ()) {
284 connection = empathy_contact_get_connection (contact);
286 if (!tp_proxy_has_interface_by_id (connection,
287 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
290 item = gtk_check_menu_item_new_with_mnemonic (_("_Block Contact"));
292 tp_contact = empathy_contact_get_tp_contact (contact);
294 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
295 tp_contact_is_blocked (tp_contact));
297 g_signal_connect (item, "toggled",
298 G_CALLBACK (empathy_contact_block_menu_item_toggled),
305 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
306 EmpathyContact *contact)
308 empathy_chat_with_contact (contact, empathy_get_current_action_time ());
312 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
317 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
319 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
320 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
322 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
323 gtk_widget_set_sensitive (item, !empathy_contact_is_user (contact));
324 gtk_widget_show (image);
326 g_signal_connect (item, "activate",
327 G_CALLBACK (empathy_contact_chat_menu_item_activated),
334 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
335 EmpathyContact *contact)
337 empathy_call_new_with_streams (empathy_contact_get_id (contact),
338 empathy_contact_get_account (contact),
340 empathy_get_current_action_time ());
344 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
349 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
351 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
352 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
354 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
355 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact) &&
356 !empathy_contact_is_user (contact));
357 gtk_widget_show (image);
359 g_signal_connect (item, "activate",
360 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
367 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
368 EmpathyContact *contact)
370 empathy_call_new_with_streams (empathy_contact_get_id (contact),
371 empathy_contact_get_account (contact),
373 empathy_get_current_action_time ());
377 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
382 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
384 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
385 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
387 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
388 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact) &&
389 !empathy_contact_is_user (contact));
390 gtk_widget_show (image);
392 g_signal_connect (item, "activate",
393 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
400 contact_log_menu_item_activate_cb (EmpathyContact *contact)
402 empathy_log_window_show (empathy_contact_get_account (contact),
403 empathy_contact_get_id (contact),
408 empathy_contact_log_menu_item_new (EmpathyContact *contact)
410 TplLogManager *manager;
416 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
418 manager = tpl_log_manager_dup_singleton ();
419 entity = tpl_entity_new_from_tp_contact (empathy_contact_get_tp_contact (contact),
422 have_log = tpl_log_manager_exists (manager,
423 empathy_contact_get_account (contact),
425 TPL_EVENT_MASK_TEXT);
427 g_object_unref (entity);
428 g_object_unref (manager);
430 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
431 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
433 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
434 gtk_widget_set_sensitive (item, have_log);
435 gtk_widget_show (image);
437 g_signal_connect_swapped (item, "activate",
438 G_CALLBACK (contact_log_menu_item_activate_cb),
445 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
450 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
452 item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
453 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
455 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact) &&
456 !empathy_contact_is_user (contact));
457 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
458 gtk_widget_show (image);
460 g_signal_connect_swapped (item, "activate",
461 G_CALLBACK (empathy_send_file_with_file_chooser),
468 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
473 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
475 item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
476 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
478 gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
480 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
481 gtk_widget_show (image);
483 g_signal_connect_swapped (item, "activate",
484 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
491 contact_info_menu_item_activate_cb (EmpathyContact *contact)
493 empathy_contact_information_dialog_show (contact, NULL);
497 empathy_contact_info_menu_item_new (EmpathyContact *contact)
502 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
504 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
505 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
507 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
508 gtk_widget_show (image);
510 g_signal_connect_swapped (item, "activate",
511 G_CALLBACK (contact_info_menu_item_activate_cb),
518 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
520 empathy_contact_edit_dialog_show (contact, NULL);
524 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
526 EmpathyContactManager *manager;
529 gboolean enable = FALSE;
531 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
533 if (empathy_contact_manager_initialized ()) {
534 TpConnection *connection;
535 EmpathyContactListFlags flags;
537 manager = empathy_contact_manager_dup_singleton ();
538 connection = empathy_contact_get_connection (contact);
539 flags = empathy_contact_manager_get_flags_for_connection (
540 manager, connection);
542 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
543 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
545 g_object_unref (manager);
548 item = gtk_image_menu_item_new_with_mnemonic (
549 C_("Edit contact (contextual menu)",
551 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
553 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
554 gtk_widget_show (image);
556 gtk_widget_set_sensitive (item, enable);
558 g_signal_connect_swapped (item, "activate",
559 G_CALLBACK (contact_edit_menu_item_activate_cb),
566 EmpathyContact *contact;
567 EmpathyChatroom *chatroom;
570 static RoomSubMenuData *
571 room_sub_menu_data_new (EmpathyContact *contact,
572 EmpathyChatroom *chatroom)
574 RoomSubMenuData *data;
576 data = g_slice_new (RoomSubMenuData);
577 data->contact = g_object_ref (contact);
578 data->chatroom = g_object_ref (chatroom);
583 room_sub_menu_data_free (RoomSubMenuData *data)
585 g_object_unref (data->contact);
586 g_object_unref (data->chatroom);
587 g_slice_free (RoomSubMenuData, data);
591 room_sub_menu_activate_cb (GtkWidget *item,
592 RoomSubMenuData *data)
596 chat = empathy_chatroom_get_tp_chat (data->chatroom);
598 /* channel was invalidated. Ignoring */
602 /* send invitation */
603 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
604 _("Inviting you to this room"));
608 create_room_sub_menu (EmpathyContact *contact,
609 EmpathyChatroom *chatroom)
612 RoomSubMenuData *data;
614 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
615 data = room_sub_menu_data_new (contact, chatroom);
616 g_signal_connect_data (item, "activate",
617 G_CALLBACK (room_sub_menu_activate_cb), data,
618 (GClosureNotify) room_sub_menu_data_free, 0);
624 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
628 GtkWidget *room_item;
629 EmpathyChatroomManager *mgr;
631 GtkWidget *submenu = NULL;
633 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
635 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
636 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
638 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
640 if (empathy_contact_is_user (contact)) {
641 gtk_widget_set_sensitive (item, FALSE);
642 gtk_widget_show (image);
646 mgr = empathy_chatroom_manager_dup_singleton (NULL);
647 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
648 empathy_contact_get_account (contact));
650 for (l = rooms; l != NULL; l = g_list_next (l)) {
651 EmpathyChatroom *chatroom = l->data;
653 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
654 if (G_UNLIKELY (submenu == NULL))
655 submenu = gtk_menu_new ();
657 room_item = create_room_sub_menu (contact, chatroom);
658 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
659 gtk_widget_show (room_item);
664 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
666 gtk_widget_set_sensitive (item, FALSE);
669 gtk_widget_show (image);
671 g_object_unref (mgr);