Akonadi

agentserver.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
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
18using namespace Akonadi;
19using namespace std::chrono_literals;
20AgentServer::AgentServer(QObject *parent)
21 : QObject(parent)
22{
23 QDBusConnection::sessionBus().registerObject(QStringLiteral(AKONADI_DBUS_AGENTSERVER_PATH), this, QDBusConnection::ExportScriptableSlots);
24}
25
26AgentServer::~AgentServer()
27{
29 if (!m_quiting) {
30 quit();
31 }
32}
33
34void 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
42bool AgentServer::started(const QString &identifier) const
43{
44 return m_agents.contains(identifier);
45}
46
47void 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
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
74void 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
84void 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
97void 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}
115
116#include "moc_agentserver.cpp"
A class that encapsulates an agent instance inside a thread.
Definition agentthread.h:17
Helper integration between Akonadi and Qt.
QCoreApplication * instance()
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
typedef QObjectList
T qobject_cast(QObject *object)
QThread * thread() const const
QObject * instance()
bool isLoaded() const const
QObjectList staticInstances()
T dequeue()
void enqueue(const T &t)
void quit()
void start(Priority priority)
bool wait(QDeadlineTimer deadline)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.