]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Hook up the callback to throw up the standard Add Contact dialog
[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_add_menu_item_activated (GtkMenuItem *item,
130         EmpathyContact *contact)
131 {
132         /* FIXME - attempt to get parent */
133         /* FIXME - the contact dialog doesn't set the source account right */
134         empathy_new_contact_dialog_show_with_contact (NULL, contact);
135 }
136
137 GtkWidget *
138 empathy_contact_add_menu_item_new (EmpathyContact *contact)
139 {
140         GtkWidget *item;
141         GtkWidget *image;
142         EmpathyContactManager *manager;
143         GList *l, *members;
144         gboolean found = FALSE;
145
146         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
147
148         manager = empathy_contact_manager_dup_singleton ();
149         members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (manager));
150         for (l = members; l; l = l->next) {
151                 if (!found && empathy_contact_equal (l->data, contact)) {
152                         found = TRUE;
153                         /* we keep iterating so that we don't leak contact
154                          * refs */
155                 }
156
157                 g_object_unref (l->data);
158         }
159         g_list_free (members);
160
161         if (found) return NULL;
162
163         item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact..."));
164         image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
165                                               GTK_ICON_SIZE_MENU);
166         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
167
168         g_signal_connect (item, "activate",
169                         G_CALLBACK (empathy_contact_add_menu_item_activated),
170                         contact);
171
172         return item;
173 }
174
175 static void
176 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
177         EmpathyContact *contact)
178 {
179   empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
180 }
181
182 GtkWidget *
183 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
184 {
185         GtkWidget *item;
186         GtkWidget *image;
187
188         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
189
190         item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
191         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
192                                               GTK_ICON_SIZE_MENU);
193         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
194         gtk_widget_show (image);
195
196         g_signal_connect (item, "activate",
197                                   G_CALLBACK (empathy_contact_chat_menu_item_activated),
198                                   contact);
199
200         return item;
201 }
202
203 static void
204 empathy_contact_audio_call_menu_item_activated (GtkMenuItem *item,
205         EmpathyContact *contact)
206 {
207         EmpathyCallFactory *factory;
208
209         factory = empathy_call_factory_get ();
210         empathy_call_factory_new_call_with_streams (factory, contact, TRUE, FALSE);
211 }
212
213 GtkWidget *
214 empathy_contact_audio_call_menu_item_new (EmpathyContact *contact)
215 {
216         GtkWidget *item;
217         GtkWidget *image;
218
219         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
220
221         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
222         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
223                                               GTK_ICON_SIZE_MENU);
224         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
225         gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
226         gtk_widget_show (image);
227
228         g_signal_connect (item, "activate",
229                                   G_CALLBACK (empathy_contact_audio_call_menu_item_activated),
230                                   contact);
231
232         return item;
233 }
234
235 static void
236 empathy_contact_video_call_menu_item_activated (GtkMenuItem *item,
237         EmpathyContact *contact)
238 {
239         EmpathyCallFactory *factory;
240
241         factory = empathy_call_factory_get ();
242         empathy_call_factory_new_call_with_streams (factory, contact, TRUE, TRUE);
243 }
244
245 GtkWidget *
246 empathy_contact_video_call_menu_item_new (EmpathyContact *contact)
247 {
248         GtkWidget *item;
249         GtkWidget *image;
250
251         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
252
253         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
254         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
255                                               GTK_ICON_SIZE_MENU);
256         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
257         gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
258         gtk_widget_show (image);
259
260         g_signal_connect (item, "activate",
261                                   G_CALLBACK (empathy_contact_video_call_menu_item_activated),
262                                   contact);
263
264         return item;
265 }
266
267 static void
268 contact_log_menu_item_activate_cb (EmpathyContact *contact)
269 {
270         empathy_log_window_show (empathy_contact_get_account (contact),
271                                  empathy_contact_get_id (contact),
272                                  FALSE, NULL);
273 }
274
275 GtkWidget *
276 empathy_contact_log_menu_item_new (EmpathyContact *contact)
277 {
278         EmpathyLogManager *manager;
279         gboolean           have_log;
280         GtkWidget         *item;
281         GtkWidget         *image;
282
283         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
284
285         manager = empathy_log_manager_dup_singleton ();
286         have_log = empathy_log_manager_exists (manager,
287                                                empathy_contact_get_account (contact),
288                                                empathy_contact_get_id (contact),
289                                                FALSE);
290         g_object_unref (manager);
291
292         item = gtk_image_menu_item_new_with_mnemonic (_("_View Previous Conversations"));
293         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
294                                               GTK_ICON_SIZE_MENU);
295         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
296         gtk_widget_set_sensitive (item, have_log);
297         gtk_widget_show (image);
298
299         g_signal_connect_swapped (item, "activate",
300                                   G_CALLBACK (contact_log_menu_item_activate_cb),
301                                   contact);
302
303         return item;
304 }
305
306 GtkWidget *
307 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
308 {
309         GtkWidget         *item;
310         GtkWidget         *image;
311
312         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
313
314         item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
315         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
316                                               GTK_ICON_SIZE_MENU);
317         gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
318         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
319         gtk_widget_show (image);
320
321         g_signal_connect_swapped (item, "activate",
322                                   G_CALLBACK (empathy_send_file_with_file_chooser),
323                                   contact);
324
325         return item;
326 }
327
328 static void
329 contact_info_menu_item_activate_cb (EmpathyContact *contact)
330 {
331         empathy_contact_information_dialog_show (contact, NULL);
332 }
333
334 GtkWidget *
335 empathy_contact_info_menu_item_new (EmpathyContact *contact)
336 {
337         GtkWidget *item;
338         GtkWidget *image;
339
340         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
341
342         item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
343         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
344                                               GTK_ICON_SIZE_MENU);
345         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
346         gtk_widget_show (image);
347
348         g_signal_connect_swapped (item, "activate",
349                                   G_CALLBACK (contact_info_menu_item_activate_cb),
350                                   contact);
351
352         return item;
353 }
354
355 static void
356 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
357 {
358         empathy_contact_edit_dialog_show (contact, NULL);
359 }
360
361 GtkWidget *
362 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
363 {
364         EmpathyContactManager *manager;
365         GtkWidget *item;
366         GtkWidget *image;
367         gboolean enable = FALSE;
368
369         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
370
371         if (empathy_contact_manager_initialized ()) {
372                 TpConnection *connection;
373                 EmpathyContactListFlags flags;
374
375                 manager = empathy_contact_manager_dup_singleton ();
376                 connection = empathy_contact_get_connection (contact);
377                 flags = empathy_contact_manager_get_flags_for_connection (
378                                 manager, connection);
379
380                 enable = (flags & EMPATHY_CONTACT_LIST_CAN_ALIAS ||
381                           flags & EMPATHY_CONTACT_LIST_CAN_GROUP);
382
383                 g_object_unref (manager);
384         }
385
386         item = gtk_image_menu_item_new_with_mnemonic (_("_Edit"));
387         image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
388                                               GTK_ICON_SIZE_MENU);
389         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
390         gtk_widget_show (image);
391
392         gtk_widget_set_sensitive (item, enable);
393
394         g_signal_connect_swapped (item, "activate",
395                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
396                                   contact);
397
398         return item;
399 }
400
401 typedef struct  {
402         EmpathyContact *contact;
403         EmpathyChatroom *chatroom;
404 } RoomSubMenuData;
405
406 static RoomSubMenuData *
407 room_sub_menu_data_new (EmpathyContact *contact,
408                         EmpathyChatroom *chatroom)
409 {
410         RoomSubMenuData *data;
411
412         data = g_slice_new (RoomSubMenuData);
413         data->contact = g_object_ref (contact);
414         data->chatroom = g_object_ref (chatroom);
415         return data;
416 }
417
418 static void
419 room_sub_menu_data_free (RoomSubMenuData *data)
420 {
421         /* FIXME: seems this is never called... */
422         g_object_unref (data->contact);
423         g_object_unref (data->chatroom);
424         g_slice_free (RoomSubMenuData, data);
425 }
426
427 static void
428 room_sub_menu_activate_cb (GtkWidget *item,
429                            RoomSubMenuData *data)
430 {
431         TpHandle handle;
432         GArray handles = {(gchar *) &handle, 1};
433         EmpathyTpChat *chat;
434         TpChannel *channel;
435
436         chat = empathy_chatroom_get_tp_chat (data->chatroom);
437         if (chat == NULL) {
438                 /* channel was invalidated. Ignoring */
439                 return;
440         }
441
442         /* send invitation */
443         handle = empathy_contact_get_handle (data->contact);
444         channel = empathy_tp_chat_get_channel (chat);
445         tp_cli_channel_interface_group_call_add_members (channel, -1, &handles,
446                 _("Inviting to this room"), NULL, NULL, NULL, NULL);
447 }
448
449 static GtkWidget *
450 create_room_sub_menu (EmpathyContact *contact,
451                       EmpathyChatroom *chatroom)
452 {
453         GtkWidget *item;
454         RoomSubMenuData *data;
455
456         item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
457         data = room_sub_menu_data_new (contact, chatroom);
458         g_signal_connect_data (item, "activate",
459                                G_CALLBACK (room_sub_menu_activate_cb), data,
460                                (GClosureNotify) room_sub_menu_data_free, 0);
461
462         return item;
463 }
464
465 GtkWidget *
466 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
467 {
468         GtkWidget *item;
469         GtkWidget *image;
470         GtkWidget *room_item;
471         EmpathyChatroomManager *mgr;
472         GList *rooms, *l;
473         GtkWidget *submenu = NULL;
474
475         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
476
477         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
478         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
479                                               GTK_ICON_SIZE_MENU);
480         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
481
482         mgr = empathy_chatroom_manager_dup_singleton (NULL);
483         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
484                 empathy_contact_get_account (contact));
485
486         for (l = rooms; l != NULL; l = g_list_next (l)) {
487                 EmpathyChatroom *chatroom = l->data;
488
489                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
490                         if (G_UNLIKELY (submenu == NULL))
491                                 submenu = gtk_menu_new ();
492
493                         room_item = create_room_sub_menu (contact, chatroom);
494                         gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
495                         gtk_widget_show (room_item);
496                 }
497         }
498
499         if (submenu) {
500                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
501         } else {
502                 gtk_widget_set_sensitive (item, FALSE);
503         }
504
505         gtk_widget_show (image);
506
507         g_object_unref (mgr);
508         g_list_free (rooms);
509
510         return item;
511 }
512