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 <tqstringlist.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 #ifdef HAVE_DNSSD 00031 #ifdef AVAHI_API_0_6 00032 #include <avahi-client/lookup.h> 00033 #endif 00034 #endif 00035 00036 namespace DNSSD 00037 { 00038 00039 #ifdef HAVE_DNSSD 00040 #ifdef AVAHI_API_0_6 00041 void domains_callback(AvahiDomainBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain, 00042 AvahiLookupResultFlags, void* context); 00043 #else 00044 void domains_callback(AvahiDomainBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain, 00045 void* context); 00046 #endif 00047 #endif 00048 00049 00050 class DomainBrowserPrivate 00051 { 00052 public: 00053 #ifdef HAVE_DNSSD 00054 DomainBrowserPrivate(DomainBrowser* owner) : m_browseLAN(false), m_started(false), m_browser(0), m_owner(owner) {} 00055 #else 00056 DomainBrowserPrivate(DomainBrowser* owner) : m_browseLAN(false), m_started(false) {} 00057 #endif 00058 #ifdef HAVE_DNSSD 00059 ~DomainBrowserPrivate() { if (m_browser) avahi_domain_browser_free(m_browser); } 00060 #endif 00061 TQStringList m_domains; 00062 virtual void customEvent(TQCustomEvent* event); 00063 bool m_browseLAN; 00064 bool m_started; 00065 #ifdef HAVE_DNSSD 00066 AvahiDomainBrowser* m_browser; 00067 #endif 00068 DomainBrowser* m_owner; 00069 }; 00070 00071 void DomainBrowserPrivate::customEvent(TQCustomEvent* event) 00072 { 00073 if (event->type()==TQEvent::User+SD_ADDREMOVE) { 00074 AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event); 00075 if (aev->m_op==AddRemoveEvent::Add) m_owner->gotNewDomain(aev->m_domain); 00076 else m_owner->gotRemoveDomain(aev->m_domain); 00077 } 00078 } 00079 00080 00081 DomainBrowser::DomainBrowser(TQObject *parent) : TQObject(parent) 00082 { 00083 d = new DomainBrowserPrivate(this); 00084 d->m_domains = Configuration::domainList(); 00085 if (Configuration::browseLocal()) { 00086 d->m_domains+="local."; 00087 d->m_browseLAN=true; 00088 } 00089 connect(KApplication::kApplication(),TQT_SIGNAL(kipcMessage(int,int)),this, 00090 TQT_SLOT(domainListChanged(int,int))); 00091 } 00092 00093 DomainBrowser::DomainBrowser(const TQStringList& domains, bool recursive, TQObject *parent) : TQObject(parent) 00094 { 00095 d = new DomainBrowserPrivate(this); 00096 d->m_browseLAN = recursive; 00097 d->m_domains=domains; 00098 } 00099 00100 00101 DomainBrowser::~DomainBrowser() 00102 { 00103 delete d; 00104 } 00105 00106 00107 void DomainBrowser::startBrowse() 00108 { 00109 if (d->m_started) return; 00110 d->m_started=true; 00111 if (ServiceBrowser::isAvailable()!=ServiceBrowser::Working) return; 00112 TQStringList::const_iterator itEnd = d->m_domains.end(); 00113 for (TQStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) emit domainAdded(*it); 00114 #ifdef HAVE_DNSSD 00115 if (d->m_browseLAN) 00116 #ifdef AVAHI_API_0_6 00117 d->m_browser = avahi_domain_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 00118 "local.", AVAHI_DOMAIN_BROWSER_BROWSE, (AvahiLookupFlags)0, domains_callback, this); 00119 #else 00120 d->m_browser = avahi_domain_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 00121 "local.", AVAHI_DOMAIN_BROWSER_BROWSE, domains_callback, this); 00122 #endif 00123 #endif 00124 } 00125 00126 void DomainBrowser::gotNewDomain(const TQString& domain) 00127 { 00128 if (d->m_domains.contains(domain)) return; 00129 d->m_domains.append(domain); 00130 emit domainAdded(domain); 00131 } 00132 00133 void DomainBrowser::gotRemoveDomain(const TQString& domain) 00134 { 00135 d->m_domains.remove(domain); 00136 emit domainRemoved(domain); 00137 } 00138 00139 void DomainBrowser::domainListChanged(int message,int) 00140 { 00141 if (message!=KIPCDomainsChanged) return; 00142 00143 bool was_started = d->m_started; 00144 #ifdef HAVE_DNSSD 00145 if (d->m_browser) { 00146 avahi_domain_browser_free(d->m_browser); // LAN query 00147 d->m_browser=0; 00148 } 00149 #endif 00150 d->m_started = false; 00151 00152 // remove all domains and resolvers 00153 if (was_started) { 00154 TQStringList::const_iterator itEnd = d->m_domains.end(); 00155 for (TQStringList::const_iterator it=d->m_domains.begin(); it!=itEnd; ++it ) 00156 emit domainRemoved(*it); 00157 } 00158 d->m_domains.clear(); 00159 // now reread configuration and add domains 00160 Configuration::self()->readConfig(); 00161 d->m_browseLAN = Configuration::browseLocal(); 00162 d->m_domains = Configuration::domainList(); 00163 if (Configuration::browseLocal()) d->m_domains+="local"; 00164 // this will emit domainAdded() for every domain if necessary 00165 if (was_started) startBrowse(); 00166 } 00167 00168 const TQStringList& DomainBrowser::domains() const 00169 { 00170 return d->m_domains; 00171 } 00172 00173 bool DomainBrowser::isRunning() const 00174 { 00175 return d->m_started; 00176 } 00177 00178 void DomainBrowser::virtual_hook(int, void*) 00179 {} 00180 00181 #ifdef HAVE_DNSSD 00182 #ifdef AVAHI_API_0_6 00183 void domains_callback(AvahiDomainBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain, 00184 AvahiLookupResultFlags,void* context) 00185 #else 00186 void domains_callback(AvahiDomainBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain, 00187 void* context) 00188 #endif 00189 { 00190 TQObject *obj = reinterpret_cast<TQObject*>(context); 00191 AddRemoveEvent* arev=new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add : 00192 AddRemoveEvent::Remove, TQString::null, TQString::null, 00193 DNSToDomain(replyDomain)); 00194 TQApplication::postEvent(obj, arev); 00195 } 00196 #endif 00197 00198 } 00199 #include "domainbrowser.moc"