Akonadi

akonadicontrol/agentinstance.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "agentinstance.h"
8#include "akonadicontrol_debug.h"
9
10#include "agentmanager.h"
11
12AgentInstance::AgentInstance(AgentManager &manager)
13 : mManager(manager)
14{
15}
16
17AgentInstance::~AgentInstance() = default;
18
19void AgentInstance::quit()
20{
21 if (mAgentControlInterface && mAgentControlInterface->isValid()) {
22 mAgentControlInterface->quit();
23 } else {
24 mPendingQuit = true;
25 }
26}
27
28void AgentInstance::cleanup()
29{
30 if (mAgentControlInterface && mAgentControlInterface->isValid()) {
31 mAgentControlInterface->cleanup();
32 }
33}
34
35bool AgentInstance::obtainAgentInterface()
36{
37 mAgentControlInterface = findInterface<org::freedesktop::Akonadi::Agent::Control>(Akonadi::DBus::Agent, "/");
38 mAgentStatusInterface = findInterface<org::freedesktop::Akonadi::Agent::Status>(Akonadi::DBus::Agent, "/");
39
40 if (mPendingQuit && mAgentControlInterface && mAgentControlInterface->isValid()) {
41 mAgentControlInterface->quit();
42 mPendingQuit = false;
43 }
44
45 if (!mAgentControlInterface || !mAgentStatusInterface) {
46 return false;
47 }
48
49 mSearchInterface = findInterface<org::freedesktop::Akonadi::Agent::Search>(Akonadi::DBus::Agent, "/Search");
50
51 connect(mAgentStatusInterface.get(),
52 qOverload<int, const QString &>(&OrgFreedesktopAkonadiAgentStatusInterface::status),
53 this,
54 &AgentInstance::statusChanged);
55 connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::advancedStatus, this, &AgentInstance::advancedStatusChanged);
56 connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::percent, this, &AgentInstance::percentChanged);
57 connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::warning, this, &AgentInstance::warning);
58 connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::error, this, &AgentInstance::error);
59 connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::onlineChanged, this, &AgentInstance::onlineChanged);
60
61 refreshAgentStatus();
62 return true;
63}
64
65bool AgentInstance::obtainResourceInterface()
66{
67 mResourceInterface = findInterface<org::freedesktop::Akonadi::Resource>(Akonadi::DBus::Resource, "/");
68
69 if (!mResourceInterface) {
70 return false;
71 }
72
73 connect(mResourceInterface.get(), &OrgFreedesktopAkonadiResourceInterface::nameChanged, this, &AgentInstance::resourceNameChanged);
74 refreshResourceStatus();
75 return true;
76}
77
78bool AgentInstance::obtainPreprocessorInterface()
79{
80 mPreprocessorInterface = findInterface<org::freedesktop::Akonadi::Preprocessor>(Akonadi::DBus::Preprocessor, "/");
81 return mPreprocessorInterface != nullptr;
82}
83
84void AgentInstance::statusChanged(int status, const QString &statusMsg)
85{
86 if (mStatus == status && mStatusMessage == statusMsg) {
87 return;
88 }
89 mStatus = status;
90 mStatusMessage = statusMsg;
91 Q_EMIT mManager.agentInstanceStatusChanged(mIdentifier, mStatus, mStatusMessage);
92}
93
94void AgentInstance::advancedStatusChanged(const QVariantMap &status)
95{
96 Q_EMIT mManager.agentInstanceAdvancedStatusChanged(mIdentifier, status);
97}
98
99void AgentInstance::statusStateChanged(int status)
100{
101 statusChanged(status, mStatusMessage);
102}
103
104void AgentInstance::statusMessageChanged(const QString &msg)
105{
106 statusChanged(mStatus, msg);
107}
108
109void AgentInstance::percentChanged(int percent)
110{
111 if (mPercent == percent) {
112 return;
113 }
114 mPercent = percent;
115 Q_EMIT mManager.agentInstanceProgressChanged(mIdentifier, mPercent, QString());
116}
117
118void AgentInstance::warning(const QString &msg)
119{
120 Q_EMIT mManager.agentInstanceWarning(mIdentifier, msg);
121}
122
123void AgentInstance::error(const QString &msg)
124{
125 Q_EMIT mManager.agentInstanceError(mIdentifier, msg);
126}
127
128void AgentInstance::onlineChanged(bool state)
129{
130 if (mOnline == state) {
131 return;
132 }
133 mOnline = state;
134 Q_EMIT mManager.agentInstanceOnlineChanged(mIdentifier, state);
135}
136
137void AgentInstance::resourceNameChanged(const QString &name)
138{
139 if (name == mResourceName) {
140 return;
141 }
142 mResourceName = name;
143 Q_EMIT mManager.agentInstanceNameChanged(mIdentifier, name);
144}
145
146void AgentInstance::refreshAgentStatus()
147{
148 if (!hasAgentInterface()) {
149 return;
150 }
151
152 // async calls so we are not blocked by misbehaving agents
153 mAgentStatusInterface->callWithCallback(QStringLiteral("status"), QList<QVariant>(), this, SLOT(statusStateChanged(int)), SLOT(errorHandler(QDBusError)));
154 mAgentStatusInterface->callWithCallback(QStringLiteral("statusMessage"),
156 this,
157 SLOT(statusMessageChanged(QString)),
158 SLOT(errorHandler(QDBusError)));
159 mAgentStatusInterface->callWithCallback(QStringLiteral("progress"), QList<QVariant>(), this, SLOT(percentChanged(int)), SLOT(errorHandler(QDBusError)));
160 mAgentStatusInterface->callWithCallback(QStringLiteral("isOnline"), QList<QVariant>(), this, SLOT(onlineChanged(bool)), SLOT(errorHandler(QDBusError)));
161}
162
163void AgentInstance::refreshResourceStatus()
164{
165 if (!hasResourceInterface()) {
166 return;
167 }
168
169 // async call so we are not blocked by misbehaving resources
170 mResourceInterface->callWithCallback(QStringLiteral("name"), QList<QVariant>(), this, SLOT(resourceNameChanged(QString)), SLOT(errorHandler(QDBusError)));
171}
172
173void AgentInstance::errorHandler(const QDBusError &error)
174{
175 // avoid using the server tracer, can result in D-BUS lockups
176 qCCritical(AKONADICONTROL_LOG) << QStringLiteral("D-Bus communication error '%1': '%2'").arg(error.name(), error.message());
177 // TODO try again after some time, esp. on timeout errors
178}
179
180template<typename T>
181std::unique_ptr<T> AgentInstance::findInterface(Akonadi::DBus::AgentType agentType, const char *path)
182{
183 auto iface = std::make_unique<T>(Akonadi::DBus::agentServiceName(mIdentifier, agentType), QLatin1StringView(path), QDBusConnection::sessionBus(), this);
184
185 if (!iface || !iface->isValid()) {
186 qCCritical(AKONADICONTROL_LOG) << Q_FUNC_INFO << "Cannot connect to agent instance with identifier" << mIdentifier
187 << ", error message:" << (iface ? iface->lastError().message() : QString());
188 return {};
189 }
190 return iface;
191}
192
193#include "moc_agentinstance.cpp"
The agent manager has knowledge about all available agents (it scans for .desktop files in the agent ...
void agentInstanceError(const QString &agentIdentifier, const QString &message)
This signal is emitted whenever an agent instance raised an error.
void agentInstanceProgressChanged(const QString &agentIdentifier, uint progress, const QString &message)
This signal is emitted whenever the progress of an agent instance has changed.
void agentInstanceNameChanged(const QString &agentIdentifier, const QString &name)
This signal is emitted whenever the name of the agent instance has changed.
void agentInstanceStatusChanged(const QString &agentIdentifier, int status, const QString &message)
This signal is emitted whenever the status of an agent instance has changed.
void agentInstanceAdvancedStatusChanged(const QString &agentIdentifier, const QVariantMap &status)
This signal is emitted whenever the status of an agent instance has changed.
void agentInstanceWarning(const QString &agentIdentifier, const QString &message)
This signal is emitted whenever an agent instance raised a warning.
void agentInstanceOnlineChanged(const QString &agentIdentifier, bool state)
Emitted when the online state of an agent changed.
Q_SCRIPTABLE CaptureState status()
QString name(GameStandardAction id)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QDBusConnection sessionBus()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.