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;
115 }
116 if (ServerManager::isRunning() || s_instance->d->mEventLoop) {
117 qCDebug(AKONADICORE_LOG) << "Server is already running";
118 return true;
119 }
120 s_instance->d->mStarting = true;
121 if (!ServerManager::start()) {
122 qCDebug(AKONADICORE_LOG) << "ServerManager::start failed -> return false";
123 return false;
124 }
125 return s_instance->d->exec();
126}
127
129{
131 return false;
132 }
133 if (!ServerManager::isRunning() || s_instance->d->mEventLoop) {
134 return true;
135 }
136 s_instance->d->mStopping = true;
137 if (!ServerManager::stop()) {
138 return false;
139 }
140 return s_instance->d->exec();
141}
142
144{
146 if (!stop()) {
147 return false;
148 }
149 }
150 return start();
151}
152
153} // namespace Akonadi
154
155#include "control.moc"
156
157#include "moc_control.cpp"
Provides methods to control the Akonadi server process.
Definition control.h:54
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:143
static bool stop()
Stops the Akonadi server synchronously if it is currently running.
Definition control.cpp:128
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()
int exec(ProcessEventsFlags flags)
bool isRunning() const const
void quit()
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.