00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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"