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,
20 #include "agentconfigmodel.h"
24 #include <QDBusInterface>
25 #include <QMetaMethod>
26 #include <KLocalizedString>
28 AgentConfigModel::AgentConfigModel(QObject
* parent
): QAbstractTableModel(parent
)
32 void AgentConfigModel::setAgentInstance(const Akonadi::AgentInstance
& instance
)
37 m_interface
= new QDBusInterface(
38 QString::fromLatin1("org.freedesktop.Akonadi.Agent.%1").arg( instance
.identifier() ),
40 if ( !m_interface
->isValid() ) {
41 kError() << "Unable to obtain KConfigXT D-Bus interface of agent" << instance
.identifier();
48 void AgentConfigModel::reload()
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
55 const QByteArray
signature( method
.signature() );
56 if ( signature
.isEmpty() )
58 if ( signature
.startsWith( "set" ) || !signature
.contains( "()" ) ) // setter or takes parameters // krazy:exclude=strings
60 if ( signature
.startsWith( "Introspect" ) ) // D-Bus stuff // krazy:exclude=strings
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();
68 const QString settingName
= methodName
.at( 0 ).toUpper() + methodName
.mid( 1 );
69 m_settings
.append( qMakePair( settingName
, reply
.arguments().at( 0 ) ) );
75 int AgentConfigModel::columnCount(const QModelIndex
& parent
) const
81 int AgentConfigModel::rowCount(const QModelIndex
& parent
) const
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() )
91 QPair
<QString
, QVariant
> setting
= m_settings
.at( index
.row() );
92 if ( role
== Qt::DisplayRole
|| role
== Qt::EditRole
) {
93 if ( index
.column() == 0 )
95 else if ( index
.column() == 1 )
96 return setting
.second
;
97 else if ( index
.column() == 2 )
98 return setting
.second
.typeName();
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
);
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
) {
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"