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
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028
00029 #include <QTimer>
00030
00031 using namespace Akonadi;
00032
00033 static const int safetyTimeout = 10000;
00034
00038 class AgentInstanceCreateJob::Private
00039 {
00040 public:
00041 Private( AgentInstanceCreateJob* parent ) : q( parent ),
00042 parentWidget( 0 ),
00043 safetyTimer( 0 ),
00044 doConfig( false ),
00045 tooLate( false )
00046 {
00047 }
00048
00049 ~Private()
00050 {
00051 }
00052
00053 void agentInstanceAdded( const AgentInstance &instance )
00054 {
00055 if ( agentInstance == instance && !tooLate ) {
00056 safetyTimer->stop();
00057 if ( doConfig ) {
00058
00059 QTimer::singleShot( 0, q, SLOT( doConfigure() ) );
00060 } else {
00061 q->emitResult();
00062 }
00063 }
00064 }
00065
00066 void doConfigure()
00067 {
00068 agentInstance.configure( parentWidget );
00069 q->emitResult();
00070 }
00071
00072 void timeout()
00073 {
00074 tooLate = true;
00075 q->setError( KJob::UserDefinedError );
00076 q->setErrorText( i18n( "Agent instance creation timed out." ) );
00077 q->emitResult();
00078 }
00079
00080 void emitResult()
00081 {
00082 q->emitResult();
00083 }
00084
00085 AgentInstanceCreateJob* q;
00086 AgentType agentType;
00087 AgentInstance agentInstance;
00088 QWidget* parentWidget;
00089 QTimer *safetyTimer;
00090 bool doConfig;
00091 bool tooLate;
00092 };
00093
00094 AgentInstanceCreateJob::AgentInstanceCreateJob(const AgentType & agentType, QObject * parent) :
00095 KJob( parent ),
00096 d( new Private( this ) )
00097 {
00098 d->agentType = agentType;
00099 connect( AgentManager::self(), SIGNAL(instanceAdded(const Akonadi::AgentInstance&)),
00100 this, SLOT(agentInstanceAdded(const Akonadi::AgentInstance&)) );
00101
00102 d->safetyTimer = new QTimer( this );
00103 connect( d->safetyTimer, SIGNAL(timeout()), SLOT(timeout()) );
00104 }
00105
00106 AgentInstanceCreateJob::~ AgentInstanceCreateJob()
00107 {
00108 delete d;
00109 }
00110
00111 void AgentInstanceCreateJob::configure( QWidget *parent )
00112 {
00113 d->parentWidget = parent;
00114 d->doConfig = true;
00115 }
00116
00117 AgentInstance AgentInstanceCreateJob::instance() const
00118 {
00119 return d->agentInstance;
00120 }
00121
00122 void AgentInstanceCreateJob::start()
00123 {
00124 d->agentInstance = AgentManager::self()->d->createInstance( d->agentType );
00125 if ( !d->agentInstance.isValid() ) {
00126 setError( KJob::UserDefinedError );
00127 setErrorText( i18n("Unable to create agent instance." ) );
00128 QTimer::singleShot( 0, this , SLOT(emitResult()) );
00129 } else {
00130 d->safetyTimer->start( safetyTimeout );
00131 }
00132 }
00133
00134 #include "agentinstancecreatejob.moc"