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);
141 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
142 EmpathyContact *contact)
146 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
147 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
151 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
156 empathy_contact_add_menu_item_new (EmpathyContact *contact)
160 EmpathyContactManager *manager;
161 TpConnection *connection;
163 gboolean found = FALSE;
164 EmpathyContactListFlags flags;
166 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
168 if (!empathy_contact_manager_initialized ()) {
172 manager = empathy_contact_manager_dup_singleton ();
173 connection = empathy_contact_get_connection (contact);
175 flags = empathy_contact_manager_get_flags_for_connection (manager,
178 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
182 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
183 for (l = members; l; l = l->next) {
184 if (!found && empathy_contact_equal (l->data, contact)) {
186 /* we keep iterating so that we don't leak contact
190 g_object_unref (l->data);
192 g_list_free (members);
193 g_object_unref (manager);
199 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
200 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
202 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
204 g_signal_connect (item, "activate",
205 G_CALLBACK (empathy_contact_add_menu_item_activated),
212 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
213 EmpathyContact *contact)
215 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
219 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
224 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
226 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
227 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
229 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
230 gtk_widget_show (image);
232 g_signal_connect (item, "activate",
233 G_CALLBACK (empathy_contact_chat_menu_item_activated),
240 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
241 EmpathyContact *contact)
243 EmpathyCallFactory *factory;
245 factory = empathy_call_factory_get ();
246 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
250 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
255 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
257 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
258 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
260 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
261 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact));
262 gtk_widget_show (image);
264 g_signal_connect (item, "activate",
265 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
272 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
273 EmpathyContact *contact)
275 EmpathyCallFactory *factory;
277 factory = empathy_call_factory_get ();
278 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
282 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
287 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
289 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
290 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
292 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
293 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact));
294 gtk_widget_show (image);
296 g_signal_connect (item, "activate",
297 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
304 contact_log_menu_item_activate_cb (EmpathyContact *contact)
306 empathy_log_window_show (empathy_contact_get_account (contact),
307 empathy_contact_get_id (contact),
312 empathy_contact_log_menu_item_new (EmpathyContact *contact)
315 EmpathyLogManager *manager;
317 TplLogManager *manager;
318 #endif /* ENABLE_TPL */
323 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
326 manager = empathy_log_manager_dup_singleton ();
327 have_log = empathy_log_manager_exists (manager,
328 empathy_contact_get_account (contact),
329 empathy_contact_get_id (contact),
332 manager = tpl_log_manager_dup_singleton ();
333 have_log = tpl_log_manager_exists (manager,
334 empathy_contact_get_account (contact),
335 empathy_contact_get_id (contact),
337 #endif /* ENABLE_TPL */
338 g_object_unref (manager);
340 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
341 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
343 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
344 gtk_widget_set_sensitive (item, have_log);
345 gtk_widget_show (image);
347 g_signal_connect_swapped (item, "activate",
348 G_CALLBACK (contact_log_menu_item_activate_cb),
355 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
360 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
362 item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
363 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
365 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
366 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
367 gtk_widget_show (image);
369 g_signal_connect_swapped (item, "activate",
370 G_CALLBACK (empathy_send_file_with_file_chooser),
376 /* FIXME we should check if the contact supports vnc stream tube */
378 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
383 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
385 item = gtk_image_menu_item_new_with_mnemonic (_("Share my desktop"));
386 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
388 gtk_widget_set_sensitive (item, empathy_contact_can_use_stream_tube (contact));
389 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
390 gtk_widget_show (image);
392 g_signal_connect_swapped (item, "activate",
393 G_CALLBACK (empathy_share_my_desktop_share_with_contact),
400 contact_info_menu_item_activate_cb (EmpathyContact *contact)
402 empathy_contact_information_dialog_show (contact, NULL);
406 empathy_contact_info_menu_item_new (EmpathyContact *contact)
411 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
413 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
414 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
416 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
417 gtk_widget_show (image);
419 g_signal_connect_swapped (item, "activate",
420 G_CALLBACK (contact_info_menu_item_activate_cb),
427 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
429 empathy_contact_edit_dialog_show (contact, NULL);
433 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
435 EmpathyContactManager *manager;
438 gboolean enable = FALSE;
440 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
442 if (empathy_contact_manager_initialized ()) {
443 TpConnection *connection;
444 EmpathyContactListFlags flags;
446 manager = empathy_contact_manager_dup_singleton ();
447 connection = empathy_contact_get_connection (contact);
448 flags = empathy_contact_manager_get_flags_for_connection (
449 manager, connection);
451 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
452 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
454 g_object_unref (manager);
457 item = gtk_image_menu_item_new_with_mnemonic (
458 C_("Edit contact (contextual menu)",
460 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
462 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
463 gtk_widget_show (image);
465 gtk_widget_set_sensitive (item, enable);
467 g_signal_connect_swapped (item, "activate",
468 G_CALLBACK (contact_edit_menu_item_activate_cb),
475 EmpathyContact *contact;
476 EmpathyChatroom *chatroom;
479 static RoomSubMenuData *
480 room_sub_menu_data_new (EmpathyContact *contact,
481 EmpathyChatroom *chatroom)
483 RoomSubMenuData *data;
485 data = g_slice_new (RoomSubMenuData);
486 data->contact = g_object_ref (contact);
487 data->chatroom = g_object_ref (chatroom);
492 room_sub_menu_data_free (RoomSubMenuData *data)
494 g_object_unref (data->contact);
495 g_object_unref (data->chatroom);
496 g_slice_free (RoomSubMenuData, data);
500 room_sub_menu_activate_cb (GtkWidget *item,
501 RoomSubMenuData *data)
505 chat = empathy_chatroom_get_tp_chat (data->chatroom);
507 /* channel was invalidated. Ignoring */
511 /* send invitation */
512 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
513 _("Inviting you to this room"));
517 create_room_sub_menu (EmpathyContact *contact,
518 EmpathyChatroom *chatroom)
521 RoomSubMenuData *data;
523 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
524 data = room_sub_menu_data_new (contact, chatroom);
525 g_signal_connect_data (item, "activate",
526 G_CALLBACK (room_sub_menu_activate_cb), data,
527 (GClosureNotify) room_sub_menu_data_free, 0);
533 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
537 GtkWidget *room_item;
538 EmpathyChatroomManager *mgr;
540 GtkWidget *submenu = NULL;
542 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
544 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chat room"));
545 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
547 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
549 mgr = empathy_chatroom_manager_dup_singleton (NULL);
550 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
551 empathy_contact_get_account (contact));
553 for (l = rooms; l != NULL; l = g_list_next (l)) {
554 EmpathyChatroom *chatroom = l->data;
556 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
557 if (G_UNLIKELY (submenu == NULL))
558 submenu = gtk_menu_new ();
560 room_item = create_room_sub_menu (contact, chatroom);
561 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
562 gtk_widget_show (room_item);
567 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
569 gtk_widget_set_sensitive (item, FALSE);
572 gtk_widget_show (image);
574 g_object_unref (mgr);