1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2009 Ricardo Villalba <rvm@escomposlinux.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include "myprocess.h"
23
#if QT_VERSION < 0x040300
24
#define USE_TEMP_FILE 1
26
#define USE_TEMP_FILE 0
30
#define USE_TEMP_FILE 0
34
MyProcess::MyProcess(QObject * parent) : QProcess(parent)
37
setProcessChannelMode( QProcess::MergedChannels );
40
temp_file.open(); // Create temporary file
41
QString filename = temp_file.fileName();
42
setStandardOutputFile( filename );
43
qDebug("MyProcess::MyProcess: temporary file: %s", filename.toUtf8().data());
46
//connect(&temp_file, SIGNAL(readyRead()), this, SLOT(readTmpFile()) );
47
connect(&timer, SIGNAL(timeout()), this, SLOT(readTmpFile()) );
49
connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()) );
52
connect(this, SIGNAL(finished(int, QProcess::ExitStatus)),
53
this, SLOT(procFinished()) );
55
// Test splitArguments
56
//QStringList l = MyProcess::splitArguments("-opt 1 hello \"56 67\" wssx -ios");
59
void MyProcess::clearArguments() {
64
bool MyProcess::isRunning() {
65
return (state() == QProcess::Running);
68
void MyProcess::addArgument(const QString & a) {
69
if (program.isEmpty()) {
76
QStringList MyProcess::arguments() {
82
void MyProcess::start() {
83
remaining_output.clear();
85
QProcess::start(program, arg);
88
//bool r = temp_file.open(QIODevice::ReadOnly);
89
bool r = temp_file.open();
91
qDebug("MyProcess::start: r: %d", r);
95
void MyProcess::readStdOut() {
96
genericRead( readAllStandardOutput() );
100
void MyProcess::readTmpFile() {
101
genericRead( temp_file.readAll() );
104
void MyProcess::genericRead(QByteArray buffer) {
105
QByteArray ba = remaining_output + buffer;
108
int pos = canReadLine(ba, from_pos);
110
//qDebug("MyProcess::read: pos: %d", pos);
113
//QByteArray line = ba.left(pos);
114
QByteArray line = ba.mid(start, pos-start);
115
//ba = ba.mid(pos+1);
118
if ((from_pos < ba.size()) && (ba.at(from_pos)=='\n')) from_pos++;
122
emit lineAvailable(line);
124
pos = canReadLine(ba, from_pos);
127
remaining_output = ba.mid(from_pos);
130
int MyProcess::canReadLine(const QByteArray & ba, int from) {
131
int pos1 = ba.indexOf('\n', from);
132
int pos2 = ba.indexOf('\r', from);
134
//qDebug("MyProcess::canReadLine: pos2: %d", pos2);
136
if ( (pos1 == -1) && (pos2 == -1) ) return -1;
139
if ( (pos1 != -1) && (pos2 != -1) ) {
141
if (pos2 == (pos1+1)) pos = pos2; // \r\n
144
if (pos1 < pos2) pos = pos1; else pos = pos2;
146
if (pos1 == -1) pos = pos2;
148
if (pos2 == -1) pos = pos1;
155
Do some clean up, and be sure that all output has been read.
157
void MyProcess::procFinished() {
158
qDebug("MyProcess::procFinished");
161
qDebug("MyProcess::procFinished: Bytes available: %ld", bytesAvailable());
162
if ( bytesAvailable() > 0 ) readStdOut();
166
qDebug("MyProcess::procFinished: Bytes available: %ld", temp_file.bytesAvailable());
167
if ( temp_file.bytesAvailable() > 0 ) readTmpFile();
168
qDebug("MyProcess::procFinished: Bytes available: %ld", temp_file.bytesAvailable());
174
QStringList MyProcess::splitArguments(const QString & args) {
175
qDebug("MyProcess::splitArguments: '%s'", args.toUtf8().constData());
179
bool opened_quote = false;
181
for (int n = 0; n < args.length(); n++) {
182
if ((args[n] == QChar(' ')) && (!opened_quote)) {
183
l.append(args.mid(init_pos, n - init_pos));
187
if (args[n] == QChar('\"')) opened_quote = !opened_quote;
189
if (n == args.length()-1) {
190
l.append(args.mid(init_pos, (n - init_pos)+1));
194
for (int n = 0; n < l.count(); n++) {
195
qDebug("MyProcess::splitArguments: arg: %d '%s'", n, l[n].toUtf8().constData());
201
#include "moc_myprocess.cpp"