| 1 | /* |
| 2 | This file is part of the KDE project |
| 3 | |
| 4 | SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "servicemodel.h" |
| 10 | #include "servicebrowser.h" |
| 11 | |
| 12 | namespace KDNSSD |
| 13 | { |
| 14 | struct ServiceModelPrivate { |
| 15 | ServiceBrowser *m_browser; |
| 16 | }; |
| 17 | |
| 18 | ServiceModel::ServiceModel(ServiceBrowser *browser, QObject *parent) |
| 19 | : QAbstractItemModel(parent) |
| 20 | , d(new ServiceModelPrivate) |
| 21 | { |
| 22 | d->m_browser = browser; |
| 23 | browser->setParent(this); |
| 24 | connect(sender: browser, SIGNAL(serviceAdded(KDNSSD::RemoteService::Ptr)), receiver: this, SIGNAL(layoutChanged())); |
| 25 | connect(sender: browser, SIGNAL(serviceRemoved(KDNSSD::RemoteService::Ptr)), receiver: this, SIGNAL(layoutChanged())); |
| 26 | browser->startBrowse(); |
| 27 | } |
| 28 | |
| 29 | ServiceModel::~ServiceModel() = default; |
| 30 | |
| 31 | int ServiceModel::columnCount(const QModelIndex &) const |
| 32 | { |
| 33 | return d->m_browser->isAutoResolving() ? 3 : 1; |
| 34 | } |
| 35 | int ServiceModel::rowCount(const QModelIndex &parent) const |
| 36 | { |
| 37 | return (parent.isValid()) ? 0 : d->m_browser->services().size(); |
| 38 | } |
| 39 | |
| 40 | QModelIndex ServiceModel::parent(const QModelIndex &) const |
| 41 | { |
| 42 | return QModelIndex(); |
| 43 | } |
| 44 | |
| 45 | QModelIndex ServiceModel::index(int row, int column, const QModelIndex &parent) const |
| 46 | { |
| 47 | return hasIndex(row, column, parent) ? createIndex(arow: row, acolumn: column) : QModelIndex(); |
| 48 | } |
| 49 | |
| 50 | bool ServiceModel::hasIndex(int row, int column, const QModelIndex &parent) const |
| 51 | { |
| 52 | if (parent.isValid()) { |
| 53 | return false; |
| 54 | } |
| 55 | if (column < 0 || column >= columnCount()) { |
| 56 | return false; |
| 57 | } |
| 58 | if (row < 0 || row >= rowCount(parent)) { |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | QVariant ServiceModel::data(const QModelIndex &index, int role) const |
| 65 | { |
| 66 | if (!index.isValid()) { |
| 67 | return QVariant(); |
| 68 | } |
| 69 | if (!hasIndex(row: index.row(), column: index.column(), parent: index.parent())) { |
| 70 | return QVariant(); |
| 71 | } |
| 72 | const QList<RemoteService::Ptr> srv = d->m_browser->services(); |
| 73 | switch ((uint)role) { |
| 74 | case Qt::DisplayRole: |
| 75 | switch (index.column()) { |
| 76 | case ServiceName: |
| 77 | return srv[index.row()]->serviceName(); |
| 78 | case Host: |
| 79 | return srv[index.row()]->hostName(); |
| 80 | case Port: |
| 81 | return srv[index.row()]->port(); |
| 82 | } |
| 83 | break; |
| 84 | case ServicePtrRole: |
| 85 | QVariant ret; |
| 86 | ret.setValue(srv[index.row()]); |
| 87 | return ret; |
| 88 | } |
| 89 | return QVariant(); |
| 90 | } |
| 91 | |
| 92 | QVariant ServiceModel::(int section, Qt::Orientation orientation, int role) const |
| 93 | { |
| 94 | if (orientation != Qt::Horizontal || role != Qt::DisplayRole) { |
| 95 | return QVariant(); |
| 96 | } |
| 97 | switch (section) { |
| 98 | case ServiceName: |
| 99 | return tr(s: "Name" ); |
| 100 | case Host: |
| 101 | return tr(s: "Host" ); |
| 102 | case Port: |
| 103 | return tr(s: "Port" ); |
| 104 | } |
| 105 | return QVariant(); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | #include "moc_servicemodel.cpp" |
| 111 | |