diff options
Diffstat (limited to 'libtdenetwork/qgpgme')
-rw-r--r-- | libtdenetwork/qgpgme/CMakeLists.txt | 41 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/Makefile.am | 18 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/dataprovider.cpp | 107 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/dataprovider.h | 62 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/eventloopinteractor.cpp | 99 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/eventloopinteractor.h | 90 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/tests/Makefile.am | 9 | ||||
-rw-r--r-- | libtdenetwork/qgpgme/tests/dataprovidertest.cpp | 125 |
8 files changed, 551 insertions, 0 deletions
diff --git a/libtdenetwork/qgpgme/CMakeLists.txt b/libtdenetwork/qgpgme/CMakeLists.txt new file mode 100644 index 000000000..1de99beb5 --- /dev/null +++ b/libtdenetwork/qgpgme/CMakeLists.txt @@ -0,0 +1,41 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/libtdenetwork + ${CMAKE_SOURCE_DIR}/libtdepim + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### headers ################################### + +install( FILES + eventloopinteractor.h dataprovider.h + DESTINATION ${INCLUDE_INSTALL_DIR}/kde/qgpgme ) + + +##### qgpgme (shared) ########################### + +tde_add_library( qgpgme SHARED AUTOMOC + SOURCES + eventloopinteractor.cpp dataprovider.cpp + VERSION 0.0.0 + LINK gpgme++-shared ${TQT_LIBRARIES} + DESTINATION ${LIB_INSTALL_DIR} +) diff --git a/libtdenetwork/qgpgme/Makefile.am b/libtdenetwork/qgpgme/Makefile.am new file mode 100644 index 000000000..108760a05 --- /dev/null +++ b/libtdenetwork/qgpgme/Makefile.am @@ -0,0 +1,18 @@ +SUBDIRS = . tests + +INCLUDES = -I$(top_srcdir)/libtdenetwork $(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/libtdenetwork/qgpgme/dataprovider.cpp b/libtdenetwork/qgpgme/dataprovider.cpp new file mode 100644 index 000000000..052b84c48 --- /dev/null +++ b/libtdenetwork/qgpgme/dataprovider.cpp @@ -0,0 +1,107 @@ +/* dataprovider.cpp + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of TQGPGME. + + TQGPGME 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. + + TQGPGME 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 TQGPGME; 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( TQByteArray & ba, size_t newSize ) { + const size_t oldSize = ba.size(); + bool ok = ba.tqresize( newSize ); + if ( ok ) + memset( ba.data() + oldSize, 0, newSize - oldSize ); + return ok; +} + +QGpgME::TQByteArrayDataProvider::TQByteArrayDataProvider() + : GpgME::DataProvider(), mOff( 0 ) {} + +QGpgME::TQByteArrayDataProvider::TQByteArrayDataProvider( const TQByteArray & initialData ) + : GpgME::DataProvider(), mArray( initialData ), mOff( 0 ) {} + +QGpgME::TQByteArrayDataProvider::~TQByteArrayDataProvider() {} + +ssize_t QGpgME::TQByteArrayDataProvider::read( void * buffer, size_t bufSize ) { +#ifndef NDEBUG + //qDebug( "QGpgME::TQByteArrayDataProvider::read( %p, %d )", buffer, bufSize ); +#endif + if ( bufSize == 0 ) + return 0; + if ( mOff >= mArray.size() ) + return 0; // EOF + size_t amount = TQMIN( bufSize, mArray.size() - mOff ); + assert( amount > 0 ); + memcpy( buffer, mArray.data() + mOff, amount ); + mOff += amount; + return amount; +} + +ssize_t QGpgME::TQByteArrayDataProvider::write( const void * buffer, size_t bufSize ) { +#ifndef NDEBUG + qDebug( "QGpgME::TQByteArrayDataProvider::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::TQByteArrayDataProvider::seek( off_t offset, int whence ) { +#ifndef NDEBUG + qDebug( "QGpgME::TQByteArrayDataProvider::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::TQByteArrayDataProvider::release() { +#ifndef NDEBUG + qDebug( "QGpgME::TQByteArrayDataProvider::release()" ); +#endif + mArray = TQByteArray(); +} diff --git a/libtdenetwork/qgpgme/dataprovider.h b/libtdenetwork/qgpgme/dataprovider.h new file mode 100644 index 000000000..58c097d1e --- /dev/null +++ b/libtdenetwork/qgpgme/dataprovider.h @@ -0,0 +1,62 @@ +/* dataprovider.h + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of TQGPGME. + + TQGPGME 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. + + TQGPGME 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 TQGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- +#ifndef __TQGPGME_DATAPROVIDER_H__ +#define __TQGPGME_DATAPROVIDER_H__ + +#include <gpgmepp/interfaces/dataprovider.h> + +#include <tqcstring.h> +#include <tdepimmacros.h> + +namespace QGpgME { + + class KDE_EXPORT TQByteArrayDataProvider : public GpgME::DataProvider { + public: + TQByteArrayDataProvider(); + TQByteArrayDataProvider( const TQByteArray & initialData ); + ~TQByteArrayDataProvider(); + + const TQByteArray & 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: + TQByteArray mArray; + off_t mOff; + }; + +} // namespace QGpgME + +#endif // __TQGPGME_EVENTLOOPINTERACTOR_H__ + + diff --git a/libtdenetwork/qgpgme/eventloopinteractor.cpp b/libtdenetwork/qgpgme/eventloopinteractor.cpp new file mode 100644 index 000000000..fdf25af04 --- /dev/null +++ b/libtdenetwork/qgpgme/eventloopinteractor.cpp @@ -0,0 +1,99 @@ +/* qeventloopinteractor.cpp + Copyright (C) 2003 Klarälvdalens Datakonsult AB + + This file is part of TQGPGME. + + TQGPGME 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. + + TQGPGME 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 TQGPGME; 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 <tqsocketnotifier.h> +#include <tqapplication.h> + +using namespace GpgME; + +QGpgME::EventLoopInteractor::EventLoopInteractor( TQObject * parent, const char * name ) + : TQObject( parent, name ), GpgME::EventLoopInteractor() +{ + if ( !parent ) + if ( tqApp ) { + connect( tqApp, TQT_SIGNAL(aboutToQuit()), TQT_SLOT(deleteLater()) ); + connect( tqApp, TQT_SIGNAL(aboutToQuit()), TQT_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 ( !tqApp ) + qWarning( "QGpgME::EventLoopInteractor: Need a TQApplication 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 ) { + TQSocketNotifier * sn = new TQSocketNotifier( fd, + dir == Read ? TQSocketNotifier::Read : TQSocketNotifier::Write ); + if ( dir == Read ) + connect( sn, TQT_SIGNAL(activated(int)), TQT_SLOT(slotReadActivity(int)) ); + else + connect( sn, TQT_SIGNAL(activated(int)), TQT_SLOT(slotWriteActivity(int)) ); + ok = true; // Can above operations fails? + return sn; +} + +void QGpgME::EventLoopInteractor::unregisterWatcher( void * tag ) { + delete static_cast<TQSocketNotifier*>( 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/libtdenetwork/qgpgme/eventloopinteractor.h b/libtdenetwork/qgpgme/eventloopinteractor.h new file mode 100644 index 000000000..c1e5859a4 --- /dev/null +++ b/libtdenetwork/qgpgme/eventloopinteractor.h @@ -0,0 +1,90 @@ +/* qeventloopinteractor.h + Copyright (C) 2003 Klarälvdalens Datakonsult AB + + This file is part of TQGPGME. + + TQGPGME 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. + + TQGPGME 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 TQGPGME; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +// -*- c++ -*- +#ifndef __TQGPGME_EVENTLOOPINTERACTOR_H__ +#define __TQGPGME_EVENTLOOPINTERACTOR_H__ + +#include <gpgmepp/eventloopinteractor.h> + +#include <tqobject.h> +#include <tdepimmacros.h> + +namespace GpgME { + class Context; + class Error; + class TrustItem; + class Key; +} // namespace GpgME + +namespace QGpgME { + + class KDE_EXPORT EventLoopInteractor : public TQObject, public GpgME::EventLoopInteractor { + Q_OBJECT + TQ_OBJECT + protected: + EventLoopInteractor( TQObject * 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 // __TQGPGME_EVENTLOOPINTERACTOR_H__ + + diff --git a/libtdenetwork/qgpgme/tests/Makefile.am b/libtdenetwork/qgpgme/tests/Makefile.am new file mode 100644 index 000000000..5c2436d04 --- /dev/null +++ b/libtdenetwork/qgpgme/tests/Makefile.am @@ -0,0 +1,9 @@ +INCLUDES = -I$(top_srcdir)/libtdenetwork $(GPGME_CFLAGS) $(all_includes) + +check_PROGRAMS = dataprovidertest + +TESTS = $(check_PROGRAMS) + +dataprovidertest_SOURCES = dataprovidertest.cpp + +LDADD = ../libqgpgme.la #?$(LIB_QT) diff --git a/libtdenetwork/qgpgme/tests/dataprovidertest.cpp b/libtdenetwork/qgpgme/tests/dataprovidertest.cpp new file mode 100644 index 000000000..675c85ea8 --- /dev/null +++ b/libtdenetwork/qgpgme/tests/dataprovidertest.cpp @@ -0,0 +1,125 @@ +/* tests/dataprovidertest.cpp + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of TQGPGME's regression test suite. + + TQGPGME 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. + + TQGPGME 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 TQGPGME; 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** ) { + + { + // + // TQByteArrayDataProvider + // + + // writing: + QGpgME::TQByteArrayDataProvider qba_dp; + Data data( &qba_dp ); + + assertEqual( data.write( input, inputSize ), inputSize ); + + const TQByteArray 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 TQByteArray 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 TQByteArray 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 ); + } + + { + // + // TQByteArrayDataProvider with initial data: + // + TQByteArray ba; + ba.duplicate( input, inputSize ); + QGpgME::TQByteArrayDataProvider qba_dp( ba ); + Data data( &qba_dp ); + + assertEqual( data.seek( 0, SEEK_END ), inputSize ); + assertEqual( data.seek( 0, SEEK_SET ), 0 ); + const TQByteArray ba1 = qba_dp.data(); + assertEqual( ba1.size(), inputSize ); + assertEqual( memcmp( ba1.data(), input, inputSize ), 0 ); + } + return 0; +} |