libkdepim

collectingprocess.cpp

00001 /*
00002     collectingprocess.cpp
00003 
00004     This file is part of libkdepim.
00005     Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License as
00009     published by the Free Software Foundation; either version 2 of the
00010     License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #include "collectingprocess.h"
00034 
00035 #include <qvaluelist.h>
00036 
00037 #include <string.h>
00038 
00039 using namespace KPIM;
00040 
00041 struct CollectingProcess::Private {
00042   Private() : stdoutSize( 0 ), stderrSize( 0 )
00043     {}
00044 
00045   uint stdoutSize;
00046   QValueList<QByteArray> stdoutBuffer;
00047   uint stderrSize;
00048   QValueList<QByteArray> stderrBuffer;
00049 };
00050 
00051 
00052 CollectingProcess::CollectingProcess( QObject * parent, const char * name )
00053   : KProcess( parent, name )
00054 {
00055   d = new Private();
00056 }
00057 
00058 CollectingProcess::~CollectingProcess() {
00059   delete d; d = 0;
00060 }
00061 
00062 bool CollectingProcess::start( RunMode runmode, Communication comm ) {
00063   // prevent duplicate connection
00064   disconnect( this, SIGNAL( receivedStdout( KProcess *, char *, int ) ),
00065               this, SLOT( slotReceivedStdout( KProcess *, char *, int ) ) );
00066   if ( comm & Stdout ) {
00067     connect( this, SIGNAL( receivedStdout( KProcess *, char *, int ) ),
00068              this, SLOT( slotReceivedStdout( KProcess *, char *, int ) ) );
00069   }
00070   // prevent duplicate connection
00071   disconnect( this, SIGNAL( receivedStderr( KProcess *, char *, int ) ),
00072               this, SLOT( slotReceivedStderr( KProcess *, char *, int ) ) );
00073   if ( comm & Stderr ) {
00074     connect( this, SIGNAL( receivedStderr( KProcess *, char *, int ) ),
00075              this, SLOT( slotReceivedStderr( KProcess *, char *, int ) ) );
00076   }
00077   return KProcess::start( runmode, comm );
00078 }
00079 
00080 void CollectingProcess::slotReceivedStdout( KProcess *, char *buf, int len )
00081 {
00082   QByteArray b;
00083   b.duplicate( buf, len );
00084   d->stdoutBuffer.append( b );
00085   d->stdoutSize += len;
00086 }
00087 
00088 void CollectingProcess::slotReceivedStderr( KProcess *, char *buf, int len )
00089 {
00090   QByteArray b;
00091   b.duplicate( buf, len );
00092   d->stderrBuffer.append( b );
00093   d->stderrSize += len;
00094 }
00095 
00096 QByteArray CollectingProcess::collectedStdout()
00097 {
00098   if ( d->stdoutSize == 0 ) {
00099     return QByteArray();
00100   }
00101 
00102   uint offset = 0;
00103   QByteArray b( d->stdoutSize );
00104   for ( QValueList<QByteArray>::const_iterator it = d->stdoutBuffer.begin();
00105         it != d->stdoutBuffer.end();
00106         ++it ) {
00107     memcpy( b.data() + offset, (*it).data(), (*it).size() );
00108     offset += (*it).size();
00109   }
00110   d->stdoutBuffer.clear();
00111   d->stdoutSize = 0;
00112 
00113   return b;
00114 }
00115 
00116 QByteArray CollectingProcess::collectedStderr()
00117 {
00118   if ( d->stderrSize == 0 ) {
00119     return QByteArray();
00120   }
00121 
00122   uint offset = 0;
00123   QByteArray b( d->stderrSize );
00124   for ( QValueList<QByteArray>::const_iterator it = d->stderrBuffer.begin();
00125         it != d->stderrBuffer.end();
00126         ++it ) {
00127     memcpy( b.data() + offset, (*it).data(), (*it).size() );
00128     offset += (*it).size();
00129   }
00130   d->stderrBuffer.clear();
00131   d->stderrSize = 0;
00132 
00133   return b;
00134 }
00135 
00136 void CollectingProcess::virtual_hook( int id, void * data ) {
00137   KProcess::virtual_hook( id, data );
00138 }
00139 
00140 #include "collectingprocess.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys