remoteservice.cpp

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2004, 2005 Jakub Stachowski <qbast@go2.pl>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <qeventloop.h>
00024 #include <qapplication.h>
00025 #include <kurl.h>
00026 #ifdef HAVE_SYS_TYPES_H
00027 #include <sys/types.h>
00028 #endif
00029 #include <netinet/in.h>
00030 #include "remoteservice.h"
00031 #include "responder.h"
00032 #include "sdevent.h"
00033 
00034 #include <avahi-client/client.h>
00035 #include <avahi-common/strlst.h>
00036 #ifdef AVAHI_API_0_6
00037 #include <avahi-client/lookup.h>
00038 #endif
00039 
00040 namespace DNSSD
00041 {
00042 #ifdef AVAHI_API_0_6
00043 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
00044     const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
00045     uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context);
00046 #else
00047 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
00048     const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
00049     uint16_t port, AvahiStringList* txt, void* context);
00050 
00051 #endif
00052 class RemoteServicePrivate : public Responder
00053 {
00054 public:
00055     RemoteServicePrivate() :  m_resolved(false), m_running(false), m_resolver(0) {}
00056     bool m_resolved;
00057     bool m_running;
00058     AvahiServiceResolver* m_resolver;
00059     void stop() {
00060         m_running = false;
00061         if (m_resolver) avahi_service_resolver_free(m_resolver);
00062         m_resolver=0;
00063     }
00064   
00065 };
00066 
00067 RemoteService::RemoteService(const QString& label)
00068 {
00069     decode(label);
00070     d =  new RemoteServicePrivate();
00071 }
00072 RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
00073         : ServiceBase(name, type, domain)
00074 {
00075     d = new RemoteServicePrivate();
00076 }
00077 
00078 RemoteService::RemoteService(const KURL& url)
00079 {
00080     d = new RemoteServicePrivate();
00081     if (!url.isValid()) return;
00082     if (url.protocol()!="invitation") return;
00083     if (!url.hasPath()) return;
00084     m_hostName = url.host();
00085     m_port = url.port();
00086     m_type = url.path().section('/',1,1);
00087     m_serviceName = url.path().section('/',2);
00088     m_textData = url.queryItems();
00089     d->m_resolved=true;
00090 }
00091 
00092 RemoteService::~RemoteService()
00093 {
00094     if (d->m_resolver) avahi_service_resolver_free(d->m_resolver);
00095     delete d;
00096 }
00097 
00098 bool RemoteService::resolve()
00099 {
00100     resolveAsync();
00101     while (d->m_running && !d->m_resolved) Responder::self().process();
00102     d->stop();
00103     return d->m_resolved;
00104 }
00105 
00106 void RemoteService::resolveAsync()
00107 {
00108     if (d->m_running) return;
00109     d->m_resolved = false;
00110     // FIXME: first protocol should be set?
00111 #ifdef AVAHI_API_0_6
00112     d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00113         m_serviceName.utf8(), m_type.ascii(), domainToDNS(m_domain), AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_ADDRESS,
00114         resolve_callback, this);
00115 #else
00116     d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00117         m_serviceName.utf8(), m_type.ascii(), m_domain.utf8(), AVAHI_PROTO_UNSPEC, resolve_callback, this);
00118 #endif
00119     if (d->m_resolver) d->m_running=true;
00120         else  emit resolved(false);
00121 }
00122 
00123 bool RemoteService::isResolved() const
00124 {
00125     return d->m_resolved;
00126 }
00127 
00128 void RemoteService::customEvent(QCustomEvent* event)
00129 {
00130     if (event->type() == QEvent::User+SD_ERROR) {
00131         d->stop();
00132         d->m_resolved=false;
00133         emit resolved(false);
00134     }
00135     if (event->type() == QEvent::User+SD_RESOLVE) {
00136         ResolveEvent* rev = static_cast<ResolveEvent*>(event);
00137         m_hostName = rev->m_hostname;
00138         m_port = rev->m_port;
00139         m_textData = rev->m_txtdata;
00140         d->m_resolved = true;
00141         emit resolved(true);
00142     }
00143 }
00144 
00145 void RemoteService::virtual_hook(int, void*)
00146 {
00147     // BASE::virtual_hook(int, void*);
00148 }
00149 
00150 QDataStream & operator<< (QDataStream & s, const RemoteService & a)
00151 {
00152     s << (static_cast<ServiceBase>(a));
00153     Q_INT8 resolved = a.d->m_resolved ? 1:0;
00154     s << resolved;
00155     return s;
00156 }
00157 
00158 QDataStream & operator>> (QDataStream & s, RemoteService & a)
00159 {
00160     // stop any possible resolve going on
00161     a.d->stop();
00162     Q_INT8 resolved;
00163     operator>>(s,(static_cast<ServiceBase&>(a)));
00164     s >> resolved;
00165     a.d->m_resolved = (resolved == 1);  
00166     return s;
00167 }
00168 
00169 #ifdef AVAHI_API_0_6
00170 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
00171     const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
00172     uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context)
00173 #else
00174 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
00175     const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
00176     uint16_t port, AvahiStringList* txt, void* context)
00177 #endif
00178    {
00179       QObject *obj = reinterpret_cast<QObject*>(context);
00180       if (e != AVAHI_RESOLVER_FOUND) 
00181       {
00182          ErrorEvent err;
00183          QApplication::sendEvent(obj, &err);    
00184          return;
00185       }
00186       
00187       QMap<QString,QString> map;
00188       while (txt) 
00189       {
00190          char *key, *value;
00191          size_t size;
00192          if (avahi_string_list_get_pair(txt,&key,&value,&size)) break;
00193          map[QString::fromUtf8(key)]=(value) ? QString::fromUtf8(value) : QString::null;
00194          txt = txt->next;
00195       }
00196       ResolveEvent rev(DNSToDomain(hostname),port,map);
00197       
00198       QApplication::sendEvent(obj, &rev);
00199    }
00200 }
00201 
00202 #include "remoteservice.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys