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

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • uiserver
echocommand.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  uiserver/echocommand.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra 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
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 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 <config-kleopatra.h>
34 
35 #include "echocommand.h"
36 
37 #include <utils/input.h>
38 #include <utils/output.h>
39 
40 #include <kleo/exception.h>
41 
42 #include <gpg-error.h>
43 
44 #include <KLocalizedString>
45 
46 #include <QByteArray>
47 #include <QIODevice>
48 #include <QVariant>
49 
50 #include <string>
51 #include <algorithm>
52 
53 using namespace Kleo;
54 using namespace boost;
55 
56 static const char option_prefix[] = "prefix";
57 
58 class EchoCommand::Private {
59 public:
60  Private() : operationsInFlight( 0 ), buffer() {}
61 
62  int operationsInFlight;
63  QByteArray buffer;
64 };
65 
66 EchoCommand::EchoCommand()
67  : QObject(), AssuanCommandMixin<EchoCommand>(), d( new Private ) {}
68 
69 EchoCommand::~EchoCommand() {}
70 
71 int EchoCommand::doStart() {
72 
73  const std::vector< shared_ptr<Input> > in = inputs(), msg = messages();
74  const std::vector< shared_ptr<Output> > out = outputs();
75 
76  if ( !in.empty() && out.empty() )
77  return makeError( GPG_ERR_NOT_SUPPORTED );
78 
79  if ( !msg.empty() )
80  return makeError( GPG_ERR_NOT_SUPPORTED );
81 
82  if ( hasOption( option_prefix ) && !option( option_prefix ).toByteArray().isEmpty() )
83  return makeError( GPG_ERR_NOT_IMPLEMENTED );
84 
85  std::string keyword;
86  if ( hasOption( "inquire" ) ) {
87 #ifdef QT_STL
88  keyword = option("inquire").toString().toStdString();
89 #else
90  const QString tmpStr = option("inquire").toString();
91  const QByteArray asc = tmpStr.toLatin1();
92  keyword = std::string(asc.constData(), asc.length());
93 #endif
94  if ( keyword.empty() )
95  return makeError( GPG_ERR_INV_ARG );
96  }
97 
98 #ifdef QT_STL
99  const std::string output = option("text").toString().toStdString();
100 #else
101  const QString tmpStr = option("text").toString();
102  const QByteArray asc = tmpStr.toLatin1();
103  const std::string output = std::string(asc.constData(), asc.length());
104 #endif
105 
106  // aaand ACTION:
107 
108  // 1. echo the command line though the status channel
109  sendStatus( "ECHO", output.empty() ? QString() : QLatin1String(output.c_str()) );
110 
111  // 2. if --inquire was given, inquire more data from the client:
112  if ( !keyword.empty() ) {
113  if ( const int err = inquire( keyword.c_str(), this,
114  SLOT(slotInquireData(int,QByteArray)) ) )
115  return err;
116  else
117  ++d->operationsInFlight;
118  }
119 
120  // 3. if INPUT was given, start the data pump for input->output
121  if ( const shared_ptr<QIODevice> i = in.at(0)->ioDevice() ) {
122  const shared_ptr<QIODevice> o = out.at(0)->ioDevice();
123 
124  ++d->operationsInFlight;
125 
126  connect( i.get(), SIGNAL(readyRead()), this, SLOT(slotInputReadyRead()) );
127  connect( o.get(), SIGNAL(bytesWritten(qint64)), this, SLOT(slotOutputBytesWritten()) );
128 
129  if ( i->bytesAvailable() )
130  slotInputReadyRead();
131  }
132 
133  if ( !d->operationsInFlight )
134  done();
135  return 0;
136 }
137 
138 void EchoCommand::doCanceled() {
139 
140 }
141 
142 void EchoCommand::slotInquireData( int rc, const QByteArray & data ) {
143 
144  --d->operationsInFlight;
145 
146  if ( rc ) {
147  done( rc );
148  return;
149  }
150 
151  try {
152  sendStatus( "ECHOINQ", QLatin1String(data) );
153  if ( !d->operationsInFlight )
154  done();
155  } catch ( const Exception & e ) {
156  done( e.error(), e.message() );
157  } catch ( const std::exception & e ) {
158  done( makeError( GPG_ERR_UNEXPECTED ),
159  i18n("Caught unexpected exception in SignCommand::Private::slotMicAlgDetermined: %1",
160  QString::fromLocal8Bit( e.what() ) ) );
161  } catch ( ... ) {
162  done( makeError( GPG_ERR_UNEXPECTED ),
163  i18n("Caught unknown exception in SignCommand::Private::slotMicAlgDetermined") );
164  }
165 
166 }
167 
168 void EchoCommand::slotInputReadyRead() {
169  const shared_ptr<QIODevice> in = inputs().at(0)->ioDevice();
170  assert( in );
171 
172  QByteArray buffer;
173  buffer.resize( in->bytesAvailable() );
174  const qint64 read = in->read( buffer.data(), buffer.size() );
175  if ( read == - 1 ) {
176  done( makeError( GPG_ERR_EIO ) );
177  return;
178  }
179  if ( read == 0 || (!in->isSequential() && read == in->size()) )
180  in->close();
181 
182  buffer.resize( read );
183  d->buffer += buffer;
184 
185  slotOutputBytesWritten();
186 }
187 
188 
189 void EchoCommand::slotOutputBytesWritten() {
190  const shared_ptr<QIODevice> out = outputs().at(0)->ioDevice();
191  assert( out );
192 
193  if ( !d->buffer.isEmpty() ) {
194 
195  if ( out->bytesToWrite() )
196  return;
197 
198  const qint64 written = out->write( d->buffer );
199  if ( written == -1 ) {
200  done( makeError( GPG_ERR_EIO ) );
201  return;
202  }
203  d->buffer.remove( 0, written );
204 
205  }
206 
207  if ( out->isOpen() && d->buffer.isEmpty() && !inputs().at(0)->ioDevice()->isOpen() ) {
208  out->close();
209  if ( !--d->operationsInFlight )
210  done();
211  }
212 }
213 
Kleo::EchoCommand::EchoCommand
EchoCommand()
Definition: echocommand.cpp:66
output.h
Kleo::EchoCommand::~EchoCommand
~EchoCommand()
Definition: echocommand.cpp:69
input.h
QByteArray
Kleo::EchoCommand
GnuPG UI Server command for testing.
Definition: echocommand.h:63
QByteArray::length
int length() const
option
const char * option
Definition: kleopatraapplication.cpp:93
Kleo::AssuanCommandMixin
Definition: assuancommand.h:369
QByteArray::resize
void resize(int size)
boost::shared_ptr< QIODevice >
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
d
#define d
Definition: adduseridcommand.cpp:89
QObject
QByteArray::constData
const char * constData() const
QString
option_prefix
static const char option_prefix[]
Definition: echocommand.cpp:56
string
const char * string
Definition: verifychecksumscontroller.cpp:510
QString::toLatin1
QByteArray toLatin1() const
QLatin1String
QByteArray::data
char * data()
echocommand.h
QByteArray::size
int size() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • 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
  • pimprint

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