File indexing completed on 2024-04-28 15:22:07

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "servicemodel.h"
0010 #include "servicebrowser.h"
0011 
0012 namespace KDNSSD
0013 {
0014 struct ServiceModelPrivate {
0015     ServiceBrowser *m_browser;
0016 };
0017 
0018 ServiceModel::ServiceModel(ServiceBrowser *browser, QObject *parent)
0019     : QAbstractItemModel(parent)
0020     , d(new ServiceModelPrivate)
0021 {
0022     d->m_browser = browser;
0023     browser->setParent(this);
0024     connect(browser, SIGNAL(serviceAdded(KDNSSD::RemoteService::Ptr)), this, SIGNAL(layoutChanged()));
0025     connect(browser, SIGNAL(serviceRemoved(KDNSSD::RemoteService::Ptr)), this, SIGNAL(layoutChanged()));
0026     browser->startBrowse();
0027 }
0028 
0029 ServiceModel::~ServiceModel() = default;
0030 
0031 int ServiceModel::columnCount(const QModelIndex &) const
0032 {
0033     return d->m_browser->isAutoResolving() ? 3 : 1;
0034 }
0035 int ServiceModel::rowCount(const QModelIndex &parent) const
0036 {
0037     return (parent.isValid()) ? 0 : d->m_browser->services().size();
0038 }
0039 
0040 QModelIndex ServiceModel::parent(const QModelIndex &) const
0041 {
0042     return QModelIndex();
0043 }
0044 
0045 QModelIndex ServiceModel::index(int row, int column, const QModelIndex &parent) const
0046 {
0047     return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
0048 }
0049 
0050 bool ServiceModel::hasIndex(int row, int column, const QModelIndex &parent) const
0051 {
0052     if (parent.isValid()) {
0053         return false;
0054     }
0055     if (column < 0 || column >= columnCount()) {
0056         return false;
0057     }
0058     if (row < 0 || row >= rowCount(parent)) {
0059         return false;
0060     }
0061     return true;
0062 }
0063 
0064 QVariant ServiceModel::data(const QModelIndex &index, int role) const
0065 {
0066     if (!index.isValid()) {
0067         return QVariant();
0068     }
0069     if (!hasIndex(index.row(), index.column(), index.parent())) {
0070         return QVariant();
0071     }
0072     const QList<RemoteService::Ptr> srv = d->m_browser->services();
0073     switch ((uint)role) {
0074     case Qt::DisplayRole:
0075         switch (index.column()) {
0076         case ServiceName:
0077             return srv[index.row()]->serviceName();
0078         case Host:
0079             return srv[index.row()]->hostName();
0080         case Port:
0081             return srv[index.row()]->port();
0082         }
0083         break;
0084     case ServicePtrRole:
0085         QVariant ret;
0086         ret.setValue(srv[index.row()]);
0087         return ret;
0088     }
0089     return QVariant();
0090 }
0091 
0092 QVariant ServiceModel::headerData(int section, Qt::Orientation orientation, int role) const
0093 {
0094     if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
0095         return QVariant();
0096     }
0097     switch (section) {
0098     case ServiceName:
0099         return tr("Name");
0100     case Host:
0101         return tr("Host");
0102     case Port:
0103         return tr("Port");
0104     }
0105     return QVariant();
0106 }
0107 
0108 }
0109 
0110 #include "moc_servicemodel.cpp"