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