Akonadi

agentinstancecreatejob.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 "agentinstancecreatejob.h"
8
9#include "agentinstance.h"
10#include "agentmanager.h"
11#include "agentmanager_p.h"
12#include "controlinterface.h"
13#include "kjobprivatebase_p.h"
14#include <QDBusConnection>
15
16#include <KLocalizedString>
17
18#include <QTimer>
19
20#ifdef Q_OS_UNIX
21#include <signal.h>
22#include <sys/types.h>
23#endif
24
25using namespace Akonadi;
26
27static const int safetyTimeout = 10000; // ms
28
29namespace Akonadi
30{
31/**
32 * @internal
33 */
34class AgentInstanceCreateJobPrivate : public KJobPrivateBase
35{
36 Q_OBJECT
37public:
38 explicit AgentInstanceCreateJobPrivate(AgentInstanceCreateJob *parent)
39 : q(parent)
40 , safetyTimer(new QTimer(parent))
41 {
42 connect(AgentManager::self(), &AgentManager::instanceAdded, this, &AgentInstanceCreateJobPrivate::agentInstanceAdded);
43 connect(safetyTimer, &QTimer::timeout, this, &AgentInstanceCreateJobPrivate::timeout);
44 }
45
46 void agentInstanceAdded(const AgentInstance &instance) const
47 {
48 if (agentInstance == instance && !tooLate) {
49 safetyTimer->stop();
50 q->emitResult();
51 }
52 }
53
54 void timeout()
55 {
56 tooLate = true;
57 q->setError(KJob::UserDefinedError);
58 q->setErrorText(i18n("Agent instance creation timed out."));
59 q->emitResult();
60 }
61
62 void doStart() override;
63
64 AgentInstanceCreateJob *const q;
65 AgentType agentType;
66 QString agentTypeId;
67 AgentInstance agentInstance;
68 QTimer *const safetyTimer;
69 bool tooLate = false;
70};
71
72} // namespace Akonadi
73
75 : KJob(parent)
76 , d(new AgentInstanceCreateJobPrivate(this))
77{
78 d->agentType = agentType;
79}
80
82 : KJob(parent)
83 , d(new AgentInstanceCreateJobPrivate(this))
84{
85 d->agentTypeId = typeId;
86}
87
89
91{
92 return d->agentInstance;
93}
94
96{
97 d->start();
98}
99
100void AgentInstanceCreateJobPrivate::doStart()
101{
102 if (!agentType.isValid() && !agentTypeId.isEmpty()) {
103 agentType = AgentManager::self()->type(agentTypeId);
104 }
105
106 if (!agentType.isValid()) {
107 q->setError(KJob::UserDefinedError);
108 q->setErrorText(i18n("Unable to obtain agent type '%1'.", agentTypeId));
110 return;
111 }
112
113 agentInstance = AgentManager::self()->d->createInstance(agentType);
114 if (!agentInstance.isValid()) {
115 q->setError(KJob::UserDefinedError);
116 q->setErrorText(i18n("Unable to create agent instance."));
118 } else {
119 int timeout = safetyTimeout;
120#ifdef Q_OS_UNIX
121 // Increate the timeout when valgrinding the agent, because that slows down things a log.
122 const QString agentValgrind = QString::fromLocal8Bit(qgetenv("AKONADI_VALGRIND"));
123 if (!agentValgrind.isEmpty() && agentType.identifier().contains(agentValgrind)) {
124 timeout *= 15;
125 }
126#endif
127 // change the timeout when debugging the agent, because we need time to start the debugger
128 const QString agentDebugging = QString::fromLocal8Bit(qgetenv("AKONADI_DEBUG_WAIT"));
129 if (!agentDebugging.isEmpty()) {
130 // we are debugging
131 const QString agentDebuggingTimeout = QString::fromLocal8Bit(qgetenv("AKONADI_DEBUG_TIMEOUT"));
132 if (agentDebuggingTimeout.isEmpty()) {
133 // use default value of 150 seconds (the same as "valgrinding", this has to be checked)
134 timeout = 15 * safetyTimeout;
135 } else {
136 // use own value
137 timeout = agentDebuggingTimeout.toInt();
138 }
139 }
140 safetyTimer->start(timeout);
141 }
142}
143
144#include "agentinstancecreatejob.moc"
145
146#include "moc_agentinstancecreatejob.cpp"
~AgentInstanceCreateJob() override
Destroys the agent instance create job.
AgentInstanceCreateJob(const AgentType &type, QObject *parent=nullptr)
Creates a new agent instance create job.
void start() override
Starts the instance creation.
AgentInstance instance() const
Returns the AgentInstance object of the newly created agent instance.
A representation of an agent instance.
AgentType type(const QString &identifier) const
Returns the agent type with the given identifier or an invalid agent type if the identifier does not ...
static AgentManager * self()
Returns the global instance of the agent manager.
void instanceAdded(const Akonadi::AgentInstance &instance)
This signal is emitted whenever a new agent instance was created.
A representation of an agent type.
bool isValid() const
Returns whether the agent type is valid.
void setErrorText(const QString &errorText)
void emitResult()
void setError(int errorCode)
KJob(QObject *parent=nullptr)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
QObject(QObject *parent)
QObject * parent() const const
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
int toInt(bool *ok, int base) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:53:21 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.