add empathy-auto-salut-account-helper.c to POTFILES.in
[empathy-mirror.git] / libempathy-gtk / empathy-contact-menu.c
blobf42bc6bdb06a03ad084cd8a28e5e06a89ae0be38
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
22 #include "config.h"
24 #include <string.h>
26 #include <glib/gi18n-lib.h>
27 #include <gtk/gtk.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"
42 #include "empathy-share-my-desktop.h"
44 GtkWidget *
45 empathy_contact_menu_new (EmpathyContact *contact,
46 EmpathyContactFeatureFlags features)
48 GtkWidget *menu;
49 GtkMenuShell *shell;
50 GtkWidget *item;
52 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
54 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
55 return NULL;
58 menu = gtk_menu_new ();
59 shell = GTK_MENU_SHELL (menu);
61 /* Add Contact */
62 item = empathy_contact_add_menu_item_new (contact);
63 if (item) {
64 gtk_menu_shell_append (shell, item);
65 gtk_widget_show (item);
68 /* Chat */
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) {
76 /* Audio Call */
77 item = empathy_contact_audio_call_menu_item_new (contact);
78 gtk_menu_shell_append (shell, item);
79 gtk_widget_show (item);
81 /* Video Call */
82 item = empathy_contact_video_call_menu_item_new (contact);
83 gtk_menu_shell_append (shell, item);
84 gtk_widget_show (item);
87 /* Log */
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);
94 /* Invite */
95 item = empathy_contact_invite_menu_item_new (contact);
96 gtk_menu_shell_append (shell, item);
97 gtk_widget_show (item);
99 /* File transfer */
100 item = empathy_contact_file_transfer_menu_item_new (contact);
101 gtk_menu_shell_append (shell, item);
102 gtk_widget_show (item);
104 /* Share my desktop */
105 /* FIXME we should add the "Share my desktop" menu item if Vino is
106 a registered handler in MC5 */
107 item = empathy_contact_share_my_desktop_menu_item_new (contact);
108 gtk_menu_shell_append (shell, item);
109 gtk_widget_show (item);
111 /* Separator */
112 if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
113 EMPATHY_CONTACT_FEATURE_INFO)) {
114 item = gtk_separator_menu_item_new ();
115 gtk_menu_shell_append (shell, item);
116 gtk_widget_show (item);
119 /* Edit */
120 if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
121 item = empathy_contact_edit_menu_item_new (contact);
122 gtk_menu_shell_append (shell, item);
123 gtk_widget_show (item);
126 /* Info */
127 if (features & EMPATHY_CONTACT_FEATURE_INFO) {
128 item = empathy_contact_info_menu_item_new (contact);
129 gtk_menu_shell_append (shell, item);
130 gtk_widget_show (item);
133 return menu;
136 static void
137 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
138 EmpathyContact *contact)
140 GtkWidget *toplevel;
142 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
143 if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
144 toplevel = NULL;
147 empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
148 contact);
151 GtkWidget *
152 empathy_contact_add_menu_item_new (EmpathyContact *contact)
154 GtkWidget *item;
155 GtkWidget *image;
156 EmpathyContactManager *manager;
157 TpConnection *connection;
158 GList *l, *members;
159 gboolean found = FALSE;
160 EmpathyContactListFlags flags;
162 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
164 if (!empathy_contact_manager_initialized ()) {
165 return NULL;
168 manager = empathy_contact_manager_dup_singleton ();
169 connection = empathy_contact_get_connection (contact);
171 flags = empathy_contact_manager_get_flags_for_connection (manager,
172 connection);
174 if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
175 return NULL;
178 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
179 for (l = members; l; l = l->next) {
180 if (!found && empathy_contact_equal (l->data, contact)) {
181 found = TRUE;
182 /* we keep iterating so that we don't leak contact
183 * refs */
186 g_object_unref (l->data);
188 g_list_free (members);
189 g_object_unref (manager);
191 if (found) {
192 return NULL;
195 item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
196 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
197 GTK_ICON_SIZE_MENU);
198 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
200 g_signal_connect (item, "activate",
201 G_CALLBACK (empathy_contact_add_menu_item_activated),
202 contact);
204 return item;
207 static void
208 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
209 EmpathyContact *contact)
211 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
214 GtkWidget *
215 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
217 GtkWidget *item;
218 GtkWidget *image;
220 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
222 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
223 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
224 GTK_ICON_SIZE_MENU);
225 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
226 gtk_widget_show (image);
228 g_signal_connect (item, "activate",
229 G_CALLBACK (empathy_contact_chat_menu_item_activated),
230 contact);
232 return item;
235 static void
236 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
237 EmpathyContact *contact)
239 EmpathyCallFactory *factory;
241 factory = empathy_call_factory_get ();
242 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
245 GtkWidget *
246 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
248 GtkWidget *item;
249 GtkWidget *image;
251 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
253 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
254 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
255 GTK_ICON_SIZE_MENU);
256 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
257 gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact));
258 gtk_widget_show (image);
260 g_signal_connect (item, "activate",
261 G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
262 contact);
264 return item;
267 static void
268 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
269 EmpathyContact *contact)
271 EmpathyCallFactory *factory;
273 factory = empathy_call_factory_get ();
274 empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
277 GtkWidget *
278 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
280 GtkWidget *item;
281 GtkWidget *image;
283 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
285 item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
286 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
287 GTK_ICON_SIZE_MENU);
288 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
289 gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact));
290 gtk_widget_show (image);
292 g_signal_connect (item, "activate",
293 G_CALLBACK (empathy_contact_video_call_menu_item_activated),
294 contact);
296 return item;
299 static void
300 contact_log_menu_item_activate_cb (EmpathyContact *contact)
302 empathy_log_window_show (empathy_contact_get_account (contact),
303 empathy_contact_get_id (contact),
304 FALSE, NULL);
307 GtkWidget *
308 empathy_contact_log_menu_item_new (EmpathyContact *contact)
310 EmpathyLogManager *manager;
311 gboolean have_log;
312 GtkWidget *item;
313 GtkWidget *image;
315 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
317 manager = empathy_log_manager_dup_singleton ();
318 have_log = empathy_log_manager_exists (manager,
319 empathy_contact_get_account (contact),
320 empathy_contact_get_id (contact),
321 FALSE);
322 g_object_unref (manager);
324 item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
325 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
326 GTK_ICON_SIZE_MENU);
327 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
328 gtk_widget_set_sensitive (item, have_log);
329 gtk_widget_show (image);
331 g_signal_connect_swapped (item, "activate",
332 G_CALLBACK (contact_log_menu_item_activate_cb),
333 contact);
335 return item;
338 GtkWidget *
339 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
341 GtkWidget *item;
342 GtkWidget *image;
344 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
346 item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
347 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
348 GTK_ICON_SIZE_MENU);
349 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
350 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
351 gtk_widget_show (image);
353 g_signal_connect_swapped (item, "activate",
354 G_CALLBACK (empathy_send_file_with_file_chooser),
355 contact);
357 return item;
360 /* FIXME we should check if the contact supports vnc stream tube */
361 GtkWidget *
362 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
364 GtkWidget *item;
365 GtkWidget *image;
367 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
369 item = gtk_image_menu_item_new_with_mnemonic (_("Share my desktop"));
370 image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
371 GTK_ICON_SIZE_MENU);
372 gtk_widget_set_sensitive (item, empathy_contact_can_use_stream_tube (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_share_my_desktop_share_with_contact),
378 contact);
380 return item;
383 static void
384 contact_info_menu_item_activate_cb (EmpathyContact *contact)
386 empathy_contact_information_dialog_show (contact, NULL);
389 GtkWidget *
390 empathy_contact_info_menu_item_new (EmpathyContact *contact)
392 GtkWidget *item;
393 GtkWidget *image;
395 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
397 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
398 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
399 GTK_ICON_SIZE_MENU);
400 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
401 gtk_widget_show (image);
403 g_signal_connect_swapped (item, "activate",
404 G_CALLBACK (contact_info_menu_item_activate_cb),
405 contact);
407 return item;
410 static void
411 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
413 empathy_contact_edit_dialog_show (contact, NULL);
416 GtkWidget *
417 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
419 EmpathyContactManager *manager;
420 GtkWidget *item;
421 GtkWidget *image;
422 gboolean enable = FALSE;
424 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
426 if (empathy_contact_manager_initialized ()) {
427 TpConnection *connection;
428 EmpathyContactListFlags flags;
430 manager = empathy_contact_manager_dup_singleton ();
431 connection = empathy_contact_get_connection (contact);
432 flags = empathy_contact_manager_get_flags_for_connection (
433 manager, connection);
435 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
436 flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
438 g_object_unref (manager);
441 item = gtk_image_menu_item_new_with_mnemonic (_("_Edit"));
442 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
443 GTK_ICON_SIZE_MENU);
444 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
445 gtk_widget_show (image);
447 gtk_widget_set_sensitive (item, enable);
449 g_signal_connect_swapped (item, "activate",
450 G_CALLBACK (contact_edit_menu_item_activate_cb),
451 contact);
453 return item;
456 typedef struct {
457 EmpathyContact *contact;
458 EmpathyChatroom *chatroom;
459 } RoomSubMenuData;
461 static RoomSubMenuData *
462 room_sub_menu_data_new (EmpathyContact *contact,
463 EmpathyChatroom *chatroom)
465 RoomSubMenuData *data;
467 data = g_slice_new (RoomSubMenuData);
468 data->contact = g_object_ref (contact);
469 data->chatroom = g_object_ref (chatroom);
470 return data;
473 static void
474 room_sub_menu_data_free (RoomSubMenuData *data)
476 /* FIXME: seems this is never called... */
477 g_object_unref (data->contact);
478 g_object_unref (data->chatroom);
479 g_slice_free (RoomSubMenuData, data);
482 static void
483 room_sub_menu_activate_cb (GtkWidget *item,
484 RoomSubMenuData *data)
486 EmpathyTpChat *chat;
488 chat = empathy_chatroom_get_tp_chat (data->chatroom);
489 if (chat == NULL) {
490 /* channel was invalidated. Ignoring */
491 return;
494 /* send invitation */
495 empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
496 _("Inviting you to this room"));
499 static GtkWidget *
500 create_room_sub_menu (EmpathyContact *contact,
501 EmpathyChatroom *chatroom)
503 GtkWidget *item;
504 RoomSubMenuData *data;
506 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
507 data = room_sub_menu_data_new (contact, chatroom);
508 g_signal_connect_data (item, "activate",
509 G_CALLBACK (room_sub_menu_activate_cb), data,
510 (GClosureNotify) room_sub_menu_data_free, 0);
512 return item;
515 GtkWidget *
516 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
518 GtkWidget *item;
519 GtkWidget *image;
520 GtkWidget *room_item;
521 EmpathyChatroomManager *mgr;
522 GList *rooms, *l;
523 GtkWidget *submenu = NULL;
525 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
527 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chat room"));
528 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
529 GTK_ICON_SIZE_MENU);
530 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
532 mgr = empathy_chatroom_manager_dup_singleton (NULL);
533 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
534 empathy_contact_get_account (contact));
536 for (l = rooms; l != NULL; l = g_list_next (l)) {
537 EmpathyChatroom *chatroom = l->data;
539 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
540 if (G_UNLIKELY (submenu == NULL))
541 submenu = gtk_menu_new ();
543 room_item = create_room_sub_menu (contact, chatroom);
544 gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
545 gtk_widget_show (room_item);
549 if (submenu) {
550 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
551 } else {
552 gtk_widget_set_sensitive (item, FALSE);
555 gtk_widget_show (image);
557 g_object_unref (mgr);
558 g_list_free (rooms);
560 return item;