aboutsummaryrefslogtreecommitdiff
path: root/source/QXmppRoster.cpp
blob: 06f794300422258cb09cd6dab2c27908107be051 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (C) 2008-2010 Manjeet Dahiya
 *
 * Author:
 *	Manjeet Dahiya
 *
 * Source:
 *	http://code.google.com/p/qxmpp
 *
 * This file is a part of QXmpp library.
 *
 * 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.
 *
 */


#include "QXmppRoster.h"
#include "QXmppUtils.h"
#include "QXmppRosterIq.h"
#include "QXmppPresence.h"
#include "QXmppStream.h"

QXmppRoster::QXmppRoster(QXmppStream* stream) : m_stream(stream),
                                m_isRosterReceived(false)
{
}

QXmppRoster::~QXmppRoster()
{

}

void QXmppRoster::disconnected()
{
    m_entries = QMap<QString, QXmppRoster::QXmppRosterEntry>();
    m_presences = QMap<QString, QMap<QString, QXmppPresence> >();
    m_isRosterReceived = false;
}

void QXmppRoster::presenceReceived(const QXmppPresence& presence)
{
    QString jid = presence.from();
    QString bareJid = jidToBareJid(jid);
    QString resource = jidToResource(jid);

    if (presence.getType() == QXmppPresence::Available)
        m_presences[bareJid][resource] = presence;
    else if (presence.getType() == QXmppPresence::Unavailable)
        m_presences[bareJid].remove(resource);
    else
        return;

    emit presenceChanged(bareJid, resource);
}

void QXmppRoster::rosterIqReceived(const QXmppRosterIq& rosterIq)
{
    switch(rosterIq.type())
    {
    case QXmppIq::Set:
    case QXmppIq::Result:
        {
            QList<QXmppRosterIq::Item> items = rosterIq.items();
            for(int i = 0; i < items.count(); ++i)
            {
                QString bareJid = items.at(i).bareJid();
                m_entries[bareJid].setBareJid(bareJid);
                m_entries[bareJid].setName(items.at(i).name());
                m_entries[bareJid].setSubscriptionType(
                    static_cast<QXmppRosterEntry::SubscriptionType>(
                            items.at(i).subscriptionType()));
                m_entries[bareJid].setSubscriptionStatus(
                        items.at(i).subscriptionStatus());
                m_entries[bareJid].setGroups(items.at(i).groups());
                emit rosterChanged(bareJid);
            }
            if(rosterIq.type() == QXmppIq::Set) // send result iq
            {
                QXmppIq returnIq(QXmppIq::Result);
                returnIq.setId(rosterIq.id());
                m_stream->sendPacket(returnIq);
            }
            break;
        }
    default:
        break;
    }
}

void QXmppRoster::rosterRequestIqReceived(const QXmppRosterIq& rosterIq)
{
    switch(rosterIq.type())
    {
    case QXmppIq::Set:
    case QXmppIq::Result:
        {
            QList<QXmppRosterIq::Item> items = rosterIq.items();
            for(int i = 0; i < items.count(); ++i)
            {
                QString bareJid = items.at(i).bareJid();
                m_entries[bareJid].setBareJid(bareJid);
                m_entries[bareJid].setName(items.at(i).name());
                m_entries[bareJid].setSubscriptionType(
                    static_cast<QXmppRosterEntry::SubscriptionType>(
                            items.at(i).subscriptionType()));
                m_entries[bareJid].setSubscriptionStatus(
                        items.at(i).subscriptionStatus());
                m_entries[bareJid].setGroups(items.at(i).groups());
            }
            if(rosterIq.type() == QXmppIq::Set) // send result iq
            {
                QXmppIq returnIq(QXmppIq::Result);
                returnIq.setId(rosterIq.id());
                m_stream->sendPacket(returnIq);
            }
            m_isRosterReceived = true;
            emit rosterReceived();
            break;
        }
    default:
        break;
    }
}

/// Returns the bareJid of the roster entry.
///
/// \return bareJid as a QString
///

QString QXmppRoster::QXmppRosterEntry::bareJid() const
{
    return m_bareJid;
}

/// Returns the name of the roster entry.
///
/// \return name as a QString
///

QString QXmppRoster::QXmppRosterEntry::name() const
{
    return m_name;
}

/// Returns the subscription type of the roster entry.
///
/// \return QXmppRosterEntry::SubscriptionType
///

QXmppRoster::QXmppRosterEntry::SubscriptionType
        QXmppRoster::QXmppRosterEntry::subscriptionType() const
{
    return m_type;
}

/// Sets the subscription status of the roster entry. It is the "ask"
/// attribute in the Roster IQ stanza. Its value can be "subscribe" or "unsubscribe"
/// or empty.
///
/// \return subscription status as a QString
///
///

QString QXmppRoster::QXmppRosterEntry::subscriptionStatus() const
{
    return m_subscriptionStatus;
}

/// Returns the groups of the roster entry.
///
/// \return QSet<QString> list of all the groups
///

QSet<QString> QXmppRoster::QXmppRosterEntry::groups() const
{
    return m_groups;
}

/// Sets the bareJid of the roster entry.
///
/// \param bareJid as a QString
///

