diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/LinkLocal/LinkLocalRoster.cpp | 81 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalRoster.h | 19 |
2 files changed, 86 insertions, 14 deletions
diff --git a/Swiften/LinkLocal/LinkLocalRoster.cpp b/Swiften/LinkLocal/LinkLocalRoster.cpp index feec7b7..aedea77 100644 --- a/Swiften/LinkLocal/LinkLocalRoster.cpp +++ b/Swiften/LinkLocal/LinkLocalRoster.cpp @@ -12,37 +12,92 @@ LinkLocalRoster::LinkLocalRoster(boost::shared_ptr<DNSSDService> service) : dnsS dnsSDService->onServiceAdded.connect(boost::bind(&LinkLocalRoster::handleServiceAdded, this, _1)); dnsSDService->onServiceRemoved.connect(boost::bind(&LinkLocalRoster::handleServiceRemoved, this, _1)); dnsSDService->onServiceResolved.connect(boost::bind(&LinkLocalRoster::handleServiceResolved, this, _1, _2)); - dnsSDService->onHostnameResolved.connect(boost::bind(&LinkLocalRoster::handleHostnameResolved, this, _1, _2)); +} + +boost::shared_ptr<RosterPayload> LinkLocalRoster::getRoster() const { + boost::shared_ptr<RosterPayload> roster(new RosterPayload()); + for(ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) { + roster->addItem(getRosterItem(i->first, i->second)); + } + return roster; +} + +std::vector<boost::shared_ptr<Presence> > LinkLocalRoster::getAllPresence() const { + std::vector<boost::shared_ptr<Presence> > result; + for(ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) { + result.push_back(getPresence(i->first, i->second)); + } + return result; +} + +RosterItemPayload LinkLocalRoster::getRosterItem(const DNSSDService::Service& service, const DNSSDService::ResolveResult& resolveResult) const { + return RosterItemPayload(getJIDForService(service), getRosterName(service, resolveResult), RosterItemPayload::Both); +} + +String LinkLocalRoster::getRosterName(const DNSSDService::Service& service, const DNSSDService::ResolveResult& resolveResult) const { + if (!resolveResult.info.getNick().isEmpty()) { + return resolveResult.info.getNick(); + } + else if (!resolveResult.info.getFirstName().isEmpty()) { + String result = resolveResult.info.getFirstName(); + if (!resolveResult.info.getLastName().isEmpty()) { + result += " " + resolveResult.info.getLastName(); + } + return result; + } + else if (!resolveResult.info.getLastName().isEmpty()) { + return resolveResult.info.getLastName(); + } + return service.name; +} + +JID LinkLocalRoster::getJIDForService(const DNSSDService::Service& service) const { + return JID(service.name); +} + +boost::shared_ptr<Presence> LinkLocalRoster::getPresence(const DNSSDService::Service& service, const DNSSDService::ResolveResult& resolveResult) const { + boost::shared_ptr<Presence> presence(new Presence()); + presence->setFrom(getJIDForService(service)); + switch (resolveResult.info.getStatus()) { + case LinkLocalServiceInfo::Available: + presence->setShow(StatusShow::Online); + break; + case LinkLocalServiceInfo::Away: + presence->setShow(StatusShow::Away); + break; + case LinkLocalServiceInfo::DND: + presence->setShow(StatusShow::DND); + break; + } + presence->setStatus(resolveResult.info.getMessage()); + return presence; } void LinkLocalRoster::handleServiceAdded(const DNSSDService::Service& service) { if (selfService && *selfService == service) { return; } - std::cout << "Service added " << service.name << " " << service.type << " " << service.domain << std::endl; dnsSDService->startResolvingService(service); } void LinkLocalRoster::handleServiceRemoved(const DNSSDService::Service& service) { - std::cout << "Service removed " << service.name << " " << service.type << " " << service.domain << std::endl; dnsSDService->stopResolvingService(service); + services.erase(service); + boost::shared_ptr<RosterPayload> roster(new RosterPayload()); + roster->addItem(RosterItemPayload(getJIDForService(service), "", RosterItemPayload::Remove)); + onRosterChanged(roster); } void LinkLocalRoster::handleServiceResolved(const DNSSDService::Service& service, const DNSSDService::ResolveResult& result) { - std::cout << "Service resolved: " << service.name << "->" << result.host << " " << result.port << " " << result.info.getLastName() << std::endl; + services.insert(std::make_pair(service, result)); dnsSDService->resolveHostname(result.host); -} -void LinkLocalRoster::handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address) { - if (address) { - std::cout << "Address resolved: " << hostname << " " << address->toString() << std::endl; - } - else { - std::cout << "Unable to resolve address for " << hostname << std::endl; - } + boost::shared_ptr<RosterPayload> roster(new RosterPayload()); + roster->addItem(getRosterItem(service, result)); + onRosterChanged(roster); + onPresenceChanged(getPresence(service, result)); } - void LinkLocalRoster::handleServiceRegistered(const DNSSDService::Service& service) { selfService = service; } diff --git a/Swiften/LinkLocal/LinkLocalRoster.h b/Swiften/LinkLocal/LinkLocalRoster.h index 321bd96..33dd455 100644 --- a/Swiften/LinkLocal/LinkLocalRoster.h +++ b/Swiften/LinkLocal/LinkLocalRoster.h @@ -2,8 +2,13 @@ #include <boost/shared_ptr.hpp> #include <boost/optional.hpp> +#include <set> +#include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" #include "Swiften/LinkLocal/DNSSDService.h" +#include "Swiften/Elements/RosterPayload.h" +#include "Swiften/Elements/Presence.h" namespace Swift { class HostAddress; @@ -12,16 +17,28 @@ namespace Swift { public: LinkLocalRoster(boost::shared_ptr<DNSSDService> service); + boost::shared_ptr<RosterPayload> getRoster() const; + std::vector<boost::shared_ptr<Presence> > getAllPresence() const; + + boost::signal<void (boost::shared_ptr<RosterPayload>)> onRosterChanged; + boost::signal<void (boost::shared_ptr<Presence>)> onPresenceChanged; + private: + RosterItemPayload getRosterItem(const DNSSDService::Service& service, const DNSSDService::ResolveResult& info) const; + String getRosterName(const DNSSDService::Service& service, const DNSSDService::ResolveResult& info) const; + JID getJIDForService(const DNSSDService::Service& service) const; + boost::shared_ptr<Presence> getPresence(const DNSSDService::Service& service, const DNSSDService::ResolveResult& info) const; + void handleStopped(bool); void handleServiceRegistered(const DNSSDService::Service& service); void handleServiceAdded(const DNSSDService::Service&); void handleServiceRemoved(const DNSSDService::Service&); void handleServiceResolved(const DNSSDService::Service& service, const DNSSDService::ResolveResult& result); - void handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address); private: boost::shared_ptr<DNSSDService> dnsSDService; boost::optional<DNSSDService::Service> selfService; + typedef std::map<DNSSDService::Service, DNSSDService::ResolveResult> ServiceMap; + ServiceMap services; }; } |