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

akonadi

  • 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 #include "selftestdialog_p.h"
24 #include "erroroverlay_p.h"
25 
26 #include <kdebug.h>
27 #include <kglobal.h>
28 #include <klocalizedstring.h>
29 
30 #include <QtCore/QEventLoop>
31 #include <QtCore/QCoreApplication>
32 #include <QtCore/QTimer>
33 #include <QtCore/QPointer>
34 #include <QFrame>
35 
36 using namespace Akonadi;
37 
38 namespace Akonadi {
39 namespace Internal {
40 
41 class ControlProgressIndicator : public QFrame
42 {
43  public:
44  ControlProgressIndicator( QWidget *parent = 0 ) :
45  QFrame( parent )
46  {
47  setWindowModality( Qt::ApplicationModal );
48  resize( 400, 100 );
49  setWindowFlags( Qt::FramelessWindowHint | Qt::Dialog );
50  ui.setupUi( this );
51 
52  setFrameShadow( QFrame::Plain );
53  setFrameShape( QFrame::Box );
54  }
55 
56  void setMessage( const QString &msg )
57  {
58  ui.statusLabel->setText( msg );
59  }
60 
61  Ui::ControlProgressIndicator ui;
62 };
63 
64 class StaticControl : public Control
65 {
66  public:
67  StaticControl() : Control() {}
68 };
69 
70 }
71 
72 K_GLOBAL_STATIC( Internal::StaticControl, s_instance )
73 
74 
77 class Control::Private
78 {
79  public:
80  Private( Control *parent )
81  : mParent( parent ), mEventLoop( 0 ),
82  mProgressIndicator( 0 ),
83  mSuccess( false ),
84  mStarting( false ), mStopping( false )
85  {
86  }
87 
88  ~Private()
89  {
90  delete mProgressIndicator;
91  }
92 
93  void setupProgressIndicator( const QString &msg, QWidget *parent = 0 )
94  {
95  if ( !mProgressIndicator ) {
96  mProgressIndicator = new Internal::ControlProgressIndicator( parent );
97  }
98 
99  mProgressIndicator->setMessage( msg );
100  }
101 
102  void createErrorOverlays()
103  {
104  foreach ( QWidget *widget, mPendingOverlays ) {
105  if ( widget ) {
106  new ErrorOverlay( widget );
107  }
108  }
109  mPendingOverlays.clear();
110  }
111 
112  void cleanup()
113  {
114  s_instance.destroy();
115  }
116 
117  bool exec();
118  void serverStateChanged(ServerManager::State state);
119 
120  QPointer<Control> mParent;
121  QEventLoop *mEventLoop;
122  QPointer<Internal::ControlProgressIndicator> mProgressIndicator;
123  QList<QPointer<QWidget> > mPendingOverlays;
124  bool mSuccess;
125 
126  bool mStarting;
127  bool mStopping;
128 };
129 
130 bool Control::Private::exec()
131 {
132  if ( mProgressIndicator ) {
133  mProgressIndicator->show();
134  }
135 
136  kDebug() << "Starting/Stopping Akonadi (using an event loop).";
137  mEventLoop = new QEventLoop( mParent );
138  mEventLoop->exec();
139  mEventLoop->deleteLater();
140  mEventLoop = 0;
141 
142  if ( !mSuccess ) {
143  kWarning() << "Could not start/stop Akonadi!";
144  if ( mProgressIndicator && mStarting ) {
145  QPointer<SelfTestDialog> dlg = new SelfTestDialog( mProgressIndicator->parentWidget() );
146  dlg->exec();
147  delete dlg;
148  if ( !mParent ) {
149  return false;
150  }
151  }
152  }
153 
154  delete mProgressIndicator;
155  mProgressIndicator = 0;
156  mStarting = false;
157  mStopping = false;
158 
159  const bool rv = mSuccess;
160  mSuccess = false;
161  return rv;
162 }
163 
164 void Control::Private::serverStateChanged(ServerManager::State state)
165 {
166  kDebug() << state;
167  if ( mEventLoop && mEventLoop->isRunning() ) {
168  // ignore transient states going into the right direction
169  if ( ( mStarting && ( state == ServerManager::Starting || state == ServerManager::Upgrading ) ) ||
170  ( mStopping && state == ServerManager::Stopping ) )
171  return;
172  mEventLoop->quit();
173  mSuccess = ( mStarting && state == ServerManager::Running ) || ( mStopping && state == ServerManager::NotRunning );
174  }
175 }
176 
177 Control::Control()
178  : d( new Private( this ) )
179 {
180  connect( ServerManager::self(), SIGNAL(stateChanged(Akonadi::ServerManager::State)),
181  SLOT(serverStateChanged(Akonadi::ServerManager::State)) );
182  // mProgressIndicator is a widget, so it better be deleted before the QApplication is deleted
183  // Otherwise we get a crash in QCursor code with Qt-4.5
184  if ( QCoreApplication::instance() ) {
185  connect( QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(cleanup()) );
186  }
187 }
188 
189 Control::~Control()
190 {
191  delete d;
192 }
193 
194 bool Control::start()
195 {
196  if ( ServerManager::state() == ServerManager::Stopping ) {
197  kDebug() << "Server is currently being stopped, wont try to start it now";
198  return false;
199  }
200  if ( ServerManager::isRunning() || s_instance->d->mEventLoop ) {
201  kDebug() << "Server is already running";
202  return true;
203  }
204  s_instance->d->mStarting = true;
205  if ( !ServerManager::start() ) {
206  kDebug() << "ServerManager::start failed -> return false";
207  return false;
208  }
209  return s_instance->d->exec();
210 }
211 
212 bool Control::stop()
213 {
214  if ( ServerManager::state() == ServerManager::Starting ) {
215  return false;
216  }
217  if ( !ServerManager::isRunning() || s_instance->d->mEventLoop ) {
218  return true;
219  }
220  s_instance->d->mStopping = true;
221  if ( !ServerManager::stop() ) {
222  return false;
223  }
224  return s_instance->d->exec();
225 }
226 
227 bool Control::restart()
228 {
229  if ( ServerManager::isRunning() ) {
230  if ( !stop() ) {
231  return false;
232  }
233  }
234  return start();
235 }
236 
237 bool Control::start(QWidget * parent)
238 {
239  s_instance->d->setupProgressIndicator( i18n( "Starting Akonadi server..." ), parent );
240  return start();
241 }
242 
243 bool Control::stop(QWidget * parent)
244 {
245  s_instance->d->setupProgressIndicator( i18n( "Stopping Akonadi server..." ), parent );
246  return stop();
247 }
248 
249 bool Control::restart(QWidget * parent)
250 {
251  if ( ServerManager::isRunning() ) {
252  if ( !stop( parent ) ) {
253  return false;
254  }
255  }
256  return start( parent );
257 }
258 
259 void Control::widgetNeedsAkonadi(QWidget * widget)
260 {
261  s_instance->d->mPendingOverlays.append( widget );
262  // delay the overlay creation since we rely on widget being reparented
263  // correctly already
264  QTimer::singleShot( 0, s_instance, SLOT(createErrorOverlays()) );
265 }
266 
267 }
268 
269 #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:161
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:212
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:259
Akonadi::ServerManager::state
static State state()
Returns the state of the server.
Definition: servermanager.cpp:225
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:166
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:194
Akonadi::Control::Control
Control()
Creates the control object.
Definition: control.cpp:177
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:189
Akonadi::ServerManager::stop
static bool stop()
Stops the server.
Definition: servermanager.cpp:199
Akonadi::ServerManager::isRunning
static bool isRunning()
Checks if the server is available currently.
Definition: servermanager.cpp:220
Akonadi::Control::restart
static bool restart()
Restarts the Akonadi server synchronously.
Definition: control.cpp:227
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:51 by doxygen 1.8.6 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-4.13.3 API Reference

Skip menu "kdepimlibs-4.13.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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