]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chatroom.c
Make sure we don't expect contact id and handle to be directly ready. Add some _run_u...
[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-contact-factory.h"
27 #include "empathy-tp-group.h"
28 #include "empathy-utils.h"
29 #include "empathy-debug.h"
30
31 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
32                        EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))
33
34 #define DEBUG_DOMAIN "TpChatroom"
35
36 struct _EmpathyTpChatroomPriv {
37         EmpathyContactFactory *factory;
38         EmpathyTpGroup        *group;
39
40         gboolean               is_invited;
41         EmpathyContact        *invitor;
42         gchar                 *invit_message;
43 };
44
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);
56
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));
60
61 static void
62 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
63 {
64         GObjectClass *object_class = G_OBJECT_CLASS (klass);
65
66         object_class->finalize = tp_chatroom_finalize;
67
68         g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
69 }
70
71 static void
72 tp_chatroom_iface_init (EmpathyContactListIface *iface)
73 {
74         iface->add         = tp_chatroom_add;
75         iface->remove      = tp_chatroom_remove;
76         iface->get_members = tp_chatroom_get_members;
77 }
78
79 static void
80 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
81 {
82 }
83
84 static void
85 tp_chatroom_finalize (GObject *object)
86 {
87         EmpathyTpChatroomPriv *priv;
88         EmpathyTpChatroom     *chatroom;
89
90         chatroom = EMPATHY_TP_CHATROOM (object);
91         priv = GET_PRIV (chatroom);
92
93         g_object_unref (priv->group);
94         g_object_unref (priv->factory);
95
96         if (priv->invitor) {
97                 g_object_unref (priv->invitor);
98         }
99
100         g_free (priv->invit_message);
101
102         G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
103 }
104
105 static void
106 tp_chatroom_member_added_cb (EmpathyTpGroup    *group,
107                              EmpathyContact    *contact,
108                              EmpathyContact    *actor,
109                              guint              reason,
110                              const gchar       *message,
111                              EmpathyTpChatroom *chatroom)
112 {
113         g_signal_emit_by_name (chatroom, "members-changed",
114                                contact, actor, reason, message,
115                                TRUE);
116 }
117
118 static void
119 tp_chatroom_member_removed_cb (EmpathyTpGroup    *group,
120                                EmpathyContact    *contact,
121                                EmpathyContact    *actor,
122                                guint              reason,
123                                const gchar       *message,
124                                EmpathyTpChatroom *chatroom)
125 {
126         g_signal_emit_by_name (chatroom, "members-changed",
127                                contact, actor, reason, message,
128                                FALSE);
129 }
130 static void
131 tp_chatroom_local_pending_cb  (EmpathyTpGroup    *group,
132                                EmpathyContact    *contact,
133                                EmpathyContact    *actor,
134                                guint              reason,
135                                const gchar       *message,
136                                EmpathyTpChatroom *chatroom)
137 {
138         EmpathyTpChatroomPriv *priv = GET_PRIV (chatroom);
139
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;
144
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);
149         }
150 }
151
152 EmpathyTpChatroom *
153 empathy_tp_chatroom_new (McAccount *account,
154                          TpChan    *tp_chan)
155 {
156         EmpathyTpChatroomPriv *priv;
157         EmpathyTpChatroom     *chatroom;
158
159         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
160         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
161
162         chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
163                                  "account", account,
164                                  "tp-chan", tp_chan,
165                                  NULL);
166
167         priv = GET_PRIV (chatroom);
168
169         priv->factory = empathy_contact_factory_new ();
170         priv->group = empathy_tp_group_new (account, tp_chan);
171
172         g_signal_connect (priv->group, "member-added",
173                           G_CALLBACK (tp_chatroom_member_added_cb),
174                           chatroom);
175         g_signal_connect (priv->group, "member-removed",
176                           G_CALLBACK (tp_chatroom_member_removed_cb),
177                           chatroom);
178         g_signal_connect (priv->group, "local-pending",
179                           G_CALLBACK (tp_chatroom_local_pending_cb),
180                           chatroom);
181
182         return chatroom;
183 }
184
185 gboolean
186 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom  *chatroom,
187                                     EmpathyContact     **contact,
188                                     const gchar       **message)
189 {
190         EmpathyTpChatroomPriv *priv;
191
192         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
193
194         priv = GET_PRIV (chatroom);
195
196         if (contact) {
197                 *contact = priv->invitor;
198         }
199         if (message) {
200                 *message = priv->invit_message;
201         }
202
203         return priv->is_invited;
204 }
205
206 void
207 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
208 {
209         EmpathyTpChatroomPriv *priv;
210         EmpathyContact        *user;
211
212         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
213
214         priv = GET_PRIV (chatroom);
215
216         if (!priv->is_invited) {
217                 return;
218         }
219
220         /* Clear invitation data */
221         priv->is_invited = FALSE;
222         if (priv->invitor) {
223                 g_object_unref (priv->invitor);
224                 priv->invitor = NULL;
225         }
226         g_free (priv->invit_message);
227         priv->invit_message = NULL;
228
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);
233 }
234
235 void
236 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
237                                const gchar       *topic)
238 {
239         /* FIXME: not implemented */
240 }
241
242 static void
243 tp_chatroom_add (EmpathyContactList *list,
244                  EmpathyContact     *contact,
245                  const gchar        *message)
246 {
247         EmpathyTpChatroomPriv *priv;
248
249         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
250         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
251
252         priv = GET_PRIV (list);
253
254         empathy_tp_group_add_member (priv->group, contact, message);
255 }
256
257 static void
258 tp_chatroom_remove (EmpathyContactList *list,
259                     EmpathyContact     *contact,
260                     const gchar        *message)
261 {
262         EmpathyTpChatroomPriv *priv;
263
264         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
265         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
266
267         priv = GET_PRIV (list);
268
269         empathy_tp_group_remove_member (priv->group, contact, message);
270 }
271
272 static GList *
273 tp_chatroom_get_members (EmpathyContactList *list)
274 {
275         EmpathyTpChatroomPriv *priv;
276
277         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
278
279         priv = GET_PRIV (list);
280
281         return empathy_tp_group_get_members (priv->group);
282 }
283