KDNSSD

mdnsd-domainbrowser.cpp
1/*
2 This file is part of the KDE project
3
4 SPDX-FileCopyrightText: 2004 Jakub Stachowski <qbast@go2.pl>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "domainbrowser.h"
10#include "mdnsd-domainbrowser_p.h"
11#include "mdnsd-responder.h"
12#include "mdnsd-sdevent.h"
13#include "remoteservice.h"
14
15#include <QCoreApplication>
16#include <QHash>
17#include <QStringList>
18
19namespace KDNSSD
20{
21void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context);
22
23DomainBrowser::DomainBrowser(DomainType type, QObject *parent)
24 : QObject(parent)
25 , d(new DomainBrowserPrivate(type, this))
26{
27}
28
29DomainBrowser::~DomainBrowser() = default;
30
31void DomainBrowser::startBrowse()
32{
33 Q_D(DomainBrowser);
34 if (d->isRunning()) {
35 return;
36 }
37 DNSServiceRef ref;
38 if (DNSServiceEnumerateDomains(&ref,
39 (d->m_type == Browsing) ? kDNSServiceFlagsBrowseDomains : kDNSServiceFlagsBrowseDomains,
40 0,
41 domain_callback,
42 reinterpret_cast<void *>(d))
43 == kDNSServiceErr_NoError) {
44 d->setRef(ref);
45 }
46}
47
48void DomainBrowserPrivate::customEvent(QEvent *event)
49{
50 if (event->type() == QEvent::User + SD_ERROR) {
51 stop();
52 }
53 if (event->type() == QEvent::User + SD_ADDREMOVE) {
54 AddRemoveEvent *aev = static_cast<AddRemoveEvent *>(event);
55 if (aev->m_op == AddRemoveEvent::Add) {
56 // FIXME: check if domain name is not name+domain (there was some mdnsd weirdness)
57 if (m_domains.contains(aev->m_domain)) {
58 return;
59 }
60 m_domains.append(aev->m_domain);
61 Q_EMIT m_parent->domainAdded(aev->m_domain);
62 } else {
63 m_domains.removeAll(aev->m_domain);
64 Q_EMIT m_parent->domainRemoved(aev->m_domain);
65 }
66 }
67}
68
69QStringList DomainBrowser::domains() const
70{
71 Q_D(const DomainBrowser);
72 return d->m_domains;
73}
74
75bool DomainBrowser::isRunning() const
76{
77 Q_D(const DomainBrowser);
78 return d->isRunning();
79}
80
81void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context)
82{
83 QObject *obj = reinterpret_cast<QObject *>(context);
84 if (errorCode != kDNSServiceErr_NoError) {
85 ErrorEvent err;
87 } else {
88 // domain browser is supposed to return only _additional_ domains
89 if (flags & kDNSServiceFlagsDefault) {
90 return;
91 }
92 AddRemoveEvent arev((flags & kDNSServiceFlagsAdd) ? AddRemoveEvent::Add : AddRemoveEvent::Remove,
93 QString(),
94 QString(),
95 DNSToDomain(replyDomain),
96 !(flags & kDNSServiceFlagsMoreComing));
98 }
99}
100
101}
102
103#include "moc_domainbrowser.cpp"
104#include "moc_mdnsd-domainbrowser_p.cpp"
DomainBrowser(DomainType type, QObject *parent=nullptr)
Standard constructor.
void stop(Ekos::AlignState mode)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Type type(const QSqlDatabase &db)
bool sendEvent(QObject *receiver, QEvent *event)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.