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