fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / dnssd / avahi-domainbrowser.cpp
blobb60afc4734ea7b806ed5d68dddc5e5342aadef44
1 /* This file is part of the KDE project
3 * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "avahi-domainbrowser_p.h"
23 #include <QtCore/QSet>
24 #include <QtCore/QFile>
25 #include <QtCore/QIODevice>
26 #include <kstandarddirs.h>
27 #include <avahi-common/defs.h>
28 #include "avahi_server_interface.h"
29 #include "domainbrowser.h"
30 #include "avahi_domainbrowser_interface.h"
34 #ifndef KDE_USE_FINAL
35 Q_DECLARE_METATYPE(QList<QByteArray>)
36 #endif
37 namespace DNSSD
40 DomainBrowser::DomainBrowser(DomainType type, QObject *parent) : QObject(parent), d(new DomainBrowserPrivate(type,this))
43 DomainBrowser::~DomainBrowser()
45 delete d;
49 void DomainBrowser::startBrowse()
51 if (d->m_started) return;
52 d->m_started=true;
53 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
54 QDBusReply<QDBusObjectPath> rep=s.DomainBrowserNew(-1, -1, "", (d->m_type==Browsing) ?
55 AVAHI_DOMAIN_BROWSER_BROWSE : AVAHI_DOMAIN_BROWSER_REGISTER,0);
57 if (!rep.isValid()) return;
58 org::freedesktop::Avahi::DomainBrowser *b=new org::freedesktop::Avahi::DomainBrowser("org.freedesktop.Avahi",rep.value().path(),
59 QDBusConnection::systemBus());
60 connect(b,SIGNAL(ItemNew(int,int,const QString&,uint)),d, SLOT(gotNewDomain(int,int,const QString&, uint)));
61 connect(b,SIGNAL(ItemRemove(int,int,const QString&,uint)),d, SLOT(gotRemoveDomain(int,int,const QString&, uint)));
62 d->m_browser=b;
63 if (d->m_type==Browsing) {
64 QString domains_evar=qgetenv("AVAHI_BROWSE_DOMAINS");
65 if (!domains_evar.isEmpty()) {
66 QStringList edomains=domains_evar.split(':');
67 Q_FOREACH(const QString &s, edomains) d->gotNewDomain(-1,-1,s,0);
69 KStandardDirs dirs;
70 //FIXME: watch this file and restart browser if it changes
71 QFile domains_cfg(dirs.localxdgconfdir()+"/avahi/browse-domains");
72 if (domains_cfg.open(QIODevice::ReadOnly | QIODevice::Text))
73 while (!domains_cfg.atEnd()) d->gotNewDomain(-1,-1,QString::fromUtf8(domains_cfg.readLine().data()).trimmed(),0);
79 void DomainBrowserPrivate::gotNewDomain(int,int,const QString& domain,uint)
81 QString decoded=DNSToDomain(domain);
82 if (m_domains.contains(decoded)) return;
83 m_domains+=decoded;
84 emit m_parent->domainAdded(decoded);
87 void DomainBrowserPrivate::gotRemoveDomain(int,int,const QString& domain,uint)
89 QString decoded=DNSToDomain(domain);
90 if (!m_domains.contains(decoded)) return;
91 emit m_parent->domainRemoved(decoded);
92 m_domains.remove(decoded);
96 QStringList DomainBrowser::domains() const
98 return d->m_domains.values();
101 bool DomainBrowser::isRunning() const
103 return d->m_started;
108 #include "domainbrowser.moc"
109 #include "avahi-domainbrowser_p.moc"