From 0c377fa93ffd8538da9e71ddc71d4e8c07600a22 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Mon, 22 Mar 2010 20:37:30 +0000 Subject: Can now accept roster subscriptions. diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp index 4c55ea8..8d13057 100644 --- a/Swift/Controllers/RosterController.cpp +++ b/Swift/Controllers/RosterController.cpp @@ -142,6 +142,7 @@ void RosterController::handleIncomingPresence(boost::shared_ptr newPre } void RosterController::handleSubscriptionRequest(const JID& jid, const String& message) { + //FIXME: If already subscribed, auto-subscribe SubscriptionRequestEvent* eventPointer = new SubscriptionRequestEvent(jid, message); eventPointer->onAccept.connect(boost::bind(&RosterController::handleSubscriptionRequestAccepted, this, eventPointer)); eventPointer->onDecline.connect(boost::bind(&RosterController::handleSubscriptionRequestDeclined, this, eventPointer)); @@ -150,11 +151,14 @@ void RosterController::handleSubscriptionRequest(const JID& jid, const String& m } void RosterController::handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event) { - //FIXME: do something + presenceOracle_->confirmSubscription(event->getJID()); + if (!xmppRoster_->containsJID(event->getJID()) || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::None || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::From) { + presenceOracle_->requestSubscription(event->getJID()); + } } void RosterController::handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event) { - //FIXME: do something + presenceOracle_->cancelSubscription(event->getJID()); } void RosterController::handleAvatarChanged(const JID& jid, const String&) { diff --git a/Swift/Controllers/XMPPRosterController.cpp b/Swift/Controllers/XMPPRosterController.cpp index d089cff..01e5169 100644 --- a/Swift/Controllers/XMPPRosterController.cpp +++ b/Swift/Controllers/XMPPRosterController.cpp @@ -37,7 +37,7 @@ void XMPPRosterController::handleRosterReceived(boost::shared_ptr if (item.getSubscription() == RosterItemPayload::Remove) { xmppRoster_->removeContact(item.getJID()); } else { - xmppRoster_->addContact(item.getJID(), item.getName(), item.getGroups()); + xmppRoster_->addContact(item.getJID(), item.getName(), item.getGroups(), item.getSubscription()); } } } diff --git a/Swift/QtUI/QtSubscriptionRequestWindow.cpp b/Swift/QtUI/QtSubscriptionRequestWindow.cpp index df0c8e6..bda86f2 100644 --- a/Swift/QtUI/QtSubscriptionRequestWindow.cpp +++ b/Swift/QtUI/QtSubscriptionRequestWindow.cpp @@ -9,7 +9,7 @@ namespace Swift { QtSubscriptionRequestWindow::QtSubscriptionRequestWindow(boost::shared_ptr event, QWidget* parent) : QDialog(parent), event_(event) { - QString text = P2QSTRING(event->getJID().toString()) + " would like to add you to their roster. Would you like to add them to your roster and share your status when you're online? If you choose to defer this choice, you'll be asked again when you next login."; + QString text = P2QSTRING(event->getJID().toString()) + " would like to add you to their roster.\n Would you like to add them to your roster and share your status when you're online? \n\nIf you choose to defer this choice, you'll be asked again when you next login."; QVBoxLayout* layout = new QVBoxLayout(); QLabel* label = new QLabel(text, this); layout->addWidget(label); diff --git a/Swiften/Presence/PresenceOracle.cpp b/Swiften/Presence/PresenceOracle.cpp index d7239df..988fc10 100644 --- a/Swiften/Presence/PresenceOracle.cpp +++ b/Swiften/Presence/PresenceOracle.cpp @@ -8,9 +8,33 @@ typedef std::pair > > JIDMapPair; typedef std::pair > JIDPresencePair; PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel) { - stanzaChannel->onPresenceReceived.connect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1)); + stanzaChannel_ = stanzaChannel; + stanzaChannel_->onPresenceReceived.connect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1)); } +void PresenceOracle::cancelSubscription(const JID& jid) { + boost::shared_ptr stanza(new Presence()); + stanza->setType(Presence::Unsubscribed); + stanza->setTo(jid); + stanzaChannel_->sendPresence(stanza); +} + +void PresenceOracle::confirmSubscription(const JID& jid) { + boost::shared_ptr stanza(new Presence()); + stanza->setType(Presence::Subscribed); + stanza->setTo(jid); + stanzaChannel_->sendPresence(stanza); +} + + +void PresenceOracle::requestSubscription(const JID& jid) { + boost::shared_ptr stanza(new Presence()); + stanza->setType(Presence::Subscribe); + stanza->setTo(jid); + stanzaChannel_->sendPresence(stanza); +} + + void PresenceOracle::handleIncomingPresence(boost::shared_ptr presence) { JID bareJID = JID(presence->getFrom().toBare()); diff --git a/Swiften/Presence/PresenceOracle.h b/Swiften/Presence/PresenceOracle.h index 320a999..dc6fe5d 100644 --- a/Swiften/Presence/PresenceOracle.h +++ b/Swiften/Presence/PresenceOracle.h @@ -14,6 +14,10 @@ class PresenceOracle { PresenceOracle(StanzaChannel* stanzaChannel); ~PresenceOracle() {}; + void cancelSubscription(const JID& jid); + void confirmSubscription(const JID& jid); + void requestSubscription(const JID& jid); + boost::signal, boost::shared_ptr)> onPresenceChange; boost::signal onPresenceSubscriptionRequest; diff --git a/Swiften/Roster/XMPPRoster.cpp b/Swiften/Roster/XMPPRoster.cpp index a5cccaf..62edc45 100644 --- a/Swiften/Roster/XMPPRoster.cpp +++ b/Swiften/Roster/XMPPRoster.cpp @@ -2,17 +2,21 @@ namespace Swift { -void XMPPRoster::addContact(const JID& jid, const String& name, const std::vector& groups) { +void XMPPRoster::addContact(const JID& jid, const String& name, const std::vector& groups, RosterItemPayload::Subscription subscription) { JID bareJID(jid.toBare()); bool exists = containsJID(bareJID); + String oldName = getNameForJID(bareJID); + std::vector oldGroups = entries_[bareJID].groups; if (exists) { entries_.erase(bareJID); } - String oldName = getNameForJID(bareJID); - std::vector oldGroups = entries_[bareJID].second; - entries_[bareJID] = std::pair >(name, groups); + XMPPRosterItem item; + item.groups = groups; + item.name = name; + item.jid = jid; + item.subscription = subscription; + entries_[bareJID] = item; if (exists) { - onJIDUpdated(bareJID, oldName, oldGroups); } else { onJIDAdded(bareJID); @@ -29,11 +33,15 @@ bool XMPPRoster::containsJID(const JID& jid) { } const String& XMPPRoster::getNameForJID(const JID& jid) { - return entries_[JID(jid.toBare())].first; + return entries_[JID(jid.toBare())].name; } const std::vector& XMPPRoster::getGroupsForJID(const JID& jid) { - return entries_[JID(jid.toBare())].second; + return entries_[JID(jid.toBare())].groups; +} + +RosterItemPayload::Subscription XMPPRoster::getSubscriptionStateForJID(const JID& jid) { + return entries_[JID(jid.toBare())].subscription; } } diff --git a/Swiften/Roster/XMPPRoster.h b/Swiften/Roster/XMPPRoster.h index 7a72e00..47326c3 100644 --- a/Swiften/Roster/XMPPRoster.h +++ b/Swiften/Roster/XMPPRoster.h @@ -3,6 +3,7 @@ #include "Swiften/Base/String.h" #include "Swiften/JID/JID.h" +#include "Swiften/Elements/RosterItemPayload.h" #include #include @@ -10,14 +11,22 @@ namespace Swift { +struct XMPPRosterItem { + JID jid; + String name; + std::vector groups; + RosterItemPayload::Subscription subscription; +}; + class XMPPRoster { public: XMPPRoster() {}; ~XMPPRoster() {}; - void addContact(const JID& jid, const String& name, const std::vector& groups); + void addContact(const JID& jid, const String& name, const std::vector& groups, const RosterItemPayload::Subscription subscription); bool containsJID(const JID& jid); void removeContact(const JID& jid); + RosterItemPayload::Subscription getSubscriptionStateForJID(const JID& jid); const String& getNameForJID(const JID& jid); const std::vector& getGroupsForJID(const JID& jid); @@ -26,7 +35,8 @@ class XMPPRoster { boost::signal&)> onJIDUpdated; private: - std::map > > entries_; + //std::map > > entries_; + std::map entries_; }; } -- cgit v0.10.2-6-g49f6