aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-tp-chatroom.c
blob: a00aa581b8ceea3e2c6ca54bcff6fa10aa1c33e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007-2008 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Authors: Xavier Claessens <xclaesse@gmail.com>
 */

#include <config.h>

#include "empathy-tp-chatroom.h"
#include "empathy-contact-list.h"
#include "empathy-contact-factory.h"
#include "empathy-tp-group.h"
#include "empathy-utils.h"
#include "empathy-debug.h"

#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
               EMPATHY_TYPE_TP_CHATROOM, EmpathyTpChatroomPriv))

#define DEBUG_DOMAIN "TpChatroom"

struct _EmpathyTpChatroomPriv {
    EmpathyContactFactory *factory;
    EmpathyTpGroup        *group;

    gboolean               is_invited;
    EmpathyContact        *invitor;
    gchar                 *invit_message;
};

static void            empathy_tp_chatroom_class_init (EmpathyTpChatroomClass  *klass);
static void            tp_chatroom_iface_init         (EmpathyContactListIface *iface);
static void            empathy_tp_chatroom_init       (EmpathyTpChatroom       *chatroom);
static void            tp_chatroom_finalize           (GObject                 *object);
static void            tp_chatroom_member_added_cb    (EmpathyTpGroup          *group,
                               EmpathyContact          *contact,
                               EmpathyContact          *actor,
                               guint                    reason,
                               const gchar             *message,
                               EmpathyTpChatroom       *chatroom);
static void            tp_chatroom_member_removed_cb  (EmpathyTpGroup          *group,
                               EmpathyContact          *contact,
                               EmpathyContact          *actor,
                               guint                    reason,
                               const gchar             *message,
                               EmpathyTpChatroom       *chatroom);
static void            tp_chatroom_add                (EmpathyContactList      *list,
                               EmpathyContact           *contact,
                               const gchar             *message);
static void            tp_chatroom_remove             (EmpathyContactList      *list,
                               EmpathyContact           *contact,
                               const gchar             *message);
static GList *         tp_chatroom_get_members        (EmpathyContactList      *list);

G_DEFINE_TYPE_WITH_CODE (EmpathyTpChatroom, empathy_tp_chatroom, EMPATHY_TYPE_TP_CHAT,
             G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
                        tp_chatroom_iface_init));

static void
empathy_tp_chatroom_class_init (EmpathyTpChatroomClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = tp_chatroom_finalize;

    g_type_class_add_private (object_class, sizeof (EmpathyTpChatroomPriv));
}

static void
tp_chatroom_iface_init (EmpathyContactListIface *iface)
{
    iface->add         = tp_chatroom_add;
    iface->remove      = tp_chatroom_remove;
    iface->get_members = tp_chatroom_get_members;
}

static void
empathy_tp_chatroom_init (EmpathyTpChatroom *chatroom)
{
}

static void
tp_chatroom_finalize (GObject *object)
{
    EmpathyTpChatroomPriv *priv;
    EmpathyTpChatroom     *chatroom;

    chatroom = EMPATHY_TP_CHATROOM (object);
    priv = GET_PRIV (chatroom);

    g_object_unref (priv->group);
    g_object_unref (priv->factory);

    if (priv->invitor) {
        g_object_unref (priv->invitor);
    }

    g_free (priv->invit_message);

    G_OBJECT_CLASS (empathy_tp_chatroom_parent_class)->finalize (object);
}

