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