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/>.
21 #include <QCoreApplication>
27 #include "DownloadDiag.h"
29 DownloadDialog::DownloadDialog(QWidget
*parent
/*=NULL*/)
30 : QProgressDialog(parent
), _canceled(false), _errcode(-1)
32 _proc
.setProcessChannelMode(QProcess::MergedChannels
);
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
));
43 connect(this, SIGNAL(canceled()), this, SLOT(onCanceled()));
45 setWindowModality(Qt::WindowModal
);
49 bool DownloadDialog::canceled() const
54 bool DownloadDialog::failed() const
56 return !_errmsg
.isEmpty();
59 QString
DownloadDialog::errmsg() const
64 int DownloadDialog::errcode() const
69 void DownloadDialog::start(QStringList
& args
)
72 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< "args=" << args
;
81 _proc
.setProcessEnvironment(NomNom::proc_env());
83 _proc
.start(args
.takeFirst(), args
);
87 void DownloadDialog::onCurlStarted()
89 setLabelText(tr("Starting download..."));
92 void DownloadDialog::onCurlError(QProcess::ProcessError n
)
95 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< "code=" << n
;
100 _errmsg
= _proc
.errorString();
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
;
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
;
132 d
->setValue(lst
[PERCENT
].toInt());
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
;
149 if (rx_err
.indexIn(ln
) != -1)
151 _errmsg
= rx_err
.cap(1);
155 update_label(this, ln
);
159 void DownloadDialog::onCurlFinished(int ec
, QProcess::ExitStatus es
)
161 if (es
== QProcess::NormalExit
&& ec
== 0)
171 void DownloadDialog::onCanceled()
174 if (_proc
.state() == QProcess::Running
)
178 // vim: set ts=2 sw=2 tw=72 expandtab: