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