aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2011-05-18 08:15:45 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2011-05-18 08:15:45 +0000
commit6bee004d52df797e402e9a0c6feb21179f9389e6 (patch)
tree8ae35f3fe5fbf5b26a8aa820c1c106ed11bca198 /src
parentc8d0fcaa4d9887a74acc5174728d927af6c90717 (diff)
downloadqxmpp-6bee004d52df797e402e9a0c6feb21179f9389e6.tar.gz
add signals and slots for managing subscriptions (Closes: #92)
Diffstat (limited to 'src')
-rw-r--r--src/QXmppRosterManager.cpp108
-rw-r--r--src/QXmppRosterManager.h29
2 files changed, 87 insertions, 50 deletions
diff --git a/src/QXmppRosterManager.cpp b/src/QXmppRosterManager.cpp
index 92b150ea..0dfda018 100644
--- a/src/QXmppRosterManager.cpp
+++ b/src/QXmppRosterManager.cpp
@@ -48,6 +48,18 @@ QXmppRosterManager::QXmppRosterManager(QXmppClient* client)
Q_ASSERT(check);
}
+/// Accepts a subscription request.
+///
+/// You can call this method in reply to the subscriptionRequest() signal.
+
+bool QXmppRosterManager::acceptSubscription(const QString &bareJid)
+{
+ QXmppPresence presence;
+ presence.setTo(bareJid);
+ presence.setType(QXmppPresence::Subscribed);
+ return client()->sendPacket(presence);
+}
+
/// Upon XMPP connection, request the roster.
///
void QXmppRosterManager::connected()
@@ -89,9 +101,9 @@ bool QXmppRosterManager::handleStanza(const QDomElement &element)
void QXmppRosterManager::presenceReceived(const QXmppPresence& presence)
{
- QString jid = presence.from();
- QString bareJid = jidToBareJid(jid);
- QString resource = jidToResource(jid);
+ const QString jid = presence.from();
+ const QString bareJid = jidToBareJid(jid);
+ const QString resource = jidToResource(jid);
if (bareJid.isEmpty())
return;
@@ -110,15 +122,12 @@ void QXmppRosterManager::presenceReceived(const QXmppPresence& presence)
if (client()->configuration().autoAcceptSubscriptions())
{
// accept subscription request
- QXmppPresence presence;
- presence.setTo(jid);
- presence.setType(QXmppPresence::Subscribed);
- client()->sendPacket(presence);
+ acceptSubscription(bareJid);
// ask for reciprocal subscription
- presence.setTo(bareJid);
- presence.setType(QXmppPresence::Subscribe);
- client()->sendPacket(presence);
+ subscribe(bareJid);
+ } else {
+ emit subscriptionReceived(bareJid);
}
break;
default:
@@ -126,14 +135,26 @@ void QXmppRosterManager::presenceReceived(const QXmppPresence& presence)
}
}
+/// Refuses a subscription request.
+///
+/// You can call this method in reply to the subscriptionRequest() signal.
+
+bool QXmppRosterManager::refuseSubscription(const QString &bareJid)
+{
+ QXmppPresence presence;
+ presence.setTo(bareJid);
+ presence.setType(QXmppPresence::Unsubscribed);
+ return client()->sendPacket(presence);
+}
+
/// Removes a roster entry and cancels subscriptions to and from the contact.
///
/// As a result, the server will initiate a roster push, causing the
-/// rosterChanged() signal to be emitted.
+/// itemRemoved() signal to be emitted.
///
/// \param bareJid
-void QXmppRosterManager::removeRosterEntry(const QString &bareJid)
+bool QXmppRosterManager::removeItem(const QString &bareJid)
{
QXmppRosterIq::Item item;
item.setBareJid(bareJid);
@@ -142,7 +163,7 @@ void QXmppRosterManager::removeRosterEntry(const QString &bareJid)
QXmppRosterIq iq;
iq.setType(QXmppIq::Set);
iq.addItem(item);
- client()->sendPacket(iq);
+ return client()->sendPacket(iq);
}
void QXmppRosterManager::rosterIqReceived(const QXmppRosterIq& rosterIq)
@@ -206,6 +227,32 @@ void QXmppRosterManager::rosterIqReceived(const QXmppRosterIq& rosterIq)
}
}
+/// Requests a subscription to the given contact.
+///
+/// As a result, the server will initiate a roster push, causing the
+/// itemAdded() or itemChanged() signal to be emitted.
+
+bool QXmppRosterManager::subscribe(const QString &bareJid)
+{
+ QXmppPresence packet;
+ packet.setTo(jidToBareJid(bareJid));
+ packet.setType(QXmppPresence::Subscribe);
+ return client()->sendPacket(packet);
+}
+
+/// Removes a subscription to the given contact.
+///
+/// As a result, the server will initiate a roster push, causing the
+/// itemChanged() signal to be emitted.
+
+bool QXmppRosterManager::unsubscribe(const QString &bareJid)
+{
+ QXmppPresence packet;
+ packet.setTo(jidToBareJid(bareJid));
+ packet.setType(QXmppPresence::Unsubscribe);
+ return client()->sendPacket(packet);
+}
+
/// Function to get all the bareJids present in the roster.
///
/// \return QStringList list of all the bareJids
@@ -232,20 +279,6 @@ QXmppRosterIq::Item QXmppRosterManager::getRosterEntry(
return QXmppRosterIq::Item();
}
-/// [OBSOLETE] Returns all the roster entries in the database.
-///
-/// \return Map of bareJid and its respective QXmppRosterIq::Item
-///
-/// \note This function is obsolete, use getRosterBareJids() and
-/// getRosterEntry() to get all the roster entries.
-///
-
-QMap<QString, QXmppRosterIq::Item>
- QXmppRosterManager::getRosterEntries() const
-{
- return m_entries;
-}
-
/// Get all the associated resources with the given bareJid.
///
/// \param bareJid as a QString
@@ -298,20 +331,6 @@ QXmppPresence QXmppRosterManager::getPresence(const QString& bareJid,
}
}
-/// [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> > QXmppRosterManager::getAllPresences() const
-{
- return m_presences;
-}
-
/// Function to check whether the roster has been received or not.
///
/// \return true if roster received else false
@@ -321,3 +340,10 @@ bool QXmppRosterManager::isRosterReceived()
return m_isRosterReceived;
}
+// deprecated
+
+void QXmppRosterManager::removeRosterEntry(const QString &bareJid)
+{
+ removeItem(bareJid);
+}
+
diff --git a/src/QXmppRosterManager.h b/src/QXmppRosterManager.h
index 2ddf6445..92655bc3 100644
--- a/src/QXmppRosterManager.h
+++ b/src/QXmppRosterManager.h
@@ -22,20 +22,17 @@
*
*/
-#ifndef QXMPPROSTER_H
-#define QXMPPROSTER_H
+#ifndef QXMPPROSTERMANAGER_H
+#define QXMPPROSTERMANAGER_H
#include <QObject>
#include <QMap>
-#include <QSet>
#include <QStringList>
#include "QXmppClientExtension.h"
#include "QXmppPresence.h"
#include "QXmppRosterIq.h"
-class QXmppRosterIq;
-
/// \brief The QXmppRosterManager class provides access to a connected client's roster.
///
/// \note It's object should not be created using it's constructor. Instead
@@ -72,7 +69,6 @@ public:
bool isRosterReceived();
QStringList getRosterBareJids() const;
QXmppRosterIq::Item getRosterEntry(const QString& bareJid) const;
- void removeRosterEntry(const QString &bareJid);
QStringList getResources(const QString& bareJid) const;
QMap<QString, QXmppPresence> getAllPresencesForBareJid(
@@ -84,12 +80,18 @@ public:
bool handleStanza(const QDomElement &element);
/// \endcond
- // deprecated in release 0.2.0
+ // deprecated in release 0.4.0
/// \cond
- QMap<QString, QXmppRosterIq::Item> Q_DECL_DEPRECATED getRosterEntries() const;
- QMap<QString, QMap<QString, QXmppPresence> > Q_DECL_DEPRECATED getAllPresences() const;
+ void Q_DECL_DEPRECATED removeRosterEntry(const QString &bareJid);
/// \endcond
+public slots:
+ bool acceptSubscription(const QString &bareJid);
+ bool refuseSubscription(const QString &bareJid);
+ bool removeItem(const QString &bareJid);
+ bool subscribe(const QString &bareJid);
+ bool unsubscribe(const QString &bareJid);
+
signals:
/// This signal is emitted when the Roster IQ is received after a successful
/// connection. That is the roster entries are empty before this signal is emitted.
@@ -105,6 +107,15 @@ signals:
void rosterChanged(const QString& bareJid);
/// \endcond
+ /// This signal is emitted when a contact asks to subscribe to your presence.
+ ///
+ /// You can either accept the request by calling acceptSubscription() or refuse it
+ /// by calling refuseSubscription().
+ ///
+ /// \note If you set QXmppConfiguration::autoAcceptSubscriptions() to true, this
+ /// signal will not be emitted.
+ void subscriptionReceived(const QString& bareJid);
+
/// This signal is emitted when the roster entry of a particular bareJid is
/// added as a result of roster push.
void itemAdded(const QString& bareJid);