summaryrefslogtreecommitdiffstats
blob: d4851c8ace90ae87972c1603577acb53a2844dca (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
#include "Slimber/Cocoa/Slimber.h"

#include <boost/bind.hpp>

#include "Swiften/Base/foreach.h"
#include "Swiften/Application/Platform/PlatformApplication.h"
#include "Swiften/LinkLocal/LinkLocalService.h"
#include "Swiften/LinkLocal/LinkLocalServiceBrowser.h"
#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h"
#include "Slimber/Cocoa/Menulet.h"
#include "Slimber/Server.h"
#include "Slimber/FileVCardCollection.h"

using namespace Swift;

Slimber::Slimber() {
	dnsSDQuerier = boost::shared_ptr<BonjourQuerier>(new BonjourQuerier());
	dnsSDQuerier->start();

	linkLocalServiceBrowser = new LinkLocalServiceBrowser(dnsSDQuerier);
	linkLocalServiceBrowser->onServiceAdded.connect(
			boost::bind(&Slimber::handleServicesChanged, this));
	linkLocalServiceBrowser->onServiceRemoved.connect(
			boost::bind(&Slimber::handleServicesChanged, this));
	linkLocalServiceBrowser->onServiceChanged.connect(
			boost::bind(&Slimber::handleServicesChanged, this));
	linkLocalServiceBrowser->start();

	vCardCollection = new FileVCardCollection(
			PlatformApplication("Slimber").getSettingsDir());

	server = new Server(5222, 5562, linkLocalServiceBrowser, vCardCollection);
	server->onSelfConnected.connect(
			boost::bind(&Slimber::handleSelfConnected, this, _1));

	menulet = [[Menulet alloc] init];
	handleServicesChanged();
}

Slimber::~Slimber() {
	[menulet release];
	delete server;
	delete vCardCollection;
	linkLocalServiceBrowser->stop();
	delete linkLocalServiceBrowser;
	dnsSDQuerier->stop();
}

void Slimber::handleSelfConnected(bool b) {
	[menulet setSelfConnected: b];
}

void Slimber::handleServicesChanged() {
	NSMutableArray* names = [[NSMutableArray alloc] init];
	foreach(const LinkLocalService& service, linkLocalServiceBrowser->getServices()) {
		[names addObject: [NSString stringWithUTF8String: service.getDescription().getUTF8Data()]];
	}

	[menulet setUserNames: names];
	[names release];
}