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);
47 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
48 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
49 tp_chatroom_iface_init));
52 tp_chatroom_member_added_cb (EmpathyTpGroup *group,
53 EmpathyContact *contact,
54 EmpathyContact *actor,
57 EmpathyTpChatroom *chatroom)
59 g_signal_emit_by_name (chatroom, "members-changed",
60 contact, actor, reason, message,
65 tp_chatroom_member_removed_cb (EmpathyTpGroup *group,
66 EmpathyContact *contact,
67 EmpathyContact *actor,
70 EmpathyTpChatroom *chatroom)
72 g_signal_emit_by_name (chatroom, "members-changed",
73 contact, actor, reason, message,
77 tp_chatroom_local_pending_cb (EmpathyTpGroup *group,
78 EmpathyContact *contact,
79 EmpathyContact *actor,
82 EmpathyTpChatroom *chatroom)
84 EmpathyTpChatroomPriv *priv = GET_PRIV (chatroom);
86 if (empathy_contact_is_user (contact)) {
87 priv->invitor = g_object_ref (actor);
88 priv->invit_message = g_strdup (message);
89 priv->is_invited = TRUE;
91 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
92 empathy_contact_get_id (priv->invitor),
93 empathy_contact_get_handle (priv->invitor),
99 tp_chatroom_add (EmpathyContactList *list,
100 EmpathyContact *contact,
101 const gchar *message)
103 EmpathyTpChatroomPriv *priv;
105 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
106 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
108 priv = GET_PRIV (list);
110 empathy_tp_group_add_member (priv->group, contact, message);
114 tp_chatroom_remove (EmpathyContactList *list,
115 EmpathyContact *contact,
116 const gchar *message)
118 EmpathyTpChatroomPriv *priv;
120 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
121 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
123 priv = GET_PRIV (list);
125 empathy_tp_group_remove_member (priv->group, contact, message);
129 tp_chatroom_get_members (EmpathyContactList *list)
131 EmpathyTpChatroomPriv *priv;
133 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
135 priv = GET_PRIV (list);
137 return empathy_tp_group_get_members (priv->group);
141 tp_chatroom_finalize (GObject *object)
143 EmpathyTpChatroomPriv *priv;
144 EmpathyTpChatroom *chatroom;
146 chatroom = EMPATHY_TP_CHATROOM (object);
147 priv = GET_PRIV (chatroom);
149 g_object_unref (priv->group);
152 g_object_unref (priv->invitor);
155 g_free (priv->invit_message);
157 G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
161 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
163 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 object_class->finalize = tp_chatroom_finalize;
167 g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
171 tp_chatroom_iface_init (EmpathyContactListIface *iface)
173 iface->add = tp_chatroom_add;
174 iface->remove = tp_chatroom_remove;
175 iface->get_members = tp_chatroom_get_members;
179 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
184 empathy_tp_chatroom_new (McAccount *account,
187 EmpathyTpChatroomPriv *priv;
188 EmpathyTpChatroom *chatroom;
190 TpConnection *connection;
194 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
195 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
197 mc = empathy_mission_control_new ();
198 tp_conn = mission_control_get_connection (mc, account, NULL);
199 connection = tp_conn_dup_connection (tp_conn);
200 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
202 chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
207 priv = GET_PRIV (chatroom);
209 priv->group = empathy_tp_group_new (account, tp_chan);
211 g_signal_connect (priv->group, "member-added",
212 G_CALLBACK (tp_chatroom_member_added_cb),
214 g_signal_connect (priv->group, "member-removed",
215 G_CALLBACK (tp_chatroom_member_removed_cb),
217 g_signal_connect (priv->group, "local-pending",
218 G_CALLBACK (tp_chatroom_local_pending_cb),
221 g_object_unref (channel);
222 g_object_unref (tp_conn);
223 g_object_unref (connection);
230 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom *chatroom,
231 EmpathyContact **contact,
232 const gchar **message)
234 EmpathyTpChatroomPriv *priv;
236 g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
238 priv = GET_PRIV (chatroom);
241 *contact = priv->invitor;
244 *message = priv->invit_message;
247 return priv->is_invited;
251 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
253 EmpathyTpChatroomPriv *priv;
254 EmpathyContact *user;
256 g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
258 priv = GET_PRIV (chatroom);
260 if (!priv->is_invited) {
264 /* Clear invitation data */
265 priv->is_invited = FALSE;
267 g_object_unref (priv->invitor);
268 priv->invitor = NULL;
270 g_free (priv->invit_message);
271 priv->invit_message = NULL;
273 /* Add ourself in the members of the room */
274 user = empathy_tp_group_get_self_contact (priv->group);
275 empathy_tp_group_add_member (priv->group, user, "");
276 g_object_unref (user);