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