qcp/libqcp/qcpmodel.cpp

128 lines
3.1 KiB
C++
Raw Normal View History

2010-07-24 16:16:44 +00:00
#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());
}
2010-07-24 16:16:44 +00:00
}
break;
case Qt::UserRole:
return qVariantFromValue(m_service);
break;
2010-07-24 16:16:44 +00:00
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();
}
2010-07-24 16:16:44 +00:00
}
break;
case Qt::UserRole: {
QList<Avahi::Service> 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;
2010-07-24 16:16:44 +00:00
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();
}
2010-07-24 16:16:44 +00:00
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 "<No shares available>";
}
return QVariant();
}
2010-07-24 16:16:44 +00:00
}
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";
2010-07-24 16:16:44 +00:00
}
break;
}
return QAbstractItemModel::headerData(section, orientation, role);
2010-07-24 16:16:44 +00:00
}
void BrowseModel::foundShare(QCP::Share s) {
insert(s.avahiService(), &s);
}
void BrowseModel::lostShare(QCP::Share s) {
remove(s.avahiService());
}
}