Akonadi

agentserver.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "agentserver.h"
8 #include "agentthread.h"
9 #include "akonadiagentserver_debug.h"
10 
11 #include <private/dbus_p.h>
12 
13 #include <QCoreApplication>
14 #include <QDBusConnection>
15 #include <QPluginLoader>
16 #include <QTimer>
17 
18 using namespace Akonadi;
19 using namespace std::chrono_literals;
20 AgentServer::AgentServer(QObject *parent)
21  : QObject(parent)
22 {
23  QDBusConnection::sessionBus().registerObject(QStringLiteral(AKONADI_DBUS_AGENTSERVER_PATH), this, QDBusConnection::ExportScriptableSlots);
24 }
25 
26 AgentServer::~AgentServer()
27 {
28  qCDebug(AKONADIAGENTSERVER_LOG) << Q_FUNC_INFO;
29  if (!m_quiting) {
30  quit();
31  }
32 }
33 
34 void AgentServer::agentInstanceConfigure(const QString &identifier, qlonglong windowId)
35 {
36  m_configureQueue.enqueue(ConfigureInfo(identifier, windowId));
37  if (!m_processingConfigureRequests) { // Start processing the requests if needed.
38  QTimer::singleShot(0s, this, &AgentServer::processConfigureRequest);
39  }
40 }
41 
42 bool AgentServer::started(const QString &identifier) const
43 {
44  return m_agents.contains(identifier);
45 }
46 
47 void AgentServer::startAgent(const QString &identifier, const QString &typeIdentifier, const QString &fileName)
48 {
49  qCDebug(AKONADIAGENTSERVER_LOG) << "Starting agent" << identifier << "of type" << typeIdentifier << "(file:" << fileName << ")";
50 
51  // First try to load it statically
52  const QObjectList objList = QPluginLoader::staticInstances();
53  for (QObject *plugin : objList) {
54  if (plugin->objectName() == fileName) {
55  auto thread = new AgentThread(identifier, plugin, this);
56  m_agents.insert(identifier, thread);
57  thread->start();
58  return;
59  }
60  }
61 
62  QPluginLoader *loader = m_agentLoader.load(fileName);
63  if (!loader) {
64  return; // No plugin found, debug output in AgentLoader.
65  }
66 
67  Q_ASSERT(loader->isLoaded());
68 
69  auto thread = new AgentThread(identifier, loader->instance(), this);
70  m_agents.insert(identifier, thread);
71  thread->start();
72 }
73 
74 void AgentServer::stopAgent(const QString &identifier)
75 {
76  AgentThread *thread = m_agents.take(identifier);
77  if (thread) {
78  thread->quit();
79  thread->wait();
80  delete thread;
81  }
82 }
83 
84 void AgentServer::quit()
85 {
86  Q_ASSERT(!m_quiting);
87  m_quiting = true;
88 
90  while (it.hasNext()) {
91  stopAgent(it.next().key());
92  }
93 
95 }
96 
97 void AgentServer::processConfigureRequest()
98 {
99  if (m_processingConfigureRequests) {
100  return; // Protect against reentrancy
101  }
102 
103  m_processingConfigureRequests = true;
104 
105  while (!m_configureQueue.empty()) {
106  const ConfigureInfo info = m_configureQueue.dequeue();
107  // call configure on the agent with id info.first for windowId info.second.
108  Q_ASSERT(m_agents.contains(info.first));
109  AgentThread *thread = m_agents.value(info.first);
110  thread->configure(info.second);
111  }
112 
113  m_processingConfigureRequests = false;
114 }
bool wait(QDeadlineTimer deadline)
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
void configure(qlonglong windowId)
Configures the agent.
Definition: agentthread.cpp:39
void quit()
QDBusConnection sessionBus()
QObjectList staticInstances()
QCoreApplication * instance()
A class that encapsulates an agent instance inside a thread.
Definition: agentthread.h:16
const QList< QKeySequence > & quit()
bool isLoaded() const const
QObject * instance()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jun 4 2023 03:52:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.