DNSSD
responder.cpp
Go to the documentation of this file.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 <kidna.h>
00024
00025
00026
00027 #define IDN_BROKEN_IN_MDNSRESPONDER
00028
00029 namespace DNSSD
00030 {
00031
00032 Responder::Responder(DNSServiceRef ref,QObject *parent, const char *name)
00033 : QObject(parent, name), m_ref(0), m_socket(0)
00034 {
00035 setRef(ref);
00036 }
00037
00038 void Responder::setRef(DNSServiceRef ref)
00039 {
00040 if (m_socket || m_ref) stop();
00041 m_running = false;
00042 m_ref = ref;
00043 if (m_ref == 0 ) return;
00044 #ifdef HAVE_DNSSD
00045 int fd = DNSServiceRefSockFD(ref);
00046 if (fd == -1) return;
00047 m_socket = new QSocketNotifier(fd,QSocketNotifier::Read,this);
00048 connect(m_socket,SIGNAL(activated(int)),this,SLOT(process()));
00049 m_running = true;
00050 #endif
00051 }
00052 Responder::~Responder()
00053 {
00054 stop();
00055 }
00056
00057 void Responder::stop()
00058 {
00059 if (m_socket) delete m_socket;
00060 m_socket = 0;
00061 #ifdef HAVE_DNSSD
00062 if (m_ref) DNSServiceRefDeallocate(m_ref);
00063 #endif
00064 m_ref = 0;
00065 m_running = false;
00066 }
00067
00068
00069 void Responder::process()
00070 {
00071 #ifdef HAVE_DNSSD
00072 if ( DNSServiceProcessResult(m_ref) != kDNSServiceErr_NoError) stop();
00073 #endif
00074 }
00075
00076 bool Responder::isRunning() const
00077 {
00078 return m_running;
00079 }
00080
00081 bool domainIsLocal(const QString& domain)
00082 {
00083 return domain.section('.',-1,-1).lower()=="local";
00084 }
00085
00086 QCString domainToDNS(const QString &domain)
00087 {
00088 #ifdef IDN_BROKEN_IN_MDNSRESPONDER
00089 if (domainIsLocal(domain)) return domain.utf8();
00090 else return KIDNA::toAsciiCString(domain);
00091 #else
00092 return domain.utf8();
00093 #endif
00094 }
00095
00096 QString DNSToDomain(const char* domain)
00097 {
00098 #ifdef IDN_BROKEN_IN_MDNSRESPONDER
00099 if (domainIsLocal(domain)) return QString::fromUtf8(domain);
00100 else return KIDNA::toUnicode(domain);
00101 #else
00102 return QString::fromUtf8(domain);
00103 #endif
00104 }
00105
00106
00107 }
00108 #include "responder.moc"