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-call-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-dispatcher.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-chatroom-manager.h>
35 #include <libempathy/empathy-contact-manager.h>
37 #include "empathy-contact-menu.h"
38 #include "empathy-images.h"
39 #include "empathy-log-window.h"
40 #include "empathy-contact-dialogs.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-share-my-desktop.h"
45 empathy_contact_menu_new (EmpathyContact *contact,
46 EmpathyContactFeatureFlags features)
52 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
54 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
58 menu = gtk_menu_new ();
59 shell = GTK_MENU_SHELL (menu);
62 item = empathy_contact_add_menu_item_new (contact);
64 gtk_menu_shell_append (shell, item);
65 gtk_widget_show (item);
69 if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
70 item = empathy_contact_chat_menu_item_new (contact);
71 gtk_menu_shell_append (shell, item);
72 gtk_widget_show (item);
75 if (features & EMPATHY_CONTACT_FEATURE_CALL) {
77 item = empathy_contact_audio_call_menu_item_new (contact);
78 gtk_menu_shell_append (shell, item);
79 gtk_widget_show (item);
82 item = empathy_contact_video_call_menu_item_new (contact);
83 gtk_menu_shell_append (shell, item);
84 gtk_widget_show (item);
88 if (features & EMPATHY_CONTACT_FEATURE_LOG) {
89 item = empathy_contact_log_menu_item_new (contact);
90 gtk_menu_shell_append (shell, item);
91 gtk_widget_show (item);
95 item = empathy_contact_invite_menu_item_new (contact);
96 gtk_menu_shell_append (shell, item);
97 gtk_widget_show (item);
100 if (features & EMPATHY_CONTACT_FEATURE_FT) {
101 item = empathy_contact_file_transfer_menu_item_new (contact);
102 gtk_menu_shell_append (shell, item);
103 gtk_widget_show (item);
106 /* Share my desktop */
107 /* FIXME we should add the "Share my desktop" menu item if Vino is
108 a registered handler in MC5 */
109 item = empathy_contact_share_my_desktop_menu_item_new (contact);
110 gtk_menu_shell_append (shell, item);
111 gtk_widget_show (item);
114 if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
115 EMPATHY_CONTACT_FEATURE_INFO |
116 EMPATHY_CONTACT_FEATURE_FAVOURITE)) {
117 item = gtk_separator_menu_item_new ();
118 gtk_menu_shell_append (shell, item);
119 gtk_widget_show (item);
123 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
124 item = empathy_contact_edit_menu_item_new (contact);
125 gtk_menu_shell_append (shell, item);
126 gtk_widget_show (item);
130 if (features & EMPATHY_CONTACT_FEATURE_INFO) {
131 item = empathy_contact_info_menu_item_new (contact);
132 gtk_menu_shell_append (shell, item);
133 gtk_widget_show (item);
136 /* Favorite checkbox */
137 if (features & EMPATHY_CONTACT_FEATURE_FAVOURITE) {
138 item = empathy_contact_favourite_menu_item_new (contact);
139 gtk_menu_shell_append (shell, item);
140 gtk_widget_show (item);
147 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
148 EmpathyContact *contact)
152 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
153 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
157 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
162 empathy_contact_add_menu_item_new (EmpathyContact *contact)
166 EmpathyContactManager *manager;
167 TpConnection *connection;
169 gboolean found = FALSE;
170 EmpathyContactListFlags flags;
172 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
174 if (!empathy_contact_manager_initialized ()) {
178 manager = empathy_contact_manager_dup_singleton ();
179 connection = empathy_contact_get_connection (contact);
181 flags = empathy_contact_manager_get_flags_for_connection (manager,
184 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
188 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
189 for (l = members; l; l = l->next) {
190 if (!found && empathy_contact_equal (l->data, contact)) {
192 /* we keep iterating so that we don't leak contact
196 g_object_unref (l->data);
198 g_list_free (members);
199 g_object_unref (manager);
205 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
206 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
208 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
210 g_signal_connect (item, "activate",
211 G_CALLBACK (empathy_contact_add_menu_item_activated),
218 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
219 EmpathyContact *contact)
221 empathy_dispatcher_chat_with_contact (contact, gtk_get_current_event_time ());
225 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
230 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
232 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
233 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
235 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
236 gtk_widget_set_sensitive (item, !empathy_contact_is_user (contact));
237 gtk_widget_show (image);
239 g_signal_connect (item, "activate",
240 G_CALLBACK (empathy_contact_chat_menu_item_activated),
247 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
248 EmpathyContact *contact)
251 empathy_call_factory_new_call_with_streams (contact, TRUE, FALSE,
252 gtk_get_current_event_time (), NULL);
256 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
261 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
263 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
264 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
266 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
267 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact) &&
268 !empathy_contact_is_user (contact));
269 gtk_widget_show (image);
271 g_signal_connect (item, "activate",
272 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
279 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
280 EmpathyContact *contact)
282 empathy_call_factory_new_call_with_streams (contact, TRUE, TRUE,
283 gtk_get_current_event_time (), NULL);
287 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
292 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
294 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
295 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
297 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
298 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact) &&
299 !empathy_contact_is_user (contact));
300 gtk_widget_show (image);
302 g_signal_connect (item, "activate",
303 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
310 contact_log_menu_item_activate_cb (EmpathyContact *contact)
312 empathy_log_window_show (empathy_contact_get_account (contact),
313 empathy_contact_get_id (contact),
318 empathy_contact_log_menu_item_new (EmpathyContact *contact)
320 TplLogManager *manager;
325 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
327 manager = tpl_log_manager_dup_singleton ();
328 have_log = tpl_log_manager_exists (manager,
329 empathy_contact_get_account (contact),
330 empathy_contact_get_id (contact),
333 g_object_unref (manager);
335 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
336 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
338 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
339 gtk_widget_set_sensitive (item, have_log);
340 gtk_widget_show (image);
342 g_signal_connect_swapped (item, "activate",
343 G_CALLBACK (contact_log_menu_item_activate_cb),
350 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
355 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
357 item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
358 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
360 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact) &&
361 !empathy_contact_is_user (contact));
362 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
363 gtk_widget_show (image);
365 g_signal_connect_swapped (item, "activate",
366 G_CALLBACK (empathy_send_file_with_file_chooser),
373 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
378 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
380 item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
381 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
383 gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
385 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
386 gtk_widget_show (image);
388 g_signal_connect_swapped (item, "activate",
389 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
396 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
397 EmpathyContact *contact)
399 EmpathyContactManager *manager;
400 EmpathyContactList *list;
402 manager = empathy_contact_manager_dup_singleton ();
403 list = EMPATHY_CONTACT_LIST (manager);
405 if (gtk_check_menu_item_get_active (item)) {
406 empathy_contact_list_add_to_favourites (list, contact);
408 empathy_contact_list_remove_from_favourites (list, contact);
411 g_object_unref (manager);
415 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
418 EmpathyContactManager *manager;
420 item = gtk_check_menu_item_new_with_label (_("Favorite"));
422 manager = empathy_contact_manager_dup_singleton ();
423 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
424 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
427 g_signal_connect (item, "toggled",
428 G_CALLBACK (favourite_menu_item_toggled_cb),
431 g_object_unref (manager);
436 contact_info_menu_item_activate_cb (EmpathyContact *contact)
438 empathy_contact_information_dialog_show (contact, NULL);
442 empathy_contact_info_menu_item_new (EmpathyContact *contact)
447 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
449 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
450 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
452 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
453 gtk_widget_show (image);
455 g_signal_connect_swapped (item, "activate",
456 G_CALLBACK (contact_info_menu_item_activate_cb),
463 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
465 empathy_contact_edit_dialog_show (contact, NULL);
469 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
471 EmpathyContactManager *manager;
474 gboolean enable = FALSE;
476 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
478 if (empathy_contact_manager_initialized ()) {
479 TpConnection *connection;
480 EmpathyContactListFlags flags;
482 manager = empathy_contact_manager_dup_singleton ();
483 connection = empathy_contact_get_connection (contact);
484 flags = empathy_contact_manager_get_flags_for_connection (
485 manager, connection);
487 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
488 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
490 g_object_unref (manager);
493 item = gtk_image_menu_item_new_with_mnemonic (
494 C_("Edit contact (contextual menu)",
496 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
498 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
499 gtk_widget_show (image);
501 gtk_widget_set_sensitive (item, enable);
503 g_signal_connect_swapped (item, "activate",
504 G_CALLBACK (contact_edit_menu_item_activate_cb),
511 EmpathyContact *contact;
512 EmpathyChatroom *chatroom;
515 static RoomSubMenuData *
516 room_sub_menu_data_new (EmpathyContact *contact,
517 EmpathyChatroom *chatroom)
519 RoomSubMenuData *data;
521 data = g_slice_new (RoomSubMenuData);
522 data->contact = g_object_ref (contact);
523 data->chatroom = g_object_ref (chatroom);
528 room_sub_menu_data_free (RoomSubMenuData *data)
530 g_object_unref (data->contact);
531 g_object_unref (data->chatroom);
532 g_slice_free (RoomSubMenuData, data);
536 room_sub_menu_activate_cb (GtkWidget *item,
537 RoomSubMenuData *data)
541 chat = empathy_chatroom_get_tp_chat (data->chatroom);
543 /* channel was invalidated. Ignoring */
547 /* send invitation */
548 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
549 _("Inviting you to this room"));
553 create_room_sub_menu (EmpathyContact *contact,
554 EmpathyChatroom *chatroom)
557 RoomSubMenuData *data;
559 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
560 data = room_sub_menu_data_new (contact, chatroom);
561 g_signal_connect_data (item, "activate",
562 G_CALLBACK (room_sub_menu_activate_cb), data,
563 (GClosureNotify) room_sub_menu_data_free, 0);
569 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
573 GtkWidget *room_item;
574 EmpathyChatroomManager *mgr;
576 GtkWidget *submenu = NULL;
578 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
580 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
581 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
583 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
585 if (empathy_contact_is_user (contact)) {
586 gtk_widget_set_sensitive (item, FALSE);
587 gtk_widget_show (image);
591 mgr = empathy_chatroom_manager_dup_singleton (NULL);
592 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
593 empathy_contact_get_account (contact));
595 for (l = rooms; l != NULL; l = g_list_next (l)) {
596 EmpathyChatroom *chatroom = l->data;
598 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
599 if (G_UNLIKELY (submenu == NULL))
600 submenu = gtk_menu_new ();
602 room_item = create_room_sub_menu (contact, chatroom);
603 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
604 gtk_widget_show (room_item);
609 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
611 gtk_widget_set_sensitive (item, FALSE);
614 gtk_widget_show (image);
616 g_object_unref (mgr);