]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Switch to calling CallFactory to make calls
[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-dispatcher.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-chatroom-manager.h>
34
35 #include "empathy-contact-menu.h"
36 #include "empathy-images.h"
37 #include "empathy-log-window.h"
38 #include "empathy-contact-dialogs.h"
39 #include "empathy-ui-utils.h"
40
41 GtkWidget *
42 empathy_contact_menu_new (EmpathyContact             *contact,
43                           EmpathyContactFeatureFlags  features)
44 {
45         GtkWidget    *menu;
46         GtkMenuShell *shell;
47         GtkWidget    *item;
48
49         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
50
51         if (features == EMPATHY_CONTACT_FEATURE_NONE) {
52                 return NULL;
53         }
54
55         menu = gtk_menu_new ();
56         shell = GTK_MENU_SHELL (menu);
57
58         /* Chat */
59         if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
60                 item = empathy_contact_chat_menu_item_new (contact);
61                 gtk_menu_shell_append (shell, item);
62                 gtk_widget_show (item);
63         }
64
65         /* Call */
66         if (features & EMPATHY_CONTACT_FEATURE_CALL) {
67                 item = empathy_contact_call_menu_item_new (contact);
68                 gtk_menu_shell_append (shell, item);
69                 gtk_widget_show (item);
70         }
71
72         /* Log */
73         if (features & EMPATHY_CONTACT_FEATURE_LOG) {
74                 item = empathy_contact_log_menu_item_new (contact);
75                 gtk_menu_shell_append (shell, item);
76                 gtk_widget_show (item);
77         }
78
79         /* Invite */
80         item = empathy_contact_invite_menu_item_new (contact);
81         gtk_menu_shell_append (shell, item);
82         gtk_widget_show (item);
83
84         /* File transfer */
85         item = empathy_contact_file_transfer_menu_item_new (contact);
86         gtk_menu_shell_append (shell, item);
87         gtk_widget_show (item);
88
89         /* Separator */
90         if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
91                         EMPATHY_CONTACT_FEATURE_INFO)) {
92                 item = gtk_separator_menu_item_new ();
93                 gtk_menu_shell_append (shell, item);
94                 gtk_widget_show (item);
95         }
96
97         /* Edit */
98         if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
99                 item = empathy_contact_edit_menu_item_new (contact);
100                 gtk_menu_shell_append (shell, item);
101                 gtk_widget_show (item);
102         }
103
104         /* Info */
105         if (features & EMPATHY_CONTACT_FEATURE_INFO) {
106                 item = empathy_contact_info_menu_item_new (contact);
107                 gtk_menu_shell_append (shell, item);
108                 gtk_widget_show (item);
109         }
110
111         return menu;
112 }
113
114 GtkWidget *
115 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
116 {
117         GtkWidget *item;
118         GtkWidget *image;
119
120         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
121
122         item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
123         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
124                                               GTK_ICON_SIZE_MENU);
125         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
126         gtk_widget_show (image);
127
128         g_signal_connect_swapped (item, "activate",
129                                   G_CALLBACK (empathy_dispatcher_chat_with_contact),
130                                   contact);
131         
132         return item;
133 }
134
135 static void
136 empathy_contact_call_menu_item_activated (GtkMenuItem *item,
137         EmpathyContact *contact)
138 {
139         EmpathyCallFactory *factory;
140
141         factory = empathy_call_factory_get ();
142         empathy_call_factory_new_call (factory, contact);
143 }
144
145 GtkWidget *
146 empathy_contact_call_menu_item_new (EmpathyContact *contact)
147 {
148         GtkWidget *item;
149         GtkWidget *image;
150
151         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
152
153         item = gtk_image_menu_item_new_with_mnemonic (_("_Call"));
154         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
155                                               GTK_ICON_SIZE_MENU);
156         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
157         gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
158         gtk_widget_show (image);
159
160         g_signal_connect (item, "activate",
161                                   G_CALLBACK (empathy_contact_call_menu_item_activated),
162                                   contact);
163         
164         return item;
165 }
166
167 static void
168 contact_log_menu_item_activate_cb (EmpathyContact *contact)
169 {
170         empathy_log_window_show (empathy_contact_get_account (contact),
171                                  empathy_contact_get_id (contact),
172                                  FALSE, NULL);
173 }
174
175 GtkWidget *
176 empathy_contact_log_menu_item_new (EmpathyContact *contact)
177 {
178         EmpathyLogManager *manager;
179         gboolean           have_log;
180         GtkWidget         *item;
181         GtkWidget         *image;
182
183         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
184
185         manager = empathy_log_manager_dup_singleton ();
186         have_log = empathy_log_manager_exists (manager,
187                                                empathy_contact_get_account (contact),
188                                                empathy_contact_get_id (contact),
189                                                FALSE);
190         g_object_unref (manager);
191
192         item = gtk_image_menu_item_new_with_mnemonic (_("_View Previous Conversations"));
193         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
194                                               GTK_ICON_SIZE_MENU);
195         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
196         gtk_widget_set_sensitive (item, have_log);
197         gtk_widget_show (image);
198
199         g_signal_connect_swapped (item, "activate",
200                                   G_CALLBACK (contact_log_menu_item_activate_cb),
201                                   contact);
202         
203         return item;
204 }
205
206 GtkWidget *
207 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
208 {
209         GtkWidget         *item;
210         GtkWidget         *image;
211
212         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
213
214         item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
215         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
216                                               GTK_ICON_SIZE_MENU);
217         gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
218         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
219         gtk_widget_show (image);
220
221         g_signal_connect_swapped (item, "activate",
222                                   G_CALLBACK (empathy_send_file_with_file_chooser),
223                                   contact);
224
225         return item;
226 }
227
228 static void
229 contact_info_menu_item_activate_cb (EmpathyContact *contact)
230 {
231         empathy_contact_information_dialog_show (contact, NULL, FALSE, FALSE);
232 }
233
234 GtkWidget *
235 empathy_contact_info_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 (_("Infor_mation"));
243         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
244                                               GTK_ICON_SIZE_MENU);
245         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
246         gtk_widget_show (image);
247
248         g_signal_connect_swapped (item, "activate",
249                                   G_CALLBACK (contact_info_menu_item_activate_cb),
250                                   contact);
251         
252         return item;
253 }
254
255 static void
256 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
257 {
258         empathy_contact_information_dialog_show (contact, NULL, TRUE, FALSE);
259 }
260
261 GtkWidget *
262 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
263 {
264         GtkWidget *item;
265         GtkWidget *image;
266
267         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
268
269         item = gtk_image_menu_item_new_with_mnemonic (_("_Edit"));
270         image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
271                                               GTK_ICON_SIZE_MENU);
272         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
273         gtk_widget_show (image);
274
275         g_signal_connect_swapped (item, "activate",
276                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
277                                   contact);
278         
279         return item;
280 }
281
282 typedef struct  {
283         EmpathyContact *contact;
284         EmpathyChatroom *chatroom;
285 } RoomSubMenuData;
286
287 static RoomSubMenuData *
288 room_sub_menu_data_new (EmpathyContact *contact,
289                         EmpathyChatroom *chatroom)
290 {
291         RoomSubMenuData *data;
292
293         data = g_slice_new (RoomSubMenuData);
294         data->contact = g_object_ref (contact);
295         data->chatroom = g_object_ref (chatroom);
296
297         return data;
298 }
299
300 static void
301 room_sub_menu_data_free (RoomSubMenuData *data)
302 {
303         /* FIXME: seems this is never called... */
304         g_object_unref (data->contact);
305         g_object_unref (data->chatroom);
306         g_slice_free (RoomSubMenuData, data);
307 }
308
309 static void
310 room_sub_menu_activate_cb (GtkWidget *item,
311                            RoomSubMenuData *data)
312 {
313         TpHandle handle;
314         GArray handles = {(gchar *) &handle, 1};
315         EmpathyTpChat *chat;
316         TpChannel *channel;
317
318         chat = empathy_chatroom_get_tp_chat (data->chatroom);
319         if (chat == NULL) {
320                 /* channel was invalidated. Ignoring */
321                 return;
322         }
323
324         /* send invitation */
325         handle = empathy_contact_get_handle (data->contact);
326         channel = empathy_tp_chat_get_channel (chat);
327         tp_cli_channel_interface_group_call_add_members (channel, -1, &handles,
328                 _("Inviting to this room"), NULL, NULL, NULL, NULL);
329 }
330
331 static GtkWidget *
332 create_room_sub_menu (EmpathyContact *contact,
333                       EmpathyChatroom *chatroom)
334 {
335         GtkWidget *item;
336         RoomSubMenuData *data;
337
338         item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
339         data = room_sub_menu_data_new (contact, chatroom);
340         g_signal_connect_data (item, "activate",
341                                G_CALLBACK (room_sub_menu_activate_cb), data,
342                                (GClosureNotify) room_sub_menu_data_free, 0);
343
344         return item;
345 }
346
347 GtkWidget *
348 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
349 {
350         GtkWidget *item;
351         GtkWidget *image;
352         GtkWidget *room_item;
353         EmpathyChatroomManager *mgr;
354         GList *rooms, *l;
355         GtkWidget *submenu;
356         GtkMenuShell *submenu_shell;
357         gboolean have_rooms = FALSE;
358
359         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
360
361         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
362         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
363                                               GTK_ICON_SIZE_MENU);
364         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
365
366         mgr = empathy_chatroom_manager_dup_singleton (NULL);
367         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
368                 empathy_contact_get_account (contact));
369
370         /* create rooms sub menu */
371         submenu = gtk_menu_new ();
372         submenu_shell = GTK_MENU_SHELL (submenu);
373
374         for (l = rooms; l != NULL; l = g_list_next (l)) {
375                 EmpathyChatroom *chatroom = l->data;
376
377                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
378                         have_rooms = TRUE;
379
380                         room_item = create_room_sub_menu (contact, chatroom);
381                         gtk_menu_shell_append (submenu_shell, room_item);
382                         gtk_widget_show (room_item);
383                 }
384         }
385
386         if (have_rooms) {
387                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
388         } else {
389                 gtk_widget_set_sensitive (item, FALSE);
390                 gtk_widget_destroy (submenu);
391         }
392
393         gtk_widget_show (image);
394
395         g_object_unref (mgr);
396         g_list_free (rooms);
397
398         return item;
399 }
400