qcp/libqtavahi/qtavahimodel.cpp

92 lines
2.6 KiB
C++

#include "qtavahimodel.h"
namespace Avahi {
namespace model {
Row::~Row() { }
int Row::columnCount() { return 0; } /* columns of child rows */
Row* Row::index(int /* row */) { return 0; }
int Row::rowCount() { return 0; }
Row* RowList::index(int row) {
return list.value(row);
}
int RowList::rowCount() {
return list.length();
}
QVariant Root::data(int , int ) { return QVariant(); }
}
BrowseModel::BrowseModel(Avahi::model::Root *root, QObject *parent)
: QAbstractItemModel(parent), m_root(root) { }
BrowseModel::Row *BrowseModel::getRow(const QModelIndex &index) const {
if (index.isValid()) {
return static_cast< Row* >(index.internalPointer());
} else {
return m_root;
}
}
int BrowseModel::columnCount(const QModelIndex & parent) const {
Row *row = getRow(parent);
return row ? row->columnCount() : 0;
}
QVariant BrowseModel::data(const QModelIndex & index, int role) const {
Row *row = getRow(index);
return row ? row->data(index.column(), role) : QVariant();
}
QModelIndex BrowseModel::index(int row, int column, const QModelIndex & parent) const {
Row *r = getRow(parent);
if (!r) return QModelIndex(); /* invalid */
Row *child = r->index(row);
if (!child) return QModelIndex(); /* invalid */
return createIndex(row, column, (void*) child);
}
QModelIndex BrowseModel::parent(const QModelIndex & index) const {
Row *row = getRow(index);
if (!row) return QModelIndex(); /* invalid */
Row *p = row->parent();
if (!p || p == m_root) return QModelIndex();
return createIndex(0, 0, (void*) p);
}
int BrowseModel::rowCount(const QModelIndex & parent) const {
Row *row = getRow(parent);
return row ? row->rowCount() : 0;
}
Avahi::ServiceKey BrowseModel::key(const Avahi::Service &s) {
Avahi::ServiceKey k(s);
k.interface = k.protocol = -1;
return k;
}
void BrowseModel::insert(const Avahi::Service &s, void *priv) {
Avahi::ServiceKey k = key(s);
if (m_services.contains(k)) {
EntryList *el = m_services.value(k);
beginInsertRows(createIndex(0, 0, (void*) el), 0, 0);
el->list.push_front(el->newEntry(s, priv));
endInsertRows();
} else {
EntryList *el = m_root->newList(k);
QMap<Avahi::ServiceKey, EntryList*> tmp(m_services);
tmp.insert(k, el);
int row = tmp.keys().indexOf(k);
beginInsertRows(QModelIndex(), row, row);
m_services = tmp;
m_root->list.insert(row, el);
endInsertRows();
beginInsertRows(createIndex(0, 0, (void*) el), 0, 0);
el->list.push_front(el->newEntry(s, priv));
endInsertRows();
}
}
void BrowseModel::remove(const Avahi::Service &) {
}
}