1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "avahi-servicebrowser_p.h"
22#include <QtCore/QStringList>
23#include "servicebrowser.h"
24#include "avahi_servicebrowser_interface.h"
25#include "avahi_server_interface.h"
26#include <QtCore/QHash>
27#include <QtNetwork/QHostAddress>
28#ifndef KDE_USE_FINAL
29Q_DECLARE_METATYPE(QList<QByteArray>)
30#endif
31namespace DNSSD
32{
33
34ServiceBrowser::ServiceBrowser(const QString& type,bool autoResolve,const QString& domain, const QString& subtype)
35 :d(new ServiceBrowserPrivate(this))
36{
37 d->m_type=type;
38 d->m_subtype=subtype;
39 d->m_autoResolve=autoResolve;
40 d->m_domain=domain;
41 d->m_timer.setSingleShot(true);
42}
43
44ServiceBrowser::State ServiceBrowser::isAvailable()
45{
46 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
47 QDBusReply<int> rep= s.GetState();
48 return (rep.isValid() && rep.value()==2) ? Working:Stopped;
49}
50ServiceBrowser::~ServiceBrowser()
51{
52 delete d;
53}
54
55bool ServiceBrowser::isAutoResolving() const
56{
57 return d->m_autoResolve;
58}
59
60void ServiceBrowser::startBrowse()
61{
62 if (d->m_running) return;
63 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
64 QString fullType=d->m_type;
65 if (!d->m_subtype.isEmpty()) fullType=d->m_subtype+"._sub."+d->m_type;
66 QDBusReply<QDBusObjectPath> rep=s.ServiceBrowserNew(-1, -1, fullType, domainToDNS(d->m_domain),0);
67
68 if (!rep.isValid()) {
69 emit finished();
70 return;
71 }
72 d->m_running=true;
73 d->m_browserFinished=true;
74 org::freedesktop::Avahi::ServiceBrowser *b=new org::freedesktop::Avahi::ServiceBrowser("org.freedesktop.Avahi",rep.value().path(),
75 QDBusConnection::systemBus());
76 connect(b,SIGNAL(ItemNew(int,int,QString,QString,QString,uint)),d,
77 SLOT(gotNewService(int,int,QString,QString,QString,uint)));
78 connect(b,SIGNAL(ItemRemove(int,int,QString,QString,QString,uint)),d,
79 SLOT(gotRemoveService(int,int,QString,QString,QString,uint)));
80 connect(b,SIGNAL(AllForNow()),d,SLOT(browserFinished()));
81 d->m_browser=b;
82 connect(&d->m_timer,SIGNAL(timeout()), d, SLOT(browserFinished()));
83 d->m_timer.start(domainIsLocal(d->m_domain) ? TIMEOUT_LAST_SERVICE : TIMEOUT_START_WAN);
84}
85
86void ServiceBrowserPrivate::serviceResolved(bool success)
87{
88 QObject* sender_obj = const_cast<QObject*>(sender());
89 RemoteService* svr = static_cast<RemoteService*>(sender_obj);
90 disconnect(svr,SIGNAL(resolved(bool)),this,SLOT(serviceResolved(bool)));
91 QList<RemoteService::Ptr>::Iterator it = m_duringResolve.begin();
92 QList<RemoteService::Ptr>::Iterator itEnd = m_duringResolve.end();
93 while ( it!= itEnd && svr!= (*it).data()) ++it;
94 if (it != itEnd) {
95 if (success) {
96 m_services+=(*it);
97 emit m_parent->serviceAdded(RemoteService::Ptr(svr));
98 }
99 m_duringResolve.erase(it);
100 queryFinished();
101 }
102}
103
104RemoteService::Ptr ServiceBrowserPrivate::find(RemoteService::Ptr s, const QList<RemoteService::Ptr>& where) const
105{
106 Q_FOREACH (const RemoteService::Ptr& i, where) if (*s==*i) return i;
107 return RemoteService::Ptr();
108}
109
110void ServiceBrowserPrivate::gotNewService(int,int,const QString& name, const QString& type, const QString& domain, uint)
111{
112 m_timer.start(TIMEOUT_LAST_SERVICE);
113 RemoteService::Ptr svr(new RemoteService(name, type,domain));
114 if (m_autoResolve) {
115 connect(svr.data(),SIGNAL(resolved(bool)),this,SLOT(serviceResolved(bool)));
116 m_duringResolve+=svr;
117 svr->resolveAsync();
118 } else {
119 m_services+=svr;
120 emit m_parent->serviceAdded(svr);
121 }
122}
123
124void ServiceBrowserPrivate::gotRemoveService(int,int,const QString& name, const QString& type, const QString& domain, uint)
125{
126 m_timer.start(TIMEOUT_LAST_SERVICE);
127 RemoteService::Ptr tmpl(new RemoteService(name, type,domain));
128 RemoteService::Ptr found=find(tmpl, m_duringResolve);
129 if (!found.isNull()) {
130 m_duringResolve.removeAll(found);
131 return;
132 }
133 found=find(tmpl, m_services);
134 if (found.isNull()) return;
135
136 emit m_parent->serviceRemoved(found);
137 m_services.removeAll(found);
138}
139void ServiceBrowserPrivate::browserFinished()
140{
141 m_timer.stop();
142 m_browserFinished=true;
143 queryFinished();
144}
145
146void ServiceBrowserPrivate::queryFinished()
147{
148 if (!m_duringResolve.count() && m_browserFinished) emit m_parent->finished();
149}
150
151
152QList<RemoteService::Ptr> ServiceBrowser::services() const
153{
154 return d->m_services;
155}
156
157void ServiceBrowser::virtual_hook(int, void*)
158{}
159
160QHostAddress ServiceBrowser::resolveHostName(const QString &hostname)
161{
162 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
163
164 int protocol = 0;
165 QString name;
166 int aprotocol = 0;
167 QString address;
168 uint flags = 0;
169
170 QDBusReply<int> reply = s.ResolveHostName(-1, -1, hostname, 0, (unsigned int ) 0, protocol, name, aprotocol, address, flags);
171
172 if (reply.isValid())
173 return QHostAddress(address);
174 else
175 return QHostAddress();
176}
177
178QString ServiceBrowser::getLocalHostName()
179{
180 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
181
182 QDBusReply<QString> reply = s.GetHostName();
183
184 if (reply.isValid())
185 return reply.value();
186 else
187 return QString();
188}
189
190}
191
192#include "servicebrowser.moc"
193#include "avahi-servicebrowser_p.moc"
194