]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chatroom.c
Prepare a new API for chat properties
[empathy.git] / libempathy / empathy-tp-chatroom.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
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"
29
30 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
31                        EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
32
33 #define DEBUG_DOMAIN "TpChatroom"
34
35 struct _EmpathyTpChatroomPriv {
36         EmpathyTpGroup *group;
37
38         gboolean        is_invited;
39         EmpathyContact *invitor;
40         gchar          *invit_message;
41 };
42
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
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));
50
51 static void
52 tp_chatroom_member_added_cb (EmpathyTpGroup    *group,
53                              EmpathyContact    *contact,
54                              EmpathyContact    *actor,
55                              guint              reason,
56                              const gchar       *message,
57                              EmpathyTpChatroom *chatroom)
58 {
59         g_signal_emit_by_name (chatroom, "members-changed",
60                                contact, actor, reason, message,
61                                TRUE);
62 }
63
64 static void
65 tp_chatroom_member_removed_cb (EmpathyTpGroup    *group,
66                                EmpathyContact    *contact,
67                                EmpathyContact    *actor,
68                                guint              reason,
69                                const gchar       *message,
70                                EmpathyTpChatroom *chatroom)
71 {
72         g_signal_emit_by_name (chatroom, "members-changed",
73                                contact, actor, reason, message,
74                                FALSE);
75 }
76 static void
77 tp_chatroom_local_pending_cb  (EmpathyTpGroup    *group,
78                                EmpathyContact    *contact,
79                                EmpathyContact    *actor,
80                                guint              reason,
81                                const gchar       *message,
82                                EmpathyTpChatroom *chatroom)
83 {
84         EmpathyTpChatroomPriv *priv = GET_PRIV (chatroom);
85
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;
90
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),
94                                priv->invit_message);
95         }
96 }
97
98 static void
99 tp_chatroom_add (EmpathyContactList *list,
100                  EmpathyContact     *contact,
101                  const gchar        *message)
102 {
103         EmpathyTpChatroomPriv *priv;
104
105         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
106         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
107
108         priv = GET_PRIV (list);
109
110         empathy_tp_group_add_member (priv->group, contact, message);
111 }
112
113 static void
114 tp_chatroom_remove (EmpathyContactList *list,
115                     EmpathyContact     *contact,
116                     const gchar        *message)
117 {
118         EmpathyTpChatroomPriv *priv;
119
120         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
121         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
122
123         priv = GET_PRIV (list);
124
125         empathy_tp_group_remove_member (priv->group, contact, message);
126 }
127
128 static GList *
129 tp_chatroom_get_members (EmpathyContactList *list)
130 {
131         EmpathyTpChatroomPriv *priv;
132
133         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
134
135         priv = GET_PRIV (list);
136
137         return empathy_tp_group_get_members (priv->group);
138 }
139
140 static void
141 tp_chatroom_finalize (GObject *object)
142 {
143         EmpathyTpChatroomPriv *priv;
144         EmpathyTpChatroom     *chatroom;
145
146         chatroom = EMPATHY_TP_CHATROOM (object);
147         priv = GET_PRIV (chatroom);
148
149         g_object_unref (priv->group);
150
151         if (priv->invitor) {
152                 g_object_unref (priv->invitor);
153         }
154
155         g_free (priv->invit_message);
156
157         G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
158 }
159
160 static void
161 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
162 {
163         GObjectClass *object_class = G_OBJECT_CLASS (klass);
164
165         object_class->finalize = tp_chatroom_finalize;
166
167         g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
168 }
169
170 static void
171 tp_chatroom_iface_init (EmpathyContactListIface *iface)
172 {
173         iface->add         = tp_chatroom_add;
174         iface->remove      = tp_chatroom_remove;
175         iface->get_members = tp_chatroom_get_members;
176 }
177
178 static void
179 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
180 {
181 }
182
183 EmpathyTpChatroom *
184 empathy_tp_chatroom_new (McAccount *account,
185                          TpChan    *tp_chan)
186 {
187         EmpathyTpChatroomPriv *priv;
188         EmpathyTpChatroom     *chatroom;
189         TpChannel             *channel;
190         TpConnection          *connection;
191         MissionControl        *mc;
192         TpConn                *tp_conn;
193
194         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
195         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
196
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);
201
202         chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
203                                  "account", account,
204                                  "channel", channel,
205                                  NULL);
206
207         priv = GET_PRIV (chatroom);
208
209         priv->group = empathy_tp_group_new (account, tp_chan);
210
211         g_signal_connect (priv->group, "member-added",
212                           G_CALLBACK (tp_chatroom_member_added_cb),
213                           chatroom);
214         g_signal_connect (priv->group, "member-removed",
215                           G_CALLBACK (tp_chatroom_member_removed_cb),
216                           chatroom);
217         g_signal_connect (priv->group, "local-pending",
218                           G_CALLBACK (tp_chatroom_local_pending_cb),
219                           chatroom);
220
221         g_object_unref (channel);
222         g_object_unref (tp_conn);
223         g_object_unref (connection);
224         g_object_unref (mc);
225
226         return chatroom;
227 }
228
229 gboolean
230 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom  *chatroom,
231                                     EmpathyContact     **contact,
232                                     const gchar       **message)
233 {
234         EmpathyTpChatroomPriv *priv;
235
236         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
237
238         priv = GET_PRIV (chatroom);
239
240         if (contact) {
241                 *contact = priv->invitor;
242         }
243         if (message) {
244                 *message = priv->invit_message;
245         }
246
247         return priv->is_invited;
248 }
249
250 void
251 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
252 {
253         EmpathyTpChatroomPriv *priv;
254         EmpathyContact        *user;
255
256         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
257
258         priv = GET_PRIV (chatroom);
259
260         if (!priv->is_invited) {
261                 return;
262         }
263
264         /* Clear invitation data */
265         priv->is_invited = FALSE;
266         if (priv->invitor) {
267                 g_object_unref (priv->invitor);
268                 priv->invitor = NULL;
269         }
270         g_free (priv->invit_message);
271         priv->invit_message = NULL;
272
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);
277 }
278