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>
29 #include <telepathy-logger/log-manager.h>
32 #include <libempathy/empathy-log-manager.h>
33 #endif /* ENABLE_TPL */
34 #include <libempathy/empathy-call-factory.h>
35 #include <libempathy/empathy-contact-manager.h>
36 #include <libempathy/empathy-dispatcher.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-chatroom-manager.h>
39 #include <libempathy/empathy-contact-manager.h>
41 #include "empathy-contact-menu.h"
42 #include "empathy-images.h"
43 #include "empathy-log-window.h"
44 #include "empathy-contact-dialogs.h"
45 #include "empathy-ui-utils.h"
46 #include "empathy-share-my-desktop.h"
49 empathy_contact_menu_new (EmpathyContact *contact,
50 EmpathyContactFeatureFlags features)
56 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
58 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
62 menu = gtk_menu_new ();
63 shell = GTK_MENU_SHELL (menu);
66 item = empathy_contact_add_menu_item_new (contact);
68 gtk_menu_shell_append (shell, item);
69 gtk_widget_show (item);
73 if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
74 item = empathy_contact_chat_menu_item_new (contact);
75 gtk_menu_shell_append (shell, item);
76 gtk_widget_show (item);
79 if (features & EMPATHY_CONTACT_FEATURE_CALL) {
81 item = empathy_contact_audio_call_menu_item_new (contact);
82 gtk_menu_shell_append (shell, item);
83 gtk_widget_show (item);
86 item = empathy_contact_video_call_menu_item_new (contact);
87 gtk_menu_shell_append (shell, item);
88 gtk_widget_show (item);
92 if (features & EMPATHY_CONTACT_FEATURE_LOG) {
93 item = empathy_contact_log_menu_item_new (contact);
94 gtk_menu_shell_append (shell, item);
95 gtk_widget_show (item);
99 item = empathy_contact_invite_menu_item_new (contact);
100 gtk_menu_shell_append (shell, item);
101 gtk_widget_show (item);
104 item = empathy_contact_file_transfer_menu_item_new (contact);
105 gtk_menu_shell_append (shell, item);
106 gtk_widget_show (item);
108 /* Share my desktop */
109 /* FIXME we should add the "Share my desktop" menu item if Vino is
110 a registered handler in MC5 */
111 item = empathy_contact_share_my_desktop_menu_item_new (contact);
112 gtk_menu_shell_append (shell, item);
113 gtk_widget_show (item);
116 if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
117 EMPATHY_CONTACT_FEATURE_INFO |
118 EMPATHY_CONTACT_FEATURE_FAVOURITE)) {
119 item = gtk_separator_menu_item_new ();
120 gtk_menu_shell_append (shell, item);
121 gtk_widget_show (item);
125 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
126 item = empathy_contact_edit_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 #if HAVE_FAVOURITE_CONTACTS
139 /* Favorite checkbox */
140 if (features & EMPATHY_CONTACT_FEATURE_FAVOURITE) {
141 item = empathy_contact_favourite_menu_item_new (contact);
142 gtk_menu_shell_append (shell, item);
143 gtk_widget_show (item);
151 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
152 EmpathyContact *contact)
156 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
157 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
161 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
166 empathy_contact_add_menu_item_new (EmpathyContact *contact)
170 EmpathyContactManager *manager;
171 TpConnection *connection;
173 gboolean found = FALSE;
174 EmpathyContactListFlags flags;
176 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
178 if (!empathy_contact_manager_initialized ()) {
182 manager = empathy_contact_manager_dup_singleton ();
183 connection = empathy_contact_get_connection (contact);
185 flags = empathy_contact_manager_get_flags_for_connection (manager,
188 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
192 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
193 for (l = members; l; l = l->next) {
194 if (!found && empathy_contact_equal (l->data, contact)) {
196 /* we keep iterating so that we don't leak contact
200 g_object_unref (l->data);
202 g_list_free (members);
203 g_object_unref (manager);
209 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
210 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
212 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
214 g_signal_connect (item, "activate",
215 G_CALLBACK (empathy_contact_add_menu_item_activated),
222 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
223 EmpathyContact *contact)
225 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
229 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
234 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
236 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
237 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
239 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
240 gtk_widget_show (image);
242 g_signal_connect (item, "activate",
243 G_CALLBACK (empathy_contact_chat_menu_item_activated),
250 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
251 EmpathyContact *contact)
253 EmpathyCallFactory *factory;
255 factory = empathy_call_factory_get ();
256 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
260 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
265 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
267 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
268 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
270 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
271 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact));
272 gtk_widget_show (image);
274 g_signal_connect (item, "activate",
275 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
282 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
283 EmpathyContact *contact)
285 EmpathyCallFactory *factory;
287 factory = empathy_call_factory_get ();
288 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
292 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
297 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
299 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
300 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
302 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
303 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact));
304 gtk_widget_show (image);
306 g_signal_connect (item, "activate",
307 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
314 contact_log_menu_item_activate_cb (EmpathyContact *contact)
316 empathy_log_window_show (empathy_contact_get_account (contact),
317 empathy_contact_get_id (contact),
322 empathy_contact_log_menu_item_new (EmpathyContact *contact)
325 EmpathyLogManager *manager;
327 TplLogManager *manager;
328 #endif /* ENABLE_TPL */
333 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
336 manager = empathy_log_manager_dup_singleton ();
337 have_log = empathy_log_manager_exists (manager,
338 empathy_contact_get_account (contact),
339 empathy_contact_get_id (contact),
342 manager = tpl_log_manager_dup_singleton ();
343 have_log = tpl_log_manager_exists (manager,
344 empathy_contact_get_account (contact),
345 empathy_contact_get_id (contact),
347 #endif /* ENABLE_TPL */
348 g_object_unref (manager);
350 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
351 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
353 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
354 gtk_widget_set_sensitive (item, have_log);
355 gtk_widget_show (image);
357 g_signal_connect_swapped (item, "activate",
358 G_CALLBACK (contact_log_menu_item_activate_cb),
365 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
370 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
372 item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
373 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
375 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
376 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
377 gtk_widget_show (image);
379 g_signal_connect_swapped (item, "activate",
380 G_CALLBACK (empathy_send_file_with_file_chooser),
386 /* FIXME we should check if the contact supports vnc stream tube */
388 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
393 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
395 item = gtk_image_menu_item_new_with_mnemonic (_("Share my desktop"));
396 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
398 gtk_widget_set_sensitive (item, empathy_contact_can_use_stream_tube (contact));
399 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
400 gtk_widget_show (image);
402 g_signal_connect_swapped (item, "activate",
403 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
409 #if HAVE_FAVOURITE_CONTACTS
411 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
412 EmpathyContact *contact)
414 EmpathyContactManager *manager;
415 EmpathyContactList *list;
417 manager = empathy_contact_manager_dup_singleton ();
418 list = EMPATHY_CONTACT_LIST (manager);
420 if (gtk_check_menu_item_get_active (item)) {
421 empathy_contact_list_add_to_favourites (list, contact);
423 empathy_contact_list_remove_from_favourites (list, contact);
426 g_object_unref (manager);
430 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
433 EmpathyContactManager *manager;
435 item = gtk_check_menu_item_new_with_label (_("Favorite"));
437 manager = empathy_contact_manager_dup_singleton ();
438 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
439 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
442 g_signal_connect (item, "toggled",
443 G_CALLBACK (favourite_menu_item_toggled_cb),
446 g_object_unref (manager);
452 contact_info_menu_item_activate_cb (EmpathyContact *contact)
454 empathy_contact_information_dialog_show (contact, NULL);
458 empathy_contact_info_menu_item_new (EmpathyContact *contact)
463 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
465 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
466 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
468 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
469 gtk_widget_show (image);
471 g_signal_connect_swapped (item, "activate",
472 G_CALLBACK (contact_info_menu_item_activate_cb),
479 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
481 empathy_contact_edit_dialog_show (contact, NULL);
485 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
487 EmpathyContactManager *manager;
490 gboolean enable = FALSE;
492 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
494 if (empathy_contact_manager_initialized ()) {
495 TpConnection *connection;
496 EmpathyContactListFlags flags;
498 manager = empathy_contact_manager_dup_singleton ();
499 connection = empathy_contact_get_connection (contact);
500 flags = empathy_contact_manager_get_flags_for_connection (
501 manager, connection);
503 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
504 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
506 g_object_unref (manager);
509 item = gtk_image_menu_item_new_with_mnemonic (
510 C_("Edit contact (contextual menu)",
512 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
514 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
515 gtk_widget_show (image);
517 gtk_widget_set_sensitive (item, enable);
519 g_signal_connect_swapped (item, "activate",
520 G_CALLBACK (contact_edit_menu_item_activate_cb),
527 EmpathyContact *contact;
528 EmpathyChatroom *chatroom;
531 static RoomSubMenuData *
532 room_sub_menu_data_new (EmpathyContact *contact,
533 EmpathyChatroom *chatroom)
535 RoomSubMenuData *data;
537 data = g_slice_new (RoomSubMenuData);
538 data->contact = g_object_ref (contact);
539 data->chatroom = g_object_ref (chatroom);
544 room_sub_menu_data_free (RoomSubMenuData *data)
546 g_object_unref (data->contact);
547 g_object_unref (data->chatroom);
548 g_slice_free (RoomSubMenuData, data);
552 room_sub_menu_activate_cb (GtkWidget *item,
553 RoomSubMenuData *data)
557 chat = empathy_chatroom_get_tp_chat (data->chatroom);
559 /* channel was invalidated. Ignoring */
563 /* send invitation */
564 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
565 _("Inviting you to this room"));
569 create_room_sub_menu (EmpathyContact *contact,
570 EmpathyChatroom *chatroom)
573 RoomSubMenuData *data;
575 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
576 data = room_sub_menu_data_new (contact, chatroom);
577 g_signal_connect_data (item, "activate",
578 G_CALLBACK (room_sub_menu_activate_cb), data,
579 (GClosureNotify) room_sub_menu_data_free, 0);
585 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
589 GtkWidget *room_item;
590 EmpathyChatroomManager *mgr;
592 GtkWidget *submenu = NULL;
594 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
596 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chat room"));
597 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
599 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
601 mgr = empathy_chatroom_manager_dup_singleton (NULL);
602 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
603 empathy_contact_get_account (contact));
605 for (l = rooms; l != NULL; l = g_list_next (l)) {
606 EmpathyChatroom *chatroom = l->data;
608 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
609 if (G_UNLIKELY (submenu == NULL))
610 submenu = gtk_menu_new ();
612 room_item = create_room_sub_menu (contact, chatroom);
613 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
614 gtk_widget_show (room_item);
619 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
621 gtk_widget_set_sensitive (item, FALSE);
624 gtk_widget_show (image);
626 g_object_unref (mgr);