• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 #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 <klocale.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  mEventLoop->quit();
175  mSuccess = ( mStarting && state == ServerManager::Running ) || ( mStopping && state == ServerManager::NotRunning );
176  }
177 }
178 
179 Control::Control()
180  : d( new Private( this ) )
181 {
182  connect( ServerManager::self(), SIGNAL(stateChanged(Akonadi::ServerManager::State)),
183  SLOT(serverStateChanged(Akonadi::ServerManager::State)) );
184  // mProgressIndicator is a widget, so it better be deleted before the QApplication is deleted
185  // Otherwise we get a crash in QCursor code with Qt-4.5
186  if ( QCoreApplication::instance() ) {
187  connect( QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(cleanup()) );
188  }
189 }
190 
191 Control::~Control()
192 {
193  delete d;
194 }
195 
196 bool Control::start()
197 {
198  if ( ServerManager::state() == ServerManager::Stopping ) {
199  kDebug() << "Server is currently being stopped, wont try to start it now";
200  return false;
201  }
202  if ( ServerManager::isRunning() || s_instance->d->mEventLoop ) {
203  kDebug() << "Server is already running";
204  return true;
205  }
206  s_instance->d->mStarting = true;
207  if ( !ServerManager::start() ) {
208  kDebug() << "ServerManager::start failed -> return false";
209  return false;
210  }
211  return s_instance->d->exec();
212 }
213 
214 bool Control::stop()
215 {
216  if ( ServerManager::state() == ServerManager::Starting ) {
217  return false;
218  }
219  if ( !ServerManager::isRunning() || s_instance->d->mEventLoop ) {
220  return true;
221  }
222  s_instance->d->mStopping = true;
223  if ( !ServerManager::stop() ) {
224  return false;
225  }
226  return s_instance->d->exec();
227 }
228 
229 bool Control::restart()
230 {
231  if ( ServerManager::isRunning() ) {
232  if ( !stop() ) {
233  return false;
234  }
235  }
236  return start();
237 }
238 
239 bool Control::start(QWidget * parent)
240 {
241  s_instance->d->setupProgressIndicator( i18n( "Starting Akonadi server..." ), parent );
242  return start();
243 }
244 
245 bool Control::stop(QWidget * parent)
246 {
247  s_instance->d->setupProgressIndicator( i18n( "Stopping Akonadi server..." ), parent );
248  return stop();
249 }
250 
251 bool Control::restart(QWidget * parent)
252 {
253  if ( ServerManager::isRunning() ) {
254  if ( !stop( parent ) ) {
255  return false;
256  }
257  }
258  return start( parent );
259 }
260 
261 void Control::widgetNeedsAkonadi(QWidget * widget)
262 {
263  s_instance->d->mPendingOverlays.append( widget );
264  // delay the overlay creation since we rely on widget being reparented
265  // correctly already
266  QTimer::singleShot( 0, s_instance, SLOT(createErrorOverlays()) );
267 }
268 
269 }
270 
271 #include "moc_control.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:35 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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