]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Merge branch 'check-vinagre-589225'
[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       NULL, NULL);
221 }
222
223 GtkWidget *
224 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
225 {
226         GtkWidget *item;
227         GtkWidget *image;
228
229         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
230
231         item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
232         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
233                                               GTK_ICON_SIZE_MENU);
234         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
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         EmpathyCallFactory *factory;
249
250         factory = empathy_call_factory_get ();
251         empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
252 }
253
254 GtkWidget *
255 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
256 {
257         GtkWidget *item;
258         GtkWidget *image;
259
260         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
261
262         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
263         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
264                                               GTK_ICON_SIZE_MENU);
265         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
266         gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (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         EmpathyCallFactory *factory;
281
282         factory = empathy_call_factory_get ();
283         empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
284 }
285
286 GtkWidget *
287 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
288 {
289         GtkWidget *item;
290         GtkWidget *image;
291
292         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
293
294         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
295         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
296                                               GTK_ICON_SIZE_MENU);
297         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
298         gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact));
299         gtk_widget_show (image);
300
301         g_signal_connect (item, "activate",
302                                   G_CALLBACK (empathy_contact_video_call_menu_item_activated),
303                                   contact);
304
305         return item;
306 }
307
308 static void
309 contact_log_menu_item_activate_cb (EmpathyContact *contact)
310 {
311         empathy_log_window_show (empathy_contact_get_account (contact),
312                                  empathy_contact_get_id (contact),
313                                  FALSE, NULL);
314 }
315
316 GtkWidget *
317 empathy_contact_log_menu_item_new (EmpathyContact *contact)
318 {
319         TplLogManager     *manager;
320         gboolean           have_log;
321         GtkWidget         *item;
322         GtkWidget         *image;
323
324         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
325
326         manager = tpl_log_manager_dup_singleton ();
327         have_log = tpl_log_manager_exists (manager,
328                                                empathy_contact_get_account (contact),
329                                                empathy_contact_get_id (contact),
330                                                FALSE);
331
332         g_object_unref (manager);
333
334         item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
335         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
336                                               GTK_ICON_SIZE_MENU);
337         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
338         gtk_widget_set_sensitive (item, have_log);
339         gtk_widget_show (image);
340
341         g_signal_connect_swapped (item, "activate",
342                                   G_CALLBACK (contact_log_menu_item_activate_cb),
343                                   contact);
344
345         return item;
346 }
347
348 GtkWidget *
349 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
350 {
351         GtkWidget         *item;
352         GtkWidget         *image;
353
354         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
355
356         item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
357         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
358                                               GTK_ICON_SIZE_MENU);
359         gtk_widget_set_sensitive (item, empathy_contact_can_send_files (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 /* FIXME  we should check if the contact supports vnc stream tube */
371 GtkWidget *
372 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
373 {
374         GtkWidget         *item;
375         GtkWidget         *image;
376
377         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
378
379         item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
380         image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
381                                               GTK_ICON_SIZE_MENU);
382         gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
383                 contact));
384         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
385         gtk_widget_show (image);
386
387         g_signal_connect_swapped (item, "activate",
388                                   G_CALLBACK (empathy_share_my_desktop_share_with_contact),
389                                   contact);
390
391         return item;
392 }
393
394 static void
395 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
396         EmpathyContact *contact)
397 {
398         EmpathyContactManager *manager;
399         EmpathyContactList *list;
400
401         manager = empathy_contact_manager_dup_singleton ();
402         list = EMPATHY_CONTACT_LIST (manager);
403
404         if (gtk_check_menu_item_get_active (item)) {
405                 empathy_contact_list_add_to_favourites (list, contact);
406         } else {
407                 empathy_contact_list_remove_from_favourites (list, contact);
408         }
409
410         g_object_unref (manager);
411 }
412
413 GtkWidget *
414 empathy_contact_favourite_menu_item_new (EmpathyContact *contact)
415 {
416         GtkWidget *item;
417         EmpathyContactManager *manager;
418
419         item = gtk_check_menu_item_new_with_label (_("Favorite"));
420
421         manager = empathy_contact_manager_dup_singleton ();
422         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
423                 empathy_contact_list_is_favourite (EMPATHY_CONTACT_LIST (manager),
424                                                    contact));
425
426         g_signal_connect (item, "toggled",
427                           G_CALLBACK (favourite_menu_item_toggled_cb),
428                           contact);
429
430         g_object_unref (manager);
431         return item;
432 }
433
434 static void
435 contact_info_menu_item_activate_cb (EmpathyContact *contact)
436 {
437         empathy_contact_information_dialog_show (contact, NULL);
438 }
439
440 GtkWidget *
441 empathy_contact_info_menu_item_new (EmpathyContact *contact)
442 {
443         GtkWidget *item;
444         GtkWidget *image;
445
446         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
447
448         item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
449         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
450                                               GTK_ICON_SIZE_MENU);
451         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
452         gtk_widget_show (image);
453
454         g_signal_connect_swapped (item, "activate",
455                                   G_CALLBACK (contact_info_menu_item_activate_cb),
456                                   contact);
457
458         return item;
459 }
460
461 static void
462 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
463 {
464         empathy_contact_edit_dialog_show (contact, NULL);
465 }
466
467 GtkWidget *
468 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
469 {
470         EmpathyContactManager *manager;
471         GtkWidget *item;
472         GtkWidget *image;
473         gboolean enable = FALSE;
474
475         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
476
477         if (empathy_contact_manager_initialized ()) {
478                 TpConnection *connection;
479                 EmpathyContactListFlags flags;
480
481                 manager = empathy_contact_manager_dup_singleton ();
482                 connection = empathy_contact_get_connection (contact);
483                 flags = empathy_contact_manager_get_flags_for_connection (
484                                 manager, connection);
485
486                 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
487                           flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
488
489                 g_object_unref (manager);
490         }
491
492         item = gtk_image_menu_item_new_with_mnemonic (
493                                                      C_("Edit contact (contextual menu)",
494                                                         "_Edit"));
495         image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
496                                               GTK_ICON_SIZE_MENU);
497         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
498         gtk_widget_show (image);
499
500         gtk_widget_set_sensitive (item, enable);
501
502         g_signal_connect_swapped (item, "activate",
503                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
504                                   contact);
505
506         return item;
507 }
508
509 typedef struct  {
510         EmpathyContact *contact;
511         EmpathyChatroom *chatroom;
512 } RoomSubMenuData;
513
514 static RoomSubMenuData *
515 room_sub_menu_data_new (EmpathyContact *contact,
516                         EmpathyChatroom *chatroom)
517 {
518         RoomSubMenuData *data;
519
520         data = g_slice_new (RoomSubMenuData);
521         data->contact = g_object_ref (contact);
522         data->chatroom = g_object_ref (chatroom);
523         return data;
524 }
525
526 static void
527 room_sub_menu_data_free (RoomSubMenuData *data)
528 {
529         g_object_unref (data->contact);
530         g_object_unref (data->chatroom);
531         g_slice_free (RoomSubMenuData, data);
532 }
533
534 static void
535 room_sub_menu_activate_cb (GtkWidget *item,
536                            RoomSubMenuData *data)
537 {
538         EmpathyTpChat *chat;
539
540         chat = empathy_chatroom_get_tp_chat (data->chatroom);
541         if (chat == NULL) {
542                 /* channel was invalidated. Ignoring */
543                 return;
544         }
545
546         /* send invitation */
547         empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
548                 _("Inviting you to this room"));
549 }
550
551 static GtkWidget *
552 create_room_sub_menu (EmpathyContact *contact,
553                       EmpathyChatroom *chatroom)
554 {
555         GtkWidget *item;
556         RoomSubMenuData *data;
557
558         item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
559         data = room_sub_menu_data_new (contact, chatroom);
560         g_signal_connect_data (item, "activate",
561                                G_CALLBACK (room_sub_menu_activate_cb), data,
562                                (GClosureNotify) room_sub_menu_data_free, 0);
563
564         return item;
565 }
566
567 GtkWidget *
568 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
569 {
570         GtkWidget *item;
571         GtkWidget *image;
572         GtkWidget *room_item;
573         EmpathyChatroomManager *mgr;
574         GList *rooms, *l;
575         GtkWidget *submenu = NULL;
576
577         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
578
579         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
580         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
581                                               GTK_ICON_SIZE_MENU);
582         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
583
584         mgr = empathy_chatroom_manager_dup_singleton (NULL);
585         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
586                 empathy_contact_get_account (contact));
587
588         for (l = rooms; l != NULL; l = g_list_next (l)) {
589                 EmpathyChatroom *chatroom = l->data;
590
591                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
592                         if (G_UNLIKELY (submenu == NULL))
593                                 submenu = gtk_menu_new ();
594
595                         room_item = create_room_sub_menu (contact, chatroom);
596                         gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
597                         gtk_widget_show (room_item);
598                 }
599         }
600
601         if (submenu) {
602                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
603         } else {
604                 gtk_widget_set_sensitive (item, FALSE);
605         }
606
607         gtk_widget_show (image);
608
609         g_object_unref (mgr);
610         g_list_free (rooms);
611
612         return item;
613 }
614