]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chatroom.c
Use empathy_file_lookup for glade files since some are in libempathy-gtk/ and others...
[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                                  "tp-chan", tp_chan,
206                                  NULL);
207
208         priv = GET_PRIV (chatroom);
209
210         priv->group = empathy_tp_group_new (account, tp_chan);
211
212         g_signal_connect (priv->group, "member-added",
213                           G_CALLBACK (tp_chatroom_member_added_cb),
214                           chatroom);
215         g_signal_connect (priv->group, "member-removed",
216                           G_CALLBACK (tp_chatroom_member_removed_cb),
217                           chatroom);
218         g_signal_connect (priv->group, "local-pending",
219                           G_CALLBACK (tp_chatroom_local_pending_cb),
220                           chatroom);
221
222         g_object_unref (channel);
223         g_object_unref (tp_conn);
224         g_object_unref (connection);
225         g_object_unref (mc);
226
227         return chatroom;
228 }
229
230 gboolean
231 empathy_tp_chatroom_get_invitation (EmpathyTpChatroom  *chatroom,
232                                     EmpathyContact     **contact,
233                                     const gchar       **message)
234 {
235         EmpathyTpChatroomPriv *priv;
236
237         g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);
238
239         priv = GET_PRIV (chatroom);
240
241         if (contact) {
242                 *contact = priv->invitor;
243         }
244         if (message) {
245                 *message = priv->invit_message;
246         }
247
248         return priv->is_invited;
249 }
250
251 void
252 empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
253 {
254         EmpathyTpChatroomPriv *priv;
255         EmpathyContact        *user;
256
257         g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));
258
259         priv = GET_PRIV (chatroom);
260
261         if (!priv->is_invited) {
262                 return;
263         }
264
265         /* Clear invitation data */
266         priv->is_invited = FALSE;
267         if (priv->invitor) {
268                 g_object_unref (priv->invitor);
269                 priv->invitor = NULL;
270         }
271         g_free (priv->invit_message);
272         priv->invit_message = NULL;
273
274         /* Add ourself in the members of the room */
275         user = empathy_tp_group_get_self_contact (priv->group);
276         empathy_tp_group_add_member (priv->group, user, "");
277         g_object_unref (user);
278 }
279