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 "domainmodel.h" |
22 | #include "domainbrowser.h" |
23 | #include <qstringlist.h> |
24 | |
25 | namespace DNSSD |
26 | { |
27 | |
28 | struct DomainModelPrivate |
29 | { |
30 | DomainBrowser* m_browser; |
31 | }; |
32 | |
33 | |
34 | DomainModel::DomainModel(DomainBrowser* browser, QObject* parent) |
35 | : QAbstractItemModel(parent), d(new DomainModelPrivate) |
36 | { |
37 | d->m_browser=browser; |
38 | browser->setParent(this); |
39 | connect(browser, SIGNAL(domainAdded(QString)), this, |
40 | SIGNAL(layoutChanged())); |
41 | connect(browser, SIGNAL(domainRemoved(QString)), this, |
42 | SIGNAL(layoutChanged())); |
43 | browser->startBrowse(); |
44 | } |
45 | |
46 | DomainModel::~DomainModel() |
47 | { |
48 | delete d; |
49 | } |
50 | |
51 | int DomainModel::columnCount(const QModelIndex& parent) const |
52 | { |
53 | Q_UNUSED(parent); |
54 | return 1; |
55 | } |
56 | int DomainModel::rowCount(const QModelIndex& parent ) const |
57 | { |
58 | return (parent.isValid()) ? 0 : d->m_browser->domains().size(); |
59 | } |
60 | |
61 | QModelIndex DomainModel::parent(const QModelIndex& index ) const |
62 | { |
63 | Q_UNUSED(index); |
64 | return QModelIndex(); |
65 | } |
66 | |
67 | QModelIndex DomainModel::index(int row, int column, const QModelIndex& parent ) const |
68 | { |
69 | return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex(); |
70 | } |
71 | |
72 | bool DomainModel::hasIndex(int row, int column, const QModelIndex &parent) const |
73 | { |
74 | if (parent.isValid()) return false; |
75 | if (column!=0) return false; |
76 | if (row<0 || row>=rowCount(parent)) return false; |
77 | return true; |
78 | } |
79 | |
80 | QVariant DomainModel::data(const QModelIndex& index, int role ) const |
81 | { |
82 | if (!index.isValid()) return QVariant(); |
83 | if (!hasIndex(index.row(), index.column(), index.parent())) return QVariant(); |
84 | const QStringList domains=d->m_browser->domains(); |
85 | if (role==Qt::DisplayRole) return domains[index.row()]; |
86 | return QVariant(); |
87 | } |
88 | |
89 | } |
90 | |