]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Determine if the contact is not in the Contact List and add a menu item as appropriate
[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
29 #include <libempathy/empathy-call-factory.h>
30 #include <libempathy/empathy-log-manager.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
43 GtkWidget *
44 empathy_contact_menu_new (EmpathyContact             *contact,
45                           EmpathyContactFeatureFlags  features)
46 {
47         GtkWidget    *menu;
48         GtkMenuShell *shell;
49         GtkWidget    *item;
50
51         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
52
53         if (features == EMPATHY_CONTACT_FEATURE_NONE) {
54                 return NULL;
55         }
56
57         menu = gtk_menu_new ();
58         shell = GTK_MENU_SHELL (menu);
59
60         /* Add Contact */
61         item = empathy_contact_add_menu_item_new (contact);
62         if (item) {
63                 gtk_menu_shell_append (shell, item);
64                 gtk_widget_show (item);
65         }
66
67         /* Chat */
68         if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
69                 item = empathy_contact_chat_menu_item_new (contact);
70                 gtk_menu_shell_append (shell, item);
71                 gtk_widget_show (item);
72         }
73
74         if (features & EMPATHY_CONTACT_FEATURE_CALL) {
75                 /* Audio Call */
76                 item = empathy_contact_audio_call_menu_item_new (contact);
77                 gtk_menu_shell_append (shell, item);
78                 gtk_widget_show (item);
79
80                 /* Video Call */
81                 item = empathy_contact_video_call_menu_item_new (contact);
82                 gtk_menu_shell_append (shell, item);
83                 gtk_widget_show (item);
84         }
85
86         /* Log */
87         if (features & EMPATHY_CONTACT_FEATURE_LOG) {
88                 item = empathy_contact_log_menu_item_new (contact);
89                 gtk_menu_shell_append (shell, item);
90                 gtk_widget_show (item);
91         }
92
93         /* Invite */
94         item = empathy_contact_invite_menu_item_new (contact);
95         gtk_menu_shell_append (shell, item);
96         gtk_widget_show (item);
97
98         /* File transfer */
99         item = empathy_contact_file_transfer_menu_item_new (contact);
100         gtk_menu_shell_append (shell, item);
101         gtk_widget_show (item);
102
103         /* Separator */
104         if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
105                         EMPATHY_CONTACT_FEATURE_INFO)) {
106                 item = gtk_separator_menu_item_new ();
107                 gtk_menu_shell_append (shell, item);
108                 gtk_widget_show (item);
109         }
110
111         /* Edit */
112         if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
113                 item = empathy_contact_edit_menu_item_new (contact);
114                 gtk_menu_shell_append (shell, item);
115                 gtk_widget_show (item);
116         }
117
118         /* Info */
119         if (features & EMPATHY_CONTACT_FEATURE_INFO) {
120                 item = empathy_contact_info_menu_item_new (contact);
121                 gtk_menu_shell_append (shell, item);
122                 gtk_widget_show (item);
123         }
124
125         return menu;
126 }
127
128 static void
129 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
130         EmpathyContact *contact)
131 {
132   empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
133 }
134
135 GtkWidget *
136 empathy_contact_add_menu_item_new (EmpathyContact *contact)
137 {
138         GtkWidget *item;
139         GtkWidget *image;
140         EmpathyContactManager *manager;
141         GList *l, *members;
142         gboolean found = FALSE;
143
144         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
145
146         manager = empathy_contact_manager_dup_singleton ();
147         members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
148         for (l = members; l; l = l->next) {
149                 if (!found && empathy_contact_equal (l->data, contact)) {
150                         found = TRUE;
151                         /* we keep iterating so that we don't leak contact
152                          * refs */
153                 }
154
155                 g_object_unref (l->data);
156         }
157         g_list_free (members);
158
159         if (found) return NULL;
160
161         item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact..."));
162         image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
163                                               GTK_ICON_SIZE_MENU);
164         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
165
166         /* FIXME - callback */
167
168         return item;
169 }
170
171 GtkWidget *
172 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
173 {
174         GtkWidget *item;
175         GtkWidget *image;
176
177         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
178
179         item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
180         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
181                                               GTK_ICON_SIZE_MENU);
182         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
183         gtk_widget_show (image);
184
185         g_signal_connect (item, "activate",
186                                   G_CALLBACK (empathy_contact_chat_menu_item_activated),
187                                   contact);
188
189         return item;
190 }
191
192 static void
193 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
194         EmpathyContact *contact)
195 {
196         EmpathyCallFactory *factory;
197
198         factory = empathy_call_factory_get ();
199         empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
200 }
201
202 GtkWidget *
203 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
204 {
205         GtkWidget *item;
206         GtkWidget *image;
207
208         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
209
210         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
211         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
212                                               GTK_ICON_SIZE_MENU);
213         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
214         gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
215         gtk_widget_show (image);
216
217         g_signal_connect (item, "activate",
218                                   G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
219                                   contact);
220
221         return item;
222 }
223
224 static void
225 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
226         EmpathyContact *contact)
227 {
228         EmpathyCallFactory *factory;
229
230         factory = empathy_call_factory_get ();
231         empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
232 }
233
234 GtkWidget *
235 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
236 {
237         GtkWidget *item;
238         GtkWidget *image;
239
240         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
241
242         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
243         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
244                                               GTK_ICON_SIZE_MENU);
245         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
246         gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
247         gtk_widget_show (image);
248
249         g_signal_connect (item, "activate",
250                                   G_CALLBACK (empathy_contact_video_call_menu_item_activated),
251                                   contact);
252
253         return item;
254 }
255
256 static void
257 contact_log_menu_item_activate_cb (EmpathyContact *contact)
258 {
259         empathy_log_window_show (empathy_contact_get_account (contact),
260                                  empathy_contact_get_id (contact),
261                                  FALSE, NULL);
262 }
263
264 GtkWidget *
265 empathy_contact_log_menu_item_new (EmpathyContact *contact)
266 {
267         EmpathyLogManager *manager;
268         gboolean           have_log;
269         GtkWidget         *item;
270         GtkWidget         *image;
271
272         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
273
274         manager = empathy_log_manager_dup_singleton ();
275         have_log = empathy_log_manager_exists (manager,
276                                                empathy_contact_get_account (contact),
277                                                empathy_contact_get_id (contact),
278                                                FALSE);
279         g_object_unref (manager);
280
281         item = gtk_image_menu_item_new_with_mnemonic (_("_View Previous Conversations"));
282         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
283                                               GTK_ICON_SIZE_MENU);
284         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
285         gtk_widget_set_sensitive (item, have_log);
286         gtk_widget_show (image);
287
288         g_signal_connect_swapped (item, "activate",
289                                   G_CALLBACK (contact_log_menu_item_activate_cb),
290                                   contact);
291
292         return item;
293 }
294
295 GtkWidget *
296 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
297 {
298         GtkWidget         *item;
299         GtkWidget         *image;
300
301         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
302
303         item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
304         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
305                                               GTK_ICON_SIZE_MENU);
306         gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
307         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
308         gtk_widget_show (image);
309
310         g_signal_connect_swapped (item, "activate",
311                                   G_CALLBACK (empathy_send_file_with_file_chooser),
312                                   contact);
313
314         return item;
315 }
316
317 static void
318 contact_info_menu_item_activate_cb (EmpathyContact *contact)
319 {
320         empathy_contact_information_dialog_show (contact, NULL);
321 }
322
323 GtkWidget *
324 empathy_contact_info_menu_item_new (EmpathyContact *contact)
325 {
326         GtkWidget *item;
327         GtkWidget *image;
328
329         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
330
331         item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
332         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
333                                               GTK_ICON_SIZE_MENU);
334         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
335         gtk_widget_show (image);
336
337         g_signal_connect_swapped (item, "activate",
338                                   G_CALLBACK (contact_info_menu_item_activate_cb),
339                                   contact);
340
341         return item;
342 }
343
344 static void
345 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
346 {
347         empathy_contact_edit_dialog_show (contact, NULL);
348 }
349
350 GtkWidget *
351 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
352 {
353         EmpathyContactManager *manager;
354         GtkWidget *item;
355         GtkWidget *image;
356         gboolean enable = FALSE;
357
358         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
359
360         if (empathy_contact_manager_initialized ()) {
361                 TpConnection *connection;
362                 EmpathyContactListFlags flags;
363
364                 manager = empathy_contact_manager_dup_singleton ();
365                 connection = empathy_contact_get_connection (contact);
366                 flags = empathy_contact_manager_get_flags_for_connection (
367                                 manager, connection);
368
369                 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
370                           flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
371
372                 g_object_unref (manager);
373         }
374
375         item = gtk_image_menu_item_new_with_mnemonic (_("_Edit"));
376         image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
377                                               GTK_ICON_SIZE_MENU);
378         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
379         gtk_widget_show (image);
380
381         gtk_widget_set_sensitive (item, enable);
382
383         g_signal_connect_swapped (item, "activate",
384                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
385                                   contact);
386
387         return item;
388 }
389
390 typedef struct  {
391         EmpathyContact *contact;
392         EmpathyChatroom *chatroom;
393 } RoomSubMenuData;
394
395 static RoomSubMenuData *
396 room_sub_menu_data_new (EmpathyContact *contact,
397                         EmpathyChatroom *chatroom)
398 {
399         RoomSubMenuData *data;
400
401         data = g_slice_new (RoomSubMenuData);
402         data->contact = g_object_ref (contact);
403         data->chatroom = g_object_ref (chatroom);
404         return data;
405 }
406
407 static void
408 room_sub_menu_data_free (RoomSubMenuData *data)
409 {
410         /* FIXME: seems this is never called... */
411         g_object_unref (data->contact);
412         g_object_unref (data->chatroom);
413         g_slice_free (RoomSubMenuData, data);
414 }
415
416 static void
417 room_sub_menu_activate_cb (GtkWidget *item,
418                            RoomSubMenuData *data)
419 {
420         TpHandle handle;
421         GArray handles = {(gchar *) &handle, 1};
422         EmpathyTpChat *chat;
423         TpChannel *channel;
424
425         chat = empathy_chatroom_get_tp_chat (data->chatroom);
426         if (chat == NULL) {
427                 /* channel was invalidated. Ignoring */
428                 return;
429         }
430
431         /* send invitation */
432         handle = empathy_contact_get_handle (data->contact);
433         channel = empathy_tp_chat_get_channel (chat);
434         tp_cli_channel_interface_group_call_add_members (channel, -1, &handles,
435                 _("Inviting to this room"), NULL, NULL, NULL, NULL);
436 }
437
438 static GtkWidget *
439 create_room_sub_menu (EmpathyContact *contact,
440                       EmpathyChatroom *chatroom)
441 {
442         GtkWidget *item;
443         RoomSubMenuData *data;
444
445         item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
446         data = room_sub_menu_data_new (contact, chatroom);
447         g_signal_connect_data (item, "activate",
448                                G_CALLBACK (room_sub_menu_activate_cb), data,
449                                (GClosureNotify) room_sub_menu_data_free, 0);
450
451         return item;
452 }
453
454 GtkWidget *
455 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
456 {
457         GtkWidget *item;
458         GtkWidget *image;
459         GtkWidget *room_item;
460         EmpathyChatroomManager *mgr;
461         GList *rooms, *l;
462         GtkWidget *submenu = NULL;
463
464         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
465
466         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
467         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
468                                               GTK_ICON_SIZE_MENU);
469         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
470
471         mgr = empathy_chatroom_manager_dup_singleton (NULL);
472         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
473                 empathy_contact_get_account (contact));
474
475         for (l = rooms; l != NULL; l = g_list_next (l)) {
476                 EmpathyChatroom *chatroom = l->data;
477
478                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
479                         if (G_UNLIKELY (submenu == NULL))
480                                 submenu = gtk_menu_new ();
481
482                         room_item = create_room_sub_menu (contact, chatroom);
483                         gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
484                         gtk_widget_show (room_item);
485                 }
486         }
487
488         if (submenu) {
489                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
490         } else {
491                 gtk_widget_set_sensitive (item, FALSE);
492         }
493
494         gtk_widget_show (image);
495
496         g_object_unref (mgr);
497         g_list_free (rooms);
498
499         return item;
500 }
501