File: avahi-servicebrowser.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,316 kB
  • sloc: cpp: 761,810; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (193 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download | duplicates (5)
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
/* 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 "avahi-servicebrowser_p.h"
#include <QtCore/QStringList>
#include "servicebrowser.h"
#include "avahi_servicebrowser_interface.h"
#include "avahi_server_interface.h"
#include <QtCore/QHash>
#include <QtNetwork/QHostAddress>
#ifndef KDE_USE_FINAL
Q_DECLARE_METATYPE(QList<QByteArray>)
#endif
namespace DNSSD
{

ServiceBrowser::ServiceBrowser(const QString& type,bool autoResolve,const QString& domain, const QString& subtype)
	:d(new ServiceBrowserPrivate(this))
{
	d->m_type=type;
	d->m_subtype=subtype;
	d->m_autoResolve=autoResolve;
	d->m_domain=domain;
	d->m_timer.setSingleShot(true);
}

ServiceBrowser::State ServiceBrowser::isAvailable()
{
	org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
	QDBusReply<int> rep= s.GetState();
	return (rep.isValid() && rep.value()==2) ? Working:Stopped;
}
ServiceBrowser::~ServiceBrowser()
{
    delete d;
}

bool ServiceBrowser::isAutoResolving() const
{
    return d->m_autoResolve;
}

void ServiceBrowser::startBrowse()
{
	if (d->m_running) return;
	org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
	QString fullType=d->m_type;
	if (!d->m_subtype.isEmpty()) fullType=d->m_subtype+"._sub."+d->m_type;
	QDBusReply<QDBusObjectPath> rep=s.ServiceBrowserNew(-1, -1, fullType, domainToDNS(d->m_domain),0);
	
	if (!rep.isValid()) {
	    emit finished();
	    return;
	}
	d->m_running=true;
	d->m_browserFinished=true;
	org::freedesktop::Avahi::ServiceBrowser *b=new org::freedesktop::Avahi::ServiceBrowser("org.freedesktop.Avahi",rep.value().path(),
	    QDBusConnection::systemBus());
	connect(b,SIGNAL(ItemNew(int,int,QString,QString,QString,uint)),d,
	    SLOT(gotNewService(int,int,QString,QString,QString,uint)));
	connect(b,SIGNAL(ItemRemove(int,int,QString,QString,QString,uint)),d,
	    SLOT(gotRemoveService(int,int,QString,QString,QString,uint)));
	connect(b,SIGNAL(AllForNow()),d,SLOT(browserFinished()));
	d->m_browser=b;
	connect(&d->m_timer,SIGNAL(timeout()), d, SLOT(browserFinished()));
	d->m_timer.start(domainIsLocal(d->m_domain) ? TIMEOUT_LAST_SERVICE : TIMEOUT_START_WAN);
}

void ServiceBrowserPrivate::serviceResolved(bool success)
{
	QObject* sender_obj = const_cast<QObject*>(sender());
	RemoteService* svr = static_cast<RemoteService*>(sender_obj);
	disconnect(svr,SIGNAL(resolved(bool)),this,SLOT(serviceResolved(bool)));
	QList<RemoteService::Ptr>::Iterator it = m_duringResolve.begin();
	QList<RemoteService::Ptr>::Iterator itEnd = m_duringResolve.end();
	while ( it!= itEnd && svr!= (*it).data()) ++it;
	if (it != itEnd) {
		if (success) {
		  	m_services+=(*it);
			emit m_parent->serviceAdded(RemoteService::Ptr(svr));
		}
		m_duringResolve.erase(it);
		queryFinished();
	}
}

RemoteService::Ptr ServiceBrowserPrivate::find(RemoteService::Ptr s, const QList<RemoteService::Ptr>& where) const
{
    Q_FOREACH (const RemoteService::Ptr& i, where) if (*s==*i) return i;
    return RemoteService::Ptr();
}

void ServiceBrowserPrivate::gotNewService(int,int,const QString& name, const QString& type, const QString& domain, uint)
{
	m_timer.start(TIMEOUT_LAST_SERVICE);
	RemoteService::Ptr svr(new RemoteService(name, type,domain));
	if (m_autoResolve) {
		connect(svr.data(),SIGNAL(resolved(bool)),this,SLOT(serviceResolved(bool)));
		m_duringResolve+=svr;
		svr->resolveAsync();
	} else	{
		m_services+=svr;
		emit m_parent->serviceAdded(svr);
	}
}

void ServiceBrowserPrivate::gotRemoveService(int,int,const QString& name, const QString& type, const QString& domain, uint)
{
	m_timer.start(TIMEOUT_LAST_SERVICE);
	RemoteService::Ptr tmpl(new RemoteService(name, type,domain));
	RemoteService::Ptr found=find(tmpl, m_duringResolve);
	if (!found.isNull()) {
	    m_duringResolve.removeAll(found);
	    return;
	}
	found=find(tmpl, m_services);
	if (found.isNull()) return;
	
	emit m_parent->serviceRemoved(found);
	m_services.removeAll(found);
}
void ServiceBrowserPrivate::browserFinished()
{
    m_timer.stop();
    m_browserFinished=true;
    queryFinished();
}

void ServiceBrowserPrivate::queryFinished()
{
	if (!m_duringResolve.count() && m_browserFinished) emit m_parent->finished();
}


QList<RemoteService::Ptr> ServiceBrowser::services() const
{
	return d->m_services;
}

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

QHostAddress ServiceBrowser::resolveHostName(const QString &hostname)
{
	org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());

	int protocol = 0;
	QString name;
	int aprotocol = 0;
	QString address;
	uint flags = 0;

	QDBusReply<int> reply = s.ResolveHostName(-1, -1, hostname, 0, (unsigned int ) 0, protocol, name, aprotocol, address, flags);

	if (reply.isValid())
		return QHostAddress(address);
	else
		return QHostAddress();
}

QString ServiceBrowser::getLocalHostName()
{
	org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());

	QDBusReply<QString> reply = s.GetHostName();

	if (reply.isValid())
		return reply.value();
	else
		return QString();
}

}

#include "servicebrowser.moc"
#include "avahi-servicebrowser_p.moc"