1.1.8
by Maia Kozheva
Import upstream version 0.6.6 |
1 |
/* smplayer, GUI front-end for mplayer.
|
1.2.4
by Maia Kozheva
Import upstream version 0.6.9 |
2 |
Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
|
1.1.8
by Maia Kozheva
Import upstream version 0.6.6 |
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 |
||
34 |
MyProcess::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()) ); |
|
1.2.2
by Maia Kozheva
Import upstream version 0.6.7 |
54 |
|
55 |
// Test splitArguments
|
|
56 |
//QStringList l = MyProcess::splitArguments("-opt 1 hello \"56 67\" wssx -ios");
|
|
1.1.8
by Maia Kozheva
Import upstream version 0.6.6 |
57 |
}
|
58 |
||
59 |
void MyProcess::clearArguments() { |
|
60 |
program = ""; |
|
61 |
arg.clear(); |
|
62 |
}
|
|
63 |
||
64 |
bool MyProcess::isRunning() { |
|
65 |
return (state() == QProcess::Running); |
|
66 |
}
|
|
67 |
||
68 |
void MyProcess::addArgument(const QString & a) { |
|
69 |
if (program.isEmpty()) { |
|
70 |
program = a; |
|
71 |
} else { |
|
72 |
arg.append(a); |
|
73 |
}
|
|
74 |
}
|
|
75 |
||
76 |
QStringList MyProcess::arguments() { |
|
77 |
QStringList l = arg; |
|
78 |
l.prepend(program); |
|
79 |
return l; |
|
80 |
}
|
|
81 |
||
82 |
void 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 |
||
95 |
void MyProcess::readStdOut() { |
|
96 |
genericRead( readAllStandardOutput() ); |
|
97 |
}
|
|
98 |
||
99 |
||
100 |
void MyProcess::readTmpFile() { |
|
101 |
genericRead( temp_file.readAll() ); |
|
102 |
}
|
|
103 |
||
104 |
void 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 |
#ifdef Q_OS_WIN
|
|
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 |
||
130 |
int 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 |
/*!
|
|
155 |
Do some clean up, and be sure that all output has been read.
|
|
156 |
*/
|
|
157 |
void 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 |
||
1.2.2
by Maia Kozheva
Import upstream version 0.6.7 |
174 |
QStringList 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 |
||
1.1.8
by Maia Kozheva
Import upstream version 0.6.6 |
201 |
#include "moc_myprocess.cpp" |