]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Merge branch 'bug-595305'
[empathy.git] / libempathy-gtk / empathy-contact-menu.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25
26 #include <glib/gi18n-lib.h>
27 #include <gtk/gtk.h>
28 #include <telepathy-logger/log-manager.h>
29
30 #include <libempathy/empathy-call-factory.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>
36
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"
43
44 GtkWidget *
45 empathy_contact_menu_new (EmpathyContact             *contact,
46                           EmpathyContactFeatureFlags  features)
47 {
48         GtkWidget    *menu;
49         GtkMenuShell *shell;
50         GtkWidget    *item;
51
52         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
53
54         if (features == EMPATHY_CONTACT_FEATURE_NONE) {
55                 return NULL;
56         }
57
58         menu = gtk_menu_new ();
59         shell = GTK_MENU_SHELL (menu);
60
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);
66         }
67
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);
73         }
74
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);
80
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);
85         }
86
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);
92         }
93
94         /* Invite */
95         item = empathy_contact_invite_menu_item_new (contact);
96         gtk_menu_shell_append (shell, item);
97         gtk_widget_show (item);
98
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);
103
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);
110
111         /* Separator */
112         if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
113                         EMPATHY_CONTACT_FEATURE_INFO |
114                         EMPATHY_CONTACT_FEATURE_FAVOURITE)) {
115                 item = gtk_separator_menu_item_new ();
116                 gtk_menu_shell_append (shell, item);
117                 gtk_widget_show (item);
118         }
119
120         /* Edit */
121         if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
122                 item = empathy_contact_edit_menu_item_new (contact);
123                 gtk_menu_shell_append (shell, item);
124                 gtk_widget_show (item);
125         }
126
127         /* Info */
128         if (features & EMPATHY_CONTACT_FEATURE_INFO) {
129                 item = empathy_contact_info_menu_item_new (contact);
130                 gtk_menu_shell_append (shell, item);
131                 gtk_widget_show (item);
132         }
133
134         /* Favorite checkbox */
135         if (features & EMPATHY_CONTACT_FEATURE_FAVOURITE) {
136                 item = empathy_contact_favourite_menu_item_new (contact);
137                 gtk_menu_shell_append (shell, item);
138                 gtk_widget_show (item);
139         }
140
141         return menu;
142 }
143
144 static void
145 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
146         EmpathyContact *contact)
147 {
148         GtkWidget *toplevel;
149
150         toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
151         if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
152                 toplevel = NULL;
153         }
154
155         empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
156                                                       contact);
157 }
158
159 GtkWidget *
160 empathy_contact_add_menu_item_new (EmpathyContact *contact)
161 {
162         GtkWidget *item;
163         GtkWidget *image;
164         EmpathyContactManager *manager;
165         TpConnection *connection;
166         GList *l, *members;
167         gboolean found = FALSE;
168         EmpathyContactListFlags flags;
169
170         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
171
172         if (!empathy_contact_manager_initialized ()) {
173                 return NULL;
174         }
175
176         manager = empathy_contact_manager_dup_singleton ();
177         connection = empathy_contact_get_connection (contact);
178
179         flags = empathy_contact_manager_get_flags_for_connection (manager,
180                         connection);
181
182         if (!(flags & EMPATHY_CONTACT_LIST_CAN_ADD)) {
183                 return NULL;
184         }
185
186         members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
187         for (l = members; l; l = l->next) {
188                 if (!found && empathy_contact_equal (l->data, contact)) {
189                         found = TRUE;
190                         /* we keep iterating so that we don't leak contact
191                          * refs */
192                 }
193
194                 g_object_unref (l->data);
195         }
196         g_list_free (members);
197         g_object_unref (manager);
198
199         if (found) {
200                 return NULL;
201         }
202
203         item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
204         image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
205                                               GTK_ICON_SIZE_MENU);
206         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
207
208         g_signal_connect (item, "activate",
209                         G_CALLBACK (empathy_contact_add_menu_item_activated),
210                         contact);
211
212         return item;
213 }
214
215 static void
216 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
217         EmpathyContact *contact)
218 {
219   empathy_dispatcher_chat_with_contact (contact, gtk_get_current_event_time ());
220 }
221
222 GtkWidget *
223 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
224 {
225         GtkWidget *item;
226         GtkWidget *image;
227
228         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
229
230         item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
231         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
232                                               GTK_ICON_SIZE_MENU);
233         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
234         gtk_widget_set_sensitive (item, !empathy_contact_is_user (contact));
235         gtk_widget_show (image);
236
237         g_signal_connect (item, "activate",
238                                   G_CALLBACK (empathy_contact_chat_menu_item_activated),
239                                   contact);
240
241         return item;
242 }
243
244 static void
245 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
246         EmpathyContact *contact)
247 {
248
249         empathy_call_factory_new_call_with_streams (contact, TRUE, FALSE,
250                 gtk_get_current_event_time (), NULL);
251 }
252
253 GtkWidget *
254 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
255 {
256         GtkWidget *item;
257         GtkWidget *image;
258
259         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
260
261         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
262         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
263                                               GTK_ICON_SIZE_MENU);
264         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
265         gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact) &&
266                                         !empathy_contact_is_user (contact));
267         gtk_widget_show (image);
268
269         g_signal_connect (item, "activate",
270                                   G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
271                                   contact);
272
273         return item;
274 }
275
276 static void
277 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
278         EmpathyContact *contact)
279 {
280         empathy_call_factory_new_call_with_streams (contact, TRUE, TRUE,
281                 gtk_get_current_event_time (), NULL);
282 }
283
284 GtkWidget *
285 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
286 {
287         GtkWidget *item;
288         GtkWidget *image;
289
290         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
291
292         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
293         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
294                                               GTK_ICON_SIZE_MENU);
295         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
296         gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact) &&
297                                         !empathy_contact_is_user (contact));
298         gtk_widget_show (image);
299
300         g_signal_connect (item, "activate",
301                                   G_CALLBACK (empathy_contact_video_call_menu_item_activated),
302                                   contact);
303
304         return item;
305 }
306
307 static void
308 contact_log_menu_item_activate_cb (EmpathyContact *contact)
309 {
310         empathy_log_window_show (empathy_contact_get_account (contact),
311                                  empathy_contact_get_id (contact),
312                                  FALSE, NULL);
313 }
314
315 GtkWidget *
316 empathy_contact_log_menu_item_new (EmpathyContact *contact)
317 {
318         TplLogManager     *manager;
319         gboolean           have_log;
320         GtkWidget         *item;
321         GtkWidget         *image;
322
323         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
324
325         manager = tpl_log_manager_dup_singleton ();
326         have_log = tpl_log_manager_exists (manager,
327                                                empathy_contact_get_account (contact),
328                                                empathy_contact_get_id (contact),
329                                                FALSE);
330
331         g_object_unref (manager);
332
333         item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
334         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
335                                               GTK_ICON_SIZE_MENU);
336         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
337         gtk_widget_set_sensitive (item, have_log);
338         gtk_widget_show (image);
339
340         g_signal_connect_swapped (item, "activate",
341                                   G_CALLBACK (contact_log_menu_item_activate_cb),
342                                   contact);
343
344         return item;
345 }
346
347 GtkWidget *
348 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
349 {
350         GtkWidget         *item;
351         GtkWidget         *image;
352
353         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
354
355         item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
356         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
357                                               GTK_ICON_SIZE_MENU);
358         gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact) &&
359                                         !empathy_contact_is_user (contact));
360         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
361         gtk_widget_show (image);
362
363         g_signal_connect_swapped (item, "activate",
364                                   G_CALLBACK (empathy_send_file_with_file_chooser),
365                                   contact);
366
367         return item;
368 }
369
370 GtkWidget *
371 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
372 {
373         GtkWidget         *item;
374         GtkWidget         *image;
375
376         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
377
378         item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
379         image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
380                                               GTK_ICON_SIZE_MENU);
381         gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
382                 contact));
383         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
384         gtk_widget_show (image);
385
386         g_signal_connect_swapped (item, "activate",
387                                   G_CALLBACK (empathy_share_my_desktop_share_with_contact),
388                                   contact);
389
390         return item;
391 }
392
393 static void
394 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
395         EmpathyContact *contact)
396 {
397         EmpathyContactManager *manager;
398         EmpathyContactList *list;
399
400         manager = empathy_contact_manager_dup_singleton ();
401         list = EMPATHY_CONTACT_LIST (manager);
402
403         if (gtk_check_menu_item_get_active (item)) {
404                 empathy_contact_list_add_to_favourites (list, contact);
405         } else {
406                 empathy_contact_list_remove_from_favourites (list, contact);
407         }
408
409         g_object_unref (manager);
410 }
411
412 GtkWidget *
413 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
414 {
415         GtkWidget *item;
416         EmpathyContactManager *manager;
417
418         item = gtk_check_menu_item_new_with_label (_("Favorite"));
419
420         manager = empathy_contact_manager_dup_singleton ();
421         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
422                 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
423                                                    contact));
424
425         g_signal_connect (item, "toggled",
426                           G_CALLBACK (favourite_menu_item_toggled_cb),
427                           contact);
428
429         g_object_unref (manager);
430         return item;
431 }
432
433 static void
434 contact_info_menu_item_activate_cb (EmpathyContact *contact)
435 {
436         empathy_contact_information_dialog_show (contact, NULL);
437 }
438
439 GtkWidget *
440 empathy_contact_info_menu_item_new (EmpathyContact *contact)
441 {
442         GtkWidget *item;
443         GtkWidget *image;
444
445         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
446
447         item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
448         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
449                                               GTK_ICON_SIZE_MENU);
450         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
451         gtk_widget_show (image);
452
453         g_signal_connect_swapped (item, "activate",
454                                   G_CALLBACK (contact_info_menu_item_activate_cb),
455                                   contact);
456
457         return item;
458 }
459
460 static void
461 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
462 {
463         empathy_contact_edit_dialog_show (contact, NULL);
464 }
465
466 GtkWidget *
467 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
468 {
469         EmpathyContactManager *manager;
470         GtkWidget *item;
471         GtkWidget *image;
472         gboolean enable = FALSE;
473
474         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
475
476         if (empathy_contact_manager_initialized ()) {
477                 TpConnection *connection;
478                 EmpathyContactListFlags flags;
479
480                 manager = empathy_contact_manager_dup_singleton ();
481                 connection = empathy_contact_get_connection (contact);
482                 flags = empathy_contact_manager_get_flags_for_connection (
483                                 manager, connection);
484
485                 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
486                           flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
487
488                 g_object_unref (manager);
489         }
490
491         item = gtk_image_menu_item_new_with_mnemonic (
492                                                      C_("Edit contact (contextual menu)",
493                                                         "_Edit"));
494         image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
495                                               GTK_ICON_SIZE_MENU);
496         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
497         gtk_widget_show (image);
498
499         gtk_widget_set_sensitive (item, enable);
500
501         g_signal_connect_swapped (item, "activate",
502                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
503                                   contact);
504
505         return item;
506 }
507
508 typedef struct  {
509         EmpathyContact *contact;
510         EmpathyChatroom *chatroom;
511 } RoomSubMenuData;
512
513 static RoomSubMenuData *
514 room_sub_menu_data_new (EmpathyContact *contact,
515                         EmpathyChatroom *chatroom)
516 {
517         RoomSubMenuData *data;
518
519         data = g_slice_new (RoomSubMenuData);
520         data->contact = g_object_ref (contact);
521         data->chatroom = g_object_ref (chatroom);
522         return data;
523 }
524
525 static void
526 room_sub_menu_data_free (RoomSubMenuData *data)
527 {
528         g_object_unref (data->contact);
529         g_object_unref (data->chatroom);
530         g_slice_free (RoomSubMenuData, data);
531 }
532
533 static void
534 room_sub_menu_activate_cb (GtkWidget *item,
535                            RoomSubMenuData *data)
536 {
537         EmpathyTpChat *chat;
538
539         chat = empathy_chatroom_get_tp_chat (data->chatroom);
540         if (chat == NULL) {
541                 /* channel was invalidated. Ignoring */
542                 return;
543         }
544
545         /* send invitation */
546         empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
547                 _("Inviting you to this room"));
548 }
549
550 static GtkWidget *
551 create_room_sub_menu (EmpathyContact *contact,
552                       EmpathyChatroom *chatroom)
553 {
554         GtkWidget *item;
555         RoomSubMenuData *data;
556
557         item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
558         data = room_sub_menu_data_new (contact, chatroom);
559         g_signal_connect_data (item, "activate",
560                                G_CALLBACK (room_sub_menu_activate_cb), data,
561                                (GClosureNotify) room_sub_menu_data_free, 0);
562
563         return item;
564 }
565
566 GtkWidget *
567 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
568 {
569         GtkWidget *item;
570         GtkWidget *image;
571         GtkWidget *room_item;
572         EmpathyChatroomManager *mgr;
573         GList *rooms, *l;
574         GtkWidget *submenu = NULL;
575
576         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
577
578         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
579         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
580                                               GTK_ICON_SIZE_MENU);
581         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
582
583         if (empathy_contact_is_user (contact)) {
584                 gtk_widget_set_sensitive (item, FALSE);
585                 gtk_widget_show (image);
586                 return item;
587         }
588
589         mgr = empathy_chatroom_manager_dup_singleton (NULL);
590         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
591                 empathy_contact_get_account (contact));
592
593         for (l = rooms; l != NULL; l = g_list_next (l)) {
594                 EmpathyChatroom *chatroom = l->data;
595
596                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
597                         if (G_UNLIKELY (submenu == NULL))
598                                 submenu = gtk_menu_new ();
599
600                         room_item = create_room_sub_menu (contact, chatroom);
601                         gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
602                         gtk_widget_show (room_item);
603                 }
604         }
605
606         if (submenu) {
607                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
608         } else {
609                 gtk_widget_set_sensitive (item, FALSE);
610         }
611
612         gtk_widget_show (image);
613
614         g_object_unref (mgr);
615         g_list_free (rooms);
616
617         return item;
618 }
619