1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2008 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 "servicemodel.h"
22#include "servicebrowser.h"
23#include <klocale.h>
24
25namespace DNSSD
26{
27
28struct ServiceModelPrivate
29{
30 ServiceBrowser* m_browser;
31};
32
33
34ServiceModel::ServiceModel(ServiceBrowser* browser, QObject* parent)
35 : QAbstractItemModel(parent), d(new ServiceModelPrivate)
36{
37 d->m_browser=browser;
38 browser->setParent(this);
39 connect(browser, SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)), this,
40 SIGNAL(layoutChanged()));
41 connect(browser, SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)), this,
42 SIGNAL(layoutChanged()));
43 browser->startBrowse();
44}
45
46ServiceModel::~ServiceModel()
47{
48 delete d;
49}
50
51int ServiceModel::columnCount(const QModelIndex&) const
52{
53 return d->m_browser->isAutoResolving() ? 3 : 1;
54}
55int ServiceModel::rowCount(const QModelIndex& parent ) const
56{
57 return (parent.isValid()) ? 0 : d->m_browser->services().size();
58}
59
60QModelIndex ServiceModel::parent(const QModelIndex&) const
61{
62 return QModelIndex();
63}
64
65QModelIndex ServiceModel::index(int row, int column, const QModelIndex& parent ) const
66{
67 return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
68}
69
70bool ServiceModel::hasIndex(int row, int column, const QModelIndex &parent) const
71{
72 if (parent.isValid()) return false;
73 if (column<0 || column>=columnCount()) return false;
74 if (row<0 || row>=rowCount(parent)) return false;
75 return true;
76}
77
78QVariant ServiceModel::data(const QModelIndex& index, int role ) const
79{
80 if (!index.isValid()) return QVariant();
81 if (!hasIndex(index.row(), index.column(), index.parent())) return QVariant();
82 const QList<RemoteService::Ptr> srv=d->m_browser->services();
83 switch (role) {
84 case Qt::DisplayRole:
85 switch (index.column()) {
86 case ServiceName: return srv[index.row()]->serviceName();
87 case Host: return srv[index.row()]->hostName();
88 case Port: return srv[index.row()]->port();
89 }
90 case ServicePtrRole: QVariant ret;
91 ret.setValue(srv[index.row()]);
92 return ret;
93 }
94 return QVariant();
95}
96
97QVariant ServiceModel::headerData(int section, Qt::Orientation orientation, int role) const
98{
99 if (orientation!=Qt::Horizontal || role!=Qt::DisplayRole) return QVariant();
100 switch (section) {
101 case ServiceName: return i18n("Name");
102 case Host: return i18n("Host");
103 case Port: return i18n("Port");
104 }
105 return QVariant();
106}
107
108}
109