• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

control.cpp

00001 /*
00002     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "control.h"
00021 #include "servermanager.h"
00022 #include "ui_controlprogressindicator.h"
00023 #include "selftestdialog_p.h"
00024 #include "erroroverlay_p.h"
00025 #include "firstrun.h"
00026 
00027 #include <kdebug.h>
00028 #include <kglobal.h>
00029 #include <klocale.h>
00030 
00031 #include <QtCore/QEventLoop>
00032 #include <QtCore/QTimer>
00033 #include <QtGui/QFrame>
00034 
00035 using namespace Akonadi;
00036 
00037 class ControlProgressIndicator : public QFrame
00038 {
00039   public:
00040     ControlProgressIndicator( QWidget *parent = 0 ) :
00041       QFrame( parent )
00042     {
00043       setWindowModality( Qt::ApplicationModal );
00044       resize( 400, 100 );
00045       setWindowFlags( Qt::FramelessWindowHint | Qt::Dialog );
00046       ui.setupUi( this );
00047 
00048       setFrameShadow( QFrame::Plain );
00049       setFrameShape( QFrame::Box );
00050     }
00051 
00052     void setMessage( const QString &msg )
00053     {
00054       ui.statusLabel->setText( msg );
00055     }
00056 
00057     Ui::ControlProgressIndicator ui;
00058 };
00059 
00063 class Control::Private
00064 {
00065   public:
00066     Private( Control *parent )
00067       : mParent( parent ), mEventLoop( 0 ),
00068         mProgressIndicator( 0 ),
00069         mFirstRunner( 0 ),
00070         mSuccess( false ),
00071         mStarting( false ), mStopping( false )
00072     {
00073       KGlobal::locale()->insertCatalog( QString::fromLatin1("libakonadi") );
00074       if ( ServerManager::isRunning() )
00075         mFirstRunner = new Firstrun( mParent );
00076     }
00077 
00078     void setupProgressIndicator( const QString &msg, QWidget *parent = 0 )
00079     {
00080       if ( mProgressIndicator )
00081         return;
00082       mProgressIndicator = new ControlProgressIndicator( parent );
00083       mProgressIndicator->setMessage( msg );
00084     }
00085 
00086     void createErrorOverlays()
00087     {
00088       foreach ( QWidget* widget, mPendingOverlays )
00089         new ErrorOverlay( widget );
00090       mPendingOverlays.clear();
00091     }
00092 
00093     bool exec();
00094     void serverStarted();
00095     void serverStopped();
00096 
00097     Control *mParent;
00098     QEventLoop *mEventLoop;
00099     ControlProgressIndicator *mProgressIndicator;
00100     QList<QWidget*> mPendingOverlays;
00101     Firstrun *mFirstRunner;
00102     bool mSuccess;
00103 
00104     bool mStarting;
00105     bool mStopping;
00106 };
00107 
00108 class StaticControl : public Control
00109 {
00110   public:
00111     StaticControl() : Control() {}
00112 };
00113 
00114 K_GLOBAL_STATIC( StaticControl, s_instance )
00115 
00116 
00117 bool Control::Private::exec()
00118 {
00119   if ( mProgressIndicator )
00120     mProgressIndicator->show();
00121 
00122   kDebug( 5250 ) << "Starting Akonadi (using an event loop).";
00123   mEventLoop = new QEventLoop( mParent );
00124   // safety timeout
00125   QTimer::singleShot( 10000, mEventLoop, SLOT(quit()) );
00126   mEventLoop->exec();
00127   mEventLoop->deleteLater();
00128   mEventLoop = 0;
00129 
00130   if ( !mSuccess ) {
00131     kWarning( 5250 ) << "Could not start/stop Akonadi!";
00132     if ( mProgressIndicator && mStarting ) {
00133       SelfTestDialog dlg( mProgressIndicator->parentWidget() );
00134       dlg.exec();
00135     }
00136   }
00137 
00138   delete mProgressIndicator;
00139   mProgressIndicator = 0;
00140   mStarting = false;
00141   mStopping = false;
00142 
00143   const bool rv = mSuccess;
00144   mSuccess = false;
00145   return rv;
00146 }
00147 
00148 void Control::Private::serverStarted()
00149 {
00150   if ( mEventLoop && mEventLoop->isRunning() && mStarting ) {
00151     mEventLoop->quit();
00152     mSuccess = true;
00153   }
00154   if ( !mFirstRunner )
00155     mFirstRunner = new Firstrun( mParent );
00156 }
00157 
00158 void Control::Private::serverStopped()
00159 {
00160   if ( mEventLoop && mEventLoop->isRunning() && mStopping ) {
00161     mEventLoop->quit();
00162     mSuccess = true;
00163   }
00164 }
00165 
00166 
00167 Control::Control()
00168   : d( new Private( this ) )
00169 {
00170   connect( ServerManager::self(), SIGNAL(started()), SLOT(serverStarted()) );
00171   connect( ServerManager::self(), SIGNAL(stopped()), SLOT(serverStopped()) );
00172 }
00173 
00174 Control::~Control()
00175 {
00176   delete d;
00177 }
00178 
00179 bool Control::start()
00180 {
00181   if ( s_instance->d->mStopping )
00182     return false;
00183   if ( ServerManager::isRunning() || s_instance->d->mEventLoop )
00184     return true;
00185   s_instance->d->mStarting = true;
00186   if ( !ServerManager::start() )
00187     return false;
00188   return s_instance->d->exec();
00189 }
00190 
00191 bool Control::stop()
00192 {
00193   if ( s_instance->d->mStarting )
00194     return false;
00195   if ( !ServerManager::isRunning() || s_instance->d->mEventLoop )
00196     return true;
00197   s_instance->d->mStopping = true;
00198   if ( !ServerManager::stop() )
00199     return false;
00200   return s_instance->d->exec();
00201 }
00202 
00203 bool Control::restart()
00204 {
00205   if ( ServerManager::isRunning() ) {
00206     if ( !stop() )
00207       return false;
00208   }
00209   return start();
00210 }
00211 
00212 bool Control::start(QWidget * parent)
00213 {
00214   s_instance->d->setupProgressIndicator( i18n( "Starting Akonadi server..." ), parent );
00215   return start();
00216 }
00217 
00218 bool Control::stop(QWidget * parent)
00219 {
00220   s_instance->d->setupProgressIndicator( i18n( "Stopping Akonadi server..." ), parent );
00221   return stop();
00222 }
00223 
00224 bool Control::restart(QWidget * parent)
00225 {
00226   if ( ServerManager::isRunning() ) {
00227     if ( !stop( parent ) )
00228       return false;
00229   }
00230   return start( parent );
00231 }
00232 
00233 void Control::widgetNeedsAkonadi(QWidget * widget)
00234 {
00235   s_instance->d->mPendingOverlays.append( widget );
00236   // delay the overlay creation since we rely on widget being reparented
00237   // correctly already
00238   QTimer::singleShot( 0, s_instance, SLOT(createErrorOverlays()) );
00239 }
00240 
00241 #include "control.moc"

akonadi

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal