• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

DNSSD

mdnsd-domainbrowser.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "mdnsd-domainbrowser_p.h"
00022 #include "domainbrowser.h"
00023 #include "settings.h"
00024 #include "remoteservice.h"
00025 #include "mdnsd-responder.h"
00026 #include "mdnsd-sdevent.h"
00027 
00028 #include <QtCore/QCoreApplication>
00029 #include <QtCore/QHash>
00030 #include <QtCore/QStringList>
00031 #include <QtDBus/QtDBus>
00032 
00033 namespace DNSSD
00034 {
00035 
00036 void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context);
00037 
00038 DomainBrowser::DomainBrowser(DomainType type, QObject *parent) : QObject(parent),d(new DomainBrowserPrivate(type,this))
00039 {
00040     d->m_domains = Configuration::domainList();
00041 
00042         // Those same names have to be used in the kcontrol module too.
00043         const QString dbusPath = "/libdnssd";
00044         const QString dbusInterface = "org.kde.DNSSD.DomainBrowser";
00045         QDBusConnection dbus = QDBusConnection::sessionBus();
00046         dbus.connect( QString(), dbusPath, dbusInterface, "domainListChanged", this, SLOT(domainListChanged()) );
00047 }
00048 
00049 DomainBrowser::~DomainBrowser()
00050 {
00051     delete d;
00052 }
00053 
00054 
00055 void DomainBrowser::startBrowse()
00056 {
00057     QStringList::const_iterator itEnd = d->m_domains.end();
00058     for (QStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) emit domainAdded(*it);
00059     if (d->isRunning()) return;
00060     DNSServiceRef ref;
00061     if (DNSServiceEnumerateDomains(&ref,(d->m_type==Browsing) ? kDNSServiceFlagsBrowseDomains:kDNSServiceFlagsBrowseDomains,
00062     0, domain_callback,reinterpret_cast<void*>(d)) == kDNSServiceErr_NoError) d->setRef(ref);
00063 }
00064 
00065 void DomainBrowserPrivate::customEvent(QEvent* event)
00066 {
00067     if (event->type()==QEvent::User+SD_ERROR) stop();
00068     if (event->type()==QEvent::User+SD_ADDREMOVE) {
00069         AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event);
00070         if (aev->m_op==AddRemoveEvent::Add) {
00071             //FIXME: check if domain name is not name+domain (there was some mdnsd weirdness)
00072                 if (m_domains.contains(aev->m_domain)) return;
00073             m_domains.append(aev->m_domain);
00074             emit m_parent->domainAdded(aev->m_domain);
00075         }
00076         else {
00077             m_domains.removeAll(aev->m_domain);
00078             emit m_parent->domainRemoved(aev->m_domain);
00079         }
00080     }
00081 }
00082 
00083 void DomainBrowserPrivate::domainListChanged()
00084 {
00085     bool was_running = m_running;
00086     m_running = false;
00087     if (was_running) {
00088         QStringList::const_iterator itEnd = m_domains.end();
00089         for (QStringList::const_iterator it=m_domains.begin(); it!=itEnd; ++it )
00090             emit m_parent->domainRemoved(*it);
00091     }
00092     m_domains.clear();
00093     // now reread configuration and add domains
00094     Configuration::self()->readConfig();
00095     m_domains = Configuration::domainList();
00096     // this will emit domainAdded() for every domain if necessary
00097     if (was_running) m_parent->startBrowse();
00098 }
00099 
00100 QStringList DomainBrowser::domains() const
00101 {
00102     return d->m_domains;
00103 }
00104 
00105 bool DomainBrowser::isRunning() const
00106 {
00107     return d->isRunning();
00108 }
00109 
00110 void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, const char *replyDomain, void *context)
00111 {
00112     QObject *obj = reinterpret_cast<QObject*>(context);
00113     if (errorCode != kDNSServiceErr_NoError) {
00114         ErrorEvent err;
00115         QCoreApplication::sendEvent(obj, &err);
00116     } else {
00117         // domain browser is supposed to return only _additional_ domains
00118         if (flags&kDNSServiceFlagsDefault) return;
00119         AddRemoveEvent arev((flags & kDNSServiceFlagsAdd) ? AddRemoveEvent::Add :
00120             AddRemoveEvent::Remove, QString(), QString(),
00121             DNSToDomain(replyDomain), !(flags & kDNSServiceFlagsMoreComing));
00122         QCoreApplication::sendEvent(obj, &arev);
00123     }
00124 }
00125 
00126 }
00127 
00128 
00129 
00130 #include "domainbrowser.moc"
00131 #include "mdnsd-domainbrowser_p.moc"

DNSSD

Skip menu "DNSSD"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal