diff options
Diffstat (limited to 'Swiften/LinkLocal/LinkLocalRoster.cpp')
-rw-r--r-- | Swiften/LinkLocal/LinkLocalRoster.cpp | 81 |
1 files changed, 68 insertions, 13 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; } |