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 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 #if HAVE_FAVOURITE_CONTACTS
138 /* Favorite checkbox */
139 item = empathy_contact_favourite_menu_item_new (contact);
140 gtk_menu_shell_append (shell, item);
141 gtk_widget_show (item);
148 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
149 EmpathyContact *contact)
153 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
154 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
158 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
163 empathy_contact_add_menu_item_new (EmpathyContact *contact)
167 EmpathyContactManager *manager;
168 TpConnection *connection;
170 gboolean found = FALSE;
171 EmpathyContactListFlags flags;
173 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
175 if (!empathy_contact_manager_initialized ()) {
179 manager = empathy_contact_manager_dup_singleton ();
180 connection = empathy_contact_get_connection (contact);
182 flags = empathy_contact_manager_get_flags_for_connection (manager,
185 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
189 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
190 for (l = members; l; l = l->next) {
191 if (!found && empathy_contact_equal (l->data, contact)) {
193 /* we keep iterating so that we don't leak contact
197 g_object_unref (l->data);
199 g_list_free (members);
200 g_object_unref (manager);
206 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
207 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
209 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
211 g_signal_connect (item, "activate",
212 G_CALLBACK (empathy_contact_add_menu_item_activated),
219 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
220 EmpathyContact *contact)
222 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
226 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
231 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
233 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
234 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
236 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
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)
250 EmpathyCallFactory *factory;
252 factory = empathy_call_factory_get ();
253 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
257 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
262 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
264 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
265 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
267 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
268 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (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 EmpathyCallFactory *factory;
284 factory = empathy_call_factory_get ();
285 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
289 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
294 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
296 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
297 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
299 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
300 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact));
301 gtk_widget_show (image);
303 g_signal_connect (item, "activate",
304 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
311 contact_log_menu_item_activate_cb (EmpathyContact *contact)
313 empathy_log_window_show (empathy_contact_get_account (contact),
314 empathy_contact_get_id (contact),
319 empathy_contact_log_menu_item_new (EmpathyContact *contact)
322 EmpathyLogManager *manager;
324 TplLogManager *manager;
325 #endif /* ENABLE_TPL */
330 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
333 manager = empathy_log_manager_dup_singleton ();
334 have_log = empathy_log_manager_exists (manager,
335 empathy_contact_get_account (contact),
336 empathy_contact_get_id (contact),
339 manager = tpl_log_manager_dup_singleton ();
340 have_log = tpl_log_manager_exists (manager,
341 empathy_contact_get_account (contact),
342 empathy_contact_get_id (contact),
344 #endif /* ENABLE_TPL */
345 g_object_unref (manager);
347 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
348 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
350 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
351 gtk_widget_set_sensitive (item, have_log);
352 gtk_widget_show (image);
354 g_signal_connect_swapped (item, "activate",
355 G_CALLBACK (contact_log_menu_item_activate_cb),
362 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
367 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
369 item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
370 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
372 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
373 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
374 gtk_widget_show (image);
376 g_signal_connect_swapped (item, "activate",
377 G_CALLBACK (empathy_send_file_with_file_chooser),
383 /* FIXME we should check if the contact supports vnc stream tube */
385 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
390 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
392 item = gtk_image_menu_item_new_with_mnemonic (_("Share my desktop"));
393 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
395 gtk_widget_set_sensitive (item, empathy_contact_can_use_stream_tube (contact));
396 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
397 gtk_widget_show (image);
399 g_signal_connect_swapped (item, "activate",
400 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
406 #if HAVE_FAVOURITE_CONTACTS
408 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
409 EmpathyContact *contact)
411 EmpathyContactManager *manager;
412 EmpathyContactList *list;
414 manager = empathy_contact_manager_dup_singleton ();
415 list = EMPATHY_CONTACT_LIST (manager);
417 if (gtk_check_menu_item_get_active (item)) {
418 empathy_contact_list_add_to_favourites (list, contact);
420 empathy_contact_list_remove_from_favourites (list, contact);
423 g_object_unref (manager);
427 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
430 EmpathyContactManager *manager;
432 item = gtk_check_menu_item_new_with_label (_("Favorite"));
434 manager = empathy_contact_manager_dup_singleton ();
435 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
436 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
439 g_signal_connect (item, "toggled",
440 G_CALLBACK (favourite_menu_item_toggled_cb),
443 g_object_unref (manager);
449 contact_info_menu_item_activate_cb (EmpathyContact *contact)
451 empathy_contact_information_dialog_show (contact, NULL);
455 empathy_contact_info_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 (_("Infor_mation"));
463 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
465 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
466 gtk_widget_show (image);
468 g_signal_connect_swapped (item, "activate",
469 G_CALLBACK (contact_info_menu_item_activate_cb),
476 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
478 empathy_contact_edit_dialog_show (contact, NULL);
482 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
484 EmpathyContactManager *manager;
487 gboolean enable = FALSE;
489 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
491 if (empathy_contact_manager_initialized ()) {
492 TpConnection *connection;
493 EmpathyContactListFlags flags;
495 manager = empathy_contact_manager_dup_singleton ();
496 connection = empathy_contact_get_connection (contact);
497 flags = empathy_contact_manager_get_flags_for_connection (
498 manager, connection);
500 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
501 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
503 g_object_unref (manager);
506 item = gtk_image_menu_item_new_with_mnemonic (
507 C_("Edit contact (contextual menu)",
509 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
511 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
512 gtk_widget_show (image);
514 gtk_widget_set_sensitive (item, enable);
516 g_signal_connect_swapped (item, "activate",
517 G_CALLBACK (contact_edit_menu_item_activate_cb),
524 EmpathyContact *contact;
525 EmpathyChatroom *chatroom;
528 static RoomSubMenuData *
529 room_sub_menu_data_new (EmpathyContact *contact,
530 EmpathyChatroom *chatroom)
532 RoomSubMenuData *data;
534 data = g_slice_new (RoomSubMenuData);
535 data->contact = g_object_ref (contact);
536 data->chatroom = g_object_ref (chatroom);
541 room_sub_menu_data_free (RoomSubMenuData *data)
543 g_object_unref (data->contact);
544 g_object_unref (data->chatroom);
545 g_slice_free (RoomSubMenuData, data);
549 room_sub_menu_activate_cb (GtkWidget *item,
550 RoomSubMenuData *data)
554 chat = empathy_chatroom_get_tp_chat (data->chatroom);
556 /* channel was invalidated. Ignoring */
560 /* send invitation */
561 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
562 _("Inviting you to this room"));
566 create_room_sub_menu (EmpathyContact *contact,
567 EmpathyChatroom *chatroom)
570 RoomSubMenuData *data;
572 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
573 data = room_sub_menu_data_new (contact, chatroom);
574 g_signal_connect_data (item, "activate",
575 G_CALLBACK (room_sub_menu_activate_cb), data,
576 (GClosureNotify) room_sub_menu_data_free, 0);
582 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
586 GtkWidget *room_item;
587 EmpathyChatroomManager *mgr;
589 GtkWidget *submenu = NULL;
591 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
593 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chat room"));
594 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
596 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
598 mgr = empathy_chatroom_manager_dup_singleton (NULL);
599 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
600 empathy_contact_get_account (contact));
602 for (l = rooms; l != NULL; l = g_list_next (l)) {
603 EmpathyChatroom *chatroom = l->data;
605 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
606 if (G_UNLIKELY (submenu == NULL))
607 submenu = gtk_menu_new ();
609 room_item = create_room_sub_menu (contact, chatroom);
610 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
611 gtk_widget_show (room_item);
616 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
618 gtk_widget_set_sensitive (item, FALSE);
621 gtk_widget_show (image);
623 g_object_unref (mgr);