DNSSD
servicemodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "servicemodel.h"
00022 #include "servicebrowser.h"
00023 #include <klocale.h>
00024
00025 namespace DNSSD
00026 {
00027
00028 struct ServiceModelPrivate
00029 {
00030 ServiceBrowser* m_browser;
00031 };
00032
00033
00034 ServiceModel::ServiceModel(ServiceBrowser* browser, QObject* parent)
00035 : QAbstractItemModel(parent), d(new ServiceModelPrivate)
00036 {
00037 d->m_browser=browser;
00038 browser->setParent(this);
00039 connect(browser, SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)), this,
00040 SIGNAL(layoutChanged()));
00041 connect(browser, SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)), this,
00042 SIGNAL(layoutChanged()));
00043 browser->startBrowse();
00044 }
00045
00046 ServiceModel::~ServiceModel()
00047 {
00048 delete d;
00049 }
00050
00051 int ServiceModel::columnCount(const QModelIndex&) const
00052 {
00053 return d->m_browser->isAutoResolving() ? 3 : 1;
00054 }
00055 int ServiceModel::rowCount(const QModelIndex& parent ) const
00056 {
00057 return (parent.isValid()) ? 0 : d->m_browser->services().size();
00058 }
00059
00060 QModelIndex ServiceModel::parent(const QModelIndex&) const
00061 {
00062 return QModelIndex();
00063 }
00064
00065 QModelIndex ServiceModel::index(int row, int column, const QModelIndex& parent ) const
00066 {
00067 return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
00068 }
00069
00070 bool ServiceModel::hasIndex(int row, int column, const QModelIndex &parent) const
00071 {
00072 if (parent.isValid()) return false;
00073 if (column<0 || column>=columnCount()) return false;
00074 if (row<0 || row>=rowCount(parent)) return false;
00075 return true;
00076 }
00077
00078 QVariant ServiceModel::data(const QModelIndex& index, int role ) const
00079 {
00080 if (!index.isValid()) return QVariant();
00081 if (!hasIndex(index.row(), index.column(), index.parent())) return QVariant();
00082 const QList<RemoteService::Ptr> srv=d->m_browser->services();
00083 switch (role) {
00084 case Qt::DisplayRole:
00085 switch (index.column()) {
00086 case ServiceName: return srv[index.row()]->serviceName();
00087 case Host: return srv[index.row()]->hostName();
00088 case Port: return srv[index.row()]->port();
00089 }
00090 case ServicePtrRole: QVariant ret;
00091 ret.setValue(srv[index.row()]);
00092 return ret;
00093 }
00094 return QVariant();
00095 }
00096
00097 QVariant ServiceModel::headerData(int section, Qt::Orientation orientation, int role) const
00098 {
00099 if (orientation!=Qt::Horizontal || role!=Qt::DisplayRole) return QVariant();
00100 switch (section) {
00101 case ServiceName: return i18n("Name");
00102 case Host: return i18n("Host");
00103 case Port: return i18n("Port");
00104 }
00105 return QVariant();
00106 }
00107
00108 }