• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkleo

  • sources
  • kde-4.12
  • kdepim
  • libkleo
  • backends
  • qgpgme
gnupgprocessbase.cpp
Go to the documentation of this file.
1 /*
2  gnupgprocessbase.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004 Klar�vdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include "gnupgprocessbase.h"
34 
35 #include <kdebug.h>
36 #include <kurl.h>
37 
38 #include <QSocketNotifier>
39 #include <QTextCodec>
40 #include <QStringList>
41 #include <QByteArray>
42 #include <QList>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <assert.h>
47 #include <stdio.h>
48 
49 #if 0
50 struct Kleo::GnuPGProcessBase::Private {
51  Private() : useStatusFD( false ), statnot( 0 ) {
52  statusFD[0] = statusFD[1] = -1;
53  }
54 
55  bool useStatusFD;
56  int statusFD[2];
57  QSocketNotifier * statnot;
58  QByteArray statusBuffer;
59 };
60 #endif
61 
62 Kleo::GnuPGProcessBase::GnuPGProcessBase( QObject * parent )
63  : KProcess( parent )
64 {
65  //d = new Private();
66 }
67 
68 Kleo::GnuPGProcessBase::~GnuPGProcessBase() {
69  //delete d; d = 0;
70 }
71 
72 void Kleo::GnuPGProcessBase::setUseStatusFD( bool /*use*/ ) {
73  //assert( d );
74  //d->useStatusFD = use;
75 }
76 
77 #if 0
78 bool Kleo::GnuPGProcessBase::start( RunMode runmode, Communication comm ) {
79  if ( d->useStatusFD ) {
80  // set up the status-fd. This should be in setupCommunication(),
81  // but then it's too late: we need the fd of the pipe to pass it
82  // as argument to the --status-fd option:
83  // PENDING(marc) find out why K3Process uses both pipe() and socketpair()...
84  if ( ::pipe( d->statusFD ) < 0 ) {
85  kDebug( 5150 ) <<"Kleo::GnuPGProcessBase::start: pipe(2) failed:" << perror;
86  return false;
87  }
88  ::fcntl( d->statusFD[0], F_SETFD, FD_CLOEXEC );
89  ::fcntl( d->statusFD[1], F_SETFD, FD_CLOEXEC );
90  if ( !arguments.empty() ) {
91  QList<QByteArray>::iterator it = arguments.begin();
92  ++it;
93  arguments.insert( it, "--status-fd" );
94  char buf[25];
95  sprintf( buf, "%d", d->statusFD[1] );
96  arguments.insert( it, buf );
97  arguments.insert( it, "--no-tty" );
98  //arguments.insert( it, "--enable-progress-filter" ); // gpgsm doesn't know this
99  }
100  }
101  return K3Process::start( runmode, comm );
102 }
103 
104 int Kleo::GnuPGProcessBase::setupCommunication( Communication comm ) {
105  if ( int ok = K3Process::setupCommunication( comm ) )
106  return ok;
107  if ( d->useStatusFD ) {
108  // base class impl returned error, so close our fd's, too
109  ::close( d->statusFD[0] );
110  ::close( d->statusFD[1] );
111  d->statusFD[0] = d->statusFD[1] = -1;
112  }
113  return 0; // Error
114 }
115 
116 int Kleo::GnuPGProcessBase::commSetupDoneP() {
117  if ( d->useStatusFD ) {
118  ::close( d->statusFD[1] ); // close the input end of the pipe, we're the reader
119  d->statnot = new QSocketNotifier( d->statusFD[0], QSocketNotifier::Read, this );
120  connect( d->statnot, SIGNAL(activated(int)), SLOT(slotChildStatus(int)) );
121  }
122  return K3Process::commSetupDoneP();
123 }
124 
125 int Kleo::GnuPGProcessBase::commSetupDoneC() {
126  if ( d->useStatusFD )
127  ::fcntl( d->statusFD[1], F_SETFD, 0 );
128  return K3Process::commSetupDoneC();
129 }
130 
131 void Kleo::GnuPGProcessBase::slotChildStatus( int fd ) {
132  if ( !childStatus(fd) )
133  closeStatus();
134 }
135 
136 bool Kleo::GnuPGProcessBase::closeStatus() {
137  if ( !d->useStatusFD )
138  return false;
139  d->useStatusFD = false;
140  delete d->statnot; d->statnot = 0;
141  ::close( d->statusFD[0] ); d->statusFD[0] = -1;
142  return true;
143 }
144 
145 int Kleo::GnuPGProcessBase::childStatus( int fd ) {
146  char buf[1024];
147  const int len = ::read( fd, buf, sizeof(buf)-1 );
148  if ( len > 0 ) {
149  buf[len] = 0;
150  d->statusBuffer += buf;
151  parseStatusOutput();
152  }
153  return len;
154 }
155 
156 static QString fromHexEscapedUtf8( const QByteArray & str ) {
157  return KUrl::fromPercentEncoding( str.data() /* utf-8 */ );
158 }
159 
160 void Kleo::GnuPGProcessBase::parseStatusOutput() {
161  static const char startToken[] = "[GNUPG:] ";
162  static const int startTokenLen = sizeof startToken / sizeof *startToken - 1;
163 
164  int lineStart = 0;
165  for ( int lineEnd = d->statusBuffer.indexOf( '\n' ) ; lineEnd >= 0 ; lineEnd = d->statusBuffer.indexOf( '\n', lineStart = lineEnd+1 ) ) {
166  // get next line:
167  const QByteArray line = d->statusBuffer.mid( lineStart, lineEnd - lineStart ).trimmed();
168  if ( line.isEmpty() )
169  continue;
170  // check status token
171  if ( line.left( startTokenLen ) != startToken ) {
172  kDebug( 5150 ) <<"Kleo::GnuPGProcessBase::childStatus: status-fd protocol error: line doesn't begin with \""
173  << startToken << "\"";
174  continue;
175  }
176  // remove status token:
177  const QByteArray command = line.mid( startTokenLen ).simplified() + ' ';
178  if ( command == " " ) {
179  kDebug( 5150 ) <<"Kleo::GnuPGProcessBase::childStatus: status-fd protocol error: line without content.";
180  continue;
181  }
182  // split into base and args
183  QString cmd;
184  QStringList args;
185  int tagStart = 0;
186  for ( int tagEnd = command.indexOf( ' ' ) ; tagEnd >= 0 ; tagEnd = command.indexOf( ' ', tagStart = tagEnd+1 ) ) {
187  const QByteArray tag = command.mid( tagStart, tagEnd - tagStart );
188  if ( cmd.isNull() )
189  cmd = fromHexEscapedUtf8( tag );
190  else
191  args.push_back( fromHexEscapedUtf8( tag ) );
192  }
193  emit status( this, cmd, args );
194  }
195  d->statusBuffer = d->statusBuffer.mid( lineStart );
196 }
197 #endif
198 
199 #include "gnupgprocessbase.moc"
KProcess
Kleo::GnuPGProcessBase::setUseStatusFD
void setUseStatusFD(bool use)
Definition: gnupgprocessbase.cpp:72
Kleo::GnuPGProcessBase::~GnuPGProcessBase
~GnuPGProcessBase()
Definition: gnupgprocessbase.cpp:68
QString
QObject
gnupgprocessbase.h
Kleo::GnuPGProcessBase::GnuPGProcessBase
GnuPGProcessBase(QObject *parent=0)
Definition: gnupgprocessbase.cpp:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:57:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkleo

Skip menu "libkleo"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



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