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"
43 static GtkWidget *empathy_contact_block_menu_item_new (EmpathyContact *);
46 empathy_contact_menu_new (EmpathyContact *contact,
47 EmpathyContactFeatureFlags features)
53 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
55 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
59 menu = gtk_menu_new ();
60 shell = GTK_MENU_SHELL (menu);
63 item = empathy_contact_add_menu_item_new (contact);
65 gtk_menu_shell_append (shell, item);
66 gtk_widget_show (item);
70 if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
71 item = empathy_contact_chat_menu_item_new (contact);
72 gtk_menu_shell_append (shell, item);
73 gtk_widget_show (item);
76 if (features & EMPATHY_CONTACT_FEATURE_CALL) {
78 item = empathy_contact_audio_call_menu_item_new (contact);
79 gtk_menu_shell_append (shell, item);
80 gtk_widget_show (item);
83 item = empathy_contact_video_call_menu_item_new (contact);
84 gtk_menu_shell_append (shell, item);
85 gtk_widget_show (item);
89 if (features & EMPATHY_CONTACT_FEATURE_LOG) {
90 item = empathy_contact_log_menu_item_new (contact);
91 gtk_menu_shell_append (shell, item);
92 gtk_widget_show (item);
96 item = empathy_contact_invite_menu_item_new (contact);
97 gtk_menu_shell_append (shell, item);
98 gtk_widget_show (item);
101 if (features & EMPATHY_CONTACT_FEATURE_FT) {
102 item = empathy_contact_file_transfer_menu_item_new (contact);
103 gtk_menu_shell_append (shell, item);
104 gtk_widget_show (item);
107 /* Share my desktop */
108 /* FIXME we should add the "Share my desktop" menu item if Vino is
109 a registered handler in MC5 */
110 item = empathy_contact_share_my_desktop_menu_item_new (contact);
111 gtk_menu_shell_append (shell, item);
112 gtk_widget_show (item);
115 if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
116 EMPATHY_CONTACT_FEATURE_INFO |
117 EMPATHY_CONTACT_FEATURE_FAVOURITE)) {
118 item = gtk_separator_menu_item_new ();
119 gtk_menu_shell_append (shell, item);
120 gtk_widget_show (item);
124 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
125 item = empathy_contact_edit_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 /* Favorite checkbox */
138 if (features & EMPATHY_CONTACT_FEATURE_FAVOURITE) {
139 item = empathy_contact_favourite_menu_item_new (contact);
140 gtk_menu_shell_append (shell, item);
141 gtk_widget_show (item);
144 /* Separator & Block */
145 if (features & EMPATHY_CONTACT_FEATURE_BLOCK &&
146 (item = empathy_contact_block_menu_item_new (contact)) != NULL) {
149 sep = gtk_separator_menu_item_new ();
150 gtk_menu_shell_append (shell, sep);
151 gtk_widget_show (sep);
153 gtk_menu_shell_append (shell, item);
154 gtk_widget_show (item);
161 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
162 EmpathyContact *contact)
166 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
167 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
171 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
176 empathy_contact_add_menu_item_new (EmpathyContact *contact)
180 EmpathyContactManager *manager;
181 TpConnection *connection;
183 gboolean found = FALSE;
184 EmpathyContactListFlags flags;
186 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
188 if (!empathy_contact_manager_initialized ()) {
192 manager = empathy_contact_manager_dup_singleton ();
193 connection = empathy_contact_get_connection (contact);
195 flags = empathy_contact_manager_get_flags_for_connection (manager,
198 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
202 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
203 for (l = members; l; l = l->next) {
204 if (!found && empathy_contact_equal (l->data, contact)) {
206 /* we keep iterating so that we don't leak contact
210 g_object_unref (l->data);
212 g_list_free (members);
213 g_object_unref (manager);
219 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
220 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
222 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
224 g_signal_connect (item, "activate",
225 G_CALLBACK (empathy_contact_add_menu_item_activated),
232 empathy_contact_block_menu_item_toggled (GtkCheckMenuItem *item,
233 EmpathyContact *contact)
235 static guint block_signal = 0;
236 EmpathyContactManager *manager;
237 gboolean blocked, abusive;
239 if (block_signal > 0)
242 blocked = gtk_check_menu_item_get_active (item);
245 /* confirm the user really wishes to block the contact */
248 /* gtk_menu_get_attach_widget () doesn't behave properly here
250 parent = g_object_get_data (
251 G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (item))),
254 if (!empathy_block_contact_dialog_show (GTK_WINDOW (parent),
259 manager = empathy_contact_manager_dup_singleton ();
260 empathy_contact_list_set_blocked (EMPATHY_CONTACT_LIST (manager),
261 contact, blocked, abusive);
262 g_object_unref (manager);
264 /* update the toggle with the blocked status */
266 gtk_check_menu_item_set_active (item, blocked);
271 empathy_contact_block_menu_item_new (EmpathyContact *contact)
274 EmpathyContactManager *manager;
275 TpConnection *connection;
276 EmpathyContactListFlags flags;
279 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
281 manager = empathy_contact_manager_dup_singleton ();
283 if (!empathy_contact_manager_initialized ()) {
287 connection = empathy_contact_get_connection (contact);
289 flags = empathy_contact_manager_get_flags_for_connection (manager,
292 if (!(flags & EMPATHY_CONTACT_LIST_CAN_BLOCK)) {
296 item = gtk_check_menu_item_new_with_mnemonic (_("_Block Contact"));
297 blocked = empathy_contact_list_get_blocked (
298 EMPATHY_CONTACT_LIST (manager),
301 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), blocked);
303 g_signal_connect (item, "toggled",
304 G_CALLBACK (empathy_contact_block_menu_item_toggled),
311 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
312 EmpathyContact *contact)
314 empathy_chat_with_contact (contact, gtk_get_current_event_time ());
318 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
323 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
325 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
326 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
328 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
329 gtk_widget_set_sensitive (item, !empathy_contact_is_user (contact));
330 gtk_widget_show (image);
332 g_signal_connect (item, "activate",
333 G_CALLBACK (empathy_contact_chat_menu_item_activated),
340 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
341 EmpathyContact *contact)
344 empathy_call_new_with_streams (contact, TRUE, FALSE,
345 gtk_get_current_event_time ());
349 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
354 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
356 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
357 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
359 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
360 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact) &&
361 !empathy_contact_is_user (contact));
362 gtk_widget_show (image);
364 g_signal_connect (item, "activate",
365 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
372 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
373 EmpathyContact *contact)
375 empathy_call_new_with_streams (contact, TRUE, TRUE,
376 gtk_get_current_event_time ());
380 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
385 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
387 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
388 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
390 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
391 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact) &&
392 !empathy_contact_is_user (contact));
393 gtk_widget_show (image);
395 g_signal_connect (item, "activate",
396 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
403 contact_log_menu_item_activate_cb (EmpathyContact *contact)
405 empathy_log_window_show (empathy_contact_get_account (contact),
406 empathy_contact_get_id (contact),
411 empathy_contact_log_menu_item_new (EmpathyContact *contact)
413 TplLogManager *manager;
419 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
421 manager = tpl_log_manager_dup_singleton ();
422 entity = tpl_entity_new_from_tp_contact (empathy_contact_get_tp_contact (contact),
425 have_log = tpl_log_manager_exists (manager,
426 empathy_contact_get_account (contact),
428 TPL_EVENT_MASK_TEXT);
430 g_object_unref (entity);
431 g_object_unref (manager);
433 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
434 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
436 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
437 gtk_widget_set_sensitive (item, have_log);
438 gtk_widget_show (image);
440 g_signal_connect_swapped (item, "activate",
441 G_CALLBACK (contact_log_menu_item_activate_cb),
448 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
453 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
455 item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
456 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
458 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact) &&
459 !empathy_contact_is_user (contact));
460 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
461 gtk_widget_show (image);
463 g_signal_connect_swapped (item, "activate",
464 G_CALLBACK (empathy_send_file_with_file_chooser),
471 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
476 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
478 item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
479 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
481 gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
483 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
484 gtk_widget_show (image);
486 g_signal_connect_swapped (item, "activate",
487 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
494 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
495 EmpathyContact *contact)
497 EmpathyContactManager *manager;
498 EmpathyContactList *list;
500 manager = empathy_contact_manager_dup_singleton ();
501 list = EMPATHY_CONTACT_LIST (manager);
503 if (gtk_check_menu_item_get_active (item)) {
504 empathy_contact_list_add_to_favourites (list, contact);
506 empathy_contact_list_remove_from_favourites (list, contact);
509 g_object_unref (manager);
513 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
516 EmpathyContactManager *manager;
518 item = gtk_check_menu_item_new_with_label (_("Favorite"));
520 manager = empathy_contact_manager_dup_singleton ();
521 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
522 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
525 g_signal_connect (item, "toggled",
526 G_CALLBACK (favourite_menu_item_toggled_cb),
529 g_object_unref (manager);
534 contact_info_menu_item_activate_cb (EmpathyContact *contact)
536 empathy_contact_information_dialog_show (contact, NULL);
540 empathy_contact_info_menu_item_new (EmpathyContact *contact)
545 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
547 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
548 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
550 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
551 gtk_widget_show (image);
553 g_signal_connect_swapped (item, "activate",
554 G_CALLBACK (contact_info_menu_item_activate_cb),
561 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
563 empathy_contact_edit_dialog_show (contact, NULL);
567 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
569 EmpathyContactManager *manager;
572 gboolean enable = FALSE;
574 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
576 if (empathy_contact_manager_initialized ()) {
577 TpConnection *connection;
578 EmpathyContactListFlags flags;
580 manager = empathy_contact_manager_dup_singleton ();
581 connection = empathy_contact_get_connection (contact);
582 flags = empathy_contact_manager_get_flags_for_connection (
583 manager, connection);
585 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
586 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
588 g_object_unref (manager);
591 item = gtk_image_menu_item_new_with_mnemonic (
592 C_("Edit contact (contextual menu)",
594 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
596 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
597 gtk_widget_show (image);
599 gtk_widget_set_sensitive (item, enable);
601 g_signal_connect_swapped (item, "activate",
602 G_CALLBACK (contact_edit_menu_item_activate_cb),
609 EmpathyContact *contact;
610 EmpathyChatroom *chatroom;
613 static RoomSubMenuData *
614 room_sub_menu_data_new (EmpathyContact *contact,
615 EmpathyChatroom *chatroom)
617 RoomSubMenuData *data;
619 data = g_slice_new (RoomSubMenuData);
620 data->contact = g_object_ref (contact);
621 data->chatroom = g_object_ref (chatroom);
626 room_sub_menu_data_free (RoomSubMenuData *data)
628 g_object_unref (data->contact);
629 g_object_unref (data->chatroom);
630 g_slice_free (RoomSubMenuData, data);
634 room_sub_menu_activate_cb (GtkWidget *item,
635 RoomSubMenuData *data)
639 chat = empathy_chatroom_get_tp_chat (data->chatroom);
641 /* channel was invalidated. Ignoring */
645 /* send invitation */
646 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
647 _("Inviting you to this room"));
651 create_room_sub_menu (EmpathyContact *contact,
652 EmpathyChatroom *chatroom)
655 RoomSubMenuData *data;
657 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
658 data = room_sub_menu_data_new (contact, chatroom);
659 g_signal_connect_data (item, "activate",
660 G_CALLBACK (room_sub_menu_activate_cb), data,
661 (GClosureNotify) room_sub_menu_data_free, 0);
667 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
671 GtkWidget *room_item;
672 EmpathyChatroomManager *mgr;
674 GtkWidget *submenu = NULL;
676 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
678 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
679 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
681 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
683 if (empathy_contact_is_user (contact)) {
684 gtk_widget_set_sensitive (item, FALSE);
685 gtk_widget_show (image);
689 mgr = empathy_chatroom_manager_dup_singleton (NULL);
690 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
691 empathy_contact_get_account (contact));
693 for (l = rooms; l != NULL; l = g_list_next (l)) {
694 EmpathyChatroom *chatroom = l->data;
696 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
697 if (G_UNLIKELY (submenu == NULL))
698 submenu = gtk_menu_new ();
700 room_item = create_room_sub_menu (contact, chatroom);
701 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
702 gtk_widget_show (room_item);
707 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
709 gtk_widget_set_sensitive (item, FALSE);
712 gtk_widget_show (image);
714 g_object_unref (mgr);