responder.cpp

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2004 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 "responder.h"
00022 #include <qapplication.h>
00023 #include <qeventloop.h>
00024 #include <kstaticdeleter.h>
00025 #include <kidna.h>
00026 #include <kdebug.h>
00027 #include <avahi-qt3/qt-watch.h>
00028 
00029 
00030 namespace DNSSD
00031 {
00032 
00033 static KStaticDeleter<Responder> responder_sd;
00034 Responder* Responder::m_self = 0;
00035 
00036 void client_callback(AvahiClient *, AvahiClientState s, void* u) 
00037 {
00038     Responder *r = reinterpret_cast<Responder*>(u);    
00039     emit (r->stateChanged(s));
00040 }
00041 
00042 
00043 Responder::Responder()
00044 {
00045     int error;
00046     const AvahiPoll* poll = avahi_qt_poll_get();
00047 #ifdef AVAHI_API_0_6
00048     m_client = avahi_client_new(poll, AVAHI_CLIENT_IGNORE_USER_CONFIG,client_callback, this,  &error);
00049 #else
00050     m_client = avahi_client_new(poll, client_callback, this,  &error);
00051 #endif
00052     if (!m_client) kdWarning() << "Failed to create avahi client" << endl;
00053 }
00054  
00055 Responder::~Responder()
00056 {
00057     if (m_client) avahi_client_free(m_client);
00058 }
00059 
00060 Responder& Responder::self()
00061 {
00062     if (!m_self) responder_sd.setObject(m_self, new Responder);
00063     return *m_self;
00064 }
00065 
00066 void Responder::process()
00067 {
00068     qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
00069 }
00070 
00071 AvahiClientState Responder::state() const
00072 {
00073 #ifdef AVAHI_API_0_6
00074     return (m_client) ? (avahi_client_get_state(m_client)) : AVAHI_CLIENT_FAILURE;
00075 #else
00076     return (m_client) ? (avahi_client_get_state(m_client)) : AVAHI_CLIENT_DISCONNECTED;
00077 #endif
00078 }
00079 
00080 bool domainIsLocal(const QString& domain)
00081 {
00082     return domain.section('.',-1,-1).lower()=="local";
00083 }
00084 
00085 QCString domainToDNS(const QString &domain)
00086 {
00087     if (domainIsLocal(domain)) return domain.utf8();
00088         else return KIDNA::toAsciiCString(domain);
00089 }
00090 
00091 QString DNSToDomain(const char* domain)
00092 {
00093     if (domainIsLocal(domain)) return QString::fromUtf8(domain);
00094         else return KIDNA::toUnicode(domain);
00095 }
00096 
00097 
00098 }
00099 #include "responder.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys