• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
control.cpp
1 /*
2  Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "control.h"
21 #include "servermanager.h"
22 #include "ui_controlprogressindicator.h"
23 #ifndef Q_OS_WINCE
24 #include "selftestdialog_p.h"
25 #include "erroroverlay_p.h"
26 #endif
27 
28 #include <kdebug.h>
29 #include <kglobal.h>
30 #include <klocalizedstring.h>
31 
32 #include <QtCore/QEventLoop>
33 #include <QtCore/QCoreApplication>
34 #include <QtCore/QTimer>
35 #include <QtCore/QPointer>
36 #include <QFrame>
37 
38 using namespace Akonadi;
39 
40 namespace Akonadi {
41 namespace Internal {
42 
43 class ControlProgressIndicator : public QFrame
44 {
45  public:
46  ControlProgressIndicator( QWidget *parent = 0 ) :
47  QFrame( parent )
48  {
49  setWindowModality( Qt::ApplicationModal );
50  resize( 400, 100 );
51  setWindowFlags( Qt::FramelessWindowHint | Qt::Dialog );
52  ui.setupUi( this );
53 
54  setFrameShadow( QFrame::Plain );
55  setFrameShape( QFrame::Box );
56  }
57 
58  void setMessage( const QString &msg )
59  {
60  ui.statusLabel->setText( msg );
61  }
62 
63  Ui::ControlProgressIndicator ui;
64 };
65 
66 class StaticControl : public Control
67 {
68  public:
69  StaticControl() : Control() {}
70 };
71 
72 }
73 
74 K_GLOBAL_STATIC( Internal::StaticControl, s_instance )
75 
76 
79 class Control::Private
80 {
81  public:
82  Private( Control *parent )
83  : mParent( parent ), mEventLoop( 0 ),
84  mProgressIndicator( 0 ),
85  mSuccess( false ),
86  mStarting( false ), mStopping( false )
87  {
88  }
89 
90  ~Private()
91  {
92  delete mProgressIndicator;
93  }
94 
95  void setupProgressIndicator( const QString &msg, QWidget *parent = 0 )
96  {
97  if ( !mProgressIndicator ) {
98  mProgressIndicator = new Internal::ControlProgressIndicator( parent );
99  }
100 
101  mProgressIndicator->setMessage( msg );
102  }
103 
104  void createErrorOverlays()
105  {
106 #ifndef Q_OS_WINCE
107  foreach ( QWidget *widget, mPendingOverlays ) {
108  if ( widget ) {
109  new ErrorOverlay( widget );
110  }
111  }
112 #endif
113  mPendingOverlays.clear();
114  }
115 
116  void cleanup()
117  {
118  s_instance.destroy();
119  }
120 
121  bool exec();
122  void serverStateChanged(ServerManager::State state);
123 
124  QPointer<Control> mParent;
125  QEventLoop *mEventLoop;
126  QPointer<Internal::ControlProgressIndicator> mProgressIndicator;
127  QList<QPointer<QWidget> > mPendingOverlays;
128  bool mSuccess;
129 
130  bool mStarting;
131  bool mStopping;
132 };
133 
134 bool Control::Private::exec()
135 {
136  if ( mProgressIndicator ) {
137  mProgressIndicator->show();
138  }
139 
140  kDebug() << "Starting/Stopping Akonadi (using an event loop).";
141  mEventLoop = new QEventLoop( mParent );
142  mEventLoop->exec();
143  mEventLoop->deleteLater();
144  mEventLoop = 0;
145 
146  if ( !mSuccess ) {
147  kWarning() << "Could not start/stop Akonadi!";
148 #ifndef Q_OS_WINCE
149  if ( mProgressIndicator && mStarting ) {
150  QPointer<SelfTestDialog> dlg = new SelfTestDialog( mProgressIndicator->parentWidget() );
151  dlg->exec();
152  delete dlg;
153  if ( !mParent ) {
154  return false;
155  }
156  }
157 #endif
158  }
159 
160  delete mProgressIndicator;
161  mProgressIndicator = 0;
162  mStarting = false;
163  mStopping = false;
164 
165  const bool rv = mSuccess;
166  mSuccess = false;
167  return rv;
168 }
169 
170 void Control::Private::serverStateChanged(ServerManager::State state)
171 {
172  kDebug() << state;
173  if ( mEventLoop && mEventLoop->isRunning() ) {
174  // ignore transient states going into the right direction
175  if ( ( mStarting && ( state == ServerManager::Starting || state == ServerManager::Upgrading ) ) ||
176  ( mStopping && state == ServerManager::Stopping ) )
177  return;
178  mEventLoop->quit();
179  mSuccess = ( mStarting && state == ServerManager::Running ) || ( mStopping && state == ServerManager::NotRunning );
180  }
181 }
182 
183 Control::Control()
184  : d( new Private( this ) )
185 {
186  connect( ServerManager::self(), SIGNAL(stateChanged(Akonadi::ServerManager::State)),
187  SLOT(serverStateChanged(Akonadi::ServerManager::State)) );
188  // mProgressIndicator is a widget, so it better be deleted before the QApplication is deleted
189  // Otherwise we get a crash in QCursor code with Qt-4.5
190  if ( QCoreApplication::instance() ) {
191  connect( QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(cleanup()) );
192  }
193 }
194 
195 Control::~Control()
196 {
197  delete d;
198 }
199 
200 bool Control::start()
201 {
202  if ( ServerManager::state() == ServerManager::Stopping ) {
203  kDebug() << "Server is currently being stopped, wont try to start it now";
204  return false;
205  }
206  if ( ServerManager::isRunning() || s_instance->d->mEventLoop ) {
207  kDebug() << "Server is already running";
208  return true;
209  }
210  s_instance->d->mStarting = true;
211  if ( !ServerManager::start() ) {
212  kDebug() << "ServerManager::start failed -> return false";
213  return false;
214  }
215  return s_instance->d->exec();
216 }
217 
218 bool Control::stop()
219 {
220  if ( ServerManager::state() == ServerManager::Starting ) {
221  return false;
222  }
223  if ( !ServerManager::isRunning() || s_instance->d->mEventLoop ) {
224  return true;
225  }
226  s_instance->d->mStopping = true;
227  if ( !ServerManager::stop() ) {
228  return false;
229  }
230  return s_instance->d->exec();
231 }
232 
233 bool Control::restart()
234 {
235  if ( ServerManager::isRunning() ) {
236  if ( !stop() ) {
237  return false;
238  }
239  }
240  return start();
241 }
242 
243 bool Control::start(QWidget * parent)
244 {
245  s_instance->d->setupProgressIndicator( i18n( "Starting Akonadi server..." ), parent );
246  return start();
247 }
248 
249 bool Control::stop(QWidget * parent)
250 {
251  s_instance->d->setupProgressIndicator( i18n( "Stopping Akonadi server..." ), parent );
252  return stop();
253 }
254 
255 bool Control::restart(QWidget * parent)
256 {
257  if ( ServerManager::isRunning() ) {
258  if ( !stop( parent ) ) {
259  return false;
260  }
261  }
262  return start( parent );
263 }
264 
265 void Control::widgetNeedsAkonadi(QWidget * widget)
266 {
267  s_instance->d->mPendingOverlays.append( widget );
268  // delay the overlay creation since we rely on widget being reparented
269  // correctly already
270  QTimer::singleShot( 0, s_instance, SLOT(createErrorOverlays()) );
271 }
272 
273 }
274 
275 #include "moc_control.cpp"
Akonadi::ServerManager::self
static ServerManager * self()
Returns the singleton instance of this class, for connecting to its signals.
Definition: servermanager.cpp:152
Akonadi::Control
Provides methods to control the Akonadi server process.
Definition: control.h:62
Akonadi::Control::stop
static bool stop()
Stops the Akonadi server synchronously if it is currently running.
Definition: control.cpp:218
Akonadi::ServerManager::Stopping
Server is shutting down.
Definition: servermanager.h:54
Akonadi::Control::widgetNeedsAkonadi
static void widgetNeedsAkonadi(QWidget *widget)
Disable the given widget when Akonadi is not operational and show an error overlay (given enough spac...
Definition: control.cpp:265
Akonadi::ServerManager::state
static State state()
Returns the state of the server.
Definition: servermanager.cpp:218
Akonadi::ErrorOverlay
Definition: erroroverlay_p.h:43
Akonadi::ServerManager::Upgrading
Server is performing a database upgrade as part of a new startup.
Definition: servermanager.h:56
Akonadi::ServerManager::NotRunning
Server is not running, could be no one started it yet or it failed to start.
Definition: servermanager.h:51
Akonadi::ServerManager::start
static bool start()
Starts the server.
Definition: servermanager.cpp:157
Akonadi::ServerManager::Starting
Server was started but is not yet running.
Definition: servermanager.h:52
Akonadi::Control::start
static bool start()
Starts the Akonadi server synchronously if it is not already running.
Definition: control.cpp:200
Akonadi::Control::Control
Control()
Creates the control object.
Definition: control.cpp:183
Akonadi::ServerManager::Running
Server is running and operational.
Definition: servermanager.h:53
Akonadi::ServerManager::State
State
Enum for the various states the server can be in.
Definition: servermanager.h:50
Akonadi::SelfTestDialog
A dialog that checks the current status of the Akonadi system.
Definition: selftestdialog_p.h:44
Akonadi::Control::~Control
~Control()
Destroys the control object.
Definition: control.cpp:195
Akonadi::ServerManager::stop
static bool stop()
Stops the server.
Definition: servermanager.cpp:190
Akonadi::ServerManager::isRunning
static bool isRunning()
Checks if the server is available currently.
Definition: servermanager.cpp:213
Akonadi::Control::restart
static bool restart()
Restarts the Akonadi server synchronously.
Definition: control.cpp:233
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal