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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <qstringlist.h> 00022 #include "domainbrowser.h" 00023 #include "settings.h" 00024 #include "sdevent.h" 00025 #include "responder.h" 00026 #include "remoteservice.h" 00027 #include "query.h" 00028 #include "servicebrowser.h" 00029 #include <kapplication.h> 00030 00031 namespace DNSSD 00032 { 00033 00034 #ifdef HAVE_DNSSD 00035 void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, 00036 const char *replyDomain, void *context); 00037 #endif 00038 00039 class DomainBrowserPrivate : public Responder 00040 { 00041 public: 00042 DomainBrowserPrivate(DomainBrowser* owner) : Responder(), m_browseLAN(false), m_started(false), m_owner(owner) {} 00043 QStringList m_domains; 00044 virtual void customEvent(QCustomEvent* event); 00045 bool m_browseLAN; 00046 bool m_started; 00047 DomainBrowser* m_owner; 00048 }; 00049 00050 void DomainBrowserPrivate::customEvent(QCustomEvent* event) 00051 { 00052 if (event->type()==QEvent::User+SD_ERROR) stop(); 00053 if (event->type()==QEvent::User+SD_ADDREMOVE) { 00054 AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event); 00055 if (aev->m_op==AddRemoveEvent::Add) m_owner->gotNewDomain(aev->m_domain); 00056 else m_owner->gotRemoveDomain(aev->m_domain); 00057 } 00058 } 00059 00060 DomainBrowser::DomainBrowser(QObject *parent) : QObject(parent) 00061 { 00062 d = new DomainBrowserPrivate(this); 00063 d->m_domains = Configuration::domainList(); 00064 if (Configuration::browseLocal()) { 00065 d->m_domains+="local."; 00066 d->m_browseLAN=true; 00067 } 00068 connect(KApplication::kApplication(),SIGNAL(kipcMessage(int,int)),this, 00069 SLOT(domainListChanged(int,int))); 00070 } 00071 00072 DomainBrowser::DomainBrowser(const QStringList& domains, bool recursive, QObject *parent) : QObject(parent) 00073 { 00074 d = new DomainBrowserPrivate(this); 00075 d->m_browseLAN = recursive; 00076 d->m_domains=domains; 00077 } 00078 00079 00080 DomainBrowser::~DomainBrowser() 00081 { 00082 delete d; 00083 } 00084 00085 00086 void DomainBrowser::startBrowse() 00087 { 00088 if (d->m_started) return; 00089 d->m_started=true; 00090 if (ServiceBrowser::isAvailable()!=ServiceBrowser::Working) return; 00091 QStringList::const_iterator itEnd = d->m_domains.end(); 00092 for (QStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) emit domainAdded(*it); 00093 #ifdef HAVE_DNSSD 00094 if (d->m_browseLAN) { 00095 DNSServiceRef ref; 00096 if (DNSServiceEnumerateDomains(&ref,kDNSServiceFlagsBrowseDomains,0,domain_callback, 00097 reinterpret_cast<void*>(d))==kDNSServiceErr_NoError) d->setRef(ref); 00098 } 00099 #endif 00100 } 00101 00102 void DomainBrowser::gotNewDomain(const QString& domain) 00103 { 00104 if (d->m_domains.contains(domain)) return; 00105 d->m_domains.append(domain); 00106 emit domainAdded(domain); 00107 } 00108 00109 void DomainBrowser::gotRemoveDomain(const QString& domain) 00110 { 00111 d->m_domains.remove(domain); 00112 emit domainRemoved(domain); 00113 } 00114 00115 void DomainBrowser::domainListChanged(int message,int) 00116 { 00117 if (message!=KIPCDomainsChanged) return; 00118 bool was_started = d->m_started; 00119 if (d->isRunning()) d->stop(); // LAN query 00120 d->m_started = false; 00121 // remove all domains and resolvers 00122 if (was_started) { 00123 QStringList::const_iterator itEnd = d->m_domains.end(); 00124 for (QStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) 00125 emit domainRemoved(*it); 00126 } 00127 d->m_domains.clear(); 00128 // now reread configuration and add domains 00129 Configuration::self()->readConfig(); 00130 d->m_browseLAN = Configuration::browseLocal(); 00131 d->m_domains = Configuration::domainList(); 00132 if (Configuration::browseLocal()) d->m_domains+="local."; 00133 // this will emit domainAdded() for every domain if necessary 00134 if (was_started) startBrowse(); 00135 } 00136 00137 const QStringList& DomainBrowser::domains() const 00138 { 00139 return d->m_domains; 00140 } 00141 00142 bool DomainBrowser::isRunning() const 00143 { 00144 return d->m_started; 00145 } 00146 00147 void DomainBrowser::virtual_hook(int, void*) 00148 {} 00149 00150 #ifdef HAVE_DNSSD 00151 void domain_callback(DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode, 00152 const char *replyDomain, void *context) 00153 { 00154 QObject *obj = reinterpret_cast<QObject*>(context); 00155 if (errorCode != kDNSServiceErr_NoError) { 00156 ErrorEvent err; 00157 QApplication::sendEvent(obj, &err); 00158 } else { 00159 AddRemoveEvent arev((flags & kDNSServiceFlagsAdd) ? AddRemoveEvent::Add : 00160 AddRemoveEvent::Remove, QString::null, QString::null, 00161 DNSToDomain(replyDomain), !(flags & kDNSServiceFlagsMoreComing)); 00162 QApplication::sendEvent(obj, &arev); 00163 } 00164 } 00165 #endif 00166 00167 } 00168 #include "domainbrowser.moc"