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
)
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
);
53 Download::start (const QString
& cmd
, const QString
& fpath
, Video
*video
) {
56 qDebug () << __PRETTY_FUNCTION__
;
59 Q_ASSERT (!cmd
.isEmpty ());
60 Q_ASSERT (!fpath
.isEmpty ());
61 Q_ASSERT (video
!= NULL
);
65 QStringList args
= cmd
.split (" ");
67 args
.replaceInStrings ("%u", video
->get (Video::Link
).toString ());
68 args
.replaceInStrings ("%f", fpath
);
70 log
<< args
.join (" ") + "\n";
72 _proc
.setProcessChannelMode (QProcess::MergedChannels
);
75 _proc
.start (args
.takeFirst (), args
);
80 Download::onCurlStarted ()
81 { setLabelText (tr ("Starting download ...")); }
84 Download::onCurlError (QProcess::ProcessError n
) {
86 qDebug () << __PRETTY_FUNCTION__
;
88 emit
error (NomNom::to_process_errmsg (n
));
92 static const QRegExp
rx_prog ("^(\\d+).*(\\d+:\\d+:\\d+|\\d+d \\d+h)\\s+(\\w+)$");
93 static const QRegExp
rx_err ("curl:\\s+(.*)$");
94 static const QRegExp
rx_rate ("(\\D+)"); // rate unit
97 Download::onCurlReadyRead () {
100 qDebug () << __PRETTY_FUNCTION__
;
103 static char data
[1024];
105 while (_proc
.readLine (data
, sizeof (data
))) {
107 QString ln
= QString::fromLocal8Bit (data
).simplified ();
109 if (rx_err
.indexIn (ln
) != -1) {
110 _lastError
= "curl: " +rx_err
.cap (1);
114 if (ln
.split (" ").count () < 12)
115 continue; // Full line updates only, PLZKTHXBYE.
122 if (rx_prog
.indexIn (ln
) != -1) {
132 << rx_prog
.cap (PERCENT
)
134 << rx_prog
.cap (RATE
);
139 setValue (rx_prog
.cap (PERCENT
).toInt ());
141 QString rate
= rx_prog
.cap (RATE
);
143 if (rx_rate
.indexIn (rate
) == -1) {
144 rate
= QString ("%1k").arg (rate
.toLongLong ()/1024.0,2,'f',1);
147 const QString s
= tr("Copying at %1, %2")
149 .arg (rx_prog
.cap (ETA
))
163 Download::onCurlFinished (int exitCode
, QProcess::ExitStatus es
) {
166 qDebug () << __PRETTY_FUNCTION__
;
171 case QProcess::NormalExit
:
175 default: emit
error (_lastError
); break;
179 } // Switch exitCode.
181 break; // NormalExit.
185 } // Switch ExitStatus.
191 Download::onCanceled () {
194 qDebug () << __PRETTY_FUNCTION__
;
197 if (_proc
.state () == QProcess::Running
)