Previous: , Up: Using External Event Loops   [Contents][Index]


7.9.2.6 I/O Callback Example Qt

The I/O callback interface can also be used to integrate GPGME with the Qt event loop. The following code snippets show how this can be done using the appropriate register and remove I/O callback functions. In this example, the private data of the register I/O callback function is unused. The event notifications is missing because it does not require any Qt specific setup.

#include <qsocketnotifier.h>
#include <qapplication.h>

struct IOCB {
  IOCB( GpgmeIOCb f, void * d, QSocketNotifier * n )
    : func( f ), data( d ), notifier( n ) {}
  GpgmeIOCb func;
  void * data;
  QSocketNotifier * notifier;
}

class MyApp : public QApplication {

  // ...

  static void registerGpgmeIOCallback( void * data, int fd, int dir,
                                       GpgmeIOCb func, void * func_data,
                                       void ** tag ) {
    QSocketNotifier * n =
      new QSocketNotifier( fd, dir ? QSocketNotifier::Read
                                   : QSocketNotifier::Write );
    connect( n, SIGNAL(activated(int)),
             qApp, SLOT(slotGpgmeIOCallback(int)) );
    qApp->mIOCBs.push_back( IOCB( func, func_data, n ) );
    *tag = (void*)n;
  }

  static void removeGpgmeIOCallback( void * tag ) {
    if ( !tag ) return;
    QSocketNotifier * n = static_cast<QSocketNotifier*>( tag );
    for ( QValueList<IOCB>::iterator it = qApp->mIOCBs.begin() ;
          it != qApp->mIOCBs.end() ; ++it )
      if ( it->notifier == n ) {
        delete it->notifier;
        qApp->mIOCBs.erase( it );
        return;
      }
  }

public slots:
  void slotGpgmeIOCallback( int fd ) {
    for ( QValueList<IOCB>::const_iterator it = mIOCBs.begin() ;
          it != mIOCBs.end() ; ++it )
      if ( it->notifier && it->notifier->socket() == fd )
        (*(it->func)) ( it->func_data, fd );
  }

  // ...

private:
  QValueList<IOCB> mIOCBs;
  // ...
};