libtdepim

collectingprocess.cpp
1 /*
2  collectingprocess.cpp
3 
4  This file is part of libtdepim.
5  Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org>
6 
7  This library 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  This library 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 "collectingprocess.h"
34 
35 #include <tqvaluelist.h>
36 
37 #include <string.h>
38 
39 using namespace KPIM;
40 
41 struct CollectingProcess::Private {
42  Private() : stdoutSize( 0 ), stderrSize( 0 )
43  {}
44 
45  uint stdoutSize;
46  TQValueList<TQByteArray> stdoutBuffer;
47  uint stderrSize;
48  TQValueList<TQByteArray> stderrBuffer;
49 };
50 
51 
52 CollectingProcess::CollectingProcess( TQObject * parent, const char * name )
53  : TDEProcess( parent, name )
54 {
55  d = new Private();
56 }
57 
58 CollectingProcess::~CollectingProcess() {
59  delete d; d = 0;
60 }
61 
62 bool CollectingProcess::start( RunMode runmode, Communication comm ) {
63  // prevent duplicate connection
64  disconnect( this, TQ_SIGNAL( receivedStdout( TDEProcess *, char *, int ) ),
65  this, TQ_SLOT( slotReceivedStdout( TDEProcess *, char *, int ) ) );
66  if ( comm & Stdout ) {
67  connect( this, TQ_SIGNAL( receivedStdout( TDEProcess *, char *, int ) ),
68  this, TQ_SLOT( slotReceivedStdout( TDEProcess *, char *, int ) ) );
69  }
70  // prevent duplicate connection
71  disconnect( this, TQ_SIGNAL( receivedStderr( TDEProcess *, char *, int ) ),
72  this, TQ_SLOT( slotReceivedStderr( TDEProcess *, char *, int ) ) );
73  if ( comm & Stderr ) {
74  connect( this, TQ_SIGNAL( receivedStderr( TDEProcess *, char *, int ) ),
75  this, TQ_SLOT( slotReceivedStderr( TDEProcess *, char *, int ) ) );
76  }
77  return TDEProcess::start( runmode, comm );
78 }
79 
80 void CollectingProcess::slotReceivedStdout( TDEProcess *, char *buf, int len )
81 {
82  TQByteArray b;
83  b.duplicate( buf, len );
84  d->stdoutBuffer.append( b );
85  d->stdoutSize += len;
86 }
87 
88 void CollectingProcess::slotReceivedStderr( TDEProcess *, char *buf, int len )
89 {
90  TQByteArray b;
91  b.duplicate( buf, len );
92  d->stderrBuffer.append( b );
93  d->stderrSize += len;
94 }
95 
97 {
98  if ( d->stdoutSize == 0 ) {
99  return TQByteArray();
100  }
101 
102  uint offset = 0;
103  TQByteArray b( d->stdoutSize );
104  for ( TQValueList<TQByteArray>::const_iterator it = d->stdoutBuffer.begin();
105  it != d->stdoutBuffer.end();
106  ++it ) {
107  memcpy( b.data() + offset, (*it).data(), (*it).size() );
108  offset += (*it).size();
109  }
110  d->stdoutBuffer.clear();
111  d->stdoutSize = 0;
112 
113  return b;
114 }
115 
117 {
118  if ( d->stderrSize == 0 ) {
119  return TQByteArray();
120  }
121 
122  uint offset = 0;
123  TQByteArray b( d->stderrSize );
124  for ( TQValueList<TQByteArray>::const_iterator it = d->stderrBuffer.begin();
125  it != d->stderrBuffer.end();
126  ++it ) {
127  memcpy( b.data() + offset, (*it).data(), (*it).size() );
128  offset += (*it).size();
129  }
130  d->stderrBuffer.clear();
131  d->stderrSize = 0;
132 
133  return b;
134 }
135 
136 void CollectingProcess::virtual_hook( int id, void * data ) {
137  TDEProcess::virtual_hook( id, data );
138 }
139 
140 #include "collectingprocess.moc"
TQByteArray collectedStderr()
Returns the contents of the stderr buffer and clears it afterwards.
bool start(RunMode runmode, Communication comm)
Starts the process in NotifyOnExit mode and writes in to stdin of the process.
TQByteArray collectedStdout()
Returns the contents of the stdout buffer and clears it afterwards.
TDEPIM classes for drag and drop of mails.