~ubuntu-branches/ubuntu/utopic/kdnssd-kf5/utopic

« back to all changes in this revision

Viewing changes to src/avahi-domainbrowser.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2014-07-09 17:40:10 UTC
  • Revision ID: package-import@ubuntu.com-20140709174010-42ibhh1t237wavds
Tags: upstream-5.0.0
ImportĀ upstreamĀ versionĀ 5.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "avahi-domainbrowser_p.h"
 
22
#include <QtCore/QSet>
 
23
#include <QtCore/QFile>
 
24
#include <QtCore/QIODevice>
 
25
#include <QtCore/QStandardPaths>
 
26
#include <avahi-common/defs.h>
 
27
#include "avahi_server_interface.h"
 
28
#include "domainbrowser.h"
 
29
#include "avahi_domainbrowser_interface.h"
 
30
 
 
31
namespace KDNSSD
 
32
{
 
33
 
 
34
DomainBrowser::DomainBrowser(DomainType type, QObject *parent) : QObject(parent), d(new DomainBrowserPrivate(type, this))
 
35
{}
 
36
 
 
37
DomainBrowser::~DomainBrowser()
 
38
{
 
39
    delete d;
 
40
}
 
41
 
 
42
void DomainBrowser::startBrowse()
 
43
{
 
44
    if (d->m_started) {
 
45
        return;
 
46
    }
 
47
    d->m_started = true;
 
48
    org::freedesktop::Avahi::Server s("org.freedesktop.Avahi", "/", QDBusConnection::systemBus());
 
49
    QDBusReply<QDBusObjectPath> rep = s.DomainBrowserNew(-1, -1, "", (d->m_type == Browsing) ?
 
50
                                      AVAHI_DOMAIN_BROWSER_BROWSE : AVAHI_DOMAIN_BROWSER_REGISTER, 0);
 
51
 
 
52
    if (!rep.isValid()) {
 
53
        return;
 
54
    }
 
55
    org::freedesktop::Avahi::DomainBrowser *b = new org::freedesktop::Avahi::DomainBrowser("org.freedesktop.Avahi", rep.value().path(),
 
56
            QDBusConnection::systemBus());
 
57
    connect(b, SIGNAL(ItemNew(int,int,QString,uint)), d, SLOT(gotNewDomain(int,int,QString,uint)));
 
58
    connect(b, SIGNAL(ItemRemove(int,int,QString,uint)), d, SLOT(gotRemoveDomain(int,int,QString,uint)));
 
59
    d->m_browser = b;
 
60
    if (d->m_type == Browsing) {
 
61
        QString domains_evar = qgetenv("AVAHI_BROWSE_DOMAINS");
 
62
        if (!domains_evar.isEmpty()) {
 
63
            QStringList edomains = domains_evar.split(':');
 
64
            Q_FOREACH (const QString &s, edomains) {
 
65
                d->gotNewDomain(-1, -1, s, 0);
 
66
            }
 
67
        }
 
68
        //FIXME: watch this file and restart browser if it changes
 
69
        QString confDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
 
70
        QFile domains_cfg(confDir + "/avahi/browse-domains");
 
71
        if (domains_cfg.open(QIODevice::ReadOnly | QIODevice::Text))
 
72
            while (!domains_cfg.atEnd()) {
 
73
                d->gotNewDomain(-1, -1, QString::fromUtf8(domains_cfg.readLine().data()).trimmed(), 0);
 
74
            }
 
75
 
 
76
    }
 
77
 
 
78
}
 
79
 
 
80
void DomainBrowserPrivate::gotNewDomain(int, int, const QString &domain, uint)
 
81
{
 
82
    QString decoded = DNSToDomain(domain);
 
83
    if (m_domains.contains(decoded)) {
 
84
        return;
 
85
    }
 
86
    m_domains += decoded;
 
87
    emit m_parent->domainAdded(decoded);
 
88
}
 
89
 
 
90
void DomainBrowserPrivate::gotRemoveDomain(int, int, const QString &domain, uint)
 
91
{
 
92
    QString decoded = DNSToDomain(domain);
 
93
    if (!m_domains.contains(decoded)) {
 
94
        return;
 
95
    }
 
96
    emit m_parent->domainRemoved(decoded);
 
97
    m_domains.remove(decoded);
 
98
}
 
99
 
 
100
QStringList DomainBrowser::domains() const
 
101
{
 
102
    return d->m_domains.values();
 
103
}
 
104
 
 
105
bool DomainBrowser::isRunning() const
 
106
{
 
107
    return d->m_started;
 
108
}
 
109
 
 
110
}
 
111
#include "moc_domainbrowser.cpp"
 
112
#include "moc_avahi-domainbrowser_p.cpp"