]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chatroom.c
Do not load avatar from cache if token is empty. Fixes bug #517098.
[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 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_member_added_cb    (EmpathyTpGroup          *group,
50                                                        EmpathyContact          *contact,
51                                                        EmpathyContact          *actor,
52                                                        guint                    reason,
53                                                        const gchar             *message,
54                                                        EmpathyTpChatroom       *chatroom);
55 static void            tp_chatroom_member_removed_cb  (EmpathyTpGroup          *group,
56                                                        EmpathyContact          *contact,
57                                                        EmpathyContact          *actor,
58                                                        guint                    reason,
59                                                        const gchar             *message,
60                                                        EmpathyTpChatroom       *chatroom);
61 static void            tp_chatroom_add                (EmpathyContactList      *list,
62                                                        EmpathyContact           *contact,
63                                                        const gchar             *message);
64 static void            tp_chatroom_remove             (EmpathyContactList      *list,
65                                                        EmpathyContact           *contact,
66                                                        const gchar             *message);
67 static GList *         tp_chatroom_get_members        (EmpathyContactList      *list);
68
69 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
70                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
71                                                 tp_chatroom_iface_init));
72
73 static void
74 empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
75 {
76         GObjectClass *object_class = G_OBJECT_CLASS (klass);
77
78         object_class->finalize = tp_chatroom_finalize;
79
80         g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
81 }
82
83 static void
84 tp_chatroom_iface_init (EmpathyContactListIface *iface)
85 {
86         iface->add         = tp_chatroom_add;
87         iface->remove      = tp_chatroom_remove;
88         iface->get_members = tp_chatroom_get_members;
89 }
90
91 static void
92 empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
93 {
94 }
95
96 static void
97 tp_chatroom_finalize (GObject *object)
98 {
99         EmpathyTpChatroomPriv *priv;
100         EmpathyTpChatroom     *chatroom;
101
102         chatroom = EMPATHY_TP_CHATROOM (object);
103         priv = GET_PRIV (chatroom);
104
105         g_object_unref (priv->group);
106         g_object_unref (priv->factory);
107
108         if (priv->invitor) {
109                 g_object_unref (priv->invitor);
110         }
111
112         g_free (priv->invit_message);
113
114         G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
115 }
116
117 EmpathyTpChatroom *
118 empathy_tp_chatroom_new (McAccount *account,
119                          TpChan    *tp_chan)
120 {
121         EmpathyTpChatroomPriv *priv;
122         EmpathyTpChatroom     *chatroom;
123         GList                 *members, *l;
124         EmpathyContact        *user;
125
126         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
127         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
128
129         chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
130                                  "account", account,
131                                  "tp-chan", tp_chan,
132                                  NULL);
133
134         priv = GET_PRIV (chatroom);
135
136         priv->factory = empathy_contact_factory_new ();
137         priv->group = empathy_tp_group_new (account, tp_chan);
138
139         g_signal_connect (priv->group, "member-added",
140                           G_CALLBACK (tp_chatroom_member_added_cb),
141                           chatroom);
142         g_signal_connect (priv->group, "member-removed",
143                           G_CALLBACK (tp_chatroom_member_removed_cb),
144                           chatroom);
145
146         /* Check if we are invited to join the chat */
147         user = empathy_tp_group_get_self_contact (priv->group);
148         members = empathy_tp_group_get_local_pendings (priv->group);
149         for (l = members; l; l = l->next) {
150                 EmpathyPendingInfo *info;
151
152                 info = l->data;
153
154                 if (!empathy_contact_equal (user, info->member)) {
155                         continue;
156                 }
157
158                 priv->invitor = g_object_ref (info->actor);
159                 priv->invit_message = g_strdup (info->message);
160                 priv->is_invited = TRUE;
161
162                 empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
163                                empathy_contact_get_id (priv->invitor),
164                                empathy_contact_get_handle (priv->invitor),
165                                priv->invit_message);
166         }
167
168         g_list_foreach (members, (GFunc) empathy_pending_info_free, NULL);
169         g_list_free (members);
170         g_object_unref (user);
171
172         return chatroom;
173 }
174
175 gboolean
176 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom  *chatroom,
177                                     EmpathyContact     **contact,
178                                     const gchar       **message)
179 {
180         EmpathyTpChatroomPriv *priv;
181
182         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
183
184         priv = GET_PRIV (chatroom);
185
186         if (contact) {
187                 *contact = priv->invitor;
188         }
189         if (message) {
190                 *message = priv->invit_message;
191         }
192
193         return priv->is_invited;
194 }
195
196 void
197 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
198 {
199         EmpathyTpChatroomPriv *priv;
200         EmpathyContact        *user;
201
202         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
203
204         priv = GET_PRIV (chatroom);
205
206         if (!priv->is_invited) {
207                 return;
208         }
209
210         /* Clear invitation data */
211         priv->is_invited = FALSE;
212         if (priv->invitor) {
213                 g_object_unref (priv->invitor);
214                 priv->invitor = NULL;
215         }
216         g_free (priv->invit_message);
217         priv->invit_message = NULL;
218
219         /* Add ourself in the members of the room */
220         user = empathy_tp_group_get_self_contact (priv->group);
221         empathy_tp_group_add_member (priv->group, user, "");
222         g_object_unref (user);
223 }
224
225 void
226 empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
227                                const gchar       *topic)
228 {
229         /* FIXME: not implemented */
230 }
231
232 static void
233 tp_chatroom_member_added_cb (EmpathyTpGroup    *group,
234                              EmpathyContact    *contact,
235                              EmpathyContact    *actor,
236                              guint              reason,
237                              const gchar       *message,
238                              EmpathyTpChatroom *chatroom)
239 {
240         g_signal_emit_by_name (chatroom, "members-changed",
241                                contact, actor, reason, message,
242                                TRUE);
243 }
244
245 static void
246 tp_chatroom_member_removed_cb (EmpathyTpGroup    *group,
247                                EmpathyContact    *contact,
248                                EmpathyContact    *actor,
249                                guint              reason,
250                                const gchar       *message,
251                                EmpathyTpChatroom *chatroom)
252 {
253         g_signal_emit_by_name (chatroom, "members-changed",
254                                contact, actor, reason, message,
255                                FALSE);
256 }
257
258 static void
259 tp_chatroom_add (EmpathyContactList *list,
260                  EmpathyContact     *contact,
261                  const gchar        *message)
262 {
263         EmpathyTpChatroomPriv *priv;
264
265         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
266         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
267
268         priv = GET_PRIV (list);
269
270         empathy_tp_group_add_member (priv->group, contact, message);
271 }
272
273 static void
274 tp_chatroom_remove (EmpathyContactList *list,
275                     EmpathyContact     *contact,
276                     const gchar        *message)
277 {
278         EmpathyTpChatroomPriv *priv;
279
280         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
281         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
282
283         priv = GET_PRIV (list);
284
285         empathy_tp_group_remove_member (priv->group, contact, message);
286 }
287
288 static GList *
289 tp_chatroom_get_members (EmpathyContactList *list)
290 {
291         EmpathyTpChatroomPriv *priv;
292
293         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);
294
295         priv = GET_PRIV (list);
296
297         return empathy_tp_group_get_members (priv->group);
298 }
299