1 /*
2  * Copyright (C) 2008-2021 The QXmpp developers
3  *
4  * Authors:
5  *  Manjeet Dahiya
6  *  Jeremy Lainé
7  *
8  * Source:
9  *  https://github.com/qxmpp-project/qxmpp
10  *
11  * This file is a part of QXmpp library.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  */
24 
25 #ifndef QXMPPROSTERMANAGER_H
26 #define QXMPPROSTERMANAGER_H
27 
28 #include "QXmppClientExtension.h"
29 #include "QXmppPresence.h"
30 #include "QXmppRosterIq.h"
31 
32 #include <QMap>
33 #include <QObject>
34 #include <QStringList>
35 
36 class QXmppRosterManagerPrivate;
37 
38 ///
39 /// \brief The QXmppRosterManager class provides access to a connected client's
40 /// roster.
41 ///
42 /// \note Its object should not be created using its constructor. Instead
43 /// \c QXmppClient::findExtension<QXmppRosterManager>() should be used to get
44 /// the instantiated object of this class.
45 ///
46 /// It stores all the Roster and Presence details of all the roster entries
47 /// (that is all the bareJids) in the client's friend's list. It provides the
48 /// functionality to get all the bareJids in the client's roster and Roster and
49 /// Presence details of the same.
50 ///
51 /// After the QXmpp connected successfully to the XMPP server the signal
52 /// \c QXmppClient::connected() is emitted and the roster is requested from the
53 /// server. Once QXmpp receives the roster the signal
54 /// \c QXmppRosterManager::rosterReceived() is emitted and after that the
55 /// methods of this class can be used to get the roster entries.
56 ///
57 /// \c QXmppRosterManager::isRosterReceived() can be used to find out whether
58 /// the roster has been received yet.
59 ///
60 /// The \c itemAdded(), \c itemChanged() and \c itemRemoved() signals are
61 /// emitted whenever roster entries are added, changed or removed.
62 ///
63 /// The \c presenceChanged() signal is emitted whenever the presence for a
64 /// roster item changes.
65 ///
66 /// \ingroup Managers
67 ///
68 class QXMPP_EXPORT QXmppRosterManager : public QXmppClientExtension
69 {
70     Q_OBJECT
71 
72 public:
73     QXmppRosterManager(QXmppClient *stream);
74     ~QXmppRosterManager() override;
75 
76     bool isRosterReceived() const;
77     QStringList getRosterBareJids() const;
78     QXmppRosterIq::Item getRosterEntry(const QString &bareJid) const;
79 
80     QStringList getResources(const QString &bareJid) const;
81     QMap<QString, QXmppPresence> getAllPresencesForBareJid(
82         const QString &bareJid) const;
83     QXmppPresence getPresence(const QString &bareJid,
84                               const QString &resource) const;
85 
86     /// \cond
87     bool handleStanza(const QDomElement &element) override;
88     /// \endcond
89 
90 public Q_SLOTS:
91     bool acceptSubscription(const QString &bareJid, const QString &reason = {});
92     bool refuseSubscription(const QString &bareJid, const QString &reason = {});
93     bool addItem(const QString &bareJid, const QString &name = {}, const QSet<QString> &groups = {});
94     bool removeItem(const QString &bareJid);
95     bool renameItem(const QString &bareJid, const QString &name);
96     bool subscribe(const QString &bareJid, const QString &reason = {});
97     bool unsubscribe(const QString &bareJid, const QString &reason = {});
98 
99 Q_SIGNALS:
100     /// This signal is emitted when the Roster IQ is received after a successful
101     /// connection. That is the roster entries are empty before this signal is emitted.
102     /// One should use getRosterBareJids() and getRosterEntry() only after
103     /// this signal has been emitted.
104     ///
105     /// \note If the previous stream could be resumed, this signal is not
106     /// emitted since QXmpp 1.4. Changes since the last connection are reported
107     /// via the itemAdded(), itemChanged() and itemRemoved() signals.
108     void rosterReceived();
109 
110     /// This signal is emitted when the presence of a particular bareJid and resource changes.
111     void presenceChanged(const QString &bareJid, const QString &resource);
112 
113     /// This signal is emitted when a contact asks to subscribe to your presence.
114     ///
115     /// You can either accept the request by calling acceptSubscription() or refuse it
116     /// by calling refuseSubscription().
117     ///
118     /// \note If you set QXmppConfiguration::autoAcceptSubscriptions() to true, this
119     /// signal will not be emitted.
120     void subscriptionReceived(const QString &bareJid);
121 
122     /// This signal is emitted when the roster entry of a particular bareJid is
123     /// added as a result of roster push.
124     void itemAdded(const QString &bareJid);
125 
126     /// This signal is emitted when the roster entry of a particular bareJid
127     /// changes as a result of roster push.
128     void itemChanged(const QString &bareJid);
129 
130     /// This signal is emitted when the roster entry of a particular bareJid is
131     /// removed as a result of roster push.
132     void itemRemoved(const QString &bareJid);
133 
134 private Q_SLOTS:
135     void _q_connected();
136     void _q_disconnected();
137     void _q_presenceReceived(const QXmppPresence &);
138 
139 private:
140     QXmppRosterManagerPrivate *d;
141 };
142 
143 #endif  // QXMPPROSTER_H
144