| # dnssd Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# domainbrowser.cpp

    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., 59 Temple Place - Suite 330,
    00018  * Boston, MA 02111-1307, USA.
    00019  */
    00020
    00021 #include <qstringlist.h>
    00022 #include "domainbrowser.h"
    00023 #include "settings.h"
    00024 #include "remoteservice.h"
    00025 #include "query.h"
    00026 #include <kapplication.h>
    00027
    00028 namespace DNSSD
    00029 {
    00030
    00031 class DomainBrowserPrivate
    00032 {
    00033 public:
    00034     QDict<Query> resolvers;
    00035     QStringList m_domains;
    00036     bool m_recursive;
    00037     bool m_running;
    00038 };
    00039
    00040 DomainBrowser::DomainBrowser(QObject *parent) : QObject(parent)
    00041 {
    00042     d = new DomainBrowserPrivate;
    00043     d->m_running = false;
    00044     d->resolvers.setAutoDelete(true);
    00045     d->m_recursive = Configuration::recursive();
    00046     d->m_domains = Configuration::domainList();
    00047     if (Configuration::browseLocal()) d->m_domains+="local.";
    00048     connect(KApplication::kApplication(),SIGNAL(kipcMessage(int,int)),this,
    00049             SLOT(domainListChanged(int,int)));
    00050 }
    00051
    00052 DomainBrowser::DomainBrowser(const QStringList& domains, bool recursive, QObject *parent) : QObject(parent)
    00053 {
    00054     d = new DomainBrowserPrivate;
    00055     d->m_recursive = recursive;
    00056     d->m_running = false;
    00057     d->resolvers.setAutoDelete(true);
    00058     d->m_domains=domains;
    00059 }
    00060
    00061
    00062 DomainBrowser::~DomainBrowser()
    00063 {
    00064     delete d;
    00065 }
    00066
    00067
    00068 void DomainBrowser::startBrowse()
    00069 {
    00070     if (d->m_running) return;
    00071     d->m_running=true;
    00072     QStringList::const_iterator itEnd = d->m_domains.end();
    00073     for (QStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) {
    00074         emit domainAdded(*it);
    00075         if (d->m_recursive) {
    00076             Query* b = new Query("b._dns-sd._udp",(*it));
    00077             connect(b,SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),this,
    00078                 SLOT(gotNewDomain(DNSSD::RemoteService::Ptr)));
    00079             connect(b,SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr )),this,
    00080                 SLOT(gotRemoveDomain(DNSSD::RemoteService::Ptr)));
    00081             b->startQuery();
    00082             d->resolvers.insert((*it),b);
    00083         }
    00084     }
    00085 }
    00086
    00087 void DomainBrowser::gotNewDomain(DNSSD::RemoteService::Ptr srv)
    00088 {
    00089     QString domain = srv->serviceName()+"."+srv->domain();
    00090     if (d->m_domains.contains(domain)) return;
    00091     d->m_domains.append(domain);
    00092     emit domainAdded(domain);
    00093     if (d->m_recursive && !d->resolvers[domain]) {
    00094         Query* b = new Query("b._dns-sd._udp",domain);
    00095         connect(b,SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),this,
    00096             SLOT(gotNewDomain(DNSSD::RemoteService::Ptr)));
    00097         connect(b,SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr )),this,
    00098             SLOT(gotRemoveDomain(DNSSD::RemoteService::Ptr)));
    00099         b->startQuery();
    00100         d->resolvers.insert(domain,b);
    00101     }
    00102 }
    00103
    00104 void DomainBrowser::gotRemoveDomain(DNSSD::RemoteService::Ptr srv)
    00105 {
    00106     QString domain = srv->serviceName()+"."+srv->domain();
    00107     d->m_domains.remove(domain);
    00108     emit domainRemoved(domain);
    00109     d->resolvers.remove(domain);
    00110 }
    00111
    00112 void DomainBrowser::domainListChanged(int message,int)
    00113 {
    00114     if (message!=KIPCDomainsChanged) return;
    00115     bool was_running = d->m_running;
    00116     d->m_running = false;
    00117     // remove all domains and resolvers
    00118     d->resolvers.clear();
    00119     if (was_running) {
    00120         QStringList::const_iterator itEnd = d->m_domains.end();
    00121         for (QStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it )
    00122             emit domainRemoved(*it);
    00123     }
    00124     d->m_domains.clear();
    00125     // now reread configuration and add domains
    00126     Configuration::self()->readConfig();
    00127     d->m_recursive = Configuration::recursive();
    00128     d->m_domains = Configuration::domainList();
    00129     if (Configuration::browseLocal()) d->m_domains+="local.";
    00130     // this will emit domainAdded() for every domain if necessary
    00131     if (was_running) startBrowse();
    00132 }
    00133
    00134 const QStringList& DomainBrowser::domains() const
    00135 {
    00136     return d->m_domains;
    00137 }
    00138
    00139 bool DomainBrowser::isRunning() const
    00140 {
    00141     return d->m_running;
    00142 }
    00143
    00144 void DomainBrowser::virtual_hook(int, void*)
    00145 {}
    00146
    00147 }
    00148 #include "domainbrowser.moc"

This file is part of the documentation for dnssd Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:28:17 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003