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