Use "media" instead of "video"
[nomnom.git] / src / DownloadDiag.cpp
blob384540d6074e30672083e80b2d01d88c135fd4c8
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 #ifdef _0
19 #include <QDebug>
20 #endif
22 #include "util.h"
23 #include "Preferences.h"
24 #include "DownloadDiag.h"
26 // main.cpp
27 extern SharedPreferences shPrefs;
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, Media *media)
58 Q_ASSERT (!cmd.isEmpty ());
59 Q_ASSERT (!fpath.isEmpty ());
60 Q_ASSERT (media != NULL);
62 _lastError.clear ();
64 QStringList args = cmd.split (" ");
66 args.replaceInStrings ("%u", media->get (Media::Link).toString ());
67 args.replaceInStrings ("%f", fpath);
69 _canceled = false;
71 show ();
72 _proc.start (args.takeFirst (), args);
73 exec ();
76 void
77 DownloadDialog::onCurlStarted ()
79 setLabelText (tr ("Starting download ..."));
80 if (shPrefs.get (SharedPreferences::MinWhenStarts).toBool ())
81 parentWidget ()->showMinimized ();
84 void
85 DownloadDialog::onCurlError (QProcess::ProcessError n)
87 if (!_canceled)
89 hide ();
90 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n));
91 emit error ();
92 cancel ();
96 static const QRegExp rx_err ("curl:\\s+(.*)$");
97 static const QRegExp rx_rate ("(\\D+)"); // rate unit
99 void
100 DownloadDialog::onCurlReadyRead ()
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)
111 _lastError = "curl: " +rx_err.cap (1);
112 continue;
115 QStringList lst = ln.split (" ");
117 if (lst.count () < 12)
118 continue; // Full line updates only.
120 #ifdef _0
121 qDebug () << lst;
122 #endif
124 enum
126 PERCENT = 0,
127 ETA = 10,
128 RATE = 11
131 setValue (lst[PERCENT].toInt ());
133 QString rate = lst[RATE];
135 if (rx_rate.indexIn (rate) == -1)
136 rate = QString ("%1k").arg (rate.toLongLong ()/1024.0,2,'f',1);
138 const QString s = tr("Copying at %1, %2")
139 .arg (rate)
140 .arg (lst[ETA]);
142 setLabelText (s);
147 void
148 DownloadDialog::onCurlFinished (int exitCode, QProcess::ExitStatus exitStatus)
150 if (exitStatus == QProcess::NormalExit && exitCode == 0)
152 else
154 if (!_canceled)
156 hide ();
157 NomNom::crit (parentWidget (), _lastError);
158 emit error ();
162 cancel ();
165 void
166 DownloadDialog::onCanceled ()
168 _canceled = true;
170 if (_proc.state () == QProcess::Running)
171 _proc.kill ();
174 // vim: set ts=2 sw=2 tw=72 expandtab: