akonadi
session.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "session.h"
00021 #include "session_p.h"
00022
00023 #include "imapparser_p.h"
00024 #include "job.h"
00025 #include "job_p.h"
00026 #include "servermanager_p.h"
00027 #include "xdgbasedirs_p.h"
00028
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031
00032 #include <QCoreApplication>
00033 #include <QtCore/QDir>
00034 #include <QtCore/QQueue>
00035 #include <QtCore/QThreadStorage>
00036 #include <QtCore/QTimer>
00037
00038 #include <QtNetwork/QLocalSocket>
00039
00040 #define PIPELINE_LENGTH 2
00041
00042 using namespace Akonadi;
00043
00044
00045
00046
00047 void SessionPrivate::startNext()
00048 {
00049 QTimer::singleShot( 0, mParent, SLOT(doStartNext()) );
00050 }
00051
00052 void SessionPrivate::reconnect()
00053 {
00054
00055 if ( socket->state() != QLocalSocket::ConnectedState &&
00056 socket->state() != QLocalSocket::ConnectingState ) {
00057 #ifdef Q_OS_WIN //krazy:exclude=cpp
00058 const QString namedPipe = mConnectionSettings->value( QLatin1String( "Data/NamedPipe" ), QLatin1String( "Akonadi" ) ).toString();
00059 socket->connectToServer( namedPipe );
00060 #else
00061 const QString defaultSocketDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) );
00062 const QString path = mConnectionSettings->value( QLatin1String( "Data/UnixPath" ), defaultSocketDir + QLatin1String( "/akonadiserver.socket" ) ).toString();
00063 socket->connectToServer( path );
00064 #endif
00065 }
00066 }
00067
00068 void SessionPrivate::socketError()
00069 {
00070 if ( currentJob )
00071 currentJob->d_ptr->lostConnection();
00072 connected = false;
00073 QTimer::singleShot( 1000, mParent, SLOT(reconnect()) );
00074 }
00075
00076 void SessionPrivate::dataReceived()
00077 {
00078 while ( socket->bytesAvailable() > 0 ) {
00079 if ( parser->continuationSize() > 1 ) {
00080 const QByteArray data = socket->read( qMin( socket->bytesAvailable(), parser->continuationSize() - 1 ) );
00081 parser->parseBlock( data );
00082 } else if ( socket->canReadLine() ) {
00083 if ( !parser->parseNextLine( socket->readLine() ) )
00084 continue;
00085
00086
00087 if ( parser->tag() == QByteArray("0") ) {
00088 if ( parser->data().startsWith( "OK" ) ) {
00089 connected = true;
00090 startNext();
00091 } else {
00092 kWarning( 5250 ) << "Unable to login to Akonadi server:" << parser->data();
00093 socket->close();
00094 QTimer::singleShot( 1000, mParent, SLOT(reconnect()) );
00095 }
00096 }
00097
00098
00099 if ( parser->tag() == "*" && parser->data().startsWith( "OK Akonadi" ) ) {
00100 const int pos = parser->data().indexOf( "[PROTOCOL" );
00101 if ( pos > 0 ) {
00102 qint64 tmp = 0;
00103 ImapParser::parseNumber( parser->data(), tmp, 0, pos + 9 );
00104 protocolVersion = tmp;
00105 Internal::setServerProtocolVersion( tmp );
00106 }
00107 kDebug( 5250 ) << "Server protocol version is:" << protocolVersion;
00108
00109 writeData( "0 LOGIN " + sessionId + '\n' );
00110
00111
00112 } else {
00113 if ( currentJob )
00114 currentJob->d_ptr->handleResponse( parser->tag(), parser->data() );
00115 }
00116
00117
00118 parser->reset();
00119 } else {
00120 break;
00121 }
00122 }
00123 }
00124
00125 bool SessionPrivate::canPipelineNext()
00126 {
00127 if ( queue.isEmpty() || pipeline.count() >= PIPELINE_LENGTH )
00128 return false;
00129 if ( pipeline.isEmpty() && currentJob )
00130 return currentJob->d_ptr->mWriteFinished;
00131 if ( !pipeline.isEmpty() )
00132 return pipeline.last()->d_ptr->mWriteFinished;
00133 return false;
00134 }
00135
00136 void SessionPrivate::doStartNext()
00137 {
00138 if ( !connected || (queue.isEmpty() && pipeline.isEmpty()) )
00139 return;
00140 if ( canPipelineNext() ) {
00141 Akonadi::Job *nextJob = queue.dequeue();
00142 pipeline.enqueue( nextJob );
00143 startJob( nextJob );
00144 }
00145 if ( jobRunning )
00146 return;
00147 jobRunning = true;
00148 if ( !pipeline.isEmpty() ) {
00149 currentJob = pipeline.dequeue();
00150 } else {
00151 currentJob = queue.dequeue();
00152 startJob( currentJob );
00153 }
00154 }
00155
00156 void SessionPrivate::startJob( Job *job )
00157 {
00158 if ( protocolVersion < minimumProtocolVersion() ) {
00159 job->setError( Job::ProtocolVersionMismatch );
00160 job->setErrorText( i18n( "Protocol version %1 found, expected at least %2", protocolVersion, minimumProtocolVersion() ) );
00161 job->emitResult();
00162 } else {
00163 job->d_ptr->startQueued();
00164 }
00165 }
00166
00167 void SessionPrivate::jobDone(KJob * job)
00168 {
00169 if( job == currentJob ) {
00170 if ( pipeline.isEmpty() ) {
00171 jobRunning = false;
00172 currentJob = 0;
00173 } else {
00174 currentJob = pipeline.dequeue();
00175 }
00176 startNext();
00177 }
00178
00179 else {
00180 kDebug( 5250 ) << job << "Non-current job finished.";
00181 }
00182 }
00183
00184 void SessionPrivate::jobWriteFinished( Akonadi::Job* job )
00185 {
00186 Q_ASSERT( (job == currentJob && pipeline.isEmpty()) || (job = pipeline.last()) );
00187 startNext();
00188 }
00189
00190 void SessionPrivate::jobDestroyed(QObject * job)
00191 {
00192 queue.removeAll( static_cast<Akonadi::Job*>( job ) );
00193
00194 pipeline.removeAll( static_cast<Akonadi::Job*>( job ) );
00195 if ( currentJob == job )
00196 currentJob = 0;
00197 }
00198
00199 void SessionPrivate::addJob(Job * job)
00200 {
00201 queue.append( job );
00202 QObject::connect( job, SIGNAL(result(KJob*)), mParent, SLOT(jobDone(KJob*)) );
00203 QObject::connect( job, SIGNAL(writeFinished(Akonadi::Job*)), mParent, SLOT(jobWriteFinished(Akonadi::Job*)) );
00204 QObject::connect( job, SIGNAL(destroyed(QObject*)), mParent, SLOT(jobDestroyed(QObject*)) );
00205 startNext();
00206 }
00207
00208 int SessionPrivate::nextTag()
00209 {
00210 return theNextTag++;
00211 }
00212
00213 void SessionPrivate::writeData(const QByteArray & data)
00214 {
00215 socket->write( data );
00216 }
00217
00218
00219
00220
00221 Session::Session(const QByteArray & sessionId, QObject * parent) :
00222 QObject( parent ),
00223 d( new SessionPrivate( this ) )
00224 {
00225 if ( !sessionId.isEmpty() ) {
00226 d->sessionId = sessionId;
00227 } else {
00228 d->sessionId = QCoreApplication::instance()->applicationName().toUtf8()
00229 + "-" + QByteArray::number( qrand() );
00230 }
00231
00232 d->connected = false;
00233 d->theNextTag = 1;
00234 d->currentJob = 0;
00235 d->jobRunning = false;
00236
00237 const QString connectionConfigFile = XdgBaseDirs::akonadiConnectionConfigFile();
00238
00239 QFileInfo fileInfo( connectionConfigFile );
00240 if ( !fileInfo.exists() ) {
00241 kWarning( 5250 ) << "Akonadi Client Session: connection config file '"
00242 << "akonadi/akonadiconnectionrc can not be found in '"
00243 << XdgBaseDirs::homePath( "config" ) << "' nor in any of "
00244 << XdgBaseDirs::systemPathList( "config" );
00245 }
00246
00247 d->mConnectionSettings = new QSettings( connectionConfigFile, QSettings::IniFormat );
00248
00249
00250 d->socket = new QLocalSocket( this );
00251
00252 connect( d->socket, SIGNAL(disconnected()), SLOT(socketError()) );
00253 connect( d->socket, SIGNAL(error(QLocalSocket::LocalSocketError)), SLOT(socketError()) );
00254 connect( d->socket, SIGNAL(readyRead()), SLOT(dataReceived()) );
00255 d->reconnect();
00256 }
00257
00258 Session::~Session()
00259 {
00260 clear();
00261 delete d;
00262 }
00263
00264 QByteArray Session::sessionId() const
00265 {
00266 return d->sessionId;
00267 }
00268
00269 QThreadStorage<Session*> instances;
00270
00271 void SessionPrivate::createDefaultSession( const QByteArray &sessionId )
00272 {
00273 Q_ASSERT_X( !sessionId.isEmpty(), "SessionPrivate::createDefaultSession",
00274 "You tried to create a default session with empty session id!" );
00275 Q_ASSERT_X( !instances.hasLocalData(), "SessionPrivate::createDefaultSession",
00276 "You tried to create a default session twice!" );
00277
00278 instances.setLocalData( new Session( sessionId ) );
00279 }
00280
00281 Session* Session::defaultSession()
00282 {
00283 if ( !instances.hasLocalData() )
00284 instances.setLocalData( new Session() );
00285 return instances.localData();
00286 }
00287
00288 void Session::clear()
00289 {
00290 foreach ( Job* job, d->queue )
00291 job->kill( KJob::EmitResult );
00292 d->queue.clear();
00293 if ( d->currentJob )
00294 d->currentJob->kill( KJob::EmitResult );
00295 }
00296
00297 #include "session.moc"