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