qgpgme
eventloopinteractor_win.cpp
00001 /* qeventloopinteractor.cpp 00002 Copyright (C) 2003 Klarälvdalens Datakonsult AB 00003 00004 This file is part of QGPGME. 00005 00006 QGPGME is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published 00008 by the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 QGPGME is distributed in the hope that it will be useful, but 00012 WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with QGPGME; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. */ 00020 00021 // -*- c++ -*- 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 // if this fd is already registered, gpgme registers an additional 00065 // callback for the same fd. 00066 // if there is already something to read when registering the new 00067 // callback, gpgme expects the new callback to be called, so we 00068 // trigger it" 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 // if this fd is already registered, gpgme registers an additional 00078 // callback for the same fd. 00079 // if the device is writable when registering the new 00080 // callback, gpgme expects the new callback to be called, so we 00081 // trigger it: 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(); //HACK: tell KDPipeIODevices to start their threads 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 // no setupReadSignalMapper here, since registerWatcher, 00101 // called before us, is guaranteed to have set it up 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 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:00 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:00 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.