Recent: Reverse order to "most recent first"
[nomnom.git] / src / DownloadDiag.cpp
blobbe79a5a8effaa92f5142ab3cc0a86e63faff2fb4
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 "Preferences.h"
23 #include "DownloadDiag.h"
25 // main.cpp
26 extern SharedPreferences shPrefs;
27 extern Log log;
29 DownloadDialog::DownloadDialog (QWidget *parent/*=NULL*/)
30 : QProgressDialog (parent), _canceled (false)
32 #define _wrap(s,sl) \
33 do { connect (&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
35 _wrap (started (), onCurlStarted ());
37 _wrap (error (QProcess::ProcessError),
38 onCurlError (QProcess::ProcessError));
40 _wrap (readyRead (), onCurlReadyRead ());
42 _wrap (finished (int, QProcess::ExitStatus),
43 onCurlFinished (int, QProcess::ExitStatus));
45 #undef _wrap
47 connect (this, SIGNAL(canceled()), this, SLOT (onCanceled()));
49 setWindowModality (Qt::WindowModal);
50 setAutoClose (false);
52 _proc.setProcessChannelMode (QProcess::MergedChannels);
55 void
56 DownloadDialog::start (const QString& cmd, const QString& fpath, Video *video)
58 Q_ASSERT (!cmd.isEmpty ());
59 Q_ASSERT (!fpath.isEmpty ());
60 Q_ASSERT (video != NULL);
62 _lastError.clear ();
64 QStringList args = cmd.split (" ");
66 args.replaceInStrings ("%u", video->get (Video::Link).toString ());
67 args.replaceInStrings ("%f", fpath);
69 log << args.join (" ") + "\n";
71 _canceled = false;
73 show ();
74 _proc.start (args.takeFirst (), args);
75 exec ();
78 void
79 DownloadDialog::onCurlStarted ()
81 setLabelText (tr ("Starting download ..."));
82 if (shPrefs.get (SharedPreferences::MinWhenStarts).toBool ())
83 parentWidget ()->showMinimized ();
86 void
87 DownloadDialog::onCurlError (QProcess::ProcessError n)
89 if (!_canceled)
91 hide ();
92 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n));
93 emit error ();
94 cancel ();
98 static const QRegExp rx_err ("curl:\\s+(.*)$");
99 static const QRegExp rx_rate ("(\\D+)"); // rate unit
101 void
102 DownloadDialog::onCurlReadyRead ()
105 static char data[1024];
107 while (_proc.readLine (data, sizeof (data)))
109 QString ln = QString::fromLocal8Bit (data).simplified ();
111 if (rx_err.indexIn (ln) != -1)
113 _lastError = "curl: " +rx_err.cap (1);
114 continue;
117 QStringList lst = ln.split (" ");
119 if (lst.count () < 12)
120 continue; // Full line updates only.
122 #ifdef _0
123 qDebug () << lst;
124 #endif
126 enum
128 PERCENT = 0,
129 ETA = 10,
130 RATE = 11
133 setValue (lst[PERCENT].toInt ());
135 QString rate = lst[RATE];
137 if (rx_rate.indexIn (rate) == -1)
138 rate = QString ("%1k").arg (rate.toLongLong ()/1024.0,2,'f',1);
140 const QString s = tr("Copying at %1, %2")
141 .arg (rate)
142 .arg (lst[ETA]);
144 setLabelText (s);
149 void
150 DownloadDialog::onCurlFinished (int exitCode, QProcess::ExitStatus exitStatus)
152 if (exitStatus == QProcess::NormalExit && exitCode == 0)
154 else
156 if (!_canceled)
158 hide ();
159 NomNom::crit (parentWidget (), _lastError);
160 emit error ();
164 cancel ();
167 void
168 DownloadDialog::onCanceled ()
170 _canceled = true;
172 if (_proc.state () == QProcess::Running)
173 _proc.kill ();
176 // vim: set ts=2 sw=2 tw=72 expandtab: