• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <QtGui/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  mProgressIndicator->setMessage( msg );
101  }
102 
103  void createErrorOverlays()
104  {
105 #ifndef Q_OS_WINCE
106  foreach ( QWidget* widget, mPendingOverlays )
107  if ( widget )
108  new ErrorOverlay( widget );
109 #endif
110  mPendingOverlays.clear();
111  }
112 
113  void cleanup()
114  {
115  s_instance.destroy();
116  }
117 
118  bool exec();
119  void serverStateChanged(ServerManager::State state);
120 
121  QPointer<Control> mParent;
122  QEventLoop *mEventLoop;
123  QPointer<Internal::ControlProgressIndicator> mProgressIndicator;
124  QList<QPointer<QWidget> > mPendingOverlays;
125  bool mSuccess;
126 
127  bool mStarting;
128  bool mStopping;
129 };
130 
131 bool Control::Private::exec()
132 {
133  if ( mProgressIndicator )
134  mProgressIndicator->show();
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 #ifndef Q_OS_WINCE
145  if ( mProgressIndicator && mStarting ) {
146  QPointer<SelfTestDialog> dlg = new SelfTestDialog( mProgressIndicator->parentWidget() );
147  dlg->exec();
148  delete dlg;
149  if ( !mParent )
150  return false;
151  }
152 #endif
153  }
154 
155  delete mProgressIndicator;
156  mProgressIndicator = 0;
157  mStarting = false;
158  mStopping = false;
159 
160  const bool rv = mSuccess;
161  mSuccess = false;
162  return rv;
163 }
164 
165 void Control::Private::serverStateChanged(ServerManager::State state)
166 {
167  kDebug() << state;
168  if ( mEventLoop && mEventLoop->isRunning() ) {
169  mEventLoop->quit();
170  mSuccess = (mStarting && state == ServerManager::Running) || (mStopping && state == ServerManager::NotRunning);
171  }
172 }
173 
174 Control::Control()
175  : d( new Private( this ) )
176 {
177  connect( ServerManager::self(), SIGNAL(stateChanged(Akonadi::ServerManager::State)),
178  SLOT(serverStateChanged(Akonadi::ServerManager::State)) );
179  // mProgressIndicator is a widget, so it better be deleted before the QApplication is deleted
180  // Otherwise we get a crash in QCursor code with Qt-4.5
181  if ( QCoreApplication::instance() )
182  connect( QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(cleanup()) );
183 }
184 
185 Control::~Control()
186 {
187  delete d;
188 }
189 
190 bool Control::start()
191 {
192  if ( ServerManager::state() == ServerManager::Stopping ) {
193  kDebug() << "Server is currently being stopped, wont try to start it now";
194  return false;
195  }
196  if ( ServerManager::isRunning() || s_instance->d->mEventLoop ) {
197  kDebug() << "Server is already running";
198  return true;
199  }
200  s_instance->d->mStarting = true;
201  if ( !ServerManager::start() ) {
202  kDebug() << "ServerManager::start failed -> return false";
203  return false;
204  }
205  return s_instance->d->exec();
206 }
207 
208 bool Control::stop()
209 {
210  if ( ServerManager::state() == ServerManager::Starting )
211  return false;
212  if ( !ServerManager::isRunning() || s_instance->d->mEventLoop )
213  return true;
214  s_instance->d->mStopping = true;
215  if ( !ServerManager::stop() )
216  return false;
217  return s_instance->d->exec();
218 }
219 
220 bool Control::restart()
221 {
222  if ( ServerManager::isRunning() ) {
223  if ( !stop() )
224  return false;
225  }
226  return start();
227 }
228 
229 bool Control::start(QWidget * parent)
230 {
231  s_instance->d->setupProgressIndicator( i18n( "Starting Akonadi server..." ), parent );
232  return start();
233 }
234 
235 bool Control::stop(QWidget * parent)
236 {
237  s_instance->d->setupProgressIndicator( i18n( "Stopping Akonadi server..." ), parent );
238  return stop();
239 }
240 
241 bool Control::restart(QWidget * parent)
242 {
243  if ( ServerManager::isRunning() ) {
244  if ( !stop( parent ) )
245  return false;
246  }
247  return start( parent );
248 }
249 
250 void Control::widgetNeedsAkonadi(QWidget * widget)
251 {
252  s_instance->d->mPendingOverlays.append( widget );
253  // delay the overlay creation since we rely on widget being reparented
254  // correctly already
255  QTimer::singleShot( 0, s_instance, SLOT(createErrorOverlays()) );
256 }
257 
258 }
259 
260 #include "control.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:04 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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