Merge branch 'master' of git://anongit.kde.org/kdepim
[kdepim.git] / accountwizard / resource.cpp
blob74a4e89ad7fcacb0630d9288a785ec5fc1143119
1 /*
2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
20 #include "resource.h"
22 #include <agenttype.h>
23 #include <agentmanager.h>
24 #include <agentinstancecreatejob.h>
26 #include "accountwizard_debug.h"
27 #include <KLocalizedString>
29 #include <QMetaMethod>
30 #include <QVariant>
31 #include <QtDBus/qdbusinterface.h>
32 #include <QtDBus/qdbusreply.h>
34 using namespace Akonadi;
36 static QVariant::Type argumentType(const QMetaObject *mo, const QString &method)
38 QMetaMethod m;
39 for (int i = 0; i < mo->methodCount(); ++i) {
40 const QString signature = QLatin1String(mo->method(i).methodSignature());
41 if (signature.contains(method + QLatin1Char('('))) {
42 m = mo->method(i);
43 break;
47 if (m.methodSignature().isEmpty()) {
48 qCWarning(ACCOUNTWIZARD_LOG) << "Did not find D-Bus method: " << method << " available methods are:";
49 for (int i = 0; i < mo->methodCount(); ++i) {
50 qCWarning(ACCOUNTWIZARD_LOG) << mo->method(i).methodSignature();
52 return QVariant::Invalid;
55 const QList<QByteArray> argTypes = m.parameterTypes();
56 if (argTypes.count() != 1) {
57 return QVariant::Invalid;
60 return QVariant::nameToType(argTypes.first().constData());
63 Resource::Resource(const QString &type, QObject *parent)
64 : SetupObject(parent)
65 , m_typeIdentifier(type)
66 , m_editMode(false)
70 void Resource::setOption(const QString &key, const QVariant &value)
72 m_settings.insert(key, value);
75 void Resource::setName(const QString &name)
77 m_name = name;
80 void Resource::create()
82 const AgentType type = AgentManager::self()->type(m_typeIdentifier);
83 if (!type.isValid()) {
84 Q_EMIT error(i18n("Resource type '%1' is not available.", m_typeIdentifier));
85 return;
88 // check if unique instance already exists
89 qCDebug(ACCOUNTWIZARD_LOG) << type.capabilities();
90 if (type.capabilities().contains(QStringLiteral("Unique"))) {
91 foreach (const AgentInstance &instance, AgentManager::self()->instances()) {
92 qCDebug(ACCOUNTWIZARD_LOG) << instance.type().identifier() << (instance.type() == type);
93 if (instance.type() == type) {
94 if (m_editMode) {
95 edit();
97 Q_EMIT finished(i18n("Resource '%1' is already set up.", type.name()));
98 return;
103 Q_EMIT info(i18n("Creating resource instance for '%1'...", type.name()));
104 AgentInstanceCreateJob *job = new AgentInstanceCreateJob(type, this);
105 connect(job, &AgentInstanceCreateJob::result, this, &Resource::instanceCreateResult);
106 job->start();
109 void Resource::instanceCreateResult(KJob *job)
111 if (job->error()) {
112 Q_EMIT error(i18n("Failed to create resource instance: %1", job->errorText()));
113 return;
116 m_instance = qobject_cast<AgentInstanceCreateJob *>(job)->instance();
118 if (!m_settings.isEmpty()) {
119 Q_EMIT info(i18n("Configuring resource instance..."));
120 QDBusInterface iface(QStringLiteral("org.freedesktop.Akonadi.Resource.") + m_instance.identifier(), QStringLiteral("/Settings"));
121 if (!iface.isValid()) {
122 Q_EMIT error(i18n("Unable to configure resource instance."));
123 return;
126 // configure resource
127 if (!m_name.isEmpty()) {
128 m_instance.setName(m_name);
130 QMap<QString, QVariant>::const_iterator end(m_settings.constEnd());
131 for (QMap<QString, QVariant>::const_iterator it = m_settings.constBegin(); it != end; ++it) {
132 qCDebug(ACCOUNTWIZARD_LOG) << "Setting up " << it.key() << " for agent " << m_instance.identifier();
133 const QString methodName = QStringLiteral("set%1").arg(it.key());
134 QVariant arg = it.value();
135 const QVariant::Type targetType = argumentType(iface.metaObject(), methodName);
136 if (!arg.canConvert(targetType)) {
137 Q_EMIT error(i18n("Could not convert value of setting '%1' to required type %2.", it.key(), QLatin1String(QVariant::typeToName(targetType))));
138 return;
140 arg.convert(targetType);
141 QDBusReply<void> reply = iface.call(methodName, arg);
142 if (!reply.isValid()) {
143 Q_EMIT error(i18n("Could not set setting '%1': %2", it.key(), reply.error().message()));
144 return;
147 m_instance.reconfigure();
150 if (m_editMode) {
151 edit();
153 Q_EMIT finished(i18n("Resource setup completed."));
156 void Resource::edit()
158 if (m_instance.isValid()) {
159 m_instance.configure();
163 void Resource::destroy()
165 if (m_instance.isValid()) {
166 AgentManager::self()->removeInstance(m_instance);
167 Q_EMIT info(i18n("Removed resource instance for '%1'.", m_instance.type().name()));
171 QString Resource::identifier()
173 return m_instance.identifier();
176 void Resource::reconfigure()
178 m_instance.reconfigure();
181 void Resource::setEditMode(const bool editMode)
183 m_editMode = editMode;