New Appointment -> New Task
[kdepim.git] / akonadiconsole / agentconfigmodel.cpp
blobb91a815f44b21386752721e649095aadcc6257e3
1 /*
2 Copyright (c) 2010 Volker Krause
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 <KDebug>
24 #include <QDBusInterface>
25 #include <QMetaMethod>
26 #include <KLocalizedString>
28 AgentConfigModel::AgentConfigModel(QObject* parent): QAbstractTableModel(parent)
32 void AgentConfigModel::setAgentInstance(const Akonadi::AgentInstance& instance)
34 m_settings.clear();
35 reset();
37 m_interface = new QDBusInterface(
38 QString::fromLatin1("org.freedesktop.Akonadi.Agent.%1").arg( instance.identifier() ),
39 "/Settings" );
40 if ( !m_interface->isValid() ) {
41 kError() << "Unable to obtain KConfigXT D-Bus interface of agent" << instance.identifier();
42 return;
45 reload();
48 void AgentConfigModel::reload()
50 m_settings.clear();
51 for ( int i = 0; i < m_interface->metaObject()->methodCount(); ++i ) {
52 const QMetaMethod method = m_interface->metaObject()->method( i );
53 if ( QByteArray( method.typeName() ).isEmpty() ) // returns void
54 continue;
55 const QByteArray signature( method.signature() );
56 if ( signature.isEmpty() )
57 continue;
58 if ( signature.startsWith( "set" ) || !signature.contains( "()" ) ) // setter or takes parameters // krazy:exclude=strings
59 continue;
60 if ( signature.startsWith( "Introspect" ) ) // D-Bus stuff // krazy:exclude=strings
61 continue;
62 const QString methodName = QString::fromLatin1( signature.left( signature.indexOf( '(' ) ) );
63 const QDBusMessage reply = m_interface->call( methodName );
64 if ( !reply.arguments().count() == 1 ) {
65 kError() << "call to method" << signature << "failed: " << reply.arguments() << reply.errorMessage();
66 continue;
68 const QString settingName = methodName.at( 0 ).toUpper() + methodName.mid( 1 );
69 m_settings.append( qMakePair( settingName, reply.arguments().at( 0 ) ) );
71 reset();
75 int AgentConfigModel::columnCount(const QModelIndex& parent) const
77 Q_UNUSED( parent );
78 return 3;
81 int AgentConfigModel::rowCount(const QModelIndex& parent) const
83 Q_UNUSED( parent );
84 return m_settings.size();
87 QVariant AgentConfigModel::data(const QModelIndex& index, int role) const
89 if ( index.row() < 0 || index.row() >= m_settings.size() )
90 return QVariant();
91 QPair<QString, QVariant> setting = m_settings.at( index.row() );
92 if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
93 if ( index.column() == 0 )
94 return setting.first;
95 else if ( index.column() == 1 )
96 return setting.second;
97 else if ( index.column() == 2 )
98 return setting.second.typeName();
100 return QVariant();
103 bool AgentConfigModel::setData(const QModelIndex& index, const QVariant& value, int role)
105 if ( index.column() == 1 && role == Qt::EditRole && index.row() >= 0 && index.row() < m_settings.size() ) {
106 const QPair<QString, QVariant> setting = m_settings.at( index.row() );
107 if ( setting.second != value ) {
108 m_interface->call( QString::fromLatin1( "set%1" ).arg( setting.first ), value );
109 reload();
112 return QAbstractItemModel::setData(index, value, role);
115 QVariant AgentConfigModel::headerData(int section, Qt::Orientation orientation, int role) const
117 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
118 if ( section == 0 )
119 return i18n( "Setting" );
120 else if ( section == 1 )
121 return i18n( "Value" );
122 else if ( section == 2 )
123 return i18n( "Type" );
125 return QAbstractItemModel::headerData(section, orientation, role);
128 Qt::ItemFlags AgentConfigModel::flags(const QModelIndex& index) const
130 if ( index.column() == 1 )
131 return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
132 return QAbstractItemModel::flags(index);
135 void AgentConfigModel::writeConfig()
137 m_interface->call( "writeConfig" );
140 #include "agentconfigmodel.moc"