1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <libmissioncontrol/mission-control.h>
27 #include "empathy-tp-chatroom.h"
28 #include "empathy-tp-contact-list.h"
29 #include "empathy-contact-list.h"
30 #include "empathy-contact-manager.h"
31 #include "gossip-telepathy-group.h"
32 #include "gossip-utils.h"
33 #include "gossip-debug.h"
35 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
36 EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
38 #define DEBUG_DOMAIN "TpChatroom"
40 struct _EmpathyTpChatroomPriv {
41 EmpathyContactManager *manager;
42 EmpathyTpContactList *list;
43 GossipTelepathyGroup *group;
46 GossipContact *invitor;
50 static void empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass);
51 static void tp_chatroom_iface_init (EmpathyContactListIface *iface);
52 static void empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom);
53 static void tp_chatroom_finalize (GObject *object);
54 static void tp_chatroom_members_added_cb (GossipTelepathyGroup *group,
59 EmpathyTpChatroom *list);
60 static void tp_chatroom_members_removed_cb (GossipTelepathyGroup *group,
65 EmpathyTpChatroom *list);
66 static void tp_chatroom_setup (EmpathyContactList *list);
67 static GossipContact * tp_chatroom_find (EmpathyContactList *list,
69 static void tp_chatroom_add (EmpathyContactList *list,
70 GossipContact *contact,
71 const gchar *message);
72 static void tp_chatroom_remove (EmpathyContactList *list,
73 GossipContact *contact,
74 const gchar *message);
75 static GList * tp_chatroom_get_members (EmpathyContactList *list);
77 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
78 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
79 tp_chatroom_iface_init));
82 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
84 GObjectClass *object_class = G_OBJECT_CLASS (klass);
86 object_class->finalize = tp_chatroom_finalize;
88 g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
92 tp_chatroom_iface_init (EmpathyContactListIface *iface)
94 iface->setup = tp_chatroom_setup;
95 iface->find = tp_chatroom_find;
96 iface->add = tp_chatroom_add;
97 iface->remove = tp_chatroom_remove;
98 iface->get_members = tp_chatroom_get_members;
102 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
107 tp_chatroom_finalize (GObject *object)
109 EmpathyTpChatroomPriv *priv;
110 EmpathyTpChatroom *chatroom;
112 chatroom = EMPATHY_TP_CHATROOM (object);
113 priv = GET_PRIV (chatroom);
115 g_object_unref (priv->group);
116 g_object_unref (priv->manager);
119 g_object_unref (priv->invitor);
122 g_free (priv->invit_message);
124 G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
128 empathy_tp_chatroom_new (McAccount *account,
131 EmpathyTpChatroomPriv *priv;
132 EmpathyTpChatroom *chatroom;
138 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
139 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
141 chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
146 priv = GET_PRIV (chatroom);
148 mc = gossip_mission_control_new ();
149 tp_conn = mission_control_get_connection (mc, account, NULL);
150 priv->manager = empathy_contact_manager_new ();
151 priv->list = empathy_contact_manager_get_list (priv->manager, account);
152 priv->group = gossip_telepathy_group_new (tp_chan, tp_conn);
154 g_signal_connect (priv->group, "members-added",
155 G_CALLBACK (tp_chatroom_members_added_cb),
157 g_signal_connect (priv->group, "members-removed",
158 G_CALLBACK (tp_chatroom_members_removed_cb),
161 /* Check if we are invited to join the chat */
162 self_handle = gossip_telepathy_group_get_self_handle (priv->group);
163 members = gossip_telepathy_group_get_local_pending_members_with_info (priv->group);
164 for (l = members; l; l = l->next) {
165 GossipTpGroupInfo *info;
169 if (info->member != self_handle) {
173 priv->invitor = empathy_tp_contact_list_get_from_handle (priv->list,
175 priv->invit_message = g_strdup (info->message);
176 priv->is_invited = TRUE;
178 gossip_debug (DEBUG_DOMAIN, "We are invited to join by %s: %s",
179 gossip_contact_get_name (priv->invitor),
180 priv->invit_message);
183 gossip_telepathy_group_info_list_free (members);
185 g_object_unref (tp_conn);
191 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom *chatroom,
192 GossipContact **contact,
193 const gchar **message)
195 EmpathyTpChatroomPriv *priv;
197 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
199 priv = GET_PRIV (chatroom);
202 *contact = priv->invitor;
205 *message = priv->invit_message;
208 return priv->is_invited;
212 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
214 EmpathyTpChatroomPriv *priv;
217 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
219 priv = GET_PRIV (chatroom);
221 /* Clear invitation data */
222 priv->is_invited = FALSE;
224 g_object_unref (priv->invitor);
225 priv->invitor = NULL;
227 g_free (priv->invit_message);
228 priv->invit_message = NULL;
230 /* Add ourself in the members of the room */
231 self_handle = gossip_telepathy_group_get_self_handle (priv->group);
232 gossip_telepathy_group_add_member (priv->group, self_handle,
237 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
243 tp_chatroom_members_added_cb (GossipTelepathyGroup *group,
247 const gchar *message,
248 EmpathyTpChatroom *chatroom)
250 EmpathyTpChatroomPriv *priv;
253 priv = GET_PRIV (chatroom);
255 contacts = empathy_tp_contact_list_get_from_handles (priv->list, handles);
256 for (l = contacts; l; l = l->next) {
257 GossipContact *contact;
261 g_signal_emit_by_name (chatroom, "contact-added", contact);
263 g_object_unref (contact);
265 g_list_free (contacts);
269 tp_chatroom_members_removed_cb (GossipTelepathyGroup *group,
273 const gchar *message,
274 EmpathyTpChatroom *chatroom)
276 EmpathyTpChatroomPriv *priv;
279 priv = GET_PRIV (chatroom);
281 contacts = empathy_tp_contact_list_get_from_handles (priv->list, handles);
282 for (l = contacts; l; l = l->next) {
283 GossipContact *contact;
287 g_signal_emit_by_name (chatroom, "contact-removed", contact);
289 g_object_unref (contact);
291 g_list_free (contacts);
295 tp_chatroom_setup (EmpathyContactList *list)
300 static GossipContact *
301 tp_chatroom_find (EmpathyContactList *list,
308 tp_chatroom_add (EmpathyContactList *list,
309 GossipContact *contact,
310 const gchar *message)
312 EmpathyTpChatroomPriv *priv;
314 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
315 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
317 priv = GET_PRIV (list);
319 gossip_telepathy_group_add_member (priv->group,
320 gossip_contact_get_handle (contact),
325 tp_chatroom_remove (EmpathyContactList *list,
326 GossipContact *contact,
327 const gchar *message)
329 EmpathyTpChatroomPriv *priv;
331 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
332 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
334 priv = GET_PRIV (list);
336 gossip_telepathy_group_remove_member (priv->group,
337 gossip_contact_get_handle (contact),
342 tp_chatroom_get_members (EmpathyContactList *list)
344 EmpathyTpChatroomPriv *priv;
348 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
350 priv = GET_PRIV (list);
352 members = gossip_telepathy_group_get_members (priv->group);
353 contacts = empathy_tp_contact_list_get_from_handles (priv->list, members);
354 g_array_free (members, TRUE);