responder.cpp00001
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
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"
|