symcryptrunprocessbase.cpp
1 /*
2  symcryptrunbackend.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2005 Klarälvdalens 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
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 TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), 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  TQt. 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 "symcryptrunprocessbase.h"
34 
35 #include <tdetempfile.h>
36 #include <kdebug.h>
37 #include <kshell.h>
38 
39 #include <tqtimer.h>
40 #include <tqfile.h>
41 
42 #include <cstring>
43 
44 Kleo::SymCryptRunProcessBase::SymCryptRunProcessBase( const TQString & class_, const TQString & program,
45  const TQString & keyFile, const TQString & options,
46  Operation mode,
47  TQObject * parent, const char * name )
48  : TDEProcess( parent, name ),
49  mOperation( mode ), mOptions( options )
50 {
51  *this << "symcryptrun"
52  << "--class" << class_
53  << "--program" << program
54  << "--keyfile" << keyFile
55  << ( mode == Encrypt ? "--encrypt" : "--decrypt" );
56 }
57 
58 Kleo::SymCryptRunProcessBase::~SymCryptRunProcessBase() {}
59 
60 bool Kleo::SymCryptRunProcessBase::launch( const TQByteArray & input, RunMode rm ) {
61  connect( this, TQ_SIGNAL(receivedStdout(TDEProcess*,char*,int)),
62  this, TQ_SLOT(slotReceivedStdout(TDEProcess*,char*,int)) );
63  connect( this, TQ_SIGNAL(receivedStderr(TDEProcess*,char*,int)),
64  this, TQ_SLOT(slotReceivedStderr(TDEProcess*,char*,int)) );
65  if ( rm == Block ) {
66  KTempFile tempfile;
67  tempfile.setAutoDelete( true );
68  if ( TQFile * file = tempfile.file() )
69  file->writeBlock( input );
70  else
71  return false;
72  tempfile.close();
73  *this << "--input" << tempfile.name();
74  addOptions();
75  return TDEProcess::start( Block, All );
76  } else {
77  addOptions();
78  const bool ok = TDEProcess::start( rm, All );
79  if ( !ok )
80  return ok;
81  mInput = input.copy();
82  writeStdin( mInput.begin(), mInput.size() );
83  connect( this, TQ_SIGNAL(wroteStdin(TDEProcess*)), this, TQ_SLOT(closeStdin()) );
84  return true;
85  }
86 }
87 
88 void Kleo::SymCryptRunProcessBase::addOptions() {
89  if ( !mOptions.isEmpty() )
90  {
91  const TQStringList args = KShell::splitArgs( mOptions );
92  *this << "--" << args;
93  }
94 }
95 
96 void Kleo::SymCryptRunProcessBase::slotReceivedStdout( TDEProcess * proc, char * buf, int len ) {
97  Q_ASSERT( proc == this );
98  const int oldsize = mOutput.size();
99  mOutput.resize( oldsize + len );
100  memcpy( mOutput.data() + oldsize, buf, len );
101 }
102 
103 void Kleo::SymCryptRunProcessBase::slotReceivedStderr( TDEProcess * proc, char * buf, int len ) {
104  Q_ASSERT( proc == this );
105  if ( len > 0 )
106  mStderr += TQString::fromLocal8Bit( buf, len );
107 }
108 
109 #include "symcryptrunprocessbase.moc"