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

kleopatra

  • sources
  • kde-4.12
  • 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 <KLocale>
45 
46 #include <QVariant>
47 #include <QByteArray>
48 #include <QIODevice>
49 #include <QList>
50 
51 #include <string>
52 #include <algorithm>
53 
54 using namespace Kleo;
55 using namespace boost;
56 
57 static const char option_prefix[] = "prefix";
58 
59 class EchoCommand::Private {
60 public:
61  Private() : operationsInFlight( 0 ), buffer() {}
62 
63  int operationsInFlight;
64  QByteArray buffer;
65 };
66 
67 EchoCommand::EchoCommand()
68  : QObject(), AssuanCommandMixin<EchoCommand>(), d( new Private ) {}
69 
70 EchoCommand::~EchoCommand() {}
71 
72 int EchoCommand::doStart() {
73 
74  const std::vector< shared_ptr<Input> > in = inputs(), msg = messages();
75  const std::vector< shared_ptr<Output> > out = outputs();
76 
77  if ( !in.empty() && out.empty() )
78  return makeError( GPG_ERR_NOT_SUPPORTED );
79 
80  if ( !msg.empty() )
81  return makeError( GPG_ERR_NOT_SUPPORTED );
82 
83  if ( hasOption( option_prefix ) && !option( option_prefix ).toByteArray().isEmpty() )
84  return makeError( GPG_ERR_NOT_IMPLEMENTED );
85 
86  std::string keyword;
87  if ( hasOption( "inquire" ) ) {
88 #ifdef QT_STL
89  keyword = option("inquire").toString().toStdString();
90 #else
91  const QString tmpStr = option("inquire").toString();
92  const QByteArray asc = tmpStr.toLatin1();
93  keyword = std::string(asc.constData(), asc.length());
94 #endif
95  if ( keyword.empty() )
96  return makeError( GPG_ERR_INV_ARG );
97  }
98 
99 #ifdef QT_STL
100  const std::string output = option("text").toString().toStdString();
101 #else
102  const QString tmpStr = option("text").toString();
103  const QByteArray asc = tmpStr.toLatin1();
104  const std::string output = std::string(asc.constData(), asc.length());
105 #endif
106 
107  // aaand ACTION:
108 
109  // 1. echo the command line though the status channel
110  sendStatus( "ECHO", output.empty() ? QString() : QLatin1String(output.c_str()) );
111 
112  // 2. if --inquire was given, inquire more data from the client:
113  if ( !keyword.empty() ) {
114  if ( const int err = inquire( keyword.c_str(), this,
115  SLOT(slotInquireData(int,QByteArray)) ) )
116  return err;
117  else
118  ++d->operationsInFlight;
119  }
120 
121  // 3. if INPUT was given, start the data pump for input->output
122  if ( const shared_ptr<QIODevice> i = in.at(0)->ioDevice() ) {
123  const shared_ptr<QIODevice> o = out.at(0)->ioDevice();
124 
125  ++d->operationsInFlight;
126 
127  connect( i.get(), SIGNAL(readyRead()), this, SLOT(slotInputReadyRead()) );
128  connect( o.get(), SIGNAL(bytesWritten(qint64)), this, SLOT(slotOutputBytesWritten()) );
129 
130  if ( i->bytesAvailable() )
131  slotInputReadyRead();
132  }
133 
134  if ( !d->operationsInFlight )
135  done();
136  return 0;
137 }
138 
139 void EchoCommand::doCanceled() {
140 
141 }
142 
143 void EchoCommand::slotInquireData( int rc, const QByteArray & data ) {
144 
145  --d->operationsInFlight;
146 
147  if ( rc ) {
148  done( rc );
149  return;
150  }
151 
152  try {
153  sendStatus( "ECHOINQ", QLatin1String(data) );
154  if ( !d->operationsInFlight )
155  done();
156  } catch ( const Exception & e ) {
157  done( e.error(), e.message() );
158  } catch ( const std::exception & e ) {
159  done( makeError( GPG_ERR_UNEXPECTED ),
160  i18n("Caught unexpected exception in SignCommand::Private::slotMicAlgDetermined: %1",
161  QString::fromLocal8Bit( e.what() ) ) );
162  } catch ( ... ) {
163  done( makeError( GPG_ERR_UNEXPECTED ),
164  i18n("Caught unknown exception in SignCommand::Private::slotMicAlgDetermined") );
165  }
166 
167 }
168 
169 void EchoCommand::slotInputReadyRead() {
170  const shared_ptr<QIODevice> in = inputs().at(0)->ioDevice();
171  assert( in );
172 
173  QByteArray buffer;
174  buffer.resize( in->bytesAvailable() );
175  const qint64 read = in->read( buffer.data(), buffer.size() );
176  if ( read == - 1 ) {
177  done( makeError( GPG_ERR_EIO ) );
178  return;
179  }
180  if ( read == 0 || (!in->isSequential() && read == in->size()) )
181  in->close();
182 
183  buffer.resize( read );
184  d->buffer += buffer;
185 
186  slotOutputBytesWritten();
187 }
188 
189 
190 void EchoCommand::slotOutputBytesWritten() {
191  const shared_ptr<QIODevice> out = outputs().at(0)->ioDevice();
192  assert( out );
193 
194  if ( !d->buffer.isEmpty() ) {
195 
196  if ( out->bytesToWrite() )
197  return;
198 
199  const qint64 written = out->write( d->buffer );
200  if ( written == -1 ) {
201  done( makeError( GPG_ERR_EIO ) );
202  return;
203  }
204  d->buffer.remove( 0, written );
205 
206  }
207 
208  if ( out->isOpen() && d->buffer.isEmpty() && !inputs().at(0)->ioDevice()->isOpen() ) {
209  out->close();
210  if ( !--d->operationsInFlight )
211  done();
212  }
213 }
214 
215 #include "moc_echocommand.cpp"
Kleo::EchoCommand::EchoCommand
EchoCommand()
Definition: echocommand.cpp:67
output.h
Kleo::EchoCommand::~EchoCommand
~EchoCommand()
Definition: echocommand.cpp:70
input.h
Kleo::EchoCommand
GnuPG UI Server command for testing.
Definition: echocommand.h:63
option
const char * option
Definition: kleopatraapplication.cpp:91
Kleo::AssuanCommandMixin
Definition: assuancommand.h:367
boost::shared_ptr< QIODevice >
d
#define d
Definition: adduseridcommand.cpp:90
option_prefix
static const char option_prefix[]
Definition: echocommand.cpp:57
string
const char * string
Definition: verifychecksumscontroller.cpp:511
echocommand.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 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

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