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

KDE's Doxygen guidelines are available online.