Akonadi

control.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "control.h"
8#include "akonadicore_debug.h"
9#include "servermanager.h"
10
11#include <QCoreApplication>
12#include <QEventLoop>
13#include <QPointer>
14
15using namespace Akonadi;
16
17namespace Akonadi
18{
19namespace Internal
20{
21class StaticControl : public Control
22{
24};
25
26}
27
28Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) // NOLINT(readability-redundant-member-init)
29
30/**
31 * @internal
32 */
33class ControlPrivate
34{
35public:
36 explicit ControlPrivate(Control *parent)
37 : mParent(parent)
38 {
39 }
40
41 void cleanup()
42 {
43 }
44
45 bool exec();
46 void serverStateChanged(ServerManager::State state);
47
48 QPointer<Control> mParent;
49 QEventLoop *mEventLoop = nullptr;
50 bool mSuccess = false;
51
52 bool mStarting = false;
53 bool mStopping = false;
54};
55
56bool ControlPrivate::exec()
57{
58 qCDebug(AKONADICORE_LOG) << "Starting/Stopping Akonadi (using an event loop).";
59 mEventLoop = new QEventLoop(mParent);
60 mEventLoop->exec();
61 mEventLoop->deleteLater();
62 mEventLoop = nullptr;
63
64 if (!mSuccess) {
65 qCWarning(AKONADICORE_LOG) << "Could not start/stop Akonadi!";
66 }
67
68 mStarting = false;
69 mStopping = false;
70
71 const bool rv = mSuccess;
72 mSuccess = false;
73 return rv;
74}
75
76void ControlPrivate::serverStateChanged(ServerManager::State state)
77{
78 qCDebug(AKONADICORE_LOG) << "Server state changed to" << state;
79 if (mEventLoop && mEventLoop->isRunning()) {
80 // ignore transient states going into the right direction
81 if ((mStarting && (state == ServerManager::Starting || state == ServerManager::Upgrading)) || (mStopping && state == ServerManager::Stopping)) {
82 return;
83 }
84 mEventLoop->quit();
85 mSuccess = (mStarting && state == ServerManager::Running) || (mStopping && state == ServerManager::NotRunning);
86 }
87}
88
90 : d(new ControlPrivate(this))
91{
93 d->serverStateChanged(state);
94 });
95 // mProgressIndicator is a widget, so it better be deleted before the QApplication is deleted
96 // Otherwise we get a crash in QCursor code with Qt-4.5
99 d->cleanup();
100 });
101 }
102}
103
104Control::~Control() = default;
105
107{
108 switch (ServerManager::state()) {
110 qCDebug(AKONADICORE_LOG) << "Server is currently being stopped, won't try to start it now";
111 return false;
113 qCDebug(AKONADICORE_LOG) << "Server is already broken: " << ServerManager::brokenReason();
114 return false;
119 break;
120 }
121 if (ServerManager::isRunning() || s_instance->d->mEventLoop) {
122 qCDebug(AKONADICORE_LOG) << "Server is already running";
123 return true;
124 }
125 s_instance->d->mStarting = true;
126 if (!ServerManager::start()) {
127 qCDebug(AKONADICORE_LOG) << "ServerManager::start failed -> return false";
128 return false;
129 }
130 return s_instance->d->exec();
131}
132
134{
136 return false;
137 }
138 if (!ServerManager::isRunning() || s_instance->d->mEventLoop) {
139 return true;
140 }
141 s_instance->d->mStopping = true;
142 if (!ServerManager::stop()) {
143 return false;
144 }
145 return s_instance->d->exec();
146}
147
149{
151 if (!stop()) {
152 return false;
153 }
154 }
155 return start();
156}
157
158} // namespace Akonadi
159
160#include "control.moc"
161
162#include "moc_control.cpp"
static bool start()
Starts the Akonadi server synchronously if it is not already running.
Definition control.cpp:106
~Control() override
Destroys the control object.
static bool restart()
Restarts the Akonadi server synchronously.
Definition control.cpp:148
static bool stop()
Stops the Akonadi server synchronously if it is currently running.
Definition control.cpp:133
Control()
Creates the control object.
Definition control.cpp:89
static State state()
Returns the state of the server.
static QString brokenReason()
Returns the reason why the Server is broken, if known.
static bool isRunning()
Checks if the server is available currently.
static bool start()
Starts the server.
static bool stop()
Stops the server.
State
Enum for the various states the server can be in.
@ Running
Server is running and operational.
@ Starting
Server was started but is not yet running.
@ Broken
Server is not operational and an error has been detected.
@ Upgrading
Server is performing a database upgrade as part of a new startup.
@ NotRunning
Server is not running, could be no one started it yet or it failed to start.
@ Stopping
Server is shutting down.
void stateChanged(Akonadi::ServerManager::State state)
Emitted whenever the server state changes.
static ServerManager * self()
Returns the singleton instance of this class, for connecting to its signals.
Helper integration between Akonadi and Qt.
QCoreApplication * instance()
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 31 2025 12:07:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.