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