Akonadi

akonadicontrol/agentinstance.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Volker Krause <[email protected]>
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 #include "agenttype.h"
12 
13 AgentInstance::AgentInstance(AgentManager &manager)
14  : mManager(manager)
15 {
16 }
17 
19 
20 void AgentInstance::quit()
21 {
22  if (mAgentControlInterface && mAgentControlInterface->isValid()) {
23  mAgentControlInterface->quit();
24  } else {
25  mPendingQuit = true;
26  }
27 }
28 
29 void AgentInstance::cleanup()
30 {
31  if (mAgentControlInterface && mAgentControlInterface->isValid()) {
32  mAgentControlInterface->cleanup();
33  }
34 }
35 
36 bool AgentInstance::obtainAgentInterface()
37 {
38  mAgentControlInterface = findInterface<org::freedesktop::Akonadi::Agent::Control>(Akonadi::DBus::Agent, "/");
39  mAgentStatusInterface = findInterface<org::freedesktop::Akonadi::Agent::Status>(Akonadi::DBus::Agent, "/");
40 
41  if (mPendingQuit && mAgentControlInterface && mAgentControlInterface->isValid()) {
42  mAgentControlInterface->quit();
43  mPendingQuit = false;
44  }
45 
46  if (!mAgentControlInterface || !mAgentStatusInterface) {
47  return false;
48  }
49 
50  mSearchInterface = findInterface<org::freedesktop::Akonadi::Agent::Search>(Akonadi::DBus::Agent, "/Search");
51 
52  connect(mAgentStatusInterface.get(),
53  qOverload<int, const QString &>(&OrgFreedesktopAkonadiAgentStatusInterface::status),
54  this,
55  &AgentInstance::statusChanged);
56  connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::advancedStatus, this, &AgentInstance::advancedStatusChanged);
57  connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::percent, this, &AgentInstance::percentChanged);
58  connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::warning, this, &AgentInstance::warning);
59  connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::error, this, &AgentInstance::error);
60  connect(mAgentStatusInterface.get(), &OrgFreedesktopAkonadiAgentStatusInterface::onlineChanged, this, &AgentInstance::onlineChanged);
61 
62  refreshAgentStatus();
63  return true;
64 }
65 
66 bool AgentInstance::obtainResourceInterface()
67 {
68  mResourceInterface = findInterface<org::freedesktop::Akonadi::Resource>(Akonadi::DBus::Resource, "/");
69 
70  if (!mResourceInterface) {
71  return false;
72  }
73 
74  connect(mResourceInterface.get(), &OrgFreedesktopAkonadiResourceInterface::nameChanged, this, &AgentInstance::resourceNameChanged);
75  refreshResourceStatus();
76  return true;
77 }
78 
79 bool AgentInstance::obtainPreprocessorInterface()
80 {
81  mPreprocessorInterface = findInterface<org::freedesktop::Akonadi::Preprocessor>(Akonadi::DBus::Preprocessor, "/");
82  return mPreprocessorInterface != nullptr;
83 }
84 
85 void AgentInstance::statusChanged(int status, const QString &statusMsg)
86 {
87  if (mStatus == status && mStatusMessage == statusMsg) {
88  return;
89  }
90  mStatus = status;
91  mStatusMessage = statusMsg;
92  Q_EMIT mManager.agentInstanceStatusChanged(mIdentifier, mStatus, mStatusMessage);
93 }
94 
95 void AgentInstance::advancedStatusChanged(const QVariantMap &status)
96 {
97  Q_EMIT mManager.agentInstanceAdvancedStatusChanged(mIdentifier, status);
98 }
99 
100 void AgentInstance::statusStateChanged(int status)
101 {
102  statusChanged(status, mStatusMessage);
103 }
104 
105 void AgentInstance::statusMessageChanged(const QString &msg)
106 {
107  statusChanged(mStatus, msg);
108 }
109 
110 void AgentInstance::percentChanged(int percent)
111 {
112  if (mPercent == percent) {
113  return;
114  }
115  mPercent = percent;
116  Q_EMIT mManager.agentInstanceProgressChanged(mIdentifier, mPercent, QString());
117 }
118 
119 void AgentInstance::warning(const QString &msg)
120 {
121  Q_EMIT mManager.agentInstanceWarning(mIdentifier, msg);
122 }
123 
124 void AgentInstance::error(const QString &msg)
125 {
126  Q_EMIT mManager.agentInstanceError(mIdentifier, msg);
127 }
128 
129 void AgentInstance::onlineChanged(bool state)
130 {
131  if (mOnline == state) {
132  return;
133  }
134  mOnline = state;
135  Q_EMIT mManager.agentInstanceOnlineChanged(mIdentifier, state);
136 }
137 
138 void AgentInstance::resourceNameChanged(const QString &name)
139 {
140  if (name == mResourceName) {
141  return;
142  }
143  mResourceName = name;
144  Q_EMIT mManager.agentInstanceNameChanged(mIdentifier, name);
145 }
146 
147 void AgentInstance::refreshAgentStatus()
148 {
149  if (!hasAgentInterface()) {
150  return;
151  }
152 
153  // async calls so we are not blocked by misbehaving agents
154  mAgentStatusInterface->callWithCallback(QStringLiteral("status"), QList<QVariant>(), this, SLOT(statusStateChanged(int)), SLOT(errorHandler(QDBusError)));
155  mAgentStatusInterface->callWithCallback(QStringLiteral("statusMessage"),
156  QList<QVariant>(),
157  this,
158  SLOT(statusMessageChanged(QString)),
159  SLOT(errorHandler(QDBusError)));
160  mAgentStatusInterface->callWithCallback(QStringLiteral("progress"), QList<QVariant>(), this, SLOT(percentChanged(int)), SLOT(errorHandler(QDBusError)));
161  mAgentStatusInterface->callWithCallback(QStringLiteral("isOnline"), QList<QVariant>(), this, SLOT(onlineChanged(bool)), SLOT(errorHandler(QDBusError)));
162 }
163 
164 void AgentInstance::refreshResourceStatus()
165 {
166  if (!hasResourceInterface()) {
167  return;
168  }
169 
170  // async call so we are not blocked by misbehaving resources
171  mResourceInterface->callWithCallback(QStringLiteral("name"), QList<QVariant>(), this, SLOT(resourceNameChanged(QString)), SLOT(errorHandler(QDBusError)));
172 }
173 
174 void AgentInstance::errorHandler(const QDBusError &error)
175 {
176  // avoid using the server tracer, can result in D-BUS lockups
177  qCCritical(AKONADICONTROL_LOG) << QStringLiteral("D-Bus communication error '%1': '%2'").arg(error.name(), error.message());
178  // TODO try again after some time, esp. on timeout errors
179 }
180 
181 template<typename T> std::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), QLatin1String(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 }
Status status() const
Returns the status of the agent instance.
The agent manager has knowledge about all available agents (it scans for .desktop files in the agent ...
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
Definition: item.h:32
QDBusConnection sessionBus()
QString name() const
Returns the user visible name of the agent instance.
~AgentInstance()
Destroys the agent instance object.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.