]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-menu.c
Display invite menu entry in the contact menu
[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 static void
244 contact_room_sub_menu_item_activate_cb (EmpathyContact *contact)
245 {
246   /* TODO */
247 }
248
249 static GtkWidget *
250 create_room_sub_menu_item (EmpathyContact *contact,
251                            EmpathyChatroom *chatroom)
252 {
253         GtkWidget *item;
254
255         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
256         g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
257
258   item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
259
260         g_signal_connect_swapped (item, "activate",
261                                   G_CALLBACK (contact_room_sub_menu_item_activate_cb),
262                                   contact);
263         
264         return item;
265 }
266
267 GtkWidget *
268 empathy_contact_invite_menu_item_new (EmpathyContact *contact)
269 {
270         GtkWidget *item;
271         GtkWidget *image;
272   GtkWidget *room_item;
273   EmpathyChatroomManager *mgr;
274   GList *rooms, *l;
275   GtkWidget *submenu;
276   GtkMenuShell *submenu_shell;
277   gboolean have_rooms = FALSE;
278
279         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
280
281         item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to..."));
282         image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
283                                               GTK_ICON_SIZE_MENU);
284         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
285
286   mgr = empathy_chatroom_manager_new ();
287   rooms = empathy_chatroom_manager_get_chatrooms (mgr,
288       empathy_contact_get_account (contact));
289
290   /* create rooms sub menu */
291   submenu = gtk_menu_new ();
292   submenu_shell = GTK_MENU_SHELL (submenu);
293
294   for (l = rooms; l != NULL; l = g_list_next (l))
295     {
296       EmpathyChatroom *room = l->data;
297       TpChannel *channel;
298
299       g_object_get (room, "tp-channel", &channel, NULL);
300       if (channel != NULL)
301         {
302           have_rooms = TRUE;
303
304           room_item = create_room_sub_menu_item (contact, room);
305           gtk_menu_shell_append (submenu_shell, room_item);
306           gtk_widget_show (room_item);
307
308           g_object_unref (channel);
309         }
310     }
311
312   if (have_rooms)
313     {
314       gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
315     }
316   else
317     {
318       gtk_widget_set_sensitive (item, FALSE);
319       gtk_widget_destroy (submenu);
320     }
321
322         gtk_widget_show (image);
323
324   g_object_unref (mgr);
325   g_list_free (rooms);
326
327         return item;
328 }