KDNSSD

servicemodel.cpp
1 /*
2  This file is part of the KDE project
3 
4  SPDX-FileCopyrightText: 2008 Jakub Stachowski <[email protected]>
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 
15 struct ServiceModelPrivate {
16  ServiceBrowser *m_browser;
17 };
18 
20  : QAbstractItemModel(parent), d(new ServiceModelPrivate)
21 {
22  d->m_browser = browser;
23  browser->setParent(this);
24  connect(browser, SIGNAL(serviceAdded(KDNSSD::RemoteService::Ptr)), this,
25  SIGNAL(layoutChanged()));
26  connect(browser, SIGNAL(serviceRemoved(KDNSSD::RemoteService::Ptr)), this,
27  SIGNAL(layoutChanged()));
28  browser->startBrowse();
29 }
30 
31 ServiceModel::~ServiceModel()
32 {
33  delete d;
34 }
35 
37 {
38  return d->m_browser->isAutoResolving() ? 3 : 1;
39 }
41 {
42  return (parent.isValid()) ? 0 : d->m_browser->services().size();
43 }
44 
46 {
47  return QModelIndex();
48 }
49 
50 QModelIndex ServiceModel::index(int row, int column, const QModelIndex &parent) const
51 {
52  return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
53 }
54 
55 bool ServiceModel::hasIndex(int row, int column, const QModelIndex &parent) const
56 {
57  if (parent.isValid()) {
58  return false;
59  }
60  if (column < 0 || column >= columnCount()) {
61  return false;
62  }
63  if (row < 0 || row >= rowCount(parent)) {
64  return false;
65  }
66  return true;
67 }
68 
70 {
71  if (!index.isValid()) {
72  return QVariant();
73  }
74  if (!hasIndex(index.row(), index.column(), index.parent())) {
75  return QVariant();
76  }
77  const QList<RemoteService::Ptr> srv = d->m_browser->services();
78  switch ((uint)role) {
79  case Qt::DisplayRole:
80  switch (index.column()) {
81  case ServiceName: return srv[index.row()]->serviceName();
82  case Host: return srv[index.row()]->hostName();
83  case Port: return srv[index.row()]->port();
84  }
85  break;
86  case ServicePtrRole:
87  QVariant ret;
88  ret.setValue(srv[index.row()]);
89  return ret;
90  }
91  return QVariant();
92 }
93 
94 QVariant ServiceModel::headerData(int section, Qt::Orientation orientation, int role) const
95 {
96  if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
97  return QVariant();
98  }
99  switch (section) {
100  case ServiceName: return tr("Name");
101  case Host: return tr("Host");
102  case Port: return tr("Port");
103  }
104  return QVariant();
105 }
106 
107 }
virtual void startBrowse()
Starts browsing for services.
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
gets a RemoteService::Ptr for the service
Definition: servicemodel.h:61
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isValid() const const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
DisplayRole
virtual bool hasIndex(int row, int column, const QModelIndex &parent) const
int row() const const
QModelIndex parent() const const
QModelIndex createIndex(int row, int column, void *ptr) const const
void setParent(QObject *parent)
void setValue(const T &value)
Browses for network services advertised over DNS-SD.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int column() const const
Orientation
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
ServiceModel(ServiceBrowser *browser, QObject *parent=nullptr)
Creates a model for the given service browser and starts browsing for services.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Oct 20 2020 22:46:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.