diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch) | |
tree | 67208f7c145782a7e90b123b982ca78d88cc2c87 /libkdenetwork/qgpgme | |
download | tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'libkdenetwork/qgpgme')
-rw-r--r-- | libkdenetwork/qgpgme/Makefile.am | 18 | ||||
-rw-r--r-- | libkdenetwork/qgpgme/dataprovider.cpp | 107 | ||||
-rw-r--r-- | libkdenetwork/qgpgme/dataprovider.h | 62 | ||||
-rw-r--r-- | libkdenetwork/qgpgme/eventloopinteractor.cpp | 99 | ||||
-rw-r--r-- | libkdenetwork/qgpgme/eventloopinteractor.h | 89 | ||||
-rw-r--r-- | libkdenetwork/qgpgme/tests/Makefile.am | 9 | ||||
-rw-r--r-- | libkdenetwork/qgpgme/tests/dataprovidertest.cpp | 125 |
7 files changed, 509 insertions, 0 deletions
diff --git a/libkdenetwork/qgpgme/Makefile.am b/libkdenetwork/qgpgme/Makefile.am new file mode 100644 index 000000000..87ff793f4 --- /dev/null +++ b/libkdenetwork/qgpgme/Makefile.am @@ -0,0 +1,18 @@ +SUBDIRS = . tests + +INCLUDES = -I$(top_srcdir)/libkdenetwork $(all_includes) + +lib_LTLIBRARIES = libqgpgme.la + +qgpgmedir = $(includedir)/qgpgme +qgpgme_HEADERS = eventloopinteractor.h dataprovider.h + +libqgpgme_la_SOURCES = eventloopinteractor.cpp dataprovider.cpp +libqgpgme_la_LDFLAGS = $(all_libraries) $(KDE_RPATH) -no-undefined +libqgpgme_la_LIBADD = $(LIB_QT) ../gpgmepp/libgpgme++.la + +METASOURCES = AUTO + +messages: + $(XGETTEXT) *.cpp *.h -o $(podir)/libqgpgme.pot + diff --git a/libkdenetwork/qgpgme/dataprovider.cpp b/libkdenetwork/qgpgme/dataprovider.cpp new file mode 100644 index 000000000..8b3ae2857 --- /dev/null +++ b/libkdenetwork/qgpgme/dataprovider.cpp @@ -0,0 +1,107 @@ +/* dataprovider.cpp + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of QGPGME. + + QGPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + QGPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- + +#include <config.h> + +#include <qgpgme/dataprovider.h> + +#include <string.h> +#include <errno.h> +#include <assert.h> +#include <unistd.h> + +static bool resizeAndInit( QByteArray & ba, size_t newSize ) { + const size_t oldSize = ba.size(); + bool ok = ba.resize( newSize ); + if ( ok ) + memset( ba.data() + oldSize, 0, newSize - oldSize ); + return ok; +} + +QGpgME::QByteArrayDataProvider::QByteArrayDataProvider() + : GpgME::DataProvider(), mOff( 0 ) {} + +QGpgME::QByteArrayDataProvider::QByteArrayDataProvider( const QByteArray & initialData ) + : GpgME::DataProvider(), mArray( initialData ), mOff( 0 ) {} + +QGpgME::QByteArrayDataProvider::~QByteArrayDataProvider() {} + +ssize_t QGpgME::QByteArrayDataProvider::read( void * buffer, size_t bufSize ) { +#ifndef NDEBUG + //qDebug( "QGpgME::QByteArrayDataProvider::read( %p, %d )", buffer, bufSize ); +#endif + if ( bufSize == 0 ) + return 0; + if ( mOff >= mArray.size() ) + return 0; // EOF + size_t amount = QMIN( bufSize, mArray.size() - mOff ); + assert( amount > 0 ); + memcpy( buffer, mArray.data() + mOff, amount ); + mOff += amount; + return amount; +} + +ssize_t QGpgME::QByteArrayDataProvider::write( const void * buffer, size_t bufSize ) { +#ifndef NDEBUG + qDebug( "QGpgME::QByteArrayDataProvider::write( %p, %d )", buffer, bufSize ); +#endif + if ( bufSize == 0 ) + return 0; + if ( mOff >= mArray.size() ) + resizeAndInit( mArray, mOff + bufSize ); + if ( mOff >= mArray.size() ) { + errno = EIO; + return -1; + } + assert( bufSize <= mArray.size() - mOff ); + memcpy( mArray.data() + mOff, buffer, bufSize ); + mOff += bufSize; + return bufSize; +} + +off_t QGpgME::QByteArrayDataProvider::seek( off_t offset, int whence ) { +#ifndef NDEBUG + qDebug( "QGpgME::QByteArrayDataProvider::seek( %d, %d )", int(offset), whence ); +#endif + int newOffset = mOff; + switch ( whence ) { + case SEEK_SET: + newOffset = offset; + break; + case SEEK_CUR: + newOffset += offset; + break; + case SEEK_END: + newOffset = mArray.size() + offset; + break; + default: + errno = EINVAL; + return (off_t)-1; + } + return mOff = newOffset; +} + +void QGpgME::QByteArrayDataProvider::release() { +#ifndef NDEBUG + qDebug( "QGpgME::QByteArrayDataProvider::release()" ); +#endif + mArray = QByteArray(); +} diff --git a/libkdenetwork/qgpgme/dataprovider.h b/libkdenetwork/qgpgme/dataprovider.h new file mode 100644 index 000000000..c784fa8d9 --- /dev/null +++ b/libkdenetwork/qgpgme/dataprovider.h @@ -0,0 +1,62 @@ +/* dataprovider.h + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of QGPGME. + + QGPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + QGPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- +#ifndef __QGPGME_DATAPROVIDER_H__ +#define __QGPGME_DATAPROVIDER_H__ + +#include <gpgmepp/interfaces/dataprovider.h> + +#include <qcstring.h> +#include <kdepimmacros.h> + +namespace QGpgME { + + class KDE_EXPORT QByteArrayDataProvider : public GpgME::DataProvider { + public: + QByteArrayDataProvider(); + QByteArrayDataProvider( const QByteArray & initialData ); + ~QByteArrayDataProvider(); + + const QByteArray & data() const { return mArray; } + + private: + // these shall only be accessed through the dataprovider + // interface, where they're public: + /*! \reimp */ + bool isSupported( Operation ) const { return true; } + /*! \reimp */ + ssize_t read( void * buffer, size_t bufSize ); + /*! \reimp */ + ssize_t write( const void * buffer, size_t bufSize ); + /*! \reimp */ + off_t seek( off_t offset, int whence ); + /*! \reimp */ + void release(); + + private: + QByteArray mArray; + off_t mOff; + }; + +} // namespace QGpgME + +#endif // __QGPGME_EVENTLOOPINTERACTOR_H__ + + diff --git a/libkdenetwork/qgpgme/eventloopinteractor.cpp b/libkdenetwork/qgpgme/eventloopinteractor.cpp new file mode 100644 index 000000000..40b81664e --- /dev/null +++ b/libkdenetwork/qgpgme/eventloopinteractor.cpp @@ -0,0 +1,99 @@ +/* qeventloopinteractor.cpp + Copyright (C) 2003 Klarälvdalens Datakonsult AB + + This file is part of QGPGME. + + QGPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + QGPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <qgpgme/eventloopinteractor.h> + +#include <gpgmepp/context.h> + +#include <qsocketnotifier.h> +#include <qapplication.h> + +using namespace GpgME; + +QGpgME::EventLoopInteractor::EventLoopInteractor( QObject * parent, const char * name ) + : QObject( parent, name ), GpgME::EventLoopInteractor() +{ + if ( !parent ) + if ( qApp ) { + connect( qApp, SIGNAL(aboutToQuit()), SLOT(deleteLater()) ); + connect( qApp, SIGNAL(aboutToQuit()), SIGNAL(aboutToDestroy()) ); + } + mSelf = this; +} + +QGpgME::EventLoopInteractor::~EventLoopInteractor() { + emit aboutToDestroy(); + mSelf = 0; +} + +QGpgME::EventLoopInteractor * QGpgME::EventLoopInteractor::mSelf = 0; + +QGpgME::EventLoopInteractor * QGpgME::EventLoopInteractor::instance() { + if ( !mSelf ) +#ifndef NDEBUG + if ( !qApp ) + qWarning( "QGpgME::EventLoopInteractor: Need a QApplication object before calling instance()!" ); + else +#endif + (void)new EventLoopInteractor( 0, "QGpgME::EventLoopInteractor::instance()" ); + return mSelf; +} + +void * QGpgME::EventLoopInteractor::registerWatcher( int fd, Direction dir, bool & ok ) { + QSocketNotifier * sn = new QSocketNotifier( fd, + dir == Read ? QSocketNotifier::Read : QSocketNotifier::Write ); + if ( dir == Read ) + connect( sn, SIGNAL(activated(int)), SLOT(slotReadActivity(int)) ); + else + connect( sn, SIGNAL(activated(int)), SLOT(slotWriteActivity(int)) ); + ok = true; // Can above operations fails? + return sn; +} + +void QGpgME::EventLoopInteractor::unregisterWatcher( void * tag ) { + delete static_cast<QSocketNotifier*>( tag ); +} + +void QGpgME::EventLoopInteractor::slotWriteActivity( int socket ) { + actOn( socket , Write ); +} + +void QGpgME::EventLoopInteractor::slotReadActivity( int socket ) { + actOn( socket , Read ); +} + +void QGpgME::EventLoopInteractor::nextTrustItemEvent( GpgME::Context * context, const GpgME::TrustItem & item ) { + emit nextTrustItemEventSignal( context, item ); +} + +void QGpgME::EventLoopInteractor::nextKeyEvent( GpgME::Context * context, const GpgME::Key & key ) { + emit nextKeyEventSignal( context, key ); +} + +void QGpgME::EventLoopInteractor::operationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + emit operationDoneEventSignal( context, e ); +} + +#include "eventloopinteractor.moc" diff --git a/libkdenetwork/qgpgme/eventloopinteractor.h b/libkdenetwork/qgpgme/eventloopinteractor.h new file mode 100644 index 000000000..7ac264eeb --- /dev/null +++ b/libkdenetwork/qgpgme/eventloopinteractor.h @@ -0,0 +1,89 @@ +/* qeventloopinteractor.h + Copyright (C) 2003 Klarälvdalens Datakonsult AB + + This file is part of QGPGME. + + QGPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + QGPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- +#ifndef __QGPGME_EVENTLOOPINTERACTOR_H__ +#define __QGPGME_EVENTLOOPINTERACTOR_H__ + +#include <gpgmepp/eventloopinteractor.h> + +#include <qobject.h> +#include <kdepimmacros.h> + +namespace GpgME { + class Context; + class Error; + class TrustItem; + class Key; +} // namespace GpgME + +namespace QGpgME { + + class KDE_EXPORT EventLoopInteractor : public QObject, public GpgME::EventLoopInteractor { + Q_OBJECT + protected: + EventLoopInteractor( QObject * parent, const char * name=0 ); + public: + virtual ~EventLoopInteractor(); + + static EventLoopInteractor * instance(); + + signals: + void nextTrustItemEventSignal( GpgME::Context * context, const GpgME::TrustItem & item ); + void nextKeyEventSignal( GpgME::Context * context, const GpgME::Key & key ); + void operationDoneEventSignal( GpgME::Context * context, const GpgME::Error & e ); + + void aboutToDestroy(); + + protected slots: + void slotWriteActivity( int socket ); + void slotReadActivity( int socket ); + + protected: + // + // IO Notification Interface + // + + /*! \reimp */ + void * registerWatcher( int fd, Direction dir, bool & ok ); + /*! \reimp */ + void unregisterWatcher( void * tag ); + + // + // Event Handler Interface + // + + /*! \reimp */ + void nextTrustItemEvent( GpgME::Context * context, const GpgME::TrustItem & item ); + /*! \reimp */ + void nextKeyEvent( GpgME::Context * context, const GpgME::Key & key ); + /*! \reimp */ + void operationDoneEvent( GpgME::Context * context, const GpgME::Error & e ); + + private: + class Private; + Private * d; + static EventLoopInteractor * mSelf; + }; + +} // namespace QGpgME + +#endif // __QGPGME_EVENTLOOPINTERACTOR_H__ + + diff --git a/libkdenetwork/qgpgme/tests/Makefile.am b/libkdenetwork/qgpgme/tests/Makefile.am new file mode 100644 index 000000000..1f3100e65 --- /dev/null +++ b/libkdenetwork/qgpgme/tests/Makefile.am @@ -0,0 +1,9 @@ +INCLUDES = -I$(top_srcdir)/libkdenetwork $(GPGME_CFLAGS) $(all_includes) + +check_PROGRAMS = dataprovidertest + +TESTS = $(check_PROGRAMS) + +dataprovidertest_SOURCES = dataprovidertest.cpp + +LDADD = ../libqgpgme.la #?$(LIB_QT) diff --git a/libkdenetwork/qgpgme/tests/dataprovidertest.cpp b/libkdenetwork/qgpgme/tests/dataprovidertest.cpp new file mode 100644 index 000000000..da87b0300 --- /dev/null +++ b/libkdenetwork/qgpgme/tests/dataprovidertest.cpp @@ -0,0 +1,125 @@ +/* tests/dataprovidertest.cpp + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of QGPGME's regression test suite. + + QGPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + QGPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- + +#ifdef NDEBUG +#undef NDEBUG +#endif + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <qgpgme/dataprovider.h> +#include <gpgmepp/data.h> +#include <gpgmepp/data_p.h> +#include <gpgme.h> + +#include <iostream> + +#include <string.h> +#include <unistd.h> +#include <assert.h> +#include <errno.h> +#include <stdlib.h> + +using namespace GpgME; + +static const char input[] = "foo bar baz\0foo bar baz"; +static const size_t inputSize = sizeof (input) / sizeof (*input) - 1; +static const char nulls[256] = { '\0' }; + +#define assertEqual( expr, expected ) \ +do { \ + long long foo = (expr); \ + if ( foo != (long long)expected ) { \ + std::cerr << #expr << ": expected " << expected << "; got " << foo \ + << ";errno: " << errno << "(" << strerror( errno ) << ")" << std::endl; \ + exit( 1 ); \ + } \ +} while( 0 ) + +int main( int, char** ) { + + { + // + // QByteArrayDataProvider + // + + // writing: + QGpgME::QByteArrayDataProvider qba_dp; + Data data( &qba_dp ); + + assertEqual( data.write( input, inputSize ), inputSize ); + + const QByteArray ba1 = qba_dp.data(); + assertEqual( ba1.size(), inputSize ); + assertEqual( memcmp( ba1.data(), input, inputSize ), 0 ); + + // seeking and reading: + assertEqual( data.seek( 0, SEEK_CUR ), inputSize ); + assertEqual( data.seek( 4, SEEK_SET ), 4 ); + char ch = '\0'; + assertEqual( data.read( &ch, 1 ), 1 ); + assertEqual( ch, input[4] ); + assertEqual( data.read( &ch, 1 ), 1 ); + assertEqual( ch, input[5] ); + + char buf[ inputSize + 10 ] = { '\0' }; + assertEqual( data.seek( 0, SEEK_SET ), 0 ); + assertEqual( data.read( buf, sizeof buf ), inputSize ); + assertEqual( memcmp( buf, input, inputSize ), 0 ); + + // writing single char at end: + assertEqual( data.seek( 0, SEEK_END ), inputSize ); + assertEqual( data.write( &ch, 1 ) , 1 ); + const QByteArray ba2 = qba_dp.data(); + assertEqual( ba2.size(), inputSize + 1 ); + assertEqual( memcmp( ba2.data(), input, inputSize ), 0 ); + assertEqual( ba2[inputSize], ch ); + + // writing past end of buffer: + assertEqual( data.seek( 10, SEEK_END ), inputSize + 11 ); + assertEqual( data.write( &ch, 1 ), 1 ); + const QByteArray ba3 = qba_dp.data(); + assertEqual( ba3.size(), inputSize + 12 ); + assertEqual( memcmp( ba3.data(), input, inputSize ), 0 ); + assertEqual( ba3[inputSize], ch ); + assertEqual( ba3[inputSize+11], ch ); + assertEqual( memcmp( ba3.data() + inputSize + 1, nulls, 10 ), 0 ); + } + + { + // + // QByteArrayDataProvider with initial data: + // + QByteArray ba; + ba.duplicate( input, inputSize ); + QGpgME::QByteArrayDataProvider qba_dp( ba ); + Data data( &qba_dp ); + + assertEqual( data.seek( 0, SEEK_END ), inputSize ); + assertEqual( data.seek( 0, SEEK_SET ), 0 ); + const QByteArray ba1 = qba_dp.data(); + assertEqual( ba1.size(), inputSize ); + assertEqual( memcmp( ba1.data(), input, inputSize ), 0 ); + } + return 0; +} |