void QXmppRoster::QXmppRosterEntry::setBareJid(const QString& bareJid )
{
    m_bareJid = bareJid ;
}

/// Sets the name of the roster entry.
///
/// \param name as a QString
///

void QXmppRoster::QXmppRosterEntry::setName(const QString& name)
{
    m_name = name;
}

/// Sets the subscription type of the roster entry.
///
/// \param type as a QXmppRosterEntry::SubscriptionType
///

void QXmppRoster::QXmppRosterEntry::setSubscriptionType(
        QXmppRosterEntry::SubscriptionType type)
{
    m_type = type;
}

/// Sets the subscription status of the roster entry. It is the "ask"
/// attribute in the Roster IQ stanza. Its value can be "subscribe" or "unsubscribe"
/// or empty.
///
/// \param status as a QString
///

void QXmppRoster::QXmppRosterEntry::setSubscriptionStatus(const QString& status)
{
    m_subscriptionStatus = status;
}

/// Sets the groups of the roster entry.
///
/// \param groups list of all the groups as a QSet<QString>
///

void QXmppRoster::QXmppRosterEntry::setGroups(const QSet<QString>& groups)
{
    m_groups = groups;
}

/// Function to get all the bareJids present in the roster.
///
/// \return QStringList list of all the bareJids
///

QStringList QXmppRoster::getRosterBareJids() const
{
    return m_entries.keys();
}

/// Returns the roster entry of the given bareJid. If the bareJid is not in the
/// database and empty QXmppRoster::QXmppRosterEntry will be returned.
///
/// \param bareJid as a QString
/// \return QXmppRoster::QXmppRosterEntry
///

QXmppRoster::QXmppRosterEntry QXmppRoster::getRosterEntry(
        const QString& bareJid) const
{
    // will return blank entry if bareJid does'nt exist
    if(m_entries.contains(bareJid))
        return m_entries.value(bareJid);
    else
    {
        qWarning("QXmppRoster::getRosterEntry(): bareJid doesn't exist in roster db");
        return QXmppRoster::QXmppRosterEntry();
    }
}

/// [OBSOLETE] Returns all the roster entries in the database.
///
/// \return Map of bareJid and its respective QXmppRoster::QXmppRosterEntry
///
/// \note This function is obsolete, use getRosterBareJids() and
/// getRosterEntry() to get all the roster entries.
///

QMap<QString, QXmppRoster::QXmppRosterEntry>
        QXmppRoster::getRosterEntries() const
{
    return m_entries;
}

/// Get all the associated resources with the given bareJid.
///
/// \param bareJid as a QString
/// \return list of associated resources as a QStringList
///

QStringList QXmppRoster::getResources(const QString& bareJid) const
{
    if(m_presences.contains(bareJid))
        return m_presences[bareJid].keys();
    else
        return QStringList();
}

/// Get all the presences of all the resources of the given bareJid. A bareJid
/// can have multiple resources and each resource will have a presence
/// associated with it.
///
/// \param bareJid as a QString
/// \return Map of resource and its respective presence QMap<QString, QXmppPresence>
///

QMap<QString, QXmppPresence> QXmppRoster::getAllPresencesForBareJid(
        const QString& bareJid) const
{
    if(m_presences.contains(bareJid))
        return m_presences[bareJid];
    else
        return QMap<QString, QXmppPresence>();
}

/// Get the presence of the given resource of the given bareJid.
///
/// \param bareJid as a QString
/// \param resource as a QString
/// \return QXmppPresence
///

QXmppPresence QXmppRoster::getPresence(const QString& bareJid,
                                       const QString& resource) const
{
    if(m_presences.contains(bareJid) && m_presences[bareJid].contains(resource))
        return m_presences[bareJid][resource];
    else
    {
        qWarning("QXmppRoster::getPresence(): invalid bareJid");
        return QXmppPresence();
    }
}

/// [OBSOLETE] Returns all the presence entries in the database.
///
/// \return Map of bareJid and map of resource and its presence that is
/// QMap<QString, QMap<QString, QXmppPresence> >
///
/// \note This function is obsolete, use getRosterBareJids(), getResources()
/// and getPresence() or getAllPresencesForBareJid()
/// to get all the presence entries.

QMap<QString, QMap<QString, QXmppPresence> > QXmppRoster::getAllPresences() const
{
    return m_presences;
}

/// Function to check whether the roster has been received or not.
///
/// \return true if roster received else false

bool QXmppRoster::isRosterReceived()
{
    return m_isRosterReceived;
}

QString QXmppRoster::QXmppRosterEntry::getBareJid() const
{
    return m_bareJid;
}

QString QXmppRoster::QXmppRosterEntry::getName() const
{
    return m_name;
}

QXmppRoster::QXmppRosterEntry::SubscriptionType
        QXmppRoster::QXmppRosterEntry::getSubscriptionType() const
{
    return m_type;
}

QString QXmppRoster::QXmppRosterEntry::getSubscriptionStatus() const
{
    return m_subscriptionStatus;
}

QSet<QString> QXmppRoster::QXmppRosterEntry::getGroups() const
{
    return m_groups;
}