1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include "empathy-tp-chatroom.h"
25 #include "empathy-contact-list.h"
26 #include "empathy-contact-factory.h"
27 #include "empathy-tp-group.h"
28 #include "empathy-utils.h"
29 #include "empathy-debug.h"
31 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
32 EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
34 #define DEBUG_DOMAIN "TpChatroom"
36 struct _EmpathyTpChatroomPriv {
37 EmpathyContactFactory *factory;
38 EmpathyTpGroup *group;
41 EmpathyContact *invitor;
45 static void empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass);
46 static void tp_chatroom_iface_init (EmpathyContactListIface *iface);
47 static void empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom);
48 static void tp_chatroom_finalize (GObject *object);
49 static void tp_chatroom_member_added_cb (EmpathyTpGroup *group,
50 EmpathyContact *contact,
51 EmpathyContact *actor,
54 EmpathyTpChatroom *chatroom);
55 static void tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
56 EmpathyContact *contact,
57 EmpathyContact *actor,
60 EmpathyTpChatroom *chatroom);
61 static void tp_chatroom_add (EmpathyContactList *list,
62 EmpathyContact *contact,
63 const gchar *message);
64 static void tp_chatroom_remove (EmpathyContactList *list,
65 EmpathyContact *contact,
66 const gchar *message);
67 static GList * tp_chatroom_get_members (EmpathyContactList *list);
69 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
70 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
71 tp_chatroom_iface_init));
74 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 object_class->finalize = tp_chatroom_finalize;
80 g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
84 tp_chatroom_iface_init (EmpathyContactListIface *iface)
86 iface->add = tp_chatroom_add;
87 iface->remove = tp_chatroom_remove;
88 iface->get_members = tp_chatroom_get_members;
92 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
97 tp_chatroom_finalize (GObject *object)
99 EmpathyTpChatroomPriv *priv;
100 EmpathyTpChatroom *chatroom;
102 chatroom = EMPATHY_TP_CHATROOM (object);
103 priv = GET_PRIV (chatroom);
105 g_object_unref (priv->group);
106 g_object_unref (priv->factory);
109 g_object_unref (priv->invitor);
112 g_free (priv->invit_message);
114 G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
118 empathy_tp_chatroom_new (McAccount *account,
121 EmpathyTpChatroomPriv *priv;
122 EmpathyTpChatroom *chatroom;
124 EmpathyContact *user;
126 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
127 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
129 chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
134 priv = GET_PRIV (chatroom);
136 priv->factory = empathy_contact_factory_new ();
137 priv->group = empathy_tp_group_new (account, tp_chan);
139 g_signal_connect (priv->group, "member-added",
140 G_CALLBACK (tp_chatroom_member_added_cb),
142 g_signal_connect (priv->group, "member-removed",
143 G_CALLBACK (tp_chatroom_member_removed_cb),
146 /* Check if we are invited to join the chat */
147 user = empathy_tp_group_get_self_contact (priv->group);
148 members = empathy_tp_group_get_local_pendings (priv->group);
149 for (l = members; l; l = l->next) {
150 EmpathyPendingInfo *info;
154 if (!empathy_contact_equal (user, info->member)) {
158 priv->invitor = g_object_ref (info->actor);
159 priv->invit_message = g_strdup (info->message);
160 priv->is_invited = TRUE;
162 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
163 empathy_contact_get_id (priv->invitor),
164 empathy_contact_get_handle (priv->invitor),
165 priv->invit_message);
168 g_list_foreach (members, (GFunc) empathy_pending_info_free, NULL);
169 g_list_free (members);
170 g_object_unref (user);
176 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom *chatroom,
177 EmpathyContact **contact,
178 const gchar **message)
180 EmpathyTpChatroomPriv *priv;
182 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
184 priv = GET_PRIV (chatroom);
187 *contact = priv->invitor;
190 *message = priv->invit_message;
193 return priv->is_invited;
197 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
199 EmpathyTpChatroomPriv *priv;
200 EmpathyContact *user;
202 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
204 priv = GET_PRIV (chatroom);
206 if (!priv->is_invited) {
210 /* Clear invitation data */
211 priv->is_invited = FALSE;
213 g_object_unref (priv->invitor);
214 priv->invitor = NULL;
216 g_free (priv->invit_message);
217 priv->invit_message = NULL;
219 /* Add ourself in the members of the room */
220 user = empathy_tp_group_get_self_contact (priv->group);
221 empathy_tp_group_add_member (priv->group, user, "");
222 g_object_unref (user);
226 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
229 /* FIXME: not implemented */
233 tp_chatroom_member_added_cb (EmpathyTpGroup *group,
234 EmpathyContact *contact,
235 EmpathyContact *actor,
237 const gchar *message,
238 EmpathyTpChatroom *chatroom)
240 g_signal_emit_by_name (chatroom, "members-changed",
241 contact, actor, reason, message,
246 tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
247 EmpathyContact *contact,
248 EmpathyContact *actor,
250 const gchar *message,
251 EmpathyTpChatroom *chatroom)
253 g_signal_emit_by_name (chatroom, "members-changed",
254 contact, actor, reason, message,
259 tp_chatroom_add (EmpathyContactList *list,
260 EmpathyContact *contact,
261 const gchar *message)
263 EmpathyTpChatroomPriv *priv;
265 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
266 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
268 priv = GET_PRIV (list);
270 empathy_tp_group_add_member (priv->group, contact, message);
274 tp_chatroom_remove (EmpathyContactList *list,
275 EmpathyContact *contact,
276 const gchar *message)
278 EmpathyTpChatroomPriv *priv;
280 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
281 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
283 priv = GET_PRIV (list);
285 empathy_tp_group_remove_member (priv->group, contact, message);
289 tp_chatroom_get_members (EmpathyContactList *list)
291 EmpathyTpChatroomPriv *priv;
293 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
295 priv = GET_PRIV (list);
297 return empathy_tp_group_get_members (priv->group);