Download::start: remove qDebug
[nomnom.git] / src / Download.cpp
blob01b909da0d9a676fb41324b6db984d38573e575c
1 /*
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/>.
18 #include <QDebug>
20 #include "util.h"
21 #include "Log.h"
22 #include "Download.h"
24 // main.cpp
25 extern Log log;
27 Download::Download (QWidget *parent/*=NULL*/)
28 : QProgressDialog (parent), _canceled (false)
31 #define _wrap(s,sl) \
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));
44 #undef _wrap
46 connect (this, SIGNAL(canceled()), this, SLOT (onCanceled()));
48 setWindowModality (Qt::WindowModal);
49 setAutoClose (false);
51 _proc.setProcessChannelMode (QProcess::MergedChannels);
54 void
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);
61 _lastError.clear ();
63 QStringList args = cmd.split (" ");
65 args.replaceInStrings ("%u", video->get (Video::Link).toString ());
66 args.replaceInStrings ("%f", fpath);
68 log << args.join (" ") + "\n";
70 _canceled = false;
72 show ();
73 _proc.start (args.takeFirst (), args);
74 exec ();
77 void
78 Download::onCurlStarted ()
79 { setLabelText (tr ("Starting download ...")); }
81 void
82 Download::onCurlError (QProcess::ProcessError n) {
84 if (!_canceled) {
85 emit error (NomNom::to_process_errmsg (n));
86 cancel ();
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
94 void
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);
105 continue;
108 if (ln.split (" ").count () < 12)
109 continue; // Full line updates only, PLZKTHXBYE.
111 #ifdef _0
112 qDebug () << ln;
113 qDebug () << "--";
114 #endif
116 if (rx_prog.indexIn (ln) != -1) {
118 enum {
119 PERCENT = 1,
120 ETA,
121 RATE
124 #ifdef _0
125 qDebug ()
126 << rx_prog.cap (PERCENT)
127 << rx_prog.cap (ETA)
128 << rx_prog.cap (RATE);
129 qDebug ()
130 << ln;
131 #endif
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")
142 .arg (rate)
143 .arg (rx_prog.cap (ETA))
146 setLabelText (s);
149 else
150 log << ln;
156 void
157 Download::onCurlFinished (int exitCode, QProcess::ExitStatus exitStatus) {
159 if (exitStatus == QProcess::NormalExit && exitCode == 0)
161 else {
162 if (!_canceled)
163 emit error (_lastError);
166 cancel ();
169 void
170 Download::onCanceled () {
172 _canceled = true;
174 if (_proc.state () == QProcess::Running)
175 _proc.kill ();