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