Update NEWS for 0.3.1
[nomnom.git] / src / DownloadDiag.cpp
blob3cd4436c4a3c35820650fc68be46470d2407a086
1 /*
2 * NomNom
3 * Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include <QCoreApplication>
23 #ifdef ENABLE_VERBOSE
24 #include <QDebug>
25 #endif
27 #include "DownloadDiag.h"
29 DownloadDialog::DownloadDialog(QWidget *parent/*=NULL*/)
30 : QProgressDialog(parent), _canceled(false), _errcode(-1)
32 _proc.setProcessChannelMode(QProcess::MergedChannels);
34 #define _wrap(s,sl) \
35 do { connect(&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
36 _wrap(started(), onCurlStarted());
37 _wrap(error(QProcess::ProcessError), onCurlError(QProcess::ProcessError));
38 _wrap(readyRead(), onCurlReadyRead());
39 _wrap(finished(int, QProcess::ExitStatus),
40 onCurlFinished(int, QProcess::ExitStatus));
41 #undef _wrap
43 connect(this, SIGNAL(canceled()), this, SLOT(onCanceled()));
45 setWindowModality(Qt::WindowModal);
46 setAutoClose(false);
49 bool DownloadDialog::canceled() const
51 return _canceled;
54 bool DownloadDialog::failed() const
56 return !_errmsg.isEmpty();
59 QString DownloadDialog::errmsg() const
61 return _errmsg;
64 int DownloadDialog::errcode() const
66 return _errcode;
69 void DownloadDialog::start(QStringList& args)
71 #ifdef ENABLE_VERBOSE
72 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "args=" << args;
73 #endif
74 _canceled = false;
76 _errmsg.clear();
77 _args = args;
79 show();
80 #ifdef _1
81 _proc.setProcessEnvironment(NomNom::proc_env());
82 #endif
83 _proc.start(args.takeFirst(), args);
84 exec();
87 void DownloadDialog::onCurlStarted()
89 setLabelText(tr("Starting download..."));
92 void DownloadDialog::onCurlError(QProcess::ProcessError n)
94 #ifdef ENABLE_VERBOSE
95 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "code=" << n;
96 #endif
97 if (!_canceled)
99 hide();
100 _errmsg = _proc.errorString();
101 _errcode = n;
102 emit error();
104 cancel();
107 static const QRegExp rx_rate("(\\D+)"); // rate unit
109 static void update_label(QProgressDialog *d, const QString& ln)
111 QStringList lst = ln.split(" ");
112 if (lst.count() < 12)
113 return; // Full line updates only.
115 #ifdef ENABLE_VERBOSE
116 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "lst=" << lst;
117 #endif
119 enum {PERCENT=0, ETA=10, RATE=11};
121 QString rate = lst[RATE];
122 if (rx_rate.indexIn(rate) == -1)
123 rate = QString("%1k").arg(rate.toLongLong()/1024.0,2,'f',1);
125 const QString s = qApp->translate("DownloadDialog", "Copying %1/s, %2")
126 .arg(rate).arg(lst[ETA]);
128 #ifdef ENABLE_VERBOSE
129 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "s=" << s;
130 #endif
132 d->setValue(lst[PERCENT].toInt());
133 d->setLabelText(s);
136 static const QRegExp rx_err("curl:\\s+(.*)$");
138 void DownloadDialog::onCurlReadyRead()
140 static char data[1024];
141 while (_proc.readLine(data, sizeof(data)))
143 const QString ln = QString::fromLocal8Bit(data).simplified();
145 #ifdef ENABLE_VERBOSE
146 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "ln=" << ln;
147 #endif
149 if (rx_err.indexIn(ln) != -1)
151 _errmsg = rx_err.cap(1);
152 _errcode = -1;
153 break;
155 update_label(this, ln);
159 void DownloadDialog::onCurlFinished(int ec , QProcess::ExitStatus es)
161 if (es == QProcess::NormalExit && ec == 0)
162 emit finished();
163 else
165 if(!_canceled)
166 emit error();
168 cancel();
171 void DownloadDialog::onCanceled()
173 _canceled = true;
174 if (_proc.state() == QProcess::Running)
175 _proc.kill();
178 // vim: set ts=2 sw=2 tw=72 expandtab: