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