source: smplayer/trunk/src/myprocess.cpp@ 106

Last change on this file since 106 was 93, checked in by Silvan Scherrer, 15 years ago

smplayer: 0.6.9

File size: 5.0 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
3
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.
8
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.
13
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
17*/
18
19#include "myprocess.h"
20
21#ifdef Q_OS_WIN
22
23#if QT_VERSION < 0x040300
24#define USE_TEMP_FILE 1
25#else
26#define USE_TEMP_FILE 0
27#endif
28
29#else
30#define USE_TEMP_FILE 0
31#endif
32
33
34MyProcess::MyProcess(QObject * parent) : QProcess(parent)
35{
36 clearArguments();
37 setProcessChannelMode( QProcess::MergedChannels );
38
39#if USE_TEMP_FILE
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());
44 temp_file.close();
45
46 //connect(&temp_file, SIGNAL(readyRead()), this, SLOT(readTmpFile()) );
47 connect(&timer, SIGNAL(timeout()), this, SLOT(readTmpFile()) );
48#else
49 connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()) );
50#endif
51
52 connect(this, SIGNAL(finished(int, QProcess::ExitStatus)),
53 this, SLOT(procFinished()) );
54
55 // Test splitArguments
56 //QStringList l = MyProcess::splitArguments("-opt 1 hello \"56 67\" wssx -ios");
57}
58
59void MyProcess::clearArguments() {
60 program = "";
61 arg.clear();
62}
63
64bool MyProcess::isRunning() {
65 return (state() == QProcess::Running);
66}
67
68void MyProcess::addArgument(const QString & a) {
69 if (program.isEmpty()) {
70 program = a;
71 } else {
72 arg.append(a);
73 }
74}
75
76QStringList MyProcess::arguments() {
77 QStringList l = arg;
78 l.prepend(program);
79 return l;
80}
81
82void MyProcess::start() {
83 remaining_output.clear();
84
85 QProcess::start(program, arg);
86
87#if USE_TEMP_FILE
88 //bool r = temp_file.open(QIODevice::ReadOnly);
89 bool r = temp_file.open();
90 timer.start(50);
91 qDebug("MyProcess::start: r: %d", r);
92#endif
93}
94
95void MyProcess::readStdOut() {
96 genericRead( readAllStandardOutput() );
97}
98
99
100void MyProcess::readTmpFile() {
101 genericRead( temp_file.readAll() );
102}
103
104void MyProcess::genericRead(QByteArray buffer) {
105 QByteArray ba = remaining_output + buffer;
106 int start = 0;
107 int from_pos = 0;
108 int pos = canReadLine(ba, from_pos);
109
110 //qDebug("MyProcess::read: pos: %d", pos);
111 while ( pos > -1 ) {
112 // Readline
113 //QByteArray line = ba.left(pos);
114 QByteArray line = ba.mid(start, pos-start);
115 //ba = ba.mid(pos+1);
116 from_pos = pos + 1;
117#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
118 if ((from_pos < ba.size()) && (ba.at(from_pos)=='\n')) from_pos++;
119#endif
120 start = from_pos;
121
122 emit lineAvailable(line);
123
124 pos = canReadLine(ba, from_pos);
125 }
126
127 remaining_output = ba.mid(from_pos);
128}
129
130int MyProcess::canReadLine(const QByteArray & ba, int from) {
131 int pos1 = ba.indexOf('\n', from);
132 int pos2 = ba.indexOf('\r', from);
133
134 //qDebug("MyProcess::canReadLine: pos2: %d", pos2);
135
136 if ( (pos1 == -1) && (pos2 == -1) ) return -1;
137
138 int pos = pos1;
139 if ( (pos1 != -1) && (pos2 != -1) ) {
140 /*
141 if (pos2 == (pos1+1)) pos = pos2; // \r\n
142 else
143 */
144 if (pos1 < pos2) pos = pos1; else pos = pos2;
145 } else {
146 if (pos1 == -1) pos = pos2;
147 else
148 if (pos2 == -1) pos = pos1;
149 }
150
151 return pos;
152}
153
154/*!
155Do some clean up, and be sure that all output has been read.
156*/
157void MyProcess::procFinished() {
158 qDebug("MyProcess::procFinished");
159
160#if !USE_TEMP_FILE
161 qDebug("MyProcess::procFinished: Bytes available: %ld", bytesAvailable());
162 if ( bytesAvailable() > 0 ) readStdOut();
163#else
164 timer.stop();
165
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());
169
170 temp_file.close();
171#endif
172}
173
174QStringList MyProcess::splitArguments(const QString & args) {
175 qDebug("MyProcess::splitArguments: '%s'", args.toUtf8().constData());
176
177 QStringList l;
178
179 bool opened_quote = false;
180 int init_pos = 0;
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));
184 init_pos = n+1;
185 }
186 else
187 if (args[n] == QChar('\"')) opened_quote = !opened_quote;
188
189 if (n == args.length()-1) {
190 l.append(args.mid(init_pos, (n - init_pos)+1));
191 }
192 }
193
194 for (int n = 0; n < l.count(); n++) {
195 qDebug("MyProcess::splitArguments: arg: %d '%s'", n, l[n].toUtf8().constData());
196 }
197
198 return l;
199}
200
201#include "moc_myprocess.cpp"
Note: See TracBrowser for help on using the repository browser.