]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chatroom.c
Completely reworked ContactList API. Fixes bug #471611, bug #467280, bug #459540...
[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 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include "empathy-tp-chatroom.h"
26 #include "empathy-contact-list.h"
27 #include "empathy-contact-factory.h"
28 #include "empathy-tp-group.h"
29 #include "empathy-utils.h"
30 #include "empathy-debug.h"
31
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
33                        EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
34
35 #define DEBUG_DOMAIN "TpChatroom"
36
37 struct _EmpathyTpChatroomPriv {
38         EmpathyContactFactory *factory;
39         EmpathyTpGroup        *group;
40
41         gboolean               is_invited;
42         EmpathyContact        *invitor;
43         gchar                 *invit_message;
44 };
45
46 static void            empathy_tp_chatroom_class_init (EmpathyTpChatroomClass  *klass);
47 static void            tp_chatroom_iface_init         (EmpathyContactListIface *iface);
48 static void            empathy_tp_chatroom_init       (EmpathyTpChatroom       *chatroom);
49 static void            tp_chatroom_finalize           (GObject                 *object);
50 static void            tp_chatroom_member_added_cb    (EmpathyTpGroup          *group,
51                                                        EmpathyContact          *contact,
52                                                        EmpathyContact          *actor,
53                                                        guint                    reason,
54                                                        const gchar             *message,
55                                                        EmpathyTpChatroom       *chatroom);
56 static void            tp_chatroom_member_removed_cb  (EmpathyTpGroup          *group,
57                                                        EmpathyContact          *contact,
58                                                        EmpathyContact          *actor,
59                                                        guint                    reason,
60                                                        const gchar             *message,
61                                                        EmpathyTpChatroom       *chatroom);
62 static void            tp_chatroom_add                (EmpathyContactList      *list,
63                                                        EmpathyContact           *contact,
64                                                        const gchar             *message);
65 static void            tp_chatroom_remove             (EmpathyContactList      *list,
66                                                        EmpathyContact           *contact,
67                                                        const gchar             *message);
68 static GList *         tp_chatroom_get_members        (EmpathyContactList      *list);
69
70 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
71                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
72                                                 tp_chatroom_iface_init));
73
74 static void
75 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
76 {
77         GObjectClass *object_class = G_OBJECT_CLASS (klass);
78
79         object_class->finalize = tp_chatroom_finalize;
80
81         g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
82 }
83
84 static void
85 tp_chatroom_iface_init (EmpathyContactListIface *iface)
86 {
87         iface->add         = tp_chatroom_add;
88         iface->remove      = tp_chatroom_remove;
89         iface->get_members = tp_chatroom_get_members;
90 }
91
92 static void
93 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
94 {
95 }
96
97 static void
98 tp_chatroom_finalize (GObject *object)
99 {
100         EmpathyTpChatroomPriv *priv;
101         EmpathyTpChatroom     *chatroom;
102
103         chatroom = EMPATHY_TP_CHATROOM (object);
104         priv = GET_PRIV (chatroom);
105
106         g_object_unref (priv->group);
107         g_object_unref (priv->factory);
108
109         if (priv->invitor) {
110                 g_object_unref (priv->invitor);
111         }
112
113         g_free (priv->invit_message);
114
115         G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
116 }
117
118 EmpathyTpChatroom *
119 empathy_tp_chatroom_new (McAccount *account,
120                          TpChan    *tp_chan)
121 {
122         EmpathyTpChatroomPriv *priv;
123         EmpathyTpChatroom     *chatroom;
124         GList                 *members, *l;
125         EmpathyContact        *user;
126
127         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
128         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
129
130         chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
131                                  "account", account,
132                                  "tp-chan", tp_chan,
133                                  NULL);
134
135         priv = GET_PRIV (chatroom);
136
137         priv->factory = empathy_contact_factory_new ();
138         priv->group = empathy_tp_group_new (account, tp_chan);
139
140         g_signal_connect (priv->group, "member-added",
141                           G_CALLBACK (tp_chatroom_member_added_cb),
142                           chatroom);
143         g_signal_connect (priv->group, "member-removed",
144                           G_CALLBACK (tp_chatroom_member_removed_cb),
145                           chatroom);
146
147         /* Check if we are invited to join the chat */
148         user = empathy_tp_group_get_self_contact (priv->group);
149         members = empathy_tp_group_get_local_pendings (priv->group);
150         for (l = members; l; l = l->next) {
151                 EmpathyPendingInfo *info;
152
153                 info = l->data;
154
155                 if (!empathy_contact_equal (user, info->member)) {
156                         continue;
157                 }
158
159                 priv->invitor = g_object_ref (info->actor);
160                 priv->invit_message = g_strdup (info->message);
161                 priv->is_invited = TRUE;
162
163                 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
164                                empathy_contact_get_id (priv->invitor),
165                                empathy_contact_get_handle (priv->invitor),
166                                priv->invit_message);
167         }
168
169         g_list_foreach (members, (GFunc) empathy_pending_info_free, NULL);
170         g_list_free (members);
171         g_object_unref (user);
172
173         return chatroom;
174 }
175
176 gboolean
177 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom  *chatroom,
178                                     EmpathyContact     **contact,
179                                     const gchar       **message)
180 {
181         EmpathyTpChatroomPriv *priv;
182
183         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
184
185         priv = GET_PRIV (chatroom);
186
187         if (*contact) {
188                 *contact = priv->invitor;
189         }
190         if (*message) {
191                 *message = priv->invit_message;
192         }
193
194         return priv->is_invited;
195 }
196
197 void
198 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
199 {
200         EmpathyTpChatroomPriv *priv;
201         EmpathyContact        *user;
202
203         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
204
205         priv = GET_PRIV (chatroom);
206
207         /* Clear invitation data */
208         priv->is_invited = FALSE;
209         if (priv->invitor) {
210                 g_object_unref (priv->invitor);
211                 priv->invitor = NULL;
212         }
213         g_free (priv->invit_message);
214         priv->invit_message = NULL;
215
216         /* Add ourself in the members of the room */
217         user = empathy_tp_group_get_self_contact (priv->group);
218         empathy_tp_group_add_member (priv->group, user, "");
219         g_object_unref (user);
220 }
221
222 void
223 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
224                                const gchar       *topic)
225 {
226         /* FIXME: not implemented */
227 }
228
229 static void
230 tp_chatroom_member_added_cb (EmpathyTpGroup    *group,
231                              EmpathyContact    *contact,
232                              EmpathyContact    *actor,
233                              guint              reason,
234                              const gchar       *message,
235                              EmpathyTpChatroom *chatroom)
236 {
237         g_signal_emit_by_name (chatroom, "members-changed",
238                                contact, actor, reason, message,
239                                TRUE);
240 }
241
242 static void
243 tp_chatroom_member_removed_cb (EmpathyTpGroup    *group,
244                                EmpathyContact    *contact,
245                                EmpathyContact    *actor,
246                                guint              reason,
247                                const gchar       *message,
248                                EmpathyTpChatroom *chatroom)
249 {
250         g_signal_emit_by_name (chatroom, "members-changed",
251                                contact, actor, reason, message,
252                                FALSE);
253 }
254
255 static void
256 tp_chatroom_add (EmpathyContactList *list,
257                  EmpathyContact     *contact,
258                  const gchar        *message)
259 {
260         EmpathyTpChatroomPriv *priv;
261
262         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
263         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
264
265         priv = GET_PRIV (list);
266
267         empathy_tp_group_add_member (priv->group, contact, message);
268 }
269
270 static void
271 tp_chatroom_remove (EmpathyContactList *list,
272                     EmpathyContact     *contact,
273                     const gchar        *message)
274 {
275         EmpathyTpChatroomPriv *priv;
276
277         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
278         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
279
280         priv = GET_PRIV (list);
281
282         empathy_tp_group_remove_member (priv->group, contact, message);
283 }
284
285 static GList *
286 tp_chatroom_get_members (EmpathyContactList *list)
287 {
288         EmpathyTpChatroomPriv *priv;
289
290         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
291
292         priv = GET_PRIV (list);
293
294         return empathy_tp_group_get_members (priv->group);
295 }
296