summaryrefslogtreecommitdiffstats
path: root/dnssd/domainbrowser.cpp
blob: 78cc2783a296caa621f5619e6c27ddadf1b5b2aa (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* This file is part of the KDE project
 *
 * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <tqstringlist.h>
#include "domainbrowser.h"
#include "settings.h"
#include "sdevent.h"
#include "responder.h"
#include "remoteservice.h"
#include "query.h"
#include "servicebrowser.h"
#include <tdeapplication.h>
#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
#include <avahi-client/lookup.h>
#endif
#endif

namespace DNSSD
{

#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
void domains_callback(AvahiDomainBrowser*,  AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain,
     AvahiLookupResultFlags, void* context);
#else
void domains_callback(AvahiDomainBrowser*,  AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain,
     void* context);
#endif
#endif


class DomainBrowserPrivate
{
public:
#ifdef HAVE_DNSSD
	DomainBrowserPrivate(DomainBrowser* owner) : m_browseLAN(false), m_started(false), m_browser(0), m_owner(owner) {}
#else
	DomainBrowserPrivate(DomainBrowser* owner) : m_browseLAN(false), m_started(false) {}
#endif
#ifdef HAVE_DNSSD
	~DomainBrowserPrivate() { if (m_browser) avahi_domain_browser_free(m_browser); }
#endif
	TQStringList m_domains;
	virtual void customEvent(TQCustomEvent* event);
	bool m_browseLAN;
	bool m_started;
#ifdef HAVE_DNSSD
	AvahiDomainBrowser* m_browser;
#endif
	DomainBrowser* m_owner;
};

void DomainBrowserPrivate::customEvent(TQCustomEvent* event)
{
	if (event->type()==TQEvent::User+SD_ADDREMOVE) {
		AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event);
		if (aev->m_op==AddRemoveEvent::Add) m_owner->gotNewDomain(aev->m_domain);
			else m_owner->gotRemoveDomain(aev->m_domain);
	}
}


DomainBrowser::DomainBrowser(TQObject *parent) : TQObject(parent)
{
	d = new DomainBrowserPrivate(this);
 	d->m_domains = Configuration::domainList();
	if (Configuration::browseLocal()) {
		d->m_domains+="local.";
		d->m_browseLAN=true;
	}
 	connect(TDEApplication::kApplication(),TQ_SIGNAL(kipcMessage(int,int)),this,
 	        TQ_SLOT(domainListChanged(int,int)));
}

DomainBrowser::DomainBrowser(const TQStringList& domains, bool recursive, TQObject *parent) : TQObject(parent)
{
	d = new DomainBrowserPrivate(this);
	d->m_browseLAN = recursive;
	d->m_domains=domains;
}


DomainBrowser::~DomainBrowser()
{
	delete d;
}


void DomainBrowser::startBrowse()
{
	if (d->m_started) return;
	d->m_started=true;
	if (ServiceBrowser::isAvailable()!=ServiceBrowser::Working) return;
 	TQStringList::const_iterator itEnd = d->m_domains.end();
	for (TQStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) emit domainAdded(*it);
#ifdef HAVE_DNSSD
	if (d->m_browseLAN)
#ifdef AVAHI_API_0_6
	    d->m_browser = avahi_domain_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
		"local.", AVAHI_DOMAIN_BROWSER_BROWSE, (AvahiLookupFlags)0, domains_callback, this);
#else
	    d->m_browser = avahi_domain_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
		"local.", AVAHI_DOMAIN_BROWSER_BROWSE, domains_callback, this);
#endif
#endif
}

void DomainBrowser::gotNewDomain(const TQString& domain)
{
	if (d->m_domains.contains(domain)) return;
	d->m_domains.append(domain);
	emit domainAdded(domain);
}

void DomainBrowser::gotRemoveDomain(const TQString& domain)
{
	d->m_domains.remove(domain);
	emit domainRemoved(domain);
}

void DomainBrowser::domainListChanged(int message,int)
{
	if (message!=KIPCDomainsChanged) return;

	bool was_started = d->m_started;
#ifdef HAVE_DNSSD
	if (d->m_browser) {
	    avahi_domain_browser_free(d->m_browser);  // LAN query
	    d->m_browser=0;
	}
#endif
	d->m_started = false;

	// remove all domains and resolvers
	if (was_started) {
		TQStringList::const_iterator itEnd = d->m_domains.end();
		for (TQStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it )
			emit domainRemoved(*it);
	}
	d->m_domains.clear();
	// now reread configuration and add domains
	Configuration::self()->readConfig();
	d->m_browseLAN = Configuration::browseLocal();
	d->m_domains = Configuration::domainList();
	if (Configuration::browseLocal()) d->m_domains+="local";
	// this will emit domainAdded() for every domain if necessary
	if (was_started) startBrowse();
}

const TQStringList& DomainBrowser::domains() const
{
	return d->m_domains;
}

bool DomainBrowser::isRunning() const
{
	return d->m_started;
}

void DomainBrowser::virtual_hook(int, void*)
{}

#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
void domains_callback(AvahiDomainBrowser*,  AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain,
     AvahiLookupResultFlags,void* context)
#else
void domains_callback(AvahiDomainBrowser*,  AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain,
     void* context)
#endif
{
	TQObject *obj = reinterpret_cast<TQObject*>(context);
	AddRemoveEvent* arev=new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
			AddRemoveEvent::Remove, TQString::null, TQString::null,
			DNSToDomain(replyDomain));
		TQApplication::postEvent(obj, arev);
}
#endif

}
#include "domainbrowser.moc"