#include "qcpmodel.h" namespace QCP { namespace model { Entry::Entry(Row *parent, const Avahi::Service &s, const QCP::Share &share) : Avahi::model::Entry(parent, s), m_share(share) { } QVariant Entry::data(int col, int role) { switch (role) { case Qt::DisplayRole: switch (col) { case 0: return m_service.hostname(); case 1: if (m_service.address().protocol() == QAbstractSocket::IPv6Protocol) { return QString("[%1]:%2").arg(m_service.address().toString()).arg(m_service.port()); } else { return QString("%1:%2").arg(m_service.address().toString()).arg(m_service.port()); } } break; case Qt::UserRole: return qVariantFromValue(m_service); break; default: return QVariant(); } return QVariant(); } EntryList::EntryList(Row *parent, const Avahi::ServiceKey &k) : Avahi::model::EntryList(parent, k) { } int EntryList::columnCount() { return 3; } QVariant EntryList::data(int col, int role) { switch (role) { case Qt::DisplayRole: switch (col) { case 0: return key.name; case 2: { ShareDetails *d = share().details(); if (d) return d->methodName(); } } break; case Qt::UserRole: { QList l; for (int i = 0; i < list.length(); i++) { Entry *e = dynamic_cast< Entry* >(list.at(i)); if (!e) continue; l.append(e->service()); } return qVariantFromValue(l); } break; default: return QVariant(); } return QVariant(); } QCP::Share EntryList::share() { /* try to find first share */ for (int i = 0; i < list.length(); i++) { Entry *e = dynamic_cast< Entry* >(list.at(i)); if (!e) continue; return e->share(); } return QCP::Share(); } Avahi::model::Entry* EntryList::newEntry(const Avahi::Service &s, void *priv) { QCP::Share *share = static_cast< QCP::Share* >(priv); return new Entry(this, s, *share); } int Root::columnCount() { return 3; } EntryList* Root::newList(const Avahi::ServiceKey &k) { return new EntryList(this, k); } QVariant Root::emptyData(int col, int role) { if (col == 0 && role == Qt::DisplayRole) { return ""; } return QVariant(); } } BrowseModel::BrowseModel(QObject *parent) : Avahi::BrowseModel(new QCP::model::Root(), parent), m_browse(new QCP::Browse(this)) { connect(m_browse, SIGNAL(found(QCP::Share)), this, SLOT(foundShare(QCP::Share))); connect(m_browse, SIGNAL(lost(QCP::Share)), this, SLOT(lostShare(QCP::Share))); m_browse->start(); } QVariant BrowseModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) return QVariant(); switch (role) { case Qt::DisplayRole: switch (section) { case 0: return "Name"; case 1: return "Address"; case 2: return "Method"; } break; } return QAbstractItemModel::headerData(section, orientation, role); } void BrowseModel::foundShare(QCP::Share s) { insert(s.avahiService(), &s); } void BrowseModel::lostShare(QCP::Share s) { remove(s.avahiService()); } }