Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* qeventloopinteractor.cpp
2 Copyright (C) 2003 Klarälvdalens Datakonsult AB
3
4 This file is part of QGPGME.
5
6 QGPGME is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 QGPGME is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with QGPGME; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21// -*- c++ -*-
22
23#include <qgpgme/eventloopinteractor.h>
24
25#include <gpgme++/global.h>
26
27#include <QIODevice>
28#include <QSignalMapper>
29
30Q_GLOBAL_STATIC( QSignalMapper, readSignalMapper )
31Q_GLOBAL_STATIC( QSignalMapper, writeSignalMapper )
32
33static QSignalMapper * setupReadSignalMapper( QObject * o ) {
34 QSignalMapper * sm = readSignalMapper();
35 o->connect( sm, SIGNAL(mapped(int)), SLOT(slotReadActivity(int)) );
36 return sm;
37}
38
39static QSignalMapper * setupWriteSignalMapper( QObject * o ) {
40 QSignalMapper * sm = writeSignalMapper();
41 o->connect( sm, SIGNAL(mapped(int)), SLOT(slotWriteActivity(int)) );
42 return sm;
43}
44
45namespace {
46 struct IO {
47 QIODevice * device;
48 QGpgME::EventLoopInteractor::Direction direction;
49 };
50}
51
52void * QGpgME::EventLoopInteractor::registerWatcher( int fd, Direction dir, bool & ok ) {
53 QIODevice * const iod = GpgME::getQIODevice( fd );
54 if ( !iod ) {
55 ok = false;
56 return 0;
57 }
58 if ( dir == Read ) {
59 static QSignalMapper * rsm = setupReadSignalMapper( this );
60 if ( !rsm->mapping( fd ) ) {
61 rsm->setMapping( iod, fd );
62 connect( iod, SIGNAL(readyRead()), rsm, SLOT(map()) );
63 } else {
64 // if this fd is already registered, gpgme registers an additional
65 // callback for the same fd.
66 // if there is already something to read when registering the new
67 // callback, gpgme expects the new callback to be called, so we
68 // trigger it"
69 QMetaObject::invokeMethod( this, "slotReadActivity", Qt::QueuedConnection, Q_ARG( int, fd ) );
70 }
71 } else {
72 static QSignalMapper * wsm = setupWriteSignalMapper( this );
73 if ( !wsm->mapping( fd ) ) {
74 wsm->setMapping( iod, fd );
75 connect( iod, SIGNAL(bytesWritten(qint64)), wsm, SLOT(map()) );
76 } else {
77 // if this fd is already registered, gpgme registers an additional
78 // callback for the same fd.
79 // if the device is writable when registering the new
80 // callback, gpgme expects the new callback to be called, so we
81 // trigger it:
82 QMetaObject::invokeMethod( this, "slotWriteActivity", Qt::QueuedConnection, Q_ARG( int, fd ) );
83 }
84 }
85
86 ok = true;
87 IO * const io = new IO;
88 io->device = iod;
89 io->direction = dir;
90 iod->bytesAvailable(); //HACK: tell KDPipeIODevices to start their threads
91 iod->bytesToWrite();
92 return io;
93}
94
95void QGpgME::EventLoopInteractor::unregisterWatcher( void * tag ) {
96 if ( !tag ) {
97 return;
98 }
99 const IO * const io = static_cast<IO*>( tag );
100 if ( io->direction == Read ) {
101 // no setupReadSignalMapper here, since registerWatcher,
102 // called before us, is guaranteed to have set it up
103 static QSignalMapper * rsm = readSignalMapper();
104 disconnect( io->device, SIGNAL(readyRead()), rsm, SLOT(map()) );
105 } else {
106 static QSignalMapper * wsm = writeSignalMapper();
107 disconnect( io->device, SIGNAL(bytesWritten(qint64)), wsm, SLOT(map()) );
108 }
109 delete io;
110}
111

Warning: That file was not part of the compilation database. It may have many parsing errors.