Tagged for release 2.26.0.
[empathy-mirror.git] / libempathy-gtk / empathy-contact-menu.c
blobf311b129077eeb78fa3e66579643f7c38a99c8d2
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 #include "config.h"
24 #include <string.h>
26 #include <glib/gi18n-lib.h>
27 #include <gtk/gtk.h>
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>
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"
41 GtkWidget *
42 empathy_contact_menu_new (EmpathyContact *contact,
43 EmpathyContactFeatureFlags features)
45 GtkWidget *menu;
46 GtkMenuShell *shell;
47 GtkWidget *item;
49 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
51 if (features == EMPATHY_CONTACT_FEATURE_NONE) {
52 return NULL;
55 menu = gtk_menu_new ();
56 shell = GTK_MENU_SHELL (menu);
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);
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);
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);
79 /* Invite */
80 item = empathy_contact_invite_menu_item_new (contact);
81 gtk_menu_shell_append (shell, item);
82 gtk_widget_show (item);
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);
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);
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);
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);
111 return menu;
114 static void
115 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
116 EmpathyContact *contact)
118 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
122 GtkWidget *
123 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
125 GtkWidget *item;
126 GtkWidget *image;
128 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
130 item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
131 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
132 GTK_ICON_SIZE_MENU);
133 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
134 gtk_widget_show (image);
136 g_signal_connect (item, "activate",
137 G_CALLBACK (empathy_contact_chat_menu_item_activated),
138 contact);
140 return item;
143 static void
144 empathy_contact_call_menu_item_activated (GtkMenuItem *item,
145 EmpathyContact *contact)
147 EmpathyCallFactory *factory;
149 factory = empathy_call_factory_get ();
150 empathy_call_factory_new_call (factory, contact);
153 GtkWidget *
154 empathy_contact_call_menu_item_new (EmpathyContact *contact)
156 GtkWidget *item;
157 GtkWidget *image;
159 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
161 item = gtk_image_menu_item_new_with_mnemonic (_("_Call"));
162 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
163 GTK_ICON_SIZE_MENU);
164 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
165 gtk_widget_set_sensitive (item, empathy_contact_can_voip (contact));
166 gtk_widget_show (image);
168 g_signal_connect (item, "activate",
169 G_CALLBACK (empathy_contact_call_menu_item_activated),
170 contact);
172 return item;
175 static void
176 contact_log_menu_item_activate_cb (EmpathyContact *contact)
178 empathy_log_window_show (empathy_contact_get_account (contact),
179 empathy_contact_get_id (contact),
180 FALSE, NULL);
183 GtkWidget *
184 empathy_contact_log_menu_item_new (EmpathyContact *contact)
186 EmpathyLogManager *manager;
187 gboolean have_log;
188 GtkWidget *item;
189 GtkWidget *image;
191 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
193 manager = empathy_log_manager_dup_singleton ();
194 have_log = empathy_log_manager_exists (manager,
195 empathy_contact_get_account (contact),
196 empathy_contact_get_id (contact),
197 FALSE);
198 g_object_unref (manager);
200 item = gtk_image_menu_item_new_with_mnemonic (_("_View Previous Conversations"));
201 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG,
202 GTK_ICON_SIZE_MENU);
203 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
204 gtk_widget_set_sensitive (item, have_log);
205 gtk_widget_show (image);
207 g_signal_connect_swapped (item, "activate",
208 G_CALLBACK (contact_log_menu_item_activate_cb),
209 contact);
211 return item;
214 GtkWidget *
215 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
217 GtkWidget *item;
218 GtkWidget *image;
220 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
222 item = gtk_image_menu_item_new_with_mnemonic (_("Send file"));
223 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
224 GTK_ICON_SIZE_MENU);
225 gtk_widget_set_sensitive (item, empathy_contact_can_send_files (contact));
226 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
227 gtk_widget_show (image);
229 g_signal_connect_swapped (item, "activate",
230 G_CALLBACK (empathy_send_file_with_file_chooser),
231 contact);
233 return item;
236 static void
237 contact_info_menu_item_activate_cb (EmpathyContact *contact)
239 empathy_contact_information_dialog_show (contact, NULL, FALSE, FALSE);
242 GtkWidget *
243 empathy_contact_info_menu_item_new (EmpathyContact *contact)
245 GtkWidget *item;
246 GtkWidget *image;
248 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
250 item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
251 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
252 GTK_ICON_SIZE_MENU);
253 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
254 gtk_widget_show (image);
256 g_signal_connect_swapped (item, "activate",
257 G_CALLBACK (contact_info_menu_item_activate_cb),
258 contact);
260 return item;
263 static void
264 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
266 empathy_contact_information_dialog_show (contact, NULL, TRUE, FALSE);
269 GtkWidget *
270 empathy_contact_edit_menu_item_new (EmpathyContact *contact)
272 GtkWidget *item;
273 GtkWidget *image;
275 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
277 item = gtk_image_menu_item_new_with_mnemonic (_("_Edit"));
278 image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT,
279 GTK_ICON_SIZE_MENU);
280 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
281 gtk_widget_show (image);
283 g_signal_connect_swapped (item, "activate",
284 G_CALLBACK (contact_edit_menu_item_activate_cb),
285 contact);
287 return item;
290 typedef struct {
291 EmpathyContact *contact;
292 EmpathyChatroom *chatroom;
293 } RoomSubMenuData;
295 static RoomSubMenuData *
296 room_sub_menu_data_new (EmpathyContact *contact,
297 EmpathyChatroom *chatroom)
299 RoomSubMenuData *data;
301 data = g_slice_new (RoomSubMenuData);
302 data->contact = g_object_ref (contact);
303 data->chatroom = g_object_ref (chatroom);
305 return data;
308 static void
309 room_sub_menu_data_free (RoomSubMenuData *data)
311 /* FIXME: seems this is never called... */
312 g_object_unref (data->contact);
313 g_object_unref (data->chatroom);
314 g_slice_free (RoomSubMenuData, data);
317 static void
318 room_sub_menu_activate_cb (GtkWidget *item,
319 RoomSubMenuData *data)
321 TpHandle handle;
322 GArray handles = {(gchar *) &handle, 1};
323 EmpathyTpChat *chat;
324 TpChannel *channel;
326 chat = empathy_chatroom_get_tp_chat (data->chatroom);
327 if (chat == NULL) {
328 /* channel was invalidated. Ignoring */
329 return;
332 /* send invitation */
333 handle = empathy_contact_get_handle (data->contact);
334 channel = empathy_tp_chat_get_channel (chat);
335 tp_cli_channel_interface_group_call_add_members (channel, -1, &handles,
336 _("Inviting to this room"), NULL, NULL, NULL, NULL);
339 static GtkWidget *
340 create_room_sub_menu (EmpathyContact *contact,
341 EmpathyChatroom *chatroom)
343 GtkWidget *item;
344 RoomSubMenuData *data;
346 item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
347 data = room_sub_menu_data_new (contact, chatroom);
348 g_signal_connect_data (item, "activate",
349 G_CALLBACK (room_sub_menu_activate_cb), data,
350 (GClosureNotify) room_sub_menu_data_free, 0);
352 return item;
355 GtkWidget *
356 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
358 GtkWidget *item;
359 GtkWidget *image;
360 GtkWidget *room_item;
361 EmpathyChatroomManager *mgr;
362 GList *rooms, *l;
363 GtkWidget *submenu;
364 GtkMenuShell *submenu_shell;
365 gboolean have_rooms = FALSE;
367 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
369 item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
370 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
371 GTK_ICON_SIZE_MENU);
372 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
374 mgr = empathy_chatroom_manager_dup_singleton (NULL);
375 rooms = empathy_chatroom_manager_get_chatrooms (mgr,
376 empathy_contact_get_account (contact));
378 /* create rooms sub menu */
379 submenu = gtk_menu_new ();
380 submenu_shell = GTK_MENU_SHELL (submenu);
382 for (l = rooms; l != NULL; l = g_list_next (l)) {
383 EmpathyChatroom *chatroom = l->data;
385 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
386 have_rooms = TRUE;
388 room_item = create_room_sub_menu (contact, chatroom);
389 gtk_menu_shell_append (submenu_shell, room_item);
390 gtk_widget_show (room_item);
394 if (have_rooms) {
395 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
396 } else {
397 gtk_widget_set_sensitive (item, FALSE);
398 gtk_widget_destroy (submenu);
401 gtk_widget_show (image);
403 g_object_unref (mgr);
404 g_list_free (rooms);
406 return item;