EmpathyTpChatroom *
empathy_tp_chatroom_new (McAccount *account,
             TpChan    *tp_chan)
{
    EmpathyTpChatroomPriv *priv;
    EmpathyTpChatroom     *chatroom;
    GList                 *members, *l;
    EmpathyContact        *user;

    g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
    g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);

    chatroom = g_object_new (EMPATHY_TYPE_TP_CHATROOM,
                 "account", account,
                 "tp-chan", tp_chan,
                 NULL);

    priv = GET_PRIV (chatroom);

    priv->factory = empathy_contact_factory_new ();
    priv->group = empathy_tp_group_new (account, tp_chan);

    g_signal_connect (priv->group, "member-added",
              G_CALLBACK (tp_chatroom_member_added_cb),
              chatroom);
    g_signal_connect (priv->group, "member-removed",
              G_CALLBACK (tp_chatroom_member_removed_cb),
              chatroom);

    /* Check if we are invited to join the chat */
    user = empathy_tp_group_get_self_contact (priv->group);
    members = empathy_tp_group_get_local_pendings (priv->group);
    for (l = members; l; l = l->next) {
        EmpathyPendingInfo *info;

        info = l->data;

        if (!empathy_contact_equal (user, info->member)) {
            continue;
        }

        priv->invitor = g_object_ref (info->actor);
        priv->invit_message = g_strdup (info->message);
        priv->is_invited = TRUE;

        empathy_debug (DEBUG_DOMAIN, "We are invited to join by %s (%d): %s",
                   empathy_contact_get_id (priv->invitor),
                   empathy_contact_get_handle (priv->invitor),
                   priv->invit_message);
    }

    g_list_foreach (members, (GFunc) empathy_pending_info_free, NULL);
    g_list_free (members);
    g_object_unref (user);

    return chatroom;
}

gboolean
empathy_tp_chatroom_get_invitation (EmpathyTpChatroom  *chatroom,
                    EmpathyContact     **contact,
                    const gchar       **message)
{
    EmpathyTpChatroomPriv *priv;

    g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom), FALSE);

    priv = GET_PRIV (chatroom);

    if (contact) {
        *contact = priv->invitor;
    }
    if (message) {
        *message = priv->invit_message;
    }

    return priv->is_invited;
}

void
empathy_tp_chatroom_accept_invitation (EmpathyTpChatroom *chatroom)
{
    EmpathyTpChatroomPriv *priv;
    EmpathyContact        *user;

    g_return_if_fail (EMPATHY_IS_TP_CHATROOM (chatroom));

    priv = GET_PRIV (chatroom);

    if (!priv->is_invited) {
        return;
    }

    /* Clear invitation data */
    priv->is_invited = FALSE;
    if (priv->invitor) {
        g_object_unref (priv->invitor);
        priv->invitor = NULL;
    }
    g_free (priv->invit_message);
    priv->invit_message = NULL;

    /* Add ourself in the members of the room */
    user = empathy_tp_group_get_self_contact (priv->group);
    empathy_tp_group_add_member (priv->group, user, "");
    g_object_unref (user);
}

void
empathy_tp_chatroom_set_topic (EmpathyTpChatroom *chatroom,
                   const gchar       *topic)
{
    /* FIXME: not implemented */
}

static void
tp_chatroom_member_added_cb (EmpathyTpGroup    *group,
                 EmpathyContact    *contact,
                 EmpathyContact    *actor,
                 guint              reason,
                 const gchar       *message,
                 EmpathyTpChatroom *chatroom)
{
    g_signal_emit_by_name (chatroom, "members-changed",
                   contact, actor, reason, message,
                   TRUE);
}

static void
tp_chatroom_member_removed_cb (EmpathyTpGroup    *group,
                   EmpathyContact    *contact,
                   EmpathyContact    *actor,
                   guint              reason,
                   const gchar       *message,
                   EmpathyTpChatroom *chatroom)
{
    g_signal_emit_by_name (chatroom, "members-changed",
                   contact, actor, reason, message,
                   FALSE);
}

static void
tp_chatroom_add (EmpathyContactList *list,
         EmpathyContact     *contact,
         const gchar        *message)
{
    EmpathyTpChatroomPriv *priv;

    g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
    g_return_if_fail (EMPATHY_IS_CONTACT (contact));

    priv = GET_PRIV (list);

    empathy_tp_group_add_member (priv->group, contact, message);
}

static void
tp_chatroom_remove (EmpathyContactList *list,
            EmpathyContact     *contact,
            const gchar        *message)
{
    EmpathyTpChatroomPriv *priv;

    g_return_if_fail (EMPATHY_IS_TP_CHATROOM (list));
    g_return_if_fail (EMPATHY_IS_CONTACT (contact));

    priv = GET_PRIV (list);

    empathy_tp_group_remove_member (priv->group, contact, message);
}

static GList *
tp_chatroom_get_members (EmpathyContactList *list)
{
    EmpathyTpChatroomPriv *priv;

    g_return_val_if_fail (EMPATHY_IS_TP_CHATROOM (list), NULL);

    priv = GET_PRIV (list);

    return empathy_tp_group_get_members (priv->group);
}