]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Use gtk_list_store_insert_with_values ()
[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-contact-manager.h>
31 #include <libempathy/empathy-request-util.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-chatroom-manager.h>
34 #include <libempathy/empathy-contact-manager.h>
35
36 #include "empathy-contact-menu.h"
37 #include "empathy-images.h"
38 #include "empathy-log-window.h"
39 #include "empathy-contact-dialogs.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-share-my-desktop.h"
42 #include "empathy-call-utils.h"
43
44 static GtkWidget *empathy_contact_block_menu_item_new (EmpathyContact *);
45
46 GtkWidget *
47 empathy_contact_menu_new (EmpathyContact             *contact,
48                           EmpathyContactFeatureFlags  features)
49 {
50         GtkWidget    *menu;
51         GtkMenuShell *shell;
52         GtkWidget    *item;
53
54         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
55
56         if (features == EMPATHY_CONTACT_FEATURE_NONE) {
57                 return NULL;
58         }
59
60         menu = gtk_menu_new ();
61         shell = GTK_MENU_SHELL (menu);
62
63         /* Add Contact */
64         item = empathy_contact_add_menu_item_new (contact);
65         if (item) {
66                 gtk_menu_shell_append (shell, item);
67                 gtk_widget_show (item);
68         }
69
70         /* Chat */
71         if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
72                 item = empathy_contact_chat_menu_item_new (contact);
73                 gtk_menu_shell_append (shell, item);
74                 gtk_widget_show (item);
75         }
76
77         if (features & EMPATHY_CONTACT_FEATURE_CALL) {
78                 /* Audio Call */
79                 item = empathy_contact_audio_call_menu_item_new (contact);
80                 gtk_menu_shell_append (shell, item);
81                 gtk_widget_show (item);
82
83                 /* Video Call */
84                 item = empathy_contact_video_call_menu_item_new (contact);
85                 gtk_menu_shell_append (shell, item);
86                 gtk_widget_show (item);
87         }
88
89         /* Invite */
90         item = empathy_contact_invite_menu_item_new (contact);
91         gtk_menu_shell_append (shell, item);
92         gtk_widget_show (item);
93
94         /* File transfer */
95         if (features & EMPATHY_CONTACT_FEATURE_FT) {
96                 item = empathy_contact_file_transfer_menu_item_new (contact);
97                 gtk_menu_shell_append (shell, item);
98                 gtk_widget_show (item);
99         }
100
101         /* Share my desktop */
102         /* FIXME we should add the "Share my desktop" menu item if Vino is
103         a registered handler in MC5 */
104         item = empathy_contact_share_my_desktop_menu_item_new (contact);
105         gtk_menu_shell_append (shell, item);
106         gtk_widget_show (item);
107
108         /* Separator */
109         if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
110                         EMPATHY_CONTACT_FEATURE_INFO)) {
111                 item = gtk_separator_menu_item_new ();
112                 gtk_menu_shell_append (shell, item);
113                 gtk_widget_show (item);
114         }
115
116         /* Edit */
117         if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
118                 item = empathy_contact_edit_menu_item_new (contact);
119                 gtk_menu_shell_append (shell, item);
120                 gtk_widget_show (item);
121         }
122
123         /* Log */
124         if (features & EMPATHY_CONTACT_FEATURE_LOG) {
125                 item = empathy_contact_log_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         /* Separator & Block */
138         if (features & EMPATHY_CONTACT_FEATURE_BLOCK &&
139             (item = empathy_contact_block_menu_item_new (contact)) != NULL) {
140                 GtkWidget *sep;
141
142                 sep = gtk_separator_menu_item_new ();
143                 gtk_menu_shell_append (shell, sep);
144                 gtk_widget_show (sep);
145
146                 gtk_menu_shell_append (shell, item);
147                 gtk_widget_show (item);
148         }
149
150         return menu;
151 }
152
153 static void
154 empathy_contact_add_menu_item_activated (GtkMenuItem *item,
155         EmpathyContact *contact)
156 {
157         GtkWidget *toplevel;
158
159         toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
160         if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel)) {
161                 toplevel = NULL;
162         }
163
164         empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
165                                                       contact);
166 }
167
168 GtkWidget *
169 empathy_contact_add_menu_item_new (EmpathyContact *contact)
170 {
171         GtkWidget *item;
172         GtkWidget *image;
173         EmpathyContactManager *manager;
174         TpConnection *connection;
175         GList *l, *members;
176         gboolean found = FALSE;
177
178         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
179
180         if (!empathy_contact_manager_initialized ()) {
181                 return NULL;
182         }
183
184         manager = empathy_contact_manager_dup_singleton ();
185         connection = empathy_contact_get_connection (contact);
186
187         if (!tp_connection_get_can_change_contact_list (connection))
188                 return NULL;
189
190         members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
191         for (l = members; l; l = l->next) {
192                 if (!found && empathy_contact_equal (l->data, contact)) {
193                         found = TRUE;
194                         /* we keep iterating so that we don't leak contact
195                          * refs */
196                 }
197
198                 g_object_unref (l->data);
199         }
200         g_list_free (members);
201         g_object_unref (manager);
202
203         if (found) {
204                 return NULL;
205         }
206
207         item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
208         image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
209                                               GTK_ICON_SIZE_MENU);
210         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
211
212         g_signal_connect (item, "activate",
213                         G_CALLBACK (empathy_contact_add_menu_item_activated),
214                         contact);
215
216         return item;
217 }
218
219 static void
220 empathy_contact_block_menu_item_toggled (GtkCheckMenuItem *item,
221                                          EmpathyContact   *contact)
222 {
223         static guint block_signal = 0;
224         gboolean blocked, abusive;
225         TpContact *tp_contact;
226
227         if (block_signal > 0)
228                 return;
229
230         blocked = gtk_check_menu_item_get_active (item);
231
232         if (blocked) {
233                 /* confirm the user really wishes to block the contact */
234                 GtkWidget *parent;
235                 GdkPixbuf *avatar;
236
237                 /* gtk_menu_get_attach_widget () doesn't behave properly here
238                  * for some reason */
239                 parent = g_object_get_data (
240                         G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (item))),
241                         "window");
242
243                 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48);
244
245                 if (!empathy_block_contact_dialog_show (GTK_WINDOW (parent),
246                                         contact, avatar, &abusive))
247                         return;
248         }
249
250         tp_contact = empathy_contact_get_tp_contact (contact);
251
252         if (blocked)
253                 tp_contact_block_async (tp_contact, abusive, NULL, NULL);
254         else
255                 tp_contact_unblock_async (tp_contact, NULL, NULL);
256
257         /* update the toggle with the blocked status */
258         block_signal++;
259         gtk_check_menu_item_set_active (item, blocked);
260         block_signal--;
261 }
262
263 static GtkWidget *
264 empathy_contact_block_menu_item_new (EmpathyContact *contact)
265 {
266         GtkWidget *item;
267         TpConnection *connection;
268         TpContact *tp_contact;
269
270         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
271
272         if (!empathy_contact_manager_initialized ()) {
273                 return NULL;
274         }
275
276         connection = empathy_contact_get_connection (contact);
277
278         if (!tp_proxy_has_interface_by_id (connection,
279                 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
280                 return NULL;
281
282         item = gtk_check_menu_item_new_with_mnemonic (_("_Block Contact"));
283
284         tp_contact = empathy_contact_get_tp_contact (contact);
285
286         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
287                         tp_contact_is_blocked (tp_contact));
288
289         g_signal_connect (item, "toggled",
290                         G_CALLBACK (empathy_contact_block_menu_item_toggled),
291                         contact);
292
293         return item;
294 }
295
296 static void
297 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
298         EmpathyContact *contact)
299 {
300         empathy_chat_with_contact (contact, empathy_get_current_action_time ());
301 }
302
303 GtkWidget *
304 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
305 {
306         GtkWidget *item;
307         GtkWidget *image;
308
309         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
310
311         item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
312         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
313                                               GTK_ICON_SIZE_MENU);
314         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
315         gtk_widget_set_sensitive (item, !empathy_contact_is_user (contact));
316         gtk_widget_show (image);
317
318         g_signal_connect (item, "activate",
319                                   G_CALLBACK (empathy_contact_chat_menu_item_activated),
320                                   contact);
321
322         return item;
323 }
324
325 static void
326 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
327         EmpathyContact *contact)
328 {
329         empathy_call_new_with_streams (empathy_contact_get_id (contact),
330                 empathy_contact_get_account (contact),
331                 TRUE, FALSE,
332                 empathy_get_current_action_time ());
333 }
334
335 GtkWidget *
336 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
337 {
338         GtkWidget *item;
339         GtkWidget *image;
340
341         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
342
343         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
344         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
345                                               GTK_ICON_SIZE_MENU);
346         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
347         gtk_widget_set_sensitive (item, empathy_contact_can_voip_audio (contact) &&
348                                         !empathy_contact_is_user (contact));
349         gtk_widget_show (image);
350
351         g_signal_connect (item, "activate",
352                                   G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
353                                   contact);
354
355         return item;
356 }
357
358 static void
359 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
360         EmpathyContact *contact)
361 {
362         empathy_call_new_with_streams (empathy_contact_get_id (contact),
363                 empathy_contact_get_account (contact),
364                 TRUE, TRUE,
365                 empathy_get_current_action_time ());
366 }
367
368 GtkWidget *
369 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
370 {
371         GtkWidget *item;
372         GtkWidget *image;
373
374         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
375
376         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
377         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
378                                               GTK_ICON_SIZE_MENU);
379         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
380         gtk_widget_set_sensitive (item, empathy_contact_can_voip_video (contact) &&
381                                         !empathy_contact_is_user (contact));
382         gtk_widget_show (image);
383
384         g_signal_connect (item, "activate",
385                                   G_CALLBACK (empathy_contact_video_call_menu_item_activated),
386                                   contact);
387
388         return item;
389 }
390
391 static void
392 contact_log_menu_item_activate_cb (EmpathyContact *contact)
393 {
394         empathy_log_window_show (empathy_contact_get_account (contact),
395                                  empathy_contact_get_id (contact),
396                                  FALSE, NULL);
397 }
398
399 GtkWidget *
400 empathy_contact_log_menu_item_new (EmpathyContact *contact)
401 {
402         TplLogManager     *manager;
403         TplEntity         *entity;
404         gboolean           have_log;
405         GtkWidget         *item;
406         GtkWidget         *image;
407
408         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
409
410         manager = tpl_log_manager_dup_singleton ();
411         entity = tpl_entity_new_from_tp_contact (empathy_contact_get_tp_contact (contact),
412                                                  TPL_ENTITY_CONTACT);
413
414         have_log = tpl_log_manager_exists (manager,
415                                            empathy_contact_get_account (contact),
416                                            entity,
417                                            TPL_EVENT_MASK_TEXT);
418
419         g_object_unref (entity);
420         g_object_unref (manager);
421
422         item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
423         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
424                                               GTK_ICON_SIZE_MENU);
425         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
426         gtk_widget_set_sensitive (item, have_log);
427         gtk_widget_show (image);
428
429         g_signal_connect_swapped (item, "activate",
430                                   G_CALLBACK (contact_log_menu_item_activate_cb),
431                                   contact);
432
433         return item;
434 }
435
436 GtkWidget *
437 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
438 {
439         GtkWidget         *item;
440         GtkWidget         *image;
441
442         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
443
444         item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
445         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
446                                               GTK_ICON_SIZE_MENU);
447         gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact) &&
448                                         !empathy_contact_is_user (contact));
449         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
450         gtk_widget_show (image);
451
452         g_signal_connect_swapped (item, "activate",
453                                   G_CALLBACK (empathy_send_file_with_file_chooser),
454                                   contact);
455
456         return item;
457 }
458
459 GtkWidget *
460 empathy_contact_share_my_desktop_menu_item_new (EmpathyContact *contact)
461 {
462         GtkWidget         *item;
463         GtkWidget         *image;
464
465         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
466
467         item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
468         image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK,
469                                               GTK_ICON_SIZE_MENU);
470         gtk_widget_set_sensitive (item, empathy_contact_can_use_rfb_stream_tube (
471                 contact));
472         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
473         gtk_widget_show (image);
474
475         g_signal_connect_swapped (item, "activate",
476                                   G_CALLBACK (empathy_share_my_desktop_share_with_contact),
477                                   contact);
478
479         return item;
480 }
481
482 static void
483 contact_info_menu_item_activate_cb (EmpathyContact *contact)
484 {
485         empathy_contact_information_dialog_show (contact, NULL);
486 }
487
488 GtkWidget *
489 empathy_contact_info_menu_item_new (EmpathyContact *contact)
490 {
491         GtkWidget *item;
492         GtkWidget *image;
493
494         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
495
496         item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
497         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
498                                               GTK_ICON_SIZE_MENU);
499         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
500         gtk_widget_show (image);
501
502         g_signal_connect_swapped (item, "activate",
503                                   G_CALLBACK (contact_info_menu_item_activate_cb),
504                                   contact);
505
506         return item;
507 }
508
509 static void
510 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
511 {
512         empathy_contact_edit_dialog_show (contact, NULL);
513 }
514
515 GtkWidget *
516 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
517 {
518         GtkWidget *item;
519         GtkWidget *image;
520         gboolean enable = FALSE;
521
522         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
523
524         if (empathy_contact_manager_initialized ()) {
525                 TpConnection *connection;
526
527                 connection = empathy_contact_get_connection (contact);
528
529                 enable = (tp_connection_can_set_contact_alias (connection) ||
530                           tp_connection_get_group_storage (connection) !=
531                                          TP_CONTACT_METADATA_STORAGE_TYPE_NONE);
532         }
533
534         item = gtk_image_menu_item_new_with_mnemonic (
535                                                      C_("Edit contact (contextual menu)",
536                                                         "_Edit"));
537         image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
538                                               GTK_ICON_SIZE_MENU);
539         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
540         gtk_widget_show (image);
541
542         gtk_widget_set_sensitive (item, enable);
543
544         g_signal_connect_swapped (item, "activate",
545                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
546                                   contact);
547
548         return item;
549 }
550
551 typedef struct  {
552         EmpathyContact *contact;
553         EmpathyChatroom *chatroom;
554 } RoomSubMenuData;
555
556 static RoomSubMenuData *
557 room_sub_menu_data_new (EmpathyContact *contact,
558                         EmpathyChatroom *chatroom)
559 {
560         RoomSubMenuData *data;
561
562         data = g_slice_new (RoomSubMenuData);
563         data->contact = g_object_ref (contact);
564         data->chatroom = g_object_ref (chatroom);
565         return data;
566 }
567
568 static void
569 room_sub_menu_data_free (RoomSubMenuData *data)
570 {
571         g_object_unref (data->contact);
572         g_object_unref (data->chatroom);
573         g_slice_free (RoomSubMenuData, data);
574 }
575
576 static void
577 room_sub_menu_activate_cb (GtkWidget *item,
578                            RoomSubMenuData *data)
579 {
580         EmpathyTpChat *chat;
581
582         chat = empathy_chatroom_get_tp_chat (data->chatroom);
583         if (chat == NULL) {
584                 /* channel was invalidated. Ignoring */
585                 return;
586         }
587
588         /* send invitation */
589         empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat), data->contact,
590                 _("Inviting you to this room"));
591 }
592
593 static GtkWidget *
594 create_room_sub_menu (EmpathyContact *contact,
595                       EmpathyChatroom *chatroom)
596 {
597         GtkWidget *item;
598         RoomSubMenuData *data;
599
600         item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
601         data = room_sub_menu_data_new (contact, chatroom);
602         g_signal_connect_data (item, "activate",
603                                G_CALLBACK (room_sub_menu_activate_cb), data,
604                                (GClosureNotify) room_sub_menu_data_free, 0);
605
606         return item;
607 }
608
609 GtkWidget *
610 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
611 {
612         GtkWidget *item;
613         GtkWidget *image;
614         GtkWidget *room_item;
615         EmpathyChatroomManager *mgr;
616         GList *rooms, *l;
617         GtkWidget *submenu = NULL;
618
619         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
620
621         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
622         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
623                                               GTK_ICON_SIZE_MENU);
624         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
625
626         if (empathy_contact_is_user (contact)) {
627                 gtk_widget_set_sensitive (item, FALSE);
628                 gtk_widget_show (image);
629                 return item;
630         }
631
632         mgr = empathy_chatroom_manager_dup_singleton (NULL);
633         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
634                 empathy_contact_get_account (contact));
635
636         for (l = rooms; l != NULL; l = g_list_next (l)) {
637                 EmpathyChatroom *chatroom = l->data;
638
639                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
640                         if (G_UNLIKELY (submenu == NULL))
641                                 submenu = gtk_menu_new ();
642
643                         room_item = create_room_sub_menu (contact, chatroom);
644                         gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
645                         gtk_widget_show (room_item);
646                 }
647         }
648
649         if (submenu) {
650                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
651         } else {
652                 gtk_widget_set_sensitive (item, FALSE);
653         }
654
655         gtk_widget_show (image);
656
657         g_object_unref (mgr);
658         g_list_free (rooms);
659
660         return item;
661 }
662