KDNSSD

servicemodel.cpp
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
12namespace KDNSSD
13{
14struct ServiceModelPrivate {
15 ServiceBrowser *m_browser;
16};
17
19 : QAbstractItemModel(parent)
20 , d(new ServiceModelPrivate)
21{
22 d->m_browser = browser;
23 browser->setParent(this);
24 connect(browser, SIGNAL(serviceAdded(KDNSSD::RemoteService::Ptr)), this, SIGNAL(layoutChanged()));
25 connect(browser, SIGNAL(serviceRemoved(KDNSSD::RemoteService::Ptr)), this, SIGNAL(layoutChanged()));
26 browser->startBrowse();
27}
28
29ServiceModel::~ServiceModel() = default;
30
32{
33 return d->m_browser->isAutoResolving() ? 3 : 1;
34}
35int ServiceModel::rowCount(const QModelIndex &parent) const
36{
37 return (parent.isValid()) ? 0 : d->m_browser->services().size();
38}
39
41{
42 return QModelIndex();
43}
44
45QModelIndex ServiceModel::index(int row, int column, const QModelIndex &parent) const
46{
47 return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
48}
49
50bool 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
64QVariant ServiceModel::data(const QModelIndex &index, int role) const
65{
66 if (!index.isValid()) {
67 return QVariant();
68 }
69 if (!hasIndex(index.row(), index.column(), 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
92QVariant ServiceModel::headerData(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("Name");
100 case Host:
101 return tr("Host");
102 case Port:
103 return tr("Port");
104 }
105 return QVariant();
106}
107
108}
109
110#include "moc_servicemodel.cpp"
Browses for network services advertised over DNS-SD.
virtual void startBrowse()
Starts browsing for services.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
@ ServicePtrRole
gets a RemoteService::Ptr for the service
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
ServiceModel(ServiceBrowser *browser, QObject *parent=nullptr)
Creates a model for the given service browser and starts browsing for services.
virtual bool hasIndex(int row, int column, const QModelIndex &parent) const
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex createIndex(int row, int column, const void *ptr) const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
int column() const const
bool isValid() const const
QModelIndex parent() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void setParent(QObject *parent)
QString tr(const char *sourceText, const char *disambiguation, int n)
DisplayRole
Orientation
void setValue(QVariant &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:10:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.