akonadi
control.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "control.h"
00021
00022 #include <kdebug.h>
00023 #include <kglobal.h>
00024
00025 #include <QtCore/QEventLoop>
00026 #include <QtCore/QProcess>
00027 #include <QtCore/QTimer>
00028 #include <QtDBus/QDBusConnection>
00029 #include <QtDBus/QDBusConnectionInterface>
00030
00031 #define AKONADI_CONTROL_SERVICE QLatin1String("org.freedesktop.Akonadi.Control")
00032 #define AKONADI_SERVER_SERVICE QLatin1String("org.freedesktop.Akonadi")
00033
00034 using namespace Akonadi;
00035
00039 class Control::Private
00040 {
00041 public:
00042 Private( Control *parent )
00043 : mParent( parent ), mEventLoop( 0 ), mSuccess( false )
00044 {
00045 }
00046
00047 bool startInternal();
00048 void serviceOwnerChanged( const QString&, const QString&, const QString& );
00049
00050 Control *mParent;
00051 QEventLoop *mEventLoop;
00052 bool mSuccess;
00053 };
00054
00055 class StaticControl : public Control
00056 {
00057 public:
00058 StaticControl() : Control() {}
00059 };
00060
00061 K_GLOBAL_STATIC( StaticControl, s_instance )
00062
00063
00064 bool Control::Private::startInternal()
00065 {
00066 if ( QDBusConnection::sessionBus().interface()->isServiceRegistered( AKONADI_CONTROL_SERVICE ) || mEventLoop )
00067 return true;
00068
00069 QDBusReply<void> reply = QDBusConnection::sessionBus().interface()->startService( AKONADI_CONTROL_SERVICE );
00070 if ( !reply.isValid() ) {
00071 kWarning( 5250 ) << "Unable to start Akonadi control process: "
00072 << reply.error().message();
00073
00074
00075 if ( reply.error().type() == QDBusError::ServiceUnknown ) {
00076 const bool ok = QProcess::startDetached( QLatin1String("akonadi_control") );
00077 if ( !ok ) {
00078 kWarning( 5250 ) << "Error: unable to execute binary akonadi_control";
00079 return false;
00080 }
00081 } else {
00082 return false;
00083 }
00084 }
00085
00086 mEventLoop = new QEventLoop( mParent );
00087
00088 QTimer::singleShot( 10000, mEventLoop, SLOT(quit()) );
00089 mEventLoop->exec();
00090 mEventLoop->deleteLater();
00091 mEventLoop = 0;
00092
00093 if ( !mSuccess )
00094 kWarning( 5250 ) << "Could not start Akonadi!";
00095 return mSuccess;
00096 }
00097
00098 void Control::Private::serviceOwnerChanged( const QString & name, const QString & oldOwner, const QString & newOwner )
00099 {
00100 Q_UNUSED( oldOwner );
00101 if ( name == AKONADI_SERVER_SERVICE && !newOwner.isEmpty() && mEventLoop && mEventLoop->isRunning() ) {
00102 mEventLoop->quit();
00103 mSuccess = true;
00104 }
00105 }
00106
00107
00108 Control::Control()
00109 : d( new Private( this ) )
00110 {
00111 connect( QDBusConnection::sessionBus().interface(), SIGNAL( serviceOwnerChanged( QString, QString, QString ) ),
00112 SLOT( serviceOwnerChanged( QString, QString, QString ) ) );
00113 }
00114
00115 Control::~Control()
00116 {
00117 delete d;
00118 }
00119
00120 bool Control::start()
00121 {
00122 return s_instance->d->startInternal();
00123 }
00124
00125 #include "control.moc"