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