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-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_add (EmpathyContactList *list,
50 EmpathyContact *contact,
51 const gchar *message);
52 static void tp_chatroom_remove (EmpathyContactList *list,
53 EmpathyContact *contact,
54 const gchar *message);
55 static GList * tp_chatroom_get_members (EmpathyContactList *list);
57 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
58 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
59 tp_chatroom_iface_init));
62 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
64 GObjectClass *object_class = G_OBJECT_CLASS (klass);
66 object_class->finalize = tp_chatroom_finalize;
68 g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
72 tp_chatroom_iface_init (EmpathyContactListIface *iface)
74 iface->add = tp_chatroom_add;
75 iface->remove = tp_chatroom_remove;
76 iface->get_members = tp_chatroom_get_members;
80 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
85 tp_chatroom_finalize (GObject *object)
87 EmpathyTpChatroomPriv *priv;
88 EmpathyTpChatroom *chatroom;
90 chatroom = EMPATHY_TP_CHATROOM (object);
91 priv = GET_PRIV (chatroom);
93 g_object_unref (priv->group);
94 g_object_unref (priv->factory);
97 g_object_unref (priv->invitor);
100 g_free (priv->invit_message);
102 G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
106 tp_chatroom_member_added_cb (EmpathyTpGroup *group,
107 EmpathyContact *contact,
108 EmpathyContact *actor,
110 const gchar *message,
111 EmpathyTpChatroom *chatroom)
113 g_signal_emit_by_name (chatroom, "members-changed",
114 contact, actor, reason, message,
119 tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
120 EmpathyContact *contact,
121 EmpathyContact *actor,
123 const gchar *message,
124 EmpathyTpChatroom *chatroom)
126 g_signal_emit_by_name (chatroom, "members-changed",
127 contact, actor, reason, message,
131 tp_chatroom_local_pending_cb (EmpathyTpGroup *group,
132 EmpathyContact *contact,
133 EmpathyContact *actor,
135 const gchar *message,
136 EmpathyTpChatroom *chatroom)
138 EmpathyTpChatroomPriv *priv = GET_PRIV (chatroom);
140 if (empathy_contact_is_user (contact)) {
141 priv->invitor = g_object_ref (actor);
142 priv->invit_message = g_strdup (message);
143 priv->is_invited = TRUE;
145 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
146 empathy_contact_get_id (priv->invitor),
147 empathy_contact_get_handle (priv->invitor),
148 priv->invit_message);
153 empathy_tp_chatroom_new (McAccount *account,
156 EmpathyTpChatroomPriv *priv;
157 EmpathyTpChatroom *chatroom;
159 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
160 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
162 chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
167 priv = GET_PRIV (chatroom);
169 priv->factory = empathy_contact_factory_new ();
170 priv->group = empathy_tp_group_new (account, tp_chan);
172 g_signal_connect (priv->group, "member-added",
173 G_CALLBACK (tp_chatroom_member_added_cb),
175 g_signal_connect (priv->group, "member-removed",
176 G_CALLBACK (tp_chatroom_member_removed_cb),
178 g_signal_connect (priv->group, "local-pending",
179 G_CALLBACK (tp_chatroom_local_pending_cb),
186 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom *chatroom,
187 EmpathyContact **contact,
188 const gchar **message)
190 EmpathyTpChatroomPriv *priv;
192 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
194 priv = GET_PRIV (chatroom);
197 *contact = priv->invitor;
200 *message = priv->invit_message;
203 return priv->is_invited;
207 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
209 EmpathyTpChatroomPriv *priv;
210 EmpathyContact *user;
212 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
214 priv = GET_PRIV (chatroom);
216 if (!priv->is_invited) {
220 /* Clear invitation data */
221 priv->is_invited = FALSE;
223 g_object_unref (priv->invitor);
224 priv->invitor = NULL;
226 g_free (priv->invit_message);
227 priv->invit_message = NULL;
229 /* Add ourself in the members of the room */
230 user = empathy_tp_group_get_self_contact (priv->group);
231 empathy_tp_group_add_member (priv->group, user, "");
232 g_object_unref (user);
236 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
239 /* FIXME: not implemented */
243 tp_chatroom_add (EmpathyContactList *list,
244 EmpathyContact *contact,
245 const gchar *message)
247 EmpathyTpChatroomPriv *priv;
249 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
250 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
252 priv = GET_PRIV (list);
254 empathy_tp_group_add_member (priv->group, contact, message);
258 tp_chatroom_remove (EmpathyContactList *list,
259 EmpathyContact *contact,
260 const gchar *message)
262 EmpathyTpChatroomPriv *priv;
264 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
265 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
267 priv = GET_PRIV (list);
269 empathy_tp_group_remove_member (priv->group, contact, message);
273 tp_chatroom_get_members (EmpathyContactList *list)
275 EmpathyTpChatroomPriv *priv;
277 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
279 priv = GET_PRIV (list);
281 return empathy_tp_group_get_members (priv->group);