• 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 
00026 #include <kdebug.h>
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 
00030 #include <QtCore/QEventLoop>
00031 #include <QtCore/QTimer>
00032 #include <QtGui/QFrame>
00033 
00034 using namespace Akonadi;
00035 
00036 class ControlProgressIndicator : public QFrame
00037 {
00038   public:
00039     ControlProgressIndicator( QWidget *parent = 0 ) :
00040       QFrame( parent )
00041     {
00042       setWindowModality( Qt::ApplicationModal );
00043       resize( 400, 100 );
00044       setWindowFlags( Qt::FramelessWindowHint | Qt::Dialog );
00045       ui.setupUi( this );
00046 
00047       setFrameShadow( QFrame::Plain );
00048       setFrameShape( QFrame::Box );
00049     }
00050 
00051     void setMessage( const QString &msg )
00052     {
00053       ui.statusLabel->setText( msg );
00054     }
00055 
00056     Ui::ControlProgressIndicator ui;
00057 };
00058 
00062 class Control::Private
00063 {
00064   public:
00065     Private( Control *parent )
00066       : mParent( parent ), mEventLoop( 0 ),
00067         mProgressIndicator( 0 ),
00068         mSuccess( false ),
00069         mStarting( false ), mStopping( false )
00070     {
00071       KGlobal::locale()->insertCatalog( "libakonadi" );
00072     }
00073 
00074     void setupProgressIndicator( const QString &msg, QWidget *parent = 0 )
00075     {
00076       if ( mProgressIndicator )
00077         return;
00078       mProgressIndicator = new ControlProgressIndicator( parent );
00079       mProgressIndicator->setMessage( msg );
00080     }
00081 
00082     void createErrorOverlays()
00083     {
00084       foreach ( QWidget* widget, mPendingOverlays )
00085         new ErrorOverlay( widget );
00086       mPendingOverlays.clear();
00087     }
00088 
00089     bool exec();
00090     void serverStarted();
00091     void serverStopped();
00092 
00093     Control *mParent;
00094     QEventLoop *mEventLoop;
00095     ControlProgressIndicator *mProgressIndicator;
00096     QList<QWidget*> mPendingOverlays;
00097     bool mSuccess;
00098 
00099     bool mStarting;
00100     bool mStopping;
00101 };
00102 
00103 class StaticControl : public Control
00104 {
00105   public:
00106     StaticControl() : Control() {}
00107 };
00108 
00109 K_GLOBAL_STATIC( StaticControl, s_instance )
00110 
00111 
00112 bool Control::Private::exec()
00113 {
00114   if ( mProgressIndicator )
00115     mProgressIndicator->show();
00116 
00117   kDebug( 5250 ) << "Starting Akonadi (using an event loop).";
00118   mEventLoop = new QEventLoop( mParent );
00119   // safety timeout
00120   QTimer::singleShot( 10000, mEventLoop, SLOT(quit()) );
00121   mEventLoop->exec();
00122   mEventLoop->deleteLater();
00123   mEventLoop = 0;
00124 
00125   if ( !mSuccess ) {
00126     kWarning( 5250 ) << "Could not start/stop Akonadi!";
00127     if ( mProgressIndicator && mStarting ) {
00128       SelfTestDialog dlg( mProgressIndicator->parentWidget() );
00129       dlg.exec();
00130     }
00131   }
00132 
00133   delete mProgressIndicator;
00134   mProgressIndicator = 0;
00135   mStarting = false;
00136   mStopping = false;
00137 
00138   const bool rv = mSuccess;
00139   mSuccess = false;
00140   return rv;
00141 }
00142 
00143 void Control::Private::serverStarted()
00144 {
00145   if ( mEventLoop && mEventLoop->isRunning() && mStarting ) {
00146     mEventLoop->quit();
00147     mSuccess = true;
00148   }
00149 }
00150 
00151 void Control::Private::serverStopped()
00152 {
00153   if ( mEventLoop && mEventLoop->isRunning() && mStopping ) {
00154     mEventLoop->quit();
00155     mSuccess = true;
00156   }
00157 }
00158 
00159 
00160 Control::Control()
00161   : d( new Private( this ) )
00162 {
00163   connect( ServerManager::self(), SIGNAL(started()), SLOT(serverStarted()) );
00164   connect( ServerManager::self(), SIGNAL(stopped()), SLOT(serverStopped()) );
00165 }
00166 
00167 Control::~Control()
00168 {
00169   delete d;
00170 }
00171 
00172 bool Control::start()
00173 {
00174   if ( s_instance->d->mStopping )
00175     return false;
00176   if ( ServerManager::isRunning() || s_instance->d->mEventLoop )
00177     return true;
00178   s_instance->d->mStarting = true;
00179   if ( !ServerManager::start() )
00180     return false;
00181   return s_instance->d->exec();
00182 }
00183 
00184 bool Control::stop()
00185 {
00186   if ( s_instance->d->mStarting )
00187     return false;
00188   if ( !ServerManager::isRunning() || s_instance->d->mEventLoop )
00189     return true;
00190   s_instance->d->mStopping = true;
00191   if ( !ServerManager::stop() )
00192     return false;
00193   return s_instance->d->exec();
00194 }
00195 
00196 bool Control::restart()
00197 {
00198   if ( ServerManager::isRunning() ) {
00199     if ( !stop() )
00200       return false;
00201   }
00202   return start();
00203 }
00204 
00205 bool Control::start(QWidget * parent)
00206 {
00207   s_instance->d->setupProgressIndicator( i18n( "Starting Akonadi server..." ), parent );
00208   return start();
00209 }
00210 
00211 bool Control::stop(QWidget * parent)
00212 {
00213   s_instance->d->setupProgressIndicator( i18n( "Stopping Akonadi server..." ), parent );
00214   return stop();
00215 }
00216 
00217 bool Control::restart(QWidget * parent)
00218 {
00219   if ( ServerManager::isRunning() ) {
00220     if ( !stop( parent ) )
00221       return false;
00222   }
00223   return start( parent );
00224 }
00225 
00226 void Control::widgetNeedsAkonadi(QWidget * widget)
00227 {
00228   s_instance->d->mPendingOverlays.append( widget );
00229   // delay the overlay creation since we rely on widget being reparented
00230   // correctly already
00231   QTimer::singleShot( 0, s_instance, SLOT(createErrorOverlays()) );
00232 }
00233 
00234 #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.6
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