00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <qgpgme/eventloopinteractor.h>
00024
00025 #include <gpgme++/global.h>
00026
00027 #include <QIODevice>
00028 #include <QSignalMapper>
00029
00030 Q_GLOBAL_STATIC( QSignalMapper, readSignalMapper )
00031 Q_GLOBAL_STATIC( QSignalMapper, writeSignalMapper )
00032
00033 static QSignalMapper * setupReadSignalMapper( QObject * o ) {
00034 QSignalMapper * sm = readSignalMapper();
00035 o->connect( sm, SIGNAL(mapped(int)), SLOT(slotReadActivity(int)) );
00036 return sm;
00037 }
00038
00039 static QSignalMapper * setupWriteSignalMapper( QObject * o ) {
00040 QSignalMapper * sm = writeSignalMapper();
00041 o->connect( sm, SIGNAL(mapped(int)), SLOT(slotWriteActivity(int)) );
00042 return sm;
00043 }
00044
00045 namespace {
00046 struct IO {
00047 QIODevice * device;
00048 QGpgME::EventLoopInteractor::Direction direction;
00049 };
00050 }
00051
00052 void * QGpgME::EventLoopInteractor::registerWatcher( int fd, Direction dir, bool & ok ) {
00053 QIODevice * const iod = GpgME::getQIODevice( fd );
00054 if ( !iod ) {
00055 ok = false;
00056 return 0;
00057 }
00058 if ( dir == Read ) {
00059 static QSignalMapper * rsm = setupReadSignalMapper( this );
00060 if ( !rsm->mapping( fd ) ) {
00061 rsm->setMapping( iod, fd );
00062 connect( iod, SIGNAL(readyRead()), rsm, SLOT(map()) );
00063 } else {
00064
00065
00066
00067
00068
00069 QMetaObject::invokeMethod( this, "slotReadActivity", Qt::QueuedConnection, Q_ARG( int, fd ) );
00070 }
00071 } else {
00072 static QSignalMapper * wsm = setupWriteSignalMapper( this );
00073 if ( !wsm->mapping( fd ) ) {
00074 wsm->setMapping( iod, fd );
00075 connect( iod, SIGNAL(bytesWritten(qint64)), wsm, SLOT(map()) );
00076 } else {
00077
00078
00079
00080
00081
00082 QMetaObject::invokeMethod( this, "slotWriteActivity", Qt::QueuedConnection, Q_ARG( int, fd ) );
00083 }
00084 }
00085
00086 ok = true;
00087 IO * const io = new IO;
00088 io->device = iod;
00089 io->direction = dir;
00090 iod->bytesAvailable();
00091 iod->bytesToWrite();
00092 return io;
00093 }
00094
00095 void QGpgME::EventLoopInteractor::unregisterWatcher( void * tag ) {
00096 if ( !tag )
00097 return;
00098 const IO * const io = static_cast<IO*>( tag );
00099 if ( io->direction == Read ) {
00100
00101
00102 static QSignalMapper * rsm = readSignalMapper();
00103 disconnect( io->device, SIGNAL(readyRead()), rsm, SLOT(map()) );
00104 } else {
00105 static QSignalMapper * wsm = writeSignalMapper();
00106 disconnect( io->device, SIGNAL(bytesWritten(qint64)), wsm, SLOT(map()) );
00107 }
00108 delete io;
00109 }