Astyle kdelibs
[kdepim.git] / akonadiconsole / agentconfigmodel.cpp
blobe50da4a8b15e9e756361408e8a98328b94fdb75e
1 /*
2 Copyright (c) 2010 Volker Krause <vkrause@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA.
20 #include "agentconfigmodel.h"
22 #include <QDBusInterface>
23 #include <QMetaMethod>
24 #include <KLocalizedString>
25 #include "akonadiconsole_debug.h"
27 AgentConfigModel::AgentConfigModel(QObject *parent): QAbstractTableModel(parent), m_interface(Q_NULLPTR)
31 AgentConfigModel::~AgentConfigModel()
33 delete m_interface;
36 void AgentConfigModel::setAgentInstance(const Akonadi::AgentInstance &instance)
38 m_settings.clear();
39 reset();
41 m_interface = new QDBusInterface(
42 QStringLiteral("org.freedesktop.Akonadi.Agent.%1").arg(instance.identifier()),
43 QStringLiteral("/Settings"));
44 if (!m_interface->isValid()) {
45 qCritical() << "Unable to obtain KConfigXT D-Bus interface of agent" << instance.identifier();
46 delete m_interface;
47 return;
50 reload();
53 void AgentConfigModel::reload()
55 m_settings.clear();
56 for (int i = 0; i < m_interface->metaObject()->methodCount(); ++i) {
57 const QMetaMethod method = m_interface->metaObject()->method(i);
58 if (QByteArray(method.typeName()).isEmpty()) { // returns void
59 continue;
61 const QByteArray signature(method.methodSignature());
62 if (signature.isEmpty()) {
63 continue;
65 if (signature.startsWith("set") || !signature.contains("()")) { // setter or takes parameters // krazy:exclude=strings
66 continue;
68 if (signature.startsWith("Introspect")) { // D-Bus stuff // krazy:exclude=strings
69 continue;
71 const QString methodName = QString::fromLatin1(signature.left(signature.indexOf('(')));
72 const QDBusMessage reply = m_interface->call(methodName);
73 if (reply.arguments().count() != 1) {
74 qCCritical(AKONADICONSOLE_LOG) << "call to method" << signature << "failed: " << reply.arguments() << reply.errorMessage();
75 continue;
77 const QString settingName = methodName.at(0).toUpper() + methodName.mid(1);
78 m_settings.append(qMakePair(settingName, reply.arguments().at(0)));
80 reset();
83 int AgentConfigModel::columnCount(const QModelIndex &parent) const
85 Q_UNUSED(parent);
86 return 3;
89 int AgentConfigModel::rowCount(const QModelIndex &parent) const
91 Q_UNUSED(parent);
92 return m_settings.size();
95 QVariant AgentConfigModel::data(const QModelIndex &index, int role) const
97 if (index.row() < 0 || index.row() >= m_settings.size()) {
98 return QVariant();
100 QPair<QString, QVariant> setting = m_settings.at(index.row());
101 if (role == Qt::DisplayRole || role == Qt::EditRole) {
102 if (index.column() == 0) {
103 return setting.first;
104 } else if (index.column() == 1) {
105 return setting.second;
106 } else if (index.column() == 2) {
107 return QLatin1String(setting.second.typeName());
110 return QVariant();
113 bool AgentConfigModel::setData(const QModelIndex &index, const QVariant &value, int role)
115 if (index.column() == 1 && role == Qt::EditRole && index.row() >= 0 && index.row() < m_settings.size()) {
116 const QPair<QString, QVariant> setting = m_settings.at(index.row());
117 if (setting.second != value) {
118 m_interface->call(QStringLiteral("set%1").arg(setting.first), value);
119 reload();
122 return QAbstractItemModel::setData(index, value, role);
125 QVariant AgentConfigModel::headerData(int section, Qt::Orientation orientation, int role) const
127 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
128 if (section == 0) {
129 return QStringLiteral("Setting");
130 } else if (section == 1) {
131 return QStringLiteral("Value");
132 } else if (section == 2) {
133 return QStringLiteral("Type");
136 return QAbstractItemModel::headerData(section, orientation, role);
139 Qt::ItemFlags AgentConfigModel::flags(const QModelIndex &index) const
141 if (index.column() == 1) {
142 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
144 return QAbstractItemModel::flags(index);
147 void AgentConfigModel::writeConfig()
149 m_interface->call(QStringLiteral("writeConfig"));