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