diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Roster/XMPPRoster.h | 26 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterImpl.cpp | 72 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterImpl.h | 16 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterItem.h | 53 |
4 files changed, 136 insertions, 31 deletions
diff --git a/Swiften/Roster/XMPPRoster.h b/Swiften/Roster/XMPPRoster.h index b88148d..676e8f9 100644 --- a/Swiften/Roster/XMPPRoster.h +++ b/Swiften/Roster/XMPPRoster.h @@ -6,12 +6,15 @@ #pragma once +#include <boost/optional.hpp> +#include <vector> +#include <set> +#include "Swiften/Base/boost_bsignals.h" + #include "Swiften/Base/String.h" #include "Swiften/JID/JID.h" #include "Swiften/Elements/RosterItemPayload.h" - -#include <vector> -#include "Swiften/Base/boost_bsignals.h" +#include <Swiften/Roster/XMPPRosterItem.h> namespace Swift { /** @@ -43,7 +46,22 @@ namespace Swift { /** * Returns the list of groups for the given JID. */ - virtual const std::vector<String>& getGroupsForJID(const JID& jid) = 0; + virtual std::vector<String> getGroupsForJID(const JID& jid) = 0; + + /** + * Retrieve the items in the roster. + */ + virtual std::vector<XMPPRosterItem> getItems() const = 0; + + /** + * Retrieve the item with the given JID. + */ + virtual boost::optional<XMPPRosterItem> getItem(const JID&) const = 0; + + /** + * Retrieve the list of (existing) groups. + */ + virtual std::set<String> getGroups() const = 0; public: /** diff --git a/Swiften/Roster/XMPPRosterImpl.cpp b/Swiften/Roster/XMPPRosterImpl.cpp index 762ae29..3e9e312 100644 --- a/Swiften/Roster/XMPPRosterImpl.cpp +++ b/Swiften/Roster/XMPPRosterImpl.cpp @@ -4,7 +4,8 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#include "Swiften/Roster/XMPPRosterImpl.h" +#include <Swiften/Roster/XMPPRosterImpl.h> +#include <Swiften/Base/foreach.h> namespace Swift { @@ -13,21 +14,15 @@ XMPPRosterImpl::XMPPRosterImpl() { void XMPPRosterImpl::addContact(const JID& jid, const String& name, const std::vector<String>& groups, RosterItemPayload::Subscription subscription) { JID bareJID(jid.toBare()); - bool exists = containsJID(bareJID); - String oldName = getNameForJID(bareJID); - std::vector<String> oldGroups = entries_[bareJID].groups; - if (exists) { - entries_.erase(bareJID); - } - XMPPRosterItem item; - item.groups = groups; - item.name = name; - item.jid = jid; - item.subscription = subscription; - entries_[bareJID] = item; - if (exists) { + std::map<JID, XMPPRosterItem>::iterator i = entries_.find(bareJID); + if (i != entries_.end()) { + String oldName = i->second.getName(); + std::vector<String> oldGroups = i->second.getGroups(); + i->second = XMPPRosterItem(jid, name, groups, subscription); onJIDUpdated(bareJID, oldName, oldGroups); - } else { + } + else { + entries_.insert(std::make_pair(bareJID, XMPPRosterItem(jid, name, groups, subscription))); onJIDAdded(bareJID); } } @@ -49,19 +44,58 @@ bool XMPPRosterImpl::containsJID(const JID& jid) { String XMPPRosterImpl::getNameForJID(const JID& jid) const { std::map<JID, XMPPRosterItem>::const_iterator i = entries_.find(jid.toBare()); if (i != entries_.end()) { - return i->second.name; + return i->second.getName(); } else { return ""; } } -const std::vector<String>& XMPPRosterImpl::getGroupsForJID(const JID& jid) { - return entries_[JID(jid.toBare())].groups; +std::vector<String> XMPPRosterImpl::getGroupsForJID(const JID& jid) { + std::map<JID, XMPPRosterItem>::iterator i = entries_.find(jid.toBare()); + if (i != entries_.end()) { + return i->second.getGroups(); + } + else { + return std::vector<String>(); + } } RosterItemPayload::Subscription XMPPRosterImpl::getSubscriptionStateForJID(const JID& jid) { - return entries_[JID(jid.toBare())].subscription; + std::map<JID, XMPPRosterItem>::iterator i = entries_.find(jid.toBare()); + if (i != entries_.end()) { + return i->second.getSubscription(); + } + else { + return RosterItemPayload::None; + } +} + +std::vector<XMPPRosterItem> XMPPRosterImpl::getItems() const { + std::vector<XMPPRosterItem> result; + foreach(const RosterMap::value_type& entry, entries_) { + result.push_back(entry.second); + } + return result; +} + +boost::optional<XMPPRosterItem> XMPPRosterImpl::getItem(const JID& jid) const { + std::map<JID, XMPPRosterItem>::const_iterator i = entries_.find(jid.toBare()); + if (i != entries_.end()) { + return i->second; + } + else { + return boost::optional<XMPPRosterItem>(); + } +} + +std::set<String> XMPPRosterImpl::getGroups() const { + std::set<String> result; + foreach(const RosterMap::value_type& entry, entries_) { + std::vector<String> groups = entry.second.getGroups(); + result.insert(groups.begin(), groups.end()); + } + return result; } } diff --git a/Swiften/Roster/XMPPRosterImpl.h b/Swiften/Roster/XMPPRosterImpl.h index c2d2458..f65683f 100644 --- a/Swiften/Roster/XMPPRosterImpl.h +++ b/Swiften/Roster/XMPPRosterImpl.h @@ -7,6 +7,7 @@ #pragma once #include <map> +#include <set> #include "Swiften/Roster/XMPPRoster.h" @@ -22,15 +23,14 @@ namespace Swift { bool containsJID(const JID& jid); RosterItemPayload::Subscription getSubscriptionStateForJID(const JID& jid); String getNameForJID(const JID& jid) const; - const std::vector<String>& getGroupsForJID(const JID& jid); + std::vector<String> getGroupsForJID(const JID& jid); + + virtual std::vector<XMPPRosterItem> getItems() const; + virtual boost::optional<XMPPRosterItem> getItem(const JID&) const; + virtual std::set<String> getGroups() const; private: - struct XMPPRosterItem { - JID jid; - String name; - std::vector<String> groups; - RosterItemPayload::Subscription subscription; - }; - std::map<JID, XMPPRosterItem> entries_; + typedef std::map<JID, XMPPRosterItem> RosterMap; + RosterMap entries_; }; } diff --git a/Swiften/Roster/XMPPRosterItem.h b/Swiften/Roster/XMPPRosterItem.h new file mode 100644 index 0000000..ceb7763 --- /dev/null +++ b/Swiften/Roster/XMPPRosterItem.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + + +#include <vector> + +#include <Swiften/Base/String.h> +#include <Swiften/JID/JID.h> +#include <Swiften/Elements/RosterItemPayload.h> + +namespace Swift { + class XMPPRosterItem { + public: + XMPPRosterItem(const JID& jid, const String& name, const std::vector<String>& groups, RosterItemPayload::Subscription subscription) : jid(jid), name(name), groups(groups), subscription(subscription) { + } + + const JID& getJID() const { + return jid; + } + + const String& getName() const { + return name; + } + + void setName(const String& name) { + this->name = name; + } + + const std::vector<String>& getGroups() const { + return groups; + } + + void setGroups(const std::vector<String>& groups) { + this->groups = groups; + } + + RosterItemPayload::Subscription getSubscription() const { + return subscription; + } + + private: + JID jid; + String name; + std::vector<String> groups; + RosterItemPayload::Subscription subscription; + }; +} + |