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