]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
- Split info/edit/personal dialogs into different functions.
[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
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"
40
41 GtkWidget *
42 empathy_contact_menu_new (EmpathyContact             *contact,
43                           EmpathyContactFeatureFlags  features)
44 {
45         GtkWidget    *menu;
46         GtkMenuShell *shell;
47         GtkWidget    *item;
48
49         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
50
51         if (features == EMPATHY_CONTACT_FEATURE_NONE) {
52                 return NULL;
53         }
54
55         menu = gtk_menu_new ();
56         shell = GTK_MENU_SHELL (menu);
57
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);
63         }
64
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);
70         }
71
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);
77         }
78
79         /* Invite */
80         item = empathy_contact_invite_menu_item_new (contact);
81         gtk_menu_shell_append (shell, item);
82         gtk_widget_show (item);
83
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);
88
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);
95         }
96
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);
102         }
103
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);
109         }
110
111         return menu;
112 }
113
114 static void
115 empathy_contact_chat_menu_item_activated (GtkMenuItem *item,
116         EmpathyContact *contact)
117 {
118   empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
119 }
120
121
122 GtkWidget *
123 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
124 {
125         GtkWidget *item;
126         GtkWidget *image;
127
128         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
129
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);
135
136         g_signal_connect (item, "activate",
137                                   G_CALLBACK (empathy_contact_chat_menu_item_activated),
138                                   contact);
139         
140         return item;
141 }
142
143 static void
144 empathy_contact_call_menu_item_activated (GtkMenuItem *item,
145         EmpathyContact *contact)
146 {
147         EmpathyCallFactory *factory;
148
149         factory = empathy_call_factory_get ();
150         empathy_call_factory_new_call (factory, contact);
151 }
152
153 GtkWidget *
154 empathy_contact_call_menu_item_new (EmpathyContact *contact)
155 {
156         GtkWidget *item;
157         GtkWidget *image;
158
159         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
160
161         item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_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);
167
168         g_signal_connect (item, "activate",
169                                   G_CALLBACK (empathy_contact_call_menu_item_activated),
170                                   contact);
171         
172         return item;
173 }
174
175 static void
176 contact_log_menu_item_activate_cb (EmpathyContact *contact)
177 {
178         empathy_log_window_show (empathy_contact_get_account (contact),
179                                  empathy_contact_get_id (contact),
180                                  FALSE, NULL);
181 }
182
183 GtkWidget *
184 empathy_contact_log_menu_item_new (EmpathyContact *contact)
185 {
186         EmpathyLogManager *manager;
187         gboolean           have_log;
188         GtkWidget         *item;
189         GtkWidget         *image;
190
191         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
192
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);
199
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);
206
207         g_signal_connect_swapped (item, "activate",
208                                   G_CALLBACK (contact_log_menu_item_activate_cb),
209                                   contact);
210         
211         return item;
212 }
213
214 GtkWidget *
215 empathy_contact_file_transfer_menu_item_new (EmpathyContact *contact)
216 {
217         GtkWidget         *item;
218         GtkWidget         *image;
219
220         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
221
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);
228
229         g_signal_connect_swapped (item, "activate",
230                                   G_CALLBACK (empathy_send_file_with_file_chooser),
231                                   contact);
232
233         return item;
234 }
235
236 static void
237 contact_info_menu_item_activate_cb (EmpathyContact *contact)
238 {
239         empathy_contact_information_dialog_show (contact, NULL);
240 }
241
242 GtkWidget *
243 empathy_contact_info_menu_item_new (EmpathyContact *contact)
244 {
245         GtkWidget *item;
246         GtkWidget *image;
247
248         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
249
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);
255
256         g_signal_connect_swapped (item, "activate",
257                                   G_CALLBACK (contact_info_menu_item_activate_cb),
258                                   contact);
259         
260         return item;
261 }
262
263 static void
264 contact_edit_menu_item_activate_cb (EmpathyContact *contact)
265 {
266         empathy_contact_edit_dialog_show (contact, NULL);
267 }
268
269 GtkWidget *
270 empathy_contact_edit_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 (_("_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);
282
283         g_signal_connect_swapped (item, "activate",
284                                   G_CALLBACK (contact_edit_menu_item_activate_cb),
285                                   contact);
286         
287         return item;
288 }
289
290 typedef struct  {
291         EmpathyContact *contact;
292         EmpathyChatroom *chatroom;
293 } RoomSubMenuData;
294
295 static RoomSubMenuData *
296 room_sub_menu_data_new (EmpathyContact *contact,
297                         EmpathyChatroom *chatroom)
298 {
299         RoomSubMenuData *data;
300
301         data = g_slice_new (RoomSubMenuData);
302         data->contact = g_object_ref (contact);
303         data->chatroom = g_object_ref (chatroom);
304
305         return data;
306 }
307
308 static void
309 room_sub_menu_data_free (RoomSubMenuData *data)
310 {
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);
315 }
316
317 static void
318 room_sub_menu_activate_cb (GtkWidget *item,
319                            RoomSubMenuData *data)
320 {
321         TpHandle handle;
322         GArray handles = {(gchar *) &handle, 1};
323         EmpathyTpChat *chat;
324         TpChannel *channel;
325
326         chat = empathy_chatroom_get_tp_chat (data->chatroom);
327         if (chat == NULL) {
328                 /* channel was invalidated. Ignoring */
329                 return;
330         }
331
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);
337 }
338
339 static GtkWidget *
340 create_room_sub_menu (EmpathyContact *contact,
341                       EmpathyChatroom *chatroom)
342 {
343         GtkWidget *item;
344         RoomSubMenuData *data;
345
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);
351
352         return item;
353 }
354
355 GtkWidget *
356 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
357 {
358         GtkWidget *item;
359         GtkWidget *image;
360         GtkWidget *room_item;
361         EmpathyChatroomManager *mgr;
362         GList *rooms, *l;
363         GtkWidget *submenu = NULL;
364
365         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
366
367         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
368         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
369                                               GTK_ICON_SIZE_MENU);
370         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
371
372         mgr = empathy_chatroom_manager_dup_singleton (NULL);
373         rooms = empathy_chatroom_manager_get_chatrooms (mgr,
374                 empathy_contact_get_account (contact));
375
376         for (l = rooms; l != NULL; l = g_list_next (l)) {
377                 EmpathyChatroom *chatroom = l->data;
378
379                 if (empathy_chatroom_get_tp_chat (chatroom) != NULL) {
380                         if (G_UNLIKELY (submenu == NULL))
381                                 submenu = gtk_menu_new ();
382
383                         room_item = create_room_sub_menu (contact, chatroom);
384                         gtk_menu_shell_append ((GtkMenuShell*)submenu, room_item);
385                         gtk_widget_show (room_item);
386                 }
387         }
388
389         if (submenu) {
390                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
391         } else {
392                 gtk_widget_set_sensitive (item, FALSE);
393         }
394
395         gtk_widget_show (image);
396
397         g_object_unref (mgr);
398         g_list_free (rooms);
399
400         return item;
401 }
402