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