1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2004, 2005 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-remoteservice_p.h"
22#include <netinet/in.h>
23#include <QtCore/QEventLoop>
24#include <QtCore/QCoreApplication>
25#include <kdebug.h>
26#include "remoteservice.h"
27#include "avahi_server_interface.h"
28#include "avahi_serviceresolver_interface.h"
29#ifndef KDE_USE_FINAL
30Q_DECLARE_METATYPE(QList<QByteArray>)
31#endif
32namespace DNSSD
33{
34
35RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
36 : ServiceBase(new RemoteServicePrivate(this, name,type,domain))
37{
38}
39
40
41RemoteService::~RemoteService()
42{
43}
44
45bool RemoteService::resolve()
46{
47 K_D;
48 resolveAsync();
49 while (d->m_running && !d->m_resolved) QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
50 return d->m_resolved;
51}
52
53void RemoteService::resolveAsync()
54{
55 K_D;
56 if (d->m_running) return;
57 d->m_resolved = false;
58 registerTypes();
59 kDebug() << this << ":Starting resolve of : " << d->m_serviceName << " " << d->m_type << " " << d->m_domain << "\n";
60 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
61 //FIXME: don't use LOOKUP_NO_ADDRESS if NSS unavailable
62 QDBusReply<QDBusObjectPath> rep=s.ServiceResolverNew(-1, -1, d->m_serviceName, d->m_type,
63 domainToDNS(d->m_domain), -1, 8 /*AVAHI_LOOKUP_NO_ADDRESS*/);
64 if (!rep.isValid()) {
65 emit resolved(false);
66 return;
67 }
68
69 org::freedesktop::Avahi::ServiceResolver *b=new org::freedesktop::Avahi::ServiceResolver("org.freedesktop.Avahi",rep.value().path(),
70 QDBusConnection::systemBus());
71 connect(b,SIGNAL(Found(int,int,const QString &,const QString &,const QString &,const QString &, int, const QString &,ushort,
72 const QList<QByteArray>&, uint)),d, SLOT(gotFound(int,int,const QString &,const QString &,const QString &,const QString &,
73 int, const QString &,ushort , const QList<QByteArray>&, uint)));
74 connect(b,SIGNAL(Failure(QString)),d, SLOT(gotError()));
75 d->m_running=true;
76}
77
78bool RemoteService::isResolved() const
79{
80 K_D;
81 return d->m_resolved;
82}
83
84void RemoteServicePrivate::gotError()
85{
86 m_resolved=false;
87 stop();
88
89 emit m_parent->resolved(false);
90}
91
92void RemoteServicePrivate::gotFound(int, int, const QString &name, const QString &, const QString &domain, const QString &host, int, const QString &, ushort port, const QList<QByteArray> &txt, uint)
93{
94 m_serviceName = name;
95 m_hostName = host;
96 m_port = port;
97 m_domain=DNSToDomain(domain);
98 Q_FOREACH(const QByteArray &x, txt) {
99 int pos=x.indexOf("=");
100 if (pos==-1) m_textData[x]=QByteArray();
101 else m_textData[x.mid(0,pos)]=x.mid(pos+1,x.size()-pos);
102 }
103 m_resolved = true;
104 emit m_parent->resolved(true);
105}
106
107void RemoteServicePrivate::stop()
108{
109 if (m_resolver) m_resolver->Free();
110 delete m_resolver;
111 m_resolver=0;
112 m_running=false;
113}
114
115void RemoteService::virtual_hook(int, void*)
116{
117 // BASE::virtual_hook(int, void*);
118}
119
120
121}
122
123#include "remoteservice.moc"
124#include "avahi-remoteservice_p.moc"
125