fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / dnssd / mdnsd-remoteservice.cpp
blob8a9dc7a1e1255e1ca39356b26a2c6e0f65e489df
1 /* This file is part of the KDE project
3 * Copyright (C) 2004, 2005 Jakub Stachowski <qbast@go2.pl>
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.
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.
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.
21 #include <netinet/in.h>
22 #include <QtCore/QEventLoop>
23 #include <QtCore/QCoreApplication>
24 #include <kdebug.h>
25 #include "remoteservice.h"
26 #include "servicebase_p.h"
27 #include "mdnsd-responder.h"
28 #include "mdnsd-sdevent.h"
30 namespace DNSSD
32 void resolve_callback ( DNSServiceRef,
33 DNSServiceFlags,
34 uint32_t,
35 DNSServiceErrorType errorCode,
36 const char*,
37 const char *hosttarget,
38 uint16_t port,
39 uint16_t txtLen,
40 const unsigned char *txtRecord,
41 void *context
44 #define K_D RemoteServicePrivate* d=static_cast<RemoteServicePrivate*>(this->d)
46 class RemoteServicePrivate : public Responder, public ServiceBasePrivate
48 public:
49 RemoteServicePrivate(RemoteService* parent, const QString& name,const QString& type,const QString& domain) :
50 Responder(), ServiceBasePrivate(name, type, domain, QString(), 0), m_resolved(false), m_parent(parent)
52 bool m_resolved;
53 RemoteService* m_parent;
54 virtual void customEvent(QEvent* event);
57 RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
58 : ServiceBase(new RemoteServicePrivate(this, name, type, domain))
63 RemoteService::~RemoteService()
66 bool RemoteService::resolve()
68 K_D;
69 resolveAsync();
70 while (d->isRunning() && !d->m_resolved) d->process();
71 d->stop();
72 return d->m_resolved;
75 void RemoteService::resolveAsync()
77 K_D;
78 if (d->isRunning()) return;
79 d->m_resolved = false;
80 kDebug() << this << ":Starting resolve of : " << d->m_serviceName << " " << d->m_type << " " << d->m_domain << "\n";
81 DNSServiceRef ref;
82 if (DNSServiceResolve(&ref,0,0,d->m_serviceName.toUtf8(), d->m_type.toAscii().constData(),
83 domainToDNS(d->m_domain),(DNSServiceResolveReply)resolve_callback,reinterpret_cast<void*>(d))
84 == kDNSServiceErr_NoError) d->setRef(ref);
85 if (!d->isRunning()) emit resolved(false);
88 bool RemoteService::isResolved() const
90 K_D;
91 return d->m_resolved;
94 void RemoteServicePrivate::customEvent(QEvent* event)
96 if (event->type() == QEvent::User+SD_ERROR) {
97 stop();
98 m_resolved=false;
99 emit m_parent->resolved(false);
101 if (event->type() == QEvent::User+SD_RESOLVE) {
102 ResolveEvent* rev = static_cast<ResolveEvent*>(event);
103 m_hostName = rev->m_hostname;
104 m_port = rev->m_port;
105 m_textData = rev->m_txtdata;
106 m_resolved = true;
107 emit m_parent->resolved(true);
111 void RemoteService::virtual_hook(int, void*)
113 // BASE::virtual_hook(int, void*);
117 void resolve_callback ( DNSServiceRef,
118 DNSServiceFlags,
119 uint32_t,
120 DNSServiceErrorType errorCode,
121 const char*,
122 const char *hosttarget,
123 uint16_t port,
124 uint16_t txtLen,
125 const unsigned char *txtRecord,
126 void *context
129 QObject *obj = reinterpret_cast<QObject*>(context);
130 if (errorCode != kDNSServiceErr_NoError) {
131 ErrorEvent err;
132 QCoreApplication::sendEvent(obj, &err);
133 return;
135 char key[256];
136 int index=0;
137 unsigned char valueLen;
138 kDebug() << "Resolve callback\n";
139 QMap<QString,QByteArray> map;
140 const void *voidValue = 0;
141 while (TXTRecordGetItemAtIndex(txtLen,txtRecord,index++,256,key,&valueLen,
142 &voidValue) == kDNSServiceErr_NoError)
144 if (voidValue) map[QString::fromUtf8(key)]=QByteArray((const char*)voidValue,valueLen);
145 else map[QString::fromUtf8(key)].clear();
147 ResolveEvent rev(DNSToDomain(hosttarget),ntohs(port),map);
148 QCoreApplication::sendEvent(obj, &rev);
154 #include "remoteservice.moc"