6#include <QtCore/QDebug>
9#include <QtDBus/QDBusObjectPath>
10#include <QtDBus/QDBusInterface>
11#include <QtDBus/QDBusReply>
13#include <QtXml/QDomDocument>
15using namespace Qt::StringLiterals;
29 Q_ASSERT(type == QDBusModel::PathItem);
34 s.prepend(item->name);
51QDomDocument
QDBusModel::introspect(
const QString &path)
55 QDBusInterface iface(service, path,
"org.freedesktop.DBus.Introspectable"_L1, c);
56 if (!iface.isValid()) {
57 QDBusError err(iface.lastError());
58 emit busError(tr(
"Cannot introspect object %1 at %2:\n %3 (%4)\n")
66 QDBusReply<QString> xml = iface.call(
"Introspect"_L1);
69 QDBusError err(xml.error());
71 emit busError(tr(
"Call to object %1 at %2:\n %3 (%4) failed\n")
77 emit busError(tr(
"Invalid XML received from object %1 at %2\n").arg(path).arg(service));
82 doc.setContent(xml.value());
91 while (!child.isNull()) {
93 if (child.tagName() ==
"method"_L1) {
94 item =
new QDBusItem(QDBusModel::MethodItem, child.attribute(
"name"_L1), parent);
95 item->caption = tr(
"Method: %1").arg(item->name);
99 if (n.attribute(
"direction"_L1) ==
"in"_L1)
100 item->typeSignature += n.attribute(
"type"_L1);
101 n = n.nextSiblingElement();
103 }
else if (child.tagName() ==
"signal"_L1) {
104 item =
new QDBusItem(QDBusModel::SignalItem, child.attribute(
"name"_L1), parent);
105 item->caption = tr(
"Signal: %1").arg(item->name);
106 }
else if (child.tagName() ==
"property"_L1) {
107 item =
new QDBusItem(QDBusModel::PropertyItem, child.attribute(
"name"_L1), parent);
108 item->caption = tr(
"Property: %1").arg(item->name);
110 qDebug() <<
"addMethods: unknown tag:" << child.tagName();
113 parent->children.append(item);
115 child = child.nextSiblingElement();
123 QString path = parent->path();
128 while (!child.isNull()) {
129 if (child.tagName() ==
"node"_L1) {
130 QDBusItem *item =
new QDBusItem(QDBusModel::PathItem,
131 child.attribute(
"name"_L1) +
'/'_L1, parent);
132 parent->children.append(item);
134 addMethods(item, child);
135 }
else if (child.tagName() ==
"interface"_L1) {
137 new QDBusItem(QDBusModel::InterfaceItem, child.attribute(
"name"_L1), parent);
138 parent->children.append(item);
140 addMethods(item, child);
142 qDebug() <<
"addPath: Unknown tag name:" << child.tagName();
144 child = child.nextSiblingElement();
153 root =
new QDBusItem(QDBusModel::PathItem,
"/"_L1);
167 if (column != 0 || row < 0 || row >= item->children.size())
168 return QModelIndex();
170 return createIndex(row, 0, item->children.at(row));
177 return QModelIndex();
188 const_cast<
QDBusModel *>(
this)->addPath(item);
190 return item->children.size();
204 if (role != Qt::DisplayRole)
207 return item->caption.isEmpty() ? item->name : item->caption;
212 if (role != Qt::DisplayRole || orientation == Qt::Vertical || section != 0)
215 return tr(
"Methods");
221 return item ? item->type : PathItem;
226 QModelIndex index = aIndex;
227 while (index.isValid() &&
static_cast<QDBusItem *>(index.internalPointer())->type != PathItem) {
228 index = index.parent();
235 if (!item->children.isEmpty()) {
236 beginRemoveRows(index, 0, item->children.size() - 1);
237 qDeleteAll(item->children);
238 item->children.clear();
243 if (!item->children.isEmpty()) {
244 beginInsertRows(index, 0, item->children.size() - 1);
251 QModelIndex index = aIndex;
252 while (index.isValid() &&
static_cast<QDBusItem *>(index.internalPointer())->type != PathItem) {
253 index = index.parent();
268 if (item->type == InterfaceItem)
270 if (item->parent && item->parent->type == InterfaceItem)
278 return item ? item->name : QString();
284 return item ? item->typeSignature : QString();
289 QStringList path = objectPath.path().split(
'/'_L1, Qt::SkipEmptyParts);
293 while (item && !path.isEmpty()) {
294 const QString branch = path.takeFirst() +
'/'_L1;
298 for (
int i = 0; i < item->children.size(); ++i) {
300 if (child->type == PathItem && child->name == branch) {
313 return QModelIndex();
317 if (childIdx != -1 && item && path.isEmpty())
318 return createIndex(childIdx, 0, item);
320 return QModelIndex();
QString dBusMethodName(const QModelIndex &index) const
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
QString dBusTypeSignature(const QModelIndex &index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
QModelIndex parent(const QModelIndex &child) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Returns the data for the given role and section in the header with the specified orientation.
void refresh(const QModelIndex &index=QModelIndex())
QString dBusPath(const QModelIndex &index) const
QModelIndex findObject(const QDBusObjectPath &objectPath)
Type itemType(const QModelIndex &index) const
QString dBusInterface(const QModelIndex &index) const
QDBusModel(const QString &service, const QDBusConnection &connection)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index.
QT_FORWARD_DECLARE_CLASS(QDomDocument)
QT_FORWARD_DECLARE_CLASS(QDomElement)
QList< QDBusItem * > children
QDBusItem(QDBusModel::Type aType, const QString &aName, QDBusItem *aParent=0)