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