• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.3 API Reference
  • KDE Home
  • Contact Us
 

qgpgme

  • qgpgme
dataprovider.cpp
1 /* dataprovider.cpp
2  Copyright (C) 2004 Klar�vdalens 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/dataprovider.h>
24 
25 #include <gpgme++/error.h>
26 
27 #include <QIODevice>
28 #include <QProcess>
29 
30 #include <cstdio>
31 #include <cstring>
32 #include <cassert>
33 
34 using namespace QGpgME;
35 using namespace GpgME;
36 
37 //
38 //
39 // QByteArrayDataProvider
40 //
41 //
42 
43 static bool resizeAndInit( QByteArray & ba, size_t newSize ) {
44  const size_t oldSize = ba.size();
45  ba.resize( newSize );
46  const bool ok = ( newSize == static_cast<size_t>( ba.size() ) );
47  if ( ok )
48  memset( ba.data() + oldSize, 0, newSize - oldSize );
49  return ok;
50 }
51 
52 QByteArrayDataProvider::QByteArrayDataProvider()
53  : GpgME::DataProvider(), mOff( 0 ) {}
54 
55 QByteArrayDataProvider::QByteArrayDataProvider( const QByteArray & initialData )
56  : GpgME::DataProvider(), mArray( initialData ), mOff( 0 ) {}
57 
58 QByteArrayDataProvider::~QByteArrayDataProvider() {}
59 
60 ssize_t QByteArrayDataProvider::read( void * buffer, size_t bufSize ) {
61 #ifndef NDEBUG
62  //qDebug( "QByteArrayDataProvider::read( %p, %d )", buffer, bufSize );
63 #endif
64  if ( bufSize == 0 )
65  return 0;
66  if ( !buffer ) {
67  Error::setSystemError( GPG_ERR_EINVAL );
68  return -1;
69  }
70  if ( mOff >= mArray.size() )
71  return 0; // EOF
72  size_t amount = qMin( bufSize, static_cast<size_t>( mArray.size() - mOff ) );
73  assert( amount > 0 );
74  memcpy( buffer, mArray.data() + mOff, amount );
75  mOff += amount;
76  return amount;
77 }
78 
79 ssize_t QByteArrayDataProvider::write( const void * buffer, size_t bufSize ) {
80 #ifndef NDEBUG
81  //qDebug( "QByteArrayDataProvider::write( %p, %lu )", buffer, static_cast<unsigned long>( bufSize ) );
82 #endif
83  if ( bufSize == 0 )
84  return 0;
85  if ( !buffer ) {
86  Error::setSystemError( GPG_ERR_EINVAL );
87  return -1;
88  }
89  if ( mOff >= mArray.size() )
90  resizeAndInit( mArray, mOff + bufSize );
91  if ( mOff >= mArray.size() ) {
92  Error::setSystemError( GPG_ERR_EIO );
93  return -1;
94  }
95  assert( bufSize <= static_cast<size_t>(mArray.size()) - mOff );
96  memcpy( mArray.data() + mOff, buffer, bufSize );
97  mOff += bufSize;
98  return bufSize;
99 }
100 
101 off_t QByteArrayDataProvider::seek( off_t offset, int whence ) {
102 #ifndef NDEBUG
103  //qDebug( "QByteArrayDataProvider::seek( %d, %d )", int(offset), whence );
104 #endif
105  int newOffset = mOff;
106  switch ( whence ) {
107  case SEEK_SET:
108  newOffset = offset;
109  break;
110  case SEEK_CUR:
111  newOffset += offset;
112  break;
113  case SEEK_END:
114  newOffset = mArray.size() + offset;
115  break;
116  default:
117  Error::setSystemError( GPG_ERR_EINVAL );
118  return (off_t)-1;
119  }
120  return mOff = newOffset;
121 }
122 
123 void QByteArrayDataProvider::release() {
124 #ifndef NDEBUG
125  //qDebug( "QByteArrayDataProvider::release()" );
126 #endif
127  mArray = QByteArray();
128 }
129 
130 
131 //
132 //
133 // QIODeviceDataProvider
134 //
135 //
136 
137 QIODeviceDataProvider::QIODeviceDataProvider( const boost::shared_ptr<QIODevice> & io )
138  : GpgME::DataProvider(),
139  mIO( io ),
140  mErrorOccurred( false ),
141  mHaveQProcess( qobject_cast<QProcess*>( io.get() ) )
142 {
143  assert( mIO );
144 }
145 
146 QIODeviceDataProvider::~QIODeviceDataProvider() {}
147 
148 bool QIODeviceDataProvider::isSupported( Operation op ) const {
149  const QProcess* const proc = qobject_cast<QProcess*>( mIO.get() );
150  bool canRead = true;
151  if ( proc ) {
152  canRead = proc->readChannel() == QProcess::StandardOutput;
153  }
154 
155  switch ( op ) {
156  case Read: return mIO->isReadable() && canRead;
157  case Write: return mIO->isWritable();
158  case Seek: return !mIO->isSequential();
159  case Release: return true;
160  default: return false;
161  }
162 }
163 
164 static qint64 blocking_read( const boost::shared_ptr<QIODevice> & io, char * buffer, qint64 maxSize ) {
165  while ( !io->bytesAvailable() ) {
166  if ( !io->waitForReadyRead( -1 ) ) {
167  if ( const QProcess * const p = qobject_cast<QProcess*>( io.get() ) ) {
168  if ( p->error() == QProcess::UnknownError &&
169  p->exitStatus() == QProcess::NormalExit &&
170  p->exitCode() == 0 ) {
171  return 0;
172  } else {
173  Error::setSystemError( GPG_ERR_EIO );
174  return -1;
175  }
176  } else {
177  return 0; // assume EOF (loses error cases :/ )
178  }
179  }
180  }
181  return io->read( buffer, maxSize );
182 }
183 
184 ssize_t QIODeviceDataProvider::read( void * buffer, size_t bufSize ) {
185 #ifndef NDEBUG
186  //qDebug( "QIODeviceDataProvider::read( %p, %lu )", buffer, bufSize );
187 #endif
188  if ( bufSize == 0 )
189  return 0;
190  if ( !buffer ) {
191  Error::setSystemError( GPG_ERR_EINVAL );
192  return -1;
193  }
194  const qint64 numRead = mHaveQProcess
195  ? blocking_read( mIO, static_cast<char*>(buffer), bufSize )
196  : mIO->read( static_cast<char*>(buffer), bufSize ) ;
197 
198  //workaround: some QIODevices (known example: QProcess) might not return 0 (EOF), but immediately -1 when finished. If no
199  //errno is set, gpgme doesn't detect the error and loops forever. So return 0 on the very first -1 in case errno is 0
200 
201  ssize_t rc = numRead;
202  if ( numRead < 0 && !Error::hasSystemError() ) {
203  if ( mErrorOccurred )
204  Error::setSystemError( GPG_ERR_EIO );
205  else
206  rc = 0;
207  }
208  if ( numRead < 0 )
209  mErrorOccurred = true;
210  return rc;
211 }
212 
213 ssize_t QIODeviceDataProvider::write( const void * buffer, size_t bufSize ) {
214 #ifndef NDEBUG
215  //qDebug( "QIODeviceDataProvider::write( %p, %lu )", buffer, static_cast<unsigned long>( bufSize ) );
216 #endif
217  if ( bufSize == 0 )
218  return 0;
219  if ( !buffer ) {
220  Error::setSystemError( GPG_ERR_EINVAL );
221  return -1;
222  }
223 
224  return mIO->write( static_cast<const char*>(buffer), bufSize );
225 }
226 
227 off_t QIODeviceDataProvider::seek( off_t offset, int whence ) {
228 #ifndef NDEBUG
229  //qDebug( "QIODeviceDataProvider::seek( %d, %d )", int(offset), whence );
230 #endif
231  if ( mIO->isSequential() ) {
232  Error::setSystemError( GPG_ERR_ESPIPE );
233  return (off_t)-1;
234  }
235  qint64 newOffset = mIO->pos();
236  switch ( whence ) {
237  case SEEK_SET:
238  newOffset = offset;
239  break;
240  case SEEK_CUR:
241  newOffset += offset;
242  break;
243  case SEEK_END:
244  newOffset = mIO->size() + offset;
245  break;
246  default:
247  Error::setSystemError( GPG_ERR_EINVAL );
248  return (off_t)-1;
249  }
250  if ( !mIO->seek( newOffset ) ) {
251  Error::setSystemError( GPG_ERR_EINVAL );
252  return (off_t)-1;
253  }
254  return newOffset;
255 }
256 
257 void QIODeviceDataProvider::release() {
258 #ifndef NDEBUG
259  //qDebug( "QIODeviceDataProvider::release()" );
260 #endif
261  mIO->close();
262 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:48:33 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

qgpgme

Skip menu "qgpgme"
  • Main Page
  • File List

kdepimlibs-4.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal