Pimcommon

createresource.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "createresource.h"
8
9#include "pimcommonakonadi_debug.h"
10#include <KLocalizedString>
11
12#include <Akonadi/AgentInstanceCreateJob>
13#include <Akonadi/AgentManager>
14#include <Akonadi/AgentType>
15
16#include <QDBusInterface>
17#include <QDBusReply>
18#include <QMetaMethod>
19
20using namespace Akonadi;
21
22using namespace PimCommon;
23
24CreateResource::CreateResource(QObject *parent)
25 : QObject(parent)
26{
27}
28
29CreateResource::~CreateResource() = default;
30
31// code from accountwizard
32static QMetaType::Type argumentType(const QMetaObject *mo, const QString &method)
33{
35 const int numberOfMethod(mo->methodCount());
36 for (int i = 0; i < numberOfMethod; ++i) {
37 const QString signature = QString::fromLatin1(mo->method(i).methodSignature());
38 if (signature.contains(method + QLatin1Char('('))) {
39 m = mo->method(i);
40 break;
41 }
42 }
43
44 if (m.methodSignature().isEmpty()) {
45 qCWarning(PIMCOMMONAKONADI_LOG) << "Did not find D-Bus method: " << method << " available methods are:";
46 for (int i = 0; i < numberOfMethod; ++i) {
47 qCWarning(PIMCOMMONAKONADI_LOG) << mo->method(i).methodSignature();
48 }
50 }
51
52 const QList<QByteArray> argTypes = m.parameterTypes();
53 if (argTypes.count() != 1) {
55 }
56
57 return static_cast<QMetaType::Type>(QMetaType::fromName(argTypes.first().constData()).id());
58}
59
60QString CreateResource::createResource(const QString &resources, const QString &name, const QMap<QString, QVariant> &settings, bool synchronizeTree)
61{
62 const AgentType type = AgentManager::self()->type(resources);
63 if (!type.isValid()) {
64 Q_EMIT createResourceError(i18n("Resource type '%1' is not available.", resources));
65 return {};
66 }
67
68 // check if unique instance already exists
69 qCDebug(PIMCOMMONAKONADI_LOG) << type.capabilities();
70 if (type.capabilities().contains(QLatin1StringView("Unique"))) {
71 const AgentInstance::List lstInstances = AgentManager::self()->instances();
72 for (const AgentInstance &instance : lstInstances) {
73 qCDebug(PIMCOMMONAKONADI_LOG) << instance.type().identifier() << (instance.type() == type);
74 if (instance.type() == type) {
75 Q_EMIT createResourceInfo(i18n("Resource '%1' is already set up.", type.name()));
76 return {};
77 }
78 }
79 }
80
81 Q_EMIT createResourceInfo(i18n("Creating resource instance for '%1'...", type.name()));
82 auto job = new AgentInstanceCreateJob(type, this);
83 if (job->exec()) {
84 Akonadi::AgentInstance instance = job->instance();
85
86 if (!settings.isEmpty()) {
87 Q_EMIT createResourceInfo(i18n("Configuring resource instance..."));
88 QDBusInterface iface(QLatin1StringView("org.freedesktop.Akonadi.Resource.") + instance.identifier(), QStringLiteral("/Settings"));
89 if (!iface.isValid()) {
90 Q_EMIT createResourceError(i18n("Unable to configure resource instance."));
91 return {};
92 }
93
94 // configure resource
95 if (!name.isEmpty()) {
96 instance.setName(name);
97 }
99 for (QMap<QString, QVariant>::const_iterator it = settings.constBegin(); it != end; ++it) {
100 qCDebug(PIMCOMMONAKONADI_LOG) << "Setting up " << it.key() << " for agent " << instance.identifier();
101 const QString methodName = QStringLiteral("set%1").arg(it.key());
102 QVariant arg = it.value();
103 const QMetaType::Type targetType = argumentType(iface.metaObject(), methodName);
104 if (arg.metaType().id() != targetType) {
105 Q_EMIT createResourceError(
106 i18n("Could not convert value of setting '%1' to required type %2.", it.key(), QString::fromLatin1(QMetaType(targetType).name())));
107 return {};
108 }
109 QDBusReply<void> reply = iface.call(methodName, arg);
110 if (!reply.isValid()) {
111 Q_EMIT createResourceError(i18n("Could not set setting '%1': %2", it.key(), reply.error().message()));
112 return {};
113 }
114 }
115 QDBusReply<void> reply = iface.call(QStringLiteral("save"));
116 if (!reply.isValid()) {
117 Q_EMIT createResourceError(i18n("Could not save settings: %1", reply.error().message()));
118 return {};
119 }
120 instance.reconfigure();
121 if (synchronizeTree) {
122 instance.synchronizeCollectionTree();
123 }
124 }
125
126 Q_EMIT createResourceInfo(i18n("Resource setup completed."));
127 return instance.identifier();
128 } else {
129 if (job->error()) {
130 Q_EMIT createResourceError(i18n("Failed to create resource instance: %1", job->errorText()));
131 }
132 }
133 return {};
134}
135
136#include "moc_createresource.cpp"
QString identifier() const
void setName(const QString &name)
void reconfigure() const
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
const QList< QKeySequence > & end()
folderdialogacltab.h
bool isEmpty() const const
QString message() const const
const QDBusError & error()
bool isValid() const const
qsizetype count() const const
T & first()
const_iterator constBegin() const const
const_iterator constEnd() const const
bool isEmpty() const const
QByteArray methodSignature() const const
QList< QByteArray > parameterTypes() const const
QMetaMethod method(int index) const const
int methodCount() const const
QMetaType fromName(QByteArrayView typeName)
int id() const const
Q_EMITQ_EMIT
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QMetaType metaType() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.