2
Copyright (c) 2012 Montel Laurent <montel@kde.org>
4
This program is free software; you can redistribute it and/or modify it
5
under the terms of the GNU General Public License, version 2, as
6
published by the Free Software Foundation.
8
This program is distributed in the hope that it will be useful, but
9
WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
General Public License for more details.
13
You should have received a copy of the GNU General Public License along
14
with this program; if not, write to the Free Software Foundation, Inc.,
15
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
#include "createresource.h"
23
#include <akonadi/agenttype.h>
24
#include <akonadi/agentmanager.h>
25
#include <akonadi/agentinstancecreatejob.h>
28
#include <QDBusInterface>
29
#include <QMetaMethod>
31
using namespace Akonadi;
33
using namespace PimCommon;
35
CreateResource::CreateResource()
39
CreateResource::~CreateResource()
43
//code from accountwizard
44
static QVariant::Type argumentType( const QMetaObject *mo, const QString &method )
47
const int numberOfMethod( mo->methodCount() );
48
for ( int i = 0; i < numberOfMethod; ++i ) {
49
const QString signature = QString::fromLatin1( mo->method( i ).signature() );
50
if ( signature.contains(method + QLatin1Char('(') )) {
56
if ( !m.signature() ) {
57
kWarning() << "Did not find D-Bus method: " << method << " available methods are:";
58
const int numberOfMethod(mo->methodCount());
59
for ( int i = 0; i < numberOfMethod; ++ i )
60
kWarning() << mo->method( i ).signature();
61
return QVariant::Invalid;
64
const QList<QByteArray> argTypes = m.parameterTypes();
65
if ( argTypes.count() != 1 )
66
return QVariant::Invalid;
68
return QVariant::nameToType( argTypes.first() );
71
QString CreateResource::createResource( const QString& resources, const QString& name, const QMap<QString, QVariant>& settings )
73
const AgentType type = AgentManager::self()->type( resources );
74
if ( !type.isValid() ) {
75
Q_EMIT createResourceError( i18n( "Resource type '%1' is not available.", resources ) );
79
// check if unique instance already exists
80
kDebug() << type.capabilities();
81
if ( type.capabilities().contains( QLatin1String( "Unique" ) ) ) {
82
Q_FOREACH ( const AgentInstance &instance, AgentManager::self()->instances() ) {
83
kDebug() << instance.type().identifier() << (instance.type() == type);
84
if ( instance.type() == type ) {
85
Q_EMIT createResourceInfo(i18n( "Resource '%1' is already set up.", type.name() ) );
91
Q_EMIT createResourceInfo( i18n( "Creating resource instance for '%1'...", type.name() ) );
92
AgentInstanceCreateJob *job = new AgentInstanceCreateJob( type, this );
94
Akonadi::AgentInstance instance = job->instance();
96
if ( !settings.isEmpty() ) {
97
Q_EMIT createResourceInfo( i18n( "Configuring resource instance..." ) );
98
QDBusInterface iface( "org.freedesktop.Akonadi.Resource." + instance.identifier(), "/Settings" );
99
if ( !iface.isValid() ) {
100
Q_EMIT createResourceError( i18n( "Unable to configure resource instance." ) );
104
// configure resource
105
if ( !name.isEmpty() )
106
instance.setName( name );
107
QMap<QString, QVariant>::const_iterator end( settings.constEnd());
108
for ( QMap<QString, QVariant>::const_iterator it = settings.constBegin(); it != end; ++it ) {
109
kDebug() << "Setting up " << it.key() << " for agent " << instance.identifier();
110
const QString methodName = QString::fromLatin1("set%1").arg( it.key() );
111
QVariant arg = it.value();
112
const QVariant::Type targetType = argumentType( iface.metaObject(), methodName );
113
if ( !arg.canConvert( targetType ) ) {
114
Q_EMIT createResourceError( i18n( "Could not convert value of setting '%1' to required type %2.", it.key(), QVariant::typeToName( targetType ) ) );
117
arg.convert( targetType );
118
QDBusReply<void> reply = iface.call( methodName, arg );
119
if ( !reply.isValid() ) {
120
Q_EMIT createResourceError( i18n( "Could not set setting '%1': %2", it.key(), reply.error().message() ) );
124
instance.reconfigure();
127
Q_EMIT createResourceInfo( i18n( "Resource setup completed." ) );
128
return instance.identifier();
130
if ( job->error() ) {
131
Q_EMIT createResourceError( i18n( "Failed to create resource instance: %1", job->errorText() ) );
137
#include "createresource.moc"