summaryrefslogtreecommitdiffstats
path: root/dnssd/responder.cpp
blob: 0c5a07cfa335c897c8314e6518b6dae8c1e4fdd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This file is part of the KDE project
 *
 * Copyright (C) 2004 Jakub Stachowski <[email protected]>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "responder.h"
#include <tqapplication.h>
#include <tqeventloop.h>
#include <kstaticdeleter.h>
#include <kidna.h>
#include <kdebug.h>
#ifdef HAVE_DNSSD
#include <avahi-tqt/qt-watch.h>
#endif

namespace DNSSD
{

static KStaticDeleter<Responder> responder_sd;
Responder* Responder::m_self = 0;

#ifdef HAVE_DNSSD
void client_callback(AvahiClient *, AvahiClientState s, void* u)
{
    Responder *r = reinterpret_cast<Responder*>(u);
    emit (r->stateChanged(s));
}
#endif


Responder::Responder()
{
    int error;
#ifdef HAVE_DNSSD
    const AvahiPoll* poll = avahi_qt_poll_get();
#ifdef AVAHI_API_0_6
    m_client = avahi_client_new(poll, AVAHI_CLIENT_IGNORE_USER_CONFIG,client_callback, this,  &error);
#else
    m_client = avahi_client_new(poll, client_callback, this,  &error);
#endif
    if (!m_client) kdWarning() << "Failed to create avahi client" << endl;
#endif
}

Responder::~Responder()
{
#ifdef HAVE_DNSSD
    if (m_client) avahi_client_free(m_client);
#endif
}

Responder& Responder::self()
{
    if (!m_self) responder_sd.setObject(m_self, new Responder);
    return *m_self;
}

void Responder::process()
{
    tqApp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput);
}

#ifdef HAVE_DNSSD
AvahiClientState Responder::state() const
{
#ifdef AVAHI_API_0_6
	return (m_client) ? (avahi_client_get_state(m_client)) : AVAHI_CLIENT_FAILURE;
#else
	return (m_client) ? (avahi_client_get_state(m_client)) : AVAHI_CLIENT_DISCONNECTED;
#endif
}
#endif

bool domainIsLocal(const TQString& domain)
{
	return TQString(domain.section('.',-1,-1)).lower()=="local";
}

TQCString domainToDNS(const TQString &domain)
{
	if (domainIsLocal(domain)) return domain.utf8();
		else return KIDNA::toAsciiCString(domain);
}

TQString DNSToDomain(const char* domain)
{
	if (domainIsLocal(domain)) return TQString::fromUtf8(domain);
		else return KIDNA::toUnicode(domain);
}


}
#include "responder.moc"