Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "mdnsd-domainbrowser_p.h"
22#include "domainbrowser.h"
23#include "settings.h"
24#include "remoteservice.h"
25#include "mdnsd-responder.h"
26#include "mdnsd-sdevent.h"
27
28#include <QtCore/QCoreApplication>
29#include <QtCore/QHash>
30#include <QtCore/QStringList>
31#include <QtDBus/QtDBus>
32
33namespace DNSSD
34{
35
36void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context);
37
38DomainBrowser::DomainBrowser(DomainType type, QObject *parent) : QObject(parent),d(new DomainBrowserPrivate(type,this))
39{
40 d->m_domains = Configuration::domainList();
41
42 // Those same names have to be used in the kcontrol module too.
43 const QString dbusPath = "/libdnssd";
44 const QString dbusInterface = "org.kde.DNSSD.DomainBrowser";
45 QDBusConnection dbus = QDBusConnection::sessionBus();
46 dbus.connect( QString(), dbusPath, dbusInterface, "domainListChanged", this, SLOT(domainListChanged()) );
47}
48
49DomainBrowser::~DomainBrowser()
50{
51 delete d;
52}
53
54
55void DomainBrowser::startBrowse()
56{
57 QStringList::const_iterator itEnd = d->m_domains.end();
58 for (QStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) emit domainAdded(*it);
59 if (d->isRunning()) return;
60 DNSServiceRef ref;
61 if (DNSServiceEnumerateDomains(&ref,(d->m_type==Browsing) ? kDNSServiceFlagsBrowseDomains:kDNSServiceFlagsBrowseDomains,
62 0, domain_callback,reinterpret_cast<void*>(d)) == kDNSServiceErr_NoError) d->setRef(ref);
63}
64
65void DomainBrowserPrivate::customEvent(QEvent* event)
66{
67 if (event->type()==QEvent::User+SD_ERROR) stop();
68 if (event->type()==QEvent::User+SD_ADDREMOVE) {
69 AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event);
70 if (aev->m_op==AddRemoveEvent::Add) {
71 //FIXME: check if domain name is not name+domain (there was some mdnsd weirdness)
72 if (m_domains.contains(aev->m_domain)) return;
73 m_domains.append(aev->m_domain);
74 emit m_parent->domainAdded(aev->m_domain);
75 }
76 else {
77 m_domains.removeAll(aev->m_domain);
78 emit m_parent->domainRemoved(aev->m_domain);
79 }
80 }
81}
82
83void DomainBrowserPrivate::domainListChanged()
84{
85 bool was_running = m_running;
86 m_running = false;
87 if (was_running) {
88 QStringList::const_iterator itEnd = m_domains.end();
89 for (QStringList::const_iterator it=m_domains.begin(); it!=itEnd; ++it )
90 emit m_parent->domainRemoved(*it);
91 }
92 m_domains.clear();
93 // now reread configuration and add domains
94 Configuration::self()->readConfig();
95 m_domains = Configuration::domainList();
96 // this will emit domainAdded() for every domain if necessary
97 if (was_running) m_parent->startBrowse();
98}
99
100QStringList DomainBrowser::domains() const
101{
102 return d->m_domains;
103}
104
105bool DomainBrowser::isRunning() const
106{
107 return d->isRunning();
108}
109
110void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context)
111{
112 QObject *obj = reinterpret_cast<QObject*>(context);
113 if (errorCode != kDNSServiceErr_NoError) {
114 ErrorEvent err;
115 QCoreApplication::sendEvent(obj, &err);
116 } else {
117 // domain browser is supposed to return only _additional_ domains
118 if (flags&kDNSServiceFlagsDefault) return;
119 AddRemoveEvent arev((flags & kDNSServiceFlagsAdd) ? AddRemoveEvent::Add :
120 AddRemoveEvent::Remove, QString(), QString(),
121 DNSToDomain(replyDomain), !(flags & kDNSServiceFlagsMoreComing));
122 QCoreApplication::sendEvent(obj, &arev);
123 }
124}
125
126}
127
128
129
130#include "domainbrowser.moc"
131#include "mdnsd-domainbrowser_p.moc"
132

Warning: That file was not part of the compilation database. It may have many parsing errors.