1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21
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"
31
32
33
34#ifndef KDE_USE_FINAL
35Q_DECLARE_METATYPE(QList<QByteArray>)
36#endif
37namespace DNSSD
38{
39
40DomainBrowser::DomainBrowser(DomainType type, QObject *parent) : QObject(parent), d(new DomainBrowserPrivate(type,this))
41{}
42
43DomainBrowser::~DomainBrowser()
44{
45 delete d;
46}
47
48
49void DomainBrowser::startBrowse()
50{
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);
56
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,QString,uint)),d, SLOT(gotNewDomain(int,int,QString,uint)));
61 connect(b,SIGNAL(ItemRemove(int,int,QString,uint)),d, SLOT(gotRemoveDomain(int,int,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);
68 }
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);
74
75 }
76
77}
78
79void DomainBrowserPrivate::gotNewDomain(int,int,const QString& domain,uint)
80{
81 QString decoded=DNSToDomain(domain);
82 if (m_domains.contains(decoded)) return;
83 m_domains+=decoded;
84 emit m_parent->domainAdded(decoded);
85}
86
87void DomainBrowserPrivate::gotRemoveDomain(int,int,const QString& domain,uint)
88{
89 QString decoded=DNSToDomain(domain);
90 if (!m_domains.contains(decoded)) return;
91 emit m_parent->domainRemoved(decoded);
92 m_domains.remove(decoded);
93}
94
95
96QStringList DomainBrowser::domains() const
97{
98 return d->m_domains.values();
99}
100
101bool DomainBrowser::isRunning() const
102{
103 return d->m_started;
104}
105
106
107}
108#include "domainbrowser.moc"
109#include "avahi-domainbrowser_p.moc"
110