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 "empathy-tp-chatroom.h"
26 #include "empathy-contact-list.h"
27 #include "empathy-contact-factory.h"
28 #include "empathy-tp-group.h"
29 #include "empathy-utils.h"
30 #include "empathy-debug.h"
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
33 EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
35 #define DEBUG_DOMAIN "TpChatroom"
37 struct _EmpathyTpChatroomPriv {
38 EmpathyContactFactory *factory;
39 EmpathyTpGroup *group;
42 EmpathyContact *invitor;
46 static void empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass);
47 static void tp_chatroom_iface_init (EmpathyContactListIface *iface);
48 static void empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom);
49 static void tp_chatroom_finalize (GObject *object);
50 static void tp_chatroom_member_added_cb (EmpathyTpGroup *group,
51 EmpathyContact *contact,
52 EmpathyContact *actor,
55 EmpathyTpChatroom *chatroom);
56 static void tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
57 EmpathyContact *contact,
58 EmpathyContact *actor,
61 EmpathyTpChatroom *chatroom);
62 static void tp_chatroom_add (EmpathyContactList *list,
63 EmpathyContact *contact,
64 const gchar *message);
65 static void tp_chatroom_remove (EmpathyContactList *list,
66 EmpathyContact *contact,
67 const gchar *message);
68 static GList * tp_chatroom_get_members (EmpathyContactList *list);
70 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
71 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
72 tp_chatroom_iface_init));
75 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
77 GObjectClass *object_class = G_OBJECT_CLASS (klass);
79 object_class->finalize = tp_chatroom_finalize;
81 g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
85 tp_chatroom_iface_init (EmpathyContactListIface *iface)
87 iface->add = tp_chatroom_add;
88 iface->remove = tp_chatroom_remove;
89 iface->get_members = tp_chatroom_get_members;
93 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
98 tp_chatroom_finalize (GObject *object)
100 EmpathyTpChatroomPriv *priv;
101 EmpathyTpChatroom *chatroom;
103 chatroom = EMPATHY_TP_CHATROOM (object);
104 priv = GET_PRIV (chatroom);
106 g_object_unref (priv->group);
107 g_object_unref (priv->factory);
110 g_object_unref (priv->invitor);
113 g_free (priv->invit_message);
115 G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
119 empathy_tp_chatroom_new (McAccount *account,
122 EmpathyTpChatroomPriv *priv;
123 EmpathyTpChatroom *chatroom;
125 EmpathyContact *user;
127 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
128 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
130 chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
135 priv = GET_PRIV (chatroom);
137 priv->factory = empathy_contact_factory_new ();
138 priv->group = empathy_tp_group_new (account, tp_chan);
140 g_signal_connect (priv->group, "member-added",
141 G_CALLBACK (tp_chatroom_member_added_cb),
143 g_signal_connect (priv->group, "member-removed",
144 G_CALLBACK (tp_chatroom_member_removed_cb),
147 /* Check if we are invited to join the chat */
148 user = empathy_tp_group_get_self_contact (priv->group);
149 members = empathy_tp_group_get_local_pendings (priv->group);
150 for (l = members; l; l = l->next) {
151 EmpathyPendingInfo *info;
155 if (!empathy_contact_equal (user, info->member)) {
159 priv->invitor = g_object_ref (info->actor);
160 priv->invit_message = g_strdup (info->message);
161 priv->is_invited = TRUE;
163 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
164 empathy_contact_get_id (priv->invitor),
165 empathy_contact_get_handle (priv->invitor),
166 priv->invit_message);
169 g_list_foreach (members, (GFunc) empathy_pending_info_free, NULL);
170 g_list_free (members);
171 g_object_unref (user);
177 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom *chatroom,
178 EmpathyContact **contact,
179 const gchar **message)
181 EmpathyTpChatroomPriv *priv;
183 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
185 priv = GET_PRIV (chatroom);
188 *contact = priv->invitor;
191 *message = priv->invit_message;
194 return priv->is_invited;
198 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
200 EmpathyTpChatroomPriv *priv;
201 EmpathyContact *user;
203 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
205 priv = GET_PRIV (chatroom);
207 /* Clear invitation data */
208 priv->is_invited = FALSE;
210 g_object_unref (priv->invitor);
211 priv->invitor = NULL;
213 g_free (priv->invit_message);
214 priv->invit_message = NULL;
216 /* Add ourself in the members of the room */
217 user = empathy_tp_group_get_self_contact (priv->group);
218 empathy_tp_group_add_member (priv->group, user, "");
219 g_object_unref (user);
223 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
226 /* FIXME: not implemented */
230 tp_chatroom_member_added_cb (EmpathyTpGroup *group,
231 EmpathyContact *contact,
232 EmpathyContact *actor,
234 const gchar *message,
235 EmpathyTpChatroom *chatroom)
237 g_signal_emit_by_name (chatroom, "members-changed",
238 contact, actor, reason, message,
243 tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
244 EmpathyContact *contact,
245 EmpathyContact *actor,
247 const gchar *message,
248 EmpathyTpChatroom *chatroom)
250 g_signal_emit_by_name (chatroom, "members-changed",
251 contact, actor, reason, message,
256 tp_chatroom_add (EmpathyContactList *list,
257 EmpathyContact *contact,
258 const gchar *message)
260 EmpathyTpChatroomPriv *priv;
262 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
263 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
265 priv = GET_PRIV (list);
267 empathy_tp_group_add_member (priv->group, contact, message);
271 tp_chatroom_remove (EmpathyContactList *list,
272 EmpathyContact *contact,
273 const gchar *message)
275 EmpathyTpChatroomPriv *priv;
277 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
278 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
280 priv = GET_PRIV (list);
282 empathy_tp_group_remove_member (priv->group, contact, message);
286 tp_chatroom_get_members (EmpathyContactList *list)
288 EmpathyTpChatroomPriv *priv;
290 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
292 priv = GET_PRIV (list);
294 return empathy_tp_group_get_members (priv->group);