1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 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-tp-group.h"
27 #include "empathy-utils.h"
28 #include "empathy-debug.h"
30 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
31 EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
33 #define DEBUG_DOMAIN "TpChatroom"
35 struct _EmpathyTpChatroomPriv {
36 EmpathyTpGroup *group;
39 EmpathyContact *invitor;
43 static void empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass);
44 static void tp_chatroom_iface_init (EmpathyContactListIface *iface);
45 static void empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom);
46 static void tp_chatroom_finalize (GObject *object);
47 static void tp_chatroom_add (EmpathyContactList *list,
48 EmpathyContact *contact,
49 const gchar *message);
50 static void tp_chatroom_remove (EmpathyContactList *list,
51 EmpathyContact *contact,
52 const gchar *message);
53 static GList * tp_chatroom_get_members (EmpathyContactList *list);
55 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
56 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
57 tp_chatroom_iface_init));
60 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
62 GObjectClass *object_class = G_OBJECT_CLASS (klass);
64 object_class->finalize = tp_chatroom_finalize;
66 g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
70 tp_chatroom_iface_init (EmpathyContactListIface *iface)
72 iface->add = tp_chatroom_add;
73 iface->remove = tp_chatroom_remove;
74 iface->get_members = tp_chatroom_get_members;
78 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
83 tp_chatroom_finalize (GObject *object)
85 EmpathyTpChatroomPriv *priv;
86 EmpathyTpChatroom *chatroom;
88 chatroom = EMPATHY_TP_CHATROOM (object);
89 priv = GET_PRIV (chatroom);
91 g_object_unref (priv->group);
94 g_object_unref (priv->invitor);
97 g_free (priv->invit_message);
99 G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
103 tp_chatroom_member_added_cb (EmpathyTpGroup *group,
104 EmpathyContact *contact,
105 EmpathyContact *actor,
107 const gchar *message,
108 EmpathyTpChatroom *chatroom)
110 g_signal_emit_by_name (chatroom, "members-changed",
111 contact, actor, reason, message,
116 tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
117 EmpathyContact *contact,
118 EmpathyContact *actor,
120 const gchar *message,
121 EmpathyTpChatroom *chatroom)
123 g_signal_emit_by_name (chatroom, "members-changed",
124 contact, actor, reason, message,
128 tp_chatroom_local_pending_cb (EmpathyTpGroup *group,
129 EmpathyContact *contact,
130 EmpathyContact *actor,
132 const gchar *message,
133 EmpathyTpChatroom *chatroom)
135 EmpathyTpChatroomPriv *priv = GET_PRIV (chatroom);
137 if (empathy_contact_is_user (contact)) {
138 priv->invitor = g_object_ref (actor);
139 priv->invit_message = g_strdup (message);
140 priv->is_invited = TRUE;
142 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
143 empathy_contact_get_id (priv->invitor),
144 empathy_contact_get_handle (priv->invitor),
145 priv->invit_message);
150 empathy_tp_chatroom_new (McAccount *account,
153 EmpathyTpChatroomPriv *priv;
154 EmpathyTpChatroom *chatroom;
156 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
157 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
159 chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
164 priv = GET_PRIV (chatroom);
166 priv->group = empathy_tp_group_new (account, tp_chan);
168 g_signal_connect (priv->group, "member-added",
169 G_CALLBACK (tp_chatroom_member_added_cb),
171 g_signal_connect (priv->group, "member-removed",
172 G_CALLBACK (tp_chatroom_member_removed_cb),
174 g_signal_connect (priv->group, "local-pending",
175 G_CALLBACK (tp_chatroom_local_pending_cb),
182 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom *chatroom,
183 EmpathyContact **contact,
184 const gchar **message)
186 EmpathyTpChatroomPriv *priv;
188 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
190 priv = GET_PRIV (chatroom);
193 *contact = priv->invitor;
196 *message = priv->invit_message;
199 return priv->is_invited;
203 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
205 EmpathyTpChatroomPriv *priv;
206 EmpathyContact *user;
208 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
210 priv = GET_PRIV (chatroom);
212 if (!priv->is_invited) {
216 /* Clear invitation data */
217 priv->is_invited = FALSE;
219 g_object_unref (priv->invitor);
220 priv->invitor = NULL;
222 g_free (priv->invit_message);
223 priv->invit_message = NULL;
225 /* Add ourself in the members of the room */
226 user = empathy_tp_group_get_self_contact (priv->group);
227 empathy_tp_group_add_member (priv->group, user, "");
228 g_object_unref (user);
232 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
235 /* FIXME: not implemented */
239 tp_chatroom_add (EmpathyContactList *list,
240 EmpathyContact *contact,
241 const gchar *message)
243 EmpathyTpChatroomPriv *priv;
245 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
246 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
248 priv = GET_PRIV (list);
250 empathy_tp_group_add_member (priv->group, contact, message);
254 tp_chatroom_remove (EmpathyContactList *list,
255 EmpathyContact *contact,
256 const gchar *message)
258 EmpathyTpChatroomPriv *priv;
260 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
261 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
263 priv = GET_PRIV (list);
265 empathy_tp_group_remove_member (priv->group, contact, message);
269 tp_chatroom_get_members (EmpathyContactList *list)
271 EmpathyTpChatroomPriv *priv;
273 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
275 priv = GET_PRIV (list);
277 return empathy_tp_group_get_members (priv->group);