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 <libempathy/empathy-call-factory.h>
30 #include <libempathy/empathy-log-manager.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"
44 empathy_contact_menu_new (EmpathyContact *contact,
45 EmpathyContactFeatureFlags features)
51 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
53 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
57 menu = gtk_menu_new ();
58 shell = GTK_MENU_SHELL (menu);
61 item = empathy_contact_add_menu_item_new (contact);
63 gtk_menu_shell_append (shell, item);
64 gtk_widget_show (item);
68 if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
69 item = empathy_contact_chat_menu_item_new (contact);
70 gtk_menu_shell_append (shell, item);
71 gtk_widget_show (item);
74 if (features & EMPATHY_CONTACT_FEATURE_CALL) {
76 item = empathy_contact_audio_call_menu_item_new (contact);
77 gtk_menu_shell_append (shell, item);
78 gtk_widget_show (item);
81 item = empathy_contact_video_call_menu_item_new (contact);
82 gtk_menu_shell_append (shell, item);
83 gtk_widget_show (item);
87 if (features & EMPATHY_CONTACT_FEATURE_LOG) {
88 item = empathy_contact_log_menu_item_new (contact);
89 gtk_menu_shell_append (shell, item);
90 gtk_widget_show (item);
94 item = empathy_contact_invite_menu_item_new (contact);
95 gtk_menu_shell_append (shell, item);
96 gtk_widget_show (item);
99 item = empathy_contact_file_transfer_menu_item_new (contact);
100 gtk_menu_shell_append (shell, item);
101 gtk_widget_show (item);
104 if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
105 EMPATHY_CONTACT_FEATURE_INFO)) {
106 item = gtk_separator_menu_item_new ();
107 gtk_menu_shell_append (shell, item);
108 gtk_widget_show (item);
112 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
113 item = empathy_contact_edit_menu_item_new (contact);
114 gtk_menu_shell_append (shell, item);
115 gtk_widget_show (item);
119 if (features & EMPATHY_CONTACT_FEATURE_INFO) {
120 item = empathy_contact_info_menu_item_new (contact);
121 gtk_menu_shell_append (shell, item);
122 gtk_widget_show (item);
129 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
130 EmpathyContact *contact)
132 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
136 empathy_contact_add_menu_item_new (EmpathyContact *contact)
140 EmpathyContactManager *manager;
142 gboolean found = FALSE;
144 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
146 manager = empathy_contact_manager_dup_singleton ();
147 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
148 for (l = members; l; l = l->next) {
149 if (!found && empathy_contact_equal (l->data, contact)) {
151 /* we keep iterating so that we don't leak contact
155 g_object_unref (l->data);
157 g_list_free (members);
159 if (found) return NULL;
161 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact..."));
162 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
164 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
166 /* FIXME - callback */
172 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
177 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
179 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
180 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
182 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
183 gtk_widget_show (image);
185 g_signal_connect (item, "activate",
186 G_CALLBACK (empathy_contact_chat_menu_item_activated),
193 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
194 EmpathyContact *contact)
196 EmpathyCallFactory *factory;
198 factory = empathy_call_factory_get ();
199 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
203 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
208 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
210 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
211 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
213 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
214 gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
215 gtk_widget_show (image);
217 g_signal_connect (item, "activate",
218 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
225 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
226 EmpathyContact *contact)
228 EmpathyCallFactory *factory;
230 factory = empathy_call_factory_get ();
231 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
235 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
240 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
242 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
243 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
245 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
246 gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
247 gtk_widget_show (image);
249 g_signal_connect (item, "activate",
250 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
257 contact_log_menu_item_activate_cb (EmpathyContact *contact)
259 empathy_log_window_show (empathy_contact_get_account (contact),
260 empathy_contact_get_id (contact),
265 empathy_contact_log_menu_item_new (EmpathyContact *contact)
267 EmpathyLogManager *manager;
272 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
274 manager = empathy_log_manager_dup_singleton ();
275 have_log = empathy_log_manager_exists (manager,
276 empathy_contact_get_account (contact),
277 empathy_contact_get_id (contact),
279 g_object_unref (manager);
281 item = gtk_image_menu_item_new_with_mnemonic (_("_View Previous Conversations"));
282 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
284 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
285 gtk_widget_set_sensitive (item, have_log);
286 gtk_widget_show (image);
288 g_signal_connect_swapped (item, "activate",
289 G_CALLBACK (contact_log_menu_item_activate_cb),
296 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
301 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
303 item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
304 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
306 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
307 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
308 gtk_widget_show (image);
310 g_signal_connect_swapped (item, "activate",
311 G_CALLBACK (empathy_send_file_with_file_chooser),
318 contact_info_menu_item_activate_cb (EmpathyContact *contact)
320 empathy_contact_information_dialog_show (contact, NULL);
324 empathy_contact_info_menu_item_new (EmpathyContact *contact)
329 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
331 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
332 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
334 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
335 gtk_widget_show (image);
337 g_signal_connect_swapped (item, "activate",
338 G_CALLBACK (contact_info_menu_item_activate_cb),
345 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
347 empathy_contact_edit_dialog_show (contact, NULL);
351 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
353 EmpathyContactManager *manager;
356 gboolean enable = FALSE;
358 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
360 if (empathy_contact_manager_initialized ()) {
361 TpConnection *connection;
362 EmpathyContactListFlags flags;
364 manager = empathy_contact_manager_dup_singleton ();
365 connection = empathy_contact_get_connection (contact);
366 flags = empathy_contact_manager_get_flags_for_connection (
367 manager, connection);
369 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
370 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
372 g_object_unref (manager);
375 item = gtk_image_menu_item_new_with_mnemonic (_("_Edit"));
376 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
378 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
379 gtk_widget_show (image);
381 gtk_widget_set_sensitive (item, enable);
383 g_signal_connect_swapped (item, "activate",
384 G_CALLBACK (contact_edit_menu_item_activate_cb),
391 EmpathyContact *contact;
392 EmpathyChatroom *chatroom;
395 static RoomSubMenuData *
396 room_sub_menu_data_new (EmpathyContact *contact,
397 EmpathyChatroom *chatroom)
399 RoomSubMenuData *data;
401 data = g_slice_new (RoomSubMenuData);
402 data->contact = g_object_ref (contact);
403 data->chatroom = g_object_ref (chatroom);
408 room_sub_menu_data_free (RoomSubMenuData *data)
410 /* FIXME: seems this is never called... */
411 g_object_unref (data->contact);
412 g_object_unref (data->chatroom);
413 g_slice_free (RoomSubMenuData, data);
417 room_sub_menu_activate_cb (GtkWidget *item,
418 RoomSubMenuData *data)
421 GArray handles = {(gchar *) &handle, 1};
425 chat = empathy_chatroom_get_tp_chat (data->chatroom);
427 /* channel was invalidated. Ignoring */
431 /* send invitation */
432 handle = empathy_contact_get_handle (data->contact);
433 channel = empathy_tp_chat_get_channel (chat);
434 tp_cli_channel_interface_group_call_add_members (channel, -1, &handles,
435 _("Inviting to this room"), NULL, NULL, NULL, NULL);
439 create_room_sub_menu (EmpathyContact *contact,
440 EmpathyChatroom *chatroom)
443 RoomSubMenuData *data;
445 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
446 data = room_sub_menu_data_new (contact, chatroom);
447 g_signal_connect_data (item, "activate",
448 G_CALLBACK (room_sub_menu_activate_cb), data,
449 (GClosureNotify) room_sub_menu_data_free, 0);
455 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
459 GtkWidget *room_item;
460 EmpathyChatroomManager *mgr;
462 GtkWidget *submenu = NULL;
464 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
466 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
467 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
469 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
471 mgr = empathy_chatroom_manager_dup_singleton (NULL);
472 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
473 empathy_contact_get_account (contact));
475 for (l = rooms; l != NULL; l = g_list_next (l)) {
476 EmpathyChatroom *chatroom = l->data;
478 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
479 if (G_UNLIKELY (submenu == NULL))
480 submenu = gtk_menu_new ();
482 room_item = create_room_sub_menu (contact, chatroom);
483 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
484 gtk_widget_show (room_item);
489 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
491 gtk_widget_set_sensitive (item, FALSE);
494 gtk_widget_show (image);
496 g_object_unref (mgr);