summaryrefslogtreecommitdiffstats
blob: 5a6ccfd7a7f80e5d1c2c2c945091227629ad981c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <boost/bind.hpp>
#include <iostream>

#include "Swiften/LinkLocal/LinkLocalRoster.h"
#include "Swiften/Network/HostAddress.h"

namespace Swift {

LinkLocalRoster::LinkLocalRoster(boost::shared_ptr<DNSSDService> service) : dnsSDService(service) {
	dnsSDService->onStopped.connect(boost::bind(&LinkLocalRoster::handleStopped, this, _1));
	dnsSDService->onServiceRegistered.connect(boost::bind(&LinkLocalRoster::handleServiceRegistered, this, _1));
	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));
}

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 LinkLocalServiceID& service, const DNSSDService::ResolveResult& resolveResult) const {
 return RosterItemPayload(getJIDForService(service), getRosterName(service, resolveResult), RosterItemPayload::Both);
}

String LinkLocalRoster::getRosterName(const LinkLocalServiceID& 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.getName();
}

JID LinkLocalRoster::getJIDForService(const LinkLocalServiceID& service) const {
	return JID(service.getName());
}

boost::shared_ptr<Presence> LinkLocalRoster::getPresence(const LinkLocalServiceID& 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 LinkLocalServiceID& service) {
	if (selfService && *selfService == service) {
		return;
	}
	dnsSDService->startResolvingService(service);
}

void LinkLocalRoster::handleServiceRemoved(const LinkLocalServiceID& service) {
	if (selfService && *selfService == service) {
		return;
	}
	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 LinkLocalServiceID& service, const DNSSDService::ResolveResult& result) {
	std::pair<ServiceMap::iterator, bool> r = services.insert(std::make_pair(service, result));
	if (r.second) {
		boost::shared_ptr<RosterPayload> roster(new RosterPayload());
		roster->addItem(getRosterItem(service, result));
		onRosterChanged(roster);
	}
	else {
		r.first->second = result;
	}
	onPresenceChanged(getPresence(service, result));
}

void LinkLocalRoster::handleServiceRegistered(const LinkLocalServiceID& service) {
	selfService = service;
}

void LinkLocalRoster::handleStopped(bool error) {
	std::cout << "DNSSDService stopped: " << error << std::endl;
}

bool LinkLocalRoster::hasItem(const JID& j) const {
	for(ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) {
		if (getJIDForService(i->first) == j) {
			return true;
		}
	}
	return false;
}

String LinkLocalRoster::getHostname(const JID& j) const {
	for(ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) {
		if (getJIDForService(i->first) == j) {
			return i->second.host;
		}
	}
	return "";
}

int LinkLocalRoster::getPort(const JID& j) const {
	for(ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) {
		if (getJIDForService(i->first) == j) {
			return i->second.port;
		}
	}
	return 0;
}

}