KDNSSD

mdnsd-domainbrowser.cpp
1 /*
2  This file is part of the KDE project
3 
4  SPDX-FileCopyrightText: 2004 Jakub Stachowski <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "mdnsd-domainbrowser_p.h"
10 #include "domainbrowser.h"
11 #include "remoteservice.h"
12 #include "mdnsd-responder.h"
13 #include "mdnsd-sdevent.h"
14 
15 #include <QCoreApplication>
16 #include <QHash>
17 #include <QStringList>
18 
19 namespace KDNSSD
20 {
21 
22 void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context);
23 
24 DomainBrowser::DomainBrowser(DomainType type, QObject *parent) : QObject(parent), d(new DomainBrowserPrivate(type, this))
25 {
26 }
27 
28 DomainBrowser::~DomainBrowser()
29 {
30  delete d;
31 }
32 
34 {
35  if (d->isRunning()) {
36  return;
37  }
38  DNSServiceRef ref;
39  if (DNSServiceEnumerateDomains(&ref, (d->m_type == Browsing) ? kDNSServiceFlagsBrowseDomains : kDNSServiceFlagsBrowseDomains,
40  0, domain_callback, reinterpret_cast<void *>(d)) == kDNSServiceErr_NoError) {
41  d->setRef(ref);
42  }
43 }
44 
45 void DomainBrowserPrivate::customEvent(QEvent *event)
46 {
47  if (event->type() == QEvent::User + SD_ERROR) {
48  stop();
49  }
50  if (event->type() == QEvent::User + SD_ADDREMOVE) {
51  AddRemoveEvent *aev = static_cast<AddRemoveEvent *>(event);
52  if (aev->m_op == AddRemoveEvent::Add) {
53  //FIXME: check if domain name is not name+domain (there was some mdnsd weirdness)
54  if (m_domains.contains(aev->m_domain)) {
55  return;
56  }
57  m_domains.append(aev->m_domain);
58  emit m_parent->domainAdded(aev->m_domain);
59  } else {
60  m_domains.removeAll(aev->m_domain);
61  emit m_parent->domainRemoved(aev->m_domain);
62  }
63  }
64 }
65 
67 {
68  return d->m_domains;
69 }
70 
71 bool DomainBrowser::isRunning() const
72 {
73  return d->isRunning();
74 }
75 
76 void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context)
77 {
78  QObject *obj = reinterpret_cast<QObject *>(context);
79  if (errorCode != kDNSServiceErr_NoError) {
80  ErrorEvent err;
81  QCoreApplication::sendEvent(obj, &err);
82  } else {
83  // domain browser is supposed to return only _additional_ domains
84  if (flags & kDNSServiceFlagsDefault) {
85  return;
86  }
87  AddRemoveEvent arev((flags & kDNSServiceFlagsAdd) ? AddRemoveEvent::Add :
88  AddRemoveEvent::Remove, QString(), QString(),
89  DNSToDomain(replyDomain), !(flags & kDNSServiceFlagsMoreComing));
90  QCoreApplication::sendEvent(obj, &arev);
91  }
92 }
93 
94 }
95 
96 #include "moc_domainbrowser.cpp"
bool isRunning() const
Whether the browsing has been started.
QEvent::Type type() const const
void startBrowse()
Starts browsing.
void ref()
virtual bool event(QEvent *e)
KGuiItem stop()
bool sendEvent(QObject *receiver, QEvent *event)
DomainBrowser(DomainType type, QObject *parent=nullptr)
Standard constructor.
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
QStringList domains() const
The current known list of domains of the requested DomainType.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 22:38:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.