2 * Copyright (C) 2010 Toni Gundogdu.
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 3 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, see <http://www.gnu.org/licenses/>.
27 Download::Download (QWidget
*parent
/*=NULL*/)
28 : QProgressDialog (parent
), _canceled (false)
32 do { connect (&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
34 _wrap (started (), onCurlStarted ());
36 _wrap (error (QProcess::ProcessError
),
37 onCurlError (QProcess::ProcessError
));
39 _wrap (readyRead (), onCurlReadyRead ());
41 _wrap (finished (int, QProcess::ExitStatus
),
42 onCurlFinished (int, QProcess::ExitStatus
));
46 connect (this, SIGNAL(canceled()), this, SLOT (onCanceled()));
48 setWindowModality (Qt::WindowModal
);
51 _proc
.setProcessChannelMode (QProcess::MergedChannels
);
55 Download::start (const QString
& cmd
, const QString
& fpath
, Video
*video
) {
57 Q_ASSERT (!cmd
.isEmpty ());
58 Q_ASSERT (!fpath
.isEmpty ());
59 Q_ASSERT (video
!= NULL
);
63 QStringList args
= cmd
.split (" ");
65 args
.replaceInStrings ("%u", video
->get (Video::Link
).toString ());
66 args
.replaceInStrings ("%f", fpath
);
68 log
<< args
.join (" ") + "\n";
73 _proc
.start (args
.takeFirst (), args
);
78 Download::onCurlStarted ()
79 { setLabelText (tr ("Starting download ...")); }
82 Download::onCurlError (QProcess::ProcessError n
) {
85 emit
error (NomNom::to_process_errmsg (n
));
90 static const QRegExp
rx_prog ("^(\\d+).*(\\d+:\\d+:\\d+|\\d+d \\d+h)\\s+(\\w+)$");
91 static const QRegExp
rx_err ("curl:\\s+(.*)$");
92 static const QRegExp
rx_rate ("(\\D+)"); // rate unit
95 Download::onCurlReadyRead () {
97 static char data
[1024];
99 while (_proc
.readLine (data
, sizeof (data
))) {
101 QString ln
= QString::fromLocal8Bit (data
).simplified ();
103 if (rx_err
.indexIn (ln
) != -1) {
104 _lastError
= "curl: " +rx_err
.cap (1);
108 if (ln
.split (" ").count () < 12)
109 continue; // Full line updates only, PLZKTHXBYE.
116 if (rx_prog
.indexIn (ln
) != -1) {
126 << rx_prog
.cap (PERCENT
)
128 << rx_prog
.cap (RATE
);
133 setValue (rx_prog
.cap (PERCENT
).toInt ());
135 QString rate
= rx_prog
.cap (RATE
);
137 if (rx_rate
.indexIn (rate
) == -1) {
138 rate
= QString ("%1k").arg (rate
.toLongLong ()/1024.0,2,'f',1);
141 const QString s
= tr("Copying at %1, %2")
143 .arg (rx_prog
.cap (ETA
))
157 Download::onCurlFinished (int exitCode
, QProcess::ExitStatus exitStatus
) {
159 if (exitStatus
== QProcess::NormalExit
&& exitCode
== 0)
163 emit
error (_lastError
);
170 Download::onCanceled () {
174 if (_proc
.state () == QProcess::Running
)