1 /* This file is part of the KDE project
2 Copyright 2002 John Firebaugh <jfirebaugh@kde.org>
3 Copyright 2007 Andreas Pakulat <apaku@gmx.de>
4 Copyright 2007 Oswald Buddenhagen <ossi@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "processlinemaker.h"
24 #include <QtCore/QProcess>
25 #include <QtCore/QStringList>
26 #include <QtCore/QTimer>
31 class ProcessLineMakerPrivate
39 ProcessLineMakerPrivate( ProcessLineMaker
* maker
)
44 void slotReadyReadStdout()
46 stdoutbuf
+= m_proc
->readAllStandardOutput();
54 while ( (pos
= stdoutbuf
.indexOf('\n')) != -1) {
55 if (pos
> 0 && stdoutbuf
.at(pos
- 1) == '\r')
56 lineList
<< QString::fromLocal8Bit(stdoutbuf
, pos
- 1);
58 lineList
<< QString::fromLocal8Bit(stdoutbuf
, pos
);
59 stdoutbuf
.remove(0, pos
+1);
61 emit p
->receivedStdoutLines(lineList
);
64 void slotReadyReadStderr()
66 stderrbuf
+= m_proc
->readAllStandardError();
74 while ( (pos
= stderrbuf
.indexOf('\n')) != -1) {
75 if (pos
> 0 && stderrbuf
.at(pos
- 1) == '\r')
76 lineList
<< QString::fromLocal8Bit(stderrbuf
, pos
- 1);
78 lineList
<< QString::fromLocal8Bit(stderrbuf
, pos
);
79 stderrbuf
.remove(0, pos
+1);
81 emit p
->receivedStderrLines(lineList
);
86 ProcessLineMaker::ProcessLineMaker(QObject
* parent
)
88 , d( new ProcessLineMakerPrivate( this ) )
92 ProcessLineMaker::ProcessLineMaker( QProcess
* proc
, QObject
* parent
)
94 , d( new ProcessLineMakerPrivate( this ) )
97 d
->m_proc
->setTextModeEnabled( true );
98 connect(proc
, SIGNAL(readyReadStandardOutput()),
99 this, SLOT(slotReadyReadStdout()) );
100 connect(proc
, SIGNAL(readyReadStandardError()),
101 this, SLOT(slotReadyReadStderr()) );
104 ProcessLineMaker::~ProcessLineMaker()
109 void ProcessLineMaker::slotReceivedStdout( const QByteArray
& buffer
)
111 d
->stdoutbuf
+= buffer
;
115 void ProcessLineMaker::slotReceivedStderr( const QByteArray
& buffer
)
117 d
->stderrbuf
+= buffer
;
121 void ProcessLineMaker::discardBuffers( )
123 d
->stderrbuf
.truncate(0);
124 d
->stdoutbuf
.truncate(0);
127 void ProcessLineMaker::flushBuffers()
129 if (!d
->stdoutbuf
.isEmpty())
130 emit
receivedStdoutLines(QStringList(QString::fromLocal8Bit(d
->stdoutbuf
)));
131 if (!d
->stderrbuf
.isEmpty())
132 emit
receivedStderrLines(QStringList(QString::fromLocal8Bit(d
->stderrbuf
)));
138 #include "processlinemaker.moc"