akonadi
agentinstancecreatejob.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "agentinstancecreatejob.h"
00021 #include "agentmanager.h"
00022 #include "agentmanager_p.h"
00023
00024 #include "agentinstance.h"
00025 #include "controlinterface.h"
00026
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029
00030 #include <QtCore/QTimer>
00031
00032 #ifdef Q_OS_UNIX
00033 #include <sys/types.h>
00034 #include <signal.h>
00035 #endif
00036
00037 using namespace Akonadi;
00038
00039 static const int safetyTimeout = 10000;
00040
00044 class AgentInstanceCreateJob::Private
00045 {
00046 public:
00047 Private( AgentInstanceCreateJob* parent ) : q( parent ),
00048 parentWidget( 0 ),
00049 safetyTimer( 0 ),
00050 doConfig( false ),
00051 tooLate( false )
00052 {
00053 }
00054
00055 ~Private()
00056 {
00057 }
00058
00059 void agentInstanceAdded( const AgentInstance &instance )
00060 {
00061 if ( agentInstance == instance && !tooLate ) {
00062 safetyTimer->stop();
00063 if ( doConfig ) {
00064
00065 QTimer::singleShot( 0, q, SLOT( doConfigure() ) );
00066 } else {
00067 q->emitResult();
00068 }
00069 }
00070 }
00071
00072 void doConfigure()
00073 {
00074 org::freedesktop::Akonadi::Agent::Control *agentControlIface =
00075 new org::freedesktop::Akonadi::Agent::Control( QLatin1String( "org.freedesktop.Akonadi.Agent." ) + agentInstance.identifier(),
00076 QLatin1String( "/" ), QDBusConnection::sessionBus(), q );
00077 if ( !agentControlIface || !agentControlIface->isValid() ) {
00078 if ( agentControlIface )
00079 delete agentControlIface;
00080
00081 q->setError( KJob::UserDefinedError );
00082 q->setErrorText( i18n( "Unable to access dbus interface of created agent." ) );
00083 q->emitResult();
00084 return;
00085 }
00086
00087 q->connect( agentControlIface, SIGNAL( configurationDialogAccepted() ),
00088 q, SLOT( configurationDialogAccepted() ) );
00089 q->connect( agentControlIface, SIGNAL( configurationDialogRejected() ),
00090 q, SLOT( configurationDialogRejected() ) );
00091
00092 agentInstance.configure( parentWidget );
00093 }
00094
00095 void configurationDialogAccepted()
00096 {
00097
00098
00099 q->emitResult();
00100 }
00101
00102 void configurationDialogRejected()
00103 {
00104
00105
00106
00107 AgentManager::self()->removeInstance( agentInstance );
00108
00109 q->emitResult();
00110 }
00111
00112 void timeout()
00113 {
00114 tooLate = true;
00115 q->setError( KJob::UserDefinedError );
00116 q->setErrorText( i18n( "Agent instance creation timed out." ) );
00117 q->emitResult();
00118 }
00119
00120 void emitResult()
00121 {
00122 q->emitResult();
00123 }
00124
00125 AgentInstanceCreateJob* q;
00126 AgentType agentType;
00127 AgentInstance agentInstance;
00128 QWidget* parentWidget;
00129 QTimer *safetyTimer;
00130 bool doConfig;
00131 bool tooLate;
00132 };
00133
00134 AgentInstanceCreateJob::AgentInstanceCreateJob( const AgentType & agentType, QObject * parent )
00135 : KJob( parent ),
00136 d( new Private( this ) )
00137 {
00138 d->agentType = agentType;
00139 connect( AgentManager::self(), SIGNAL( instanceAdded( const Akonadi::AgentInstance& ) ),
00140 this, SLOT( agentInstanceAdded( const Akonadi::AgentInstance& ) ) );
00141
00142 d->safetyTimer = new QTimer( this );
00143 connect( d->safetyTimer, SIGNAL( timeout() ), SLOT( timeout() ) );
00144 }
00145
00146 AgentInstanceCreateJob::~ AgentInstanceCreateJob()
00147 {
00148 delete d;
00149 }
00150
00151 void AgentInstanceCreateJob::configure( QWidget *parent )
00152 {
00153 d->parentWidget = parent;
00154 d->doConfig = true;
00155 }
00156
00157 AgentInstance AgentInstanceCreateJob::instance() const
00158 {
00159 return d->agentInstance;
00160 }
00161
00162 void AgentInstanceCreateJob::start()
00163 {
00164 d->agentInstance = AgentManager::self()->d->createInstance( d->agentType );
00165 if ( !d->agentInstance.isValid() ) {
00166 setError( KJob::UserDefinedError );
00167 setErrorText( i18n("Unable to create agent instance." ) );
00168 QTimer::singleShot( 0, this , SLOT( emitResult() ) );
00169 } else {
00170 int timeout = safetyTimeout;
00171 #ifdef Q_OS_UNIX
00172
00173 QString agentValgrind = QString::fromLocal8Bit( qgetenv( "AKONADI_VALGRIND" ) );
00174 if ( !agentValgrind.isEmpty() && d->agentType.identifier().contains( agentValgrind ) )
00175 timeout *= 15;
00176 #endif
00177 d->safetyTimer->start( timeout );
00178 }
00179 }
00180
00181 #include "agentinstancecreatejob.moc"