2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-26 17:59:08 +01:00
|
|
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "downloaditem.h"
|
|
|
|
#include "ui_downloaditem.h"
|
2011-04-08 18:52:14 +02:00
|
|
|
#include "mainapplication.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2011-04-08 18:52:14 +02:00
|
|
|
#include "tabwidget.h"
|
|
|
|
#include "webpage.h"
|
2011-05-25 14:26:36 +02:00
|
|
|
#include "downloadmanager.h"
|
2011-12-29 22:28:04 +01:00
|
|
|
#include "networkmanager.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2015-05-24 19:22:32 +02:00
|
|
|
#include "datapaths.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QListWidgetItem>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMessageBox>
|
2015-05-24 19:22:32 +02:00
|
|
|
#include <QFileIconProvider>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QDesktopServices>
|
2015-05-24 19:22:32 +02:00
|
|
|
#include <QWebEngineDownloadItem>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2013-01-29 19:55:09 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include "Shlwapi.h"
|
2017-10-03 09:33:35 +02:00
|
|
|
#include "shellapi.h"
|
2013-01-29 19:55:09 +01:00
|
|
|
#endif
|
|
|
|
|
2011-03-29 19:55:21 +02:00
|
|
|
//#define DOWNMANAGER_DEBUG
|
|
|
|
|
2015-10-04 20:59:29 +02:00
|
|
|
DownloadItem::DownloadItem(QListWidgetItem *item, QWebEngineDownloadItem* downloadItem, const QString &path, const QString &fileName, bool openFile, DownloadManager* manager)
|
2011-09-30 19:22:50 +02:00
|
|
|
: QWidget()
|
|
|
|
, ui(new Ui::DownloadItem)
|
|
|
|
, m_item(item)
|
2015-05-24 19:22:32 +02:00
|
|
|
, m_download(downloadItem)
|
2011-09-30 19:22:50 +02:00
|
|
|
, m_path(path)
|
|
|
|
, m_fileName(fileName)
|
2015-05-24 19:22:32 +02:00
|
|
|
, m_downUrl(downloadItem->url())
|
2015-10-04 20:59:29 +02:00
|
|
|
, m_openFile(openFile)
|
2011-09-30 19:22:50 +02:00
|
|
|
, m_downloading(false)
|
2011-11-04 22:34:45 +01:00
|
|
|
, m_downloadStopped(false)
|
2015-11-12 17:03:45 +01:00
|
|
|
, m_currSpeed(0)
|
2015-05-24 19:22:32 +02:00
|
|
|
, m_received(downloadItem->receivedBytes())
|
|
|
|
, m_total(downloadItem->totalBytes())
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-03-29 19:55:21 +02:00
|
|
|
#ifdef DOWNMANAGER_DEBUG
|
|
|
|
qDebug() << __FUNCTION__ << item << reply << path << fileName;
|
|
|
|
#endif
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
ui->setupUi(this);
|
2011-05-25 14:26:36 +02:00
|
|
|
setMaximumWidth(525);
|
2011-09-30 21:44:18 +02:00
|
|
|
|
2014-03-24 16:08:33 +01:00
|
|
|
ui->button->setPixmap(QIcon::fromTheme(QSL("process-stop")).pixmap(20, 20));
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->fileName->setText(m_fileName);
|
|
|
|
ui->downloadInfo->setText(tr("Remaining time unavailable"));
|
|
|
|
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
|
2011-12-29 22:28:04 +01:00
|
|
|
connect(ui->button, SIGNAL(clicked(QPoint)), this, SLOT(stop()));
|
|
|
|
connect(manager, SIGNAL(resized(QSize)), this, SLOT(parentResized(QSize)));
|
|
|
|
|
|
|
|
startDownloading();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadItem::startDownloading()
|
|
|
|
{
|
2015-05-24 19:22:32 +02:00
|
|
|
connect(m_download, &QWebEngineDownloadItem::finished, this, &DownloadItem::finished);
|
|
|
|
connect(m_download, &QWebEngineDownloadItem::downloadProgress, this, &DownloadItem::downloadProgress);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
m_downloading = true;
|
2015-05-24 19:22:32 +02:00
|
|
|
m_downTimer.start();
|
2013-01-28 10:52:55 +01:00
|
|
|
|
2017-01-20 12:03:27 +01:00
|
|
|
updateDownloadInfo(0, m_download->receivedBytes(), m_download->totalBytes());
|
2015-05-24 19:22:32 +02:00
|
|
|
|
2017-01-20 12:03:27 +01:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
// QFileIconProvider uses only suffix on Linux
|
2015-05-24 19:22:32 +02:00
|
|
|
QFileIconProvider iconProvider;
|
2017-01-20 12:03:27 +01:00
|
|
|
QIcon fileIcon = iconProvider.icon(QFileInfo(m_fileName));
|
2015-05-24 19:22:32 +02:00
|
|
|
if (!fileIcon.isNull()) {
|
|
|
|
ui->fileIcon->setPixmap(fileIcon.pixmap(30));
|
2017-01-20 12:03:27 +01:00
|
|
|
} else {
|
2015-05-24 19:22:32 +02:00
|
|
|
ui->fileIcon->setPixmap(style()->standardIcon(QStyle::SP_FileIcon).pixmap(30));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2017-01-20 12:03:27 +01:00
|
|
|
#else
|
|
|
|
ui->fileIcon->hide();
|
|
|
|
#endif
|
2011-05-25 14:26:36 +02:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:22:32 +02:00
|
|
|
void DownloadItem::parentResized(const QSize &size)
|
2011-03-18 23:04:02 +01:00
|
|
|
{
|
2015-05-24 19:22:32 +02:00
|
|
|
if (size.width() < 200) {
|
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2015-05-24 19:22:32 +02:00
|
|
|
setMaximumWidth(size.width());
|
2011-03-18 23:04:02 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void DownloadItem::finished()
|
|
|
|
{
|
2011-03-29 19:55:21 +02:00
|
|
|
#ifdef DOWNMANAGER_DEBUG
|
|
|
|
qDebug() << __FUNCTION__ << m_reply;
|
|
|
|
#endif
|
2013-01-28 10:52:55 +01:00
|
|
|
|
2015-10-04 20:59:29 +02:00
|
|
|
bool success = false;
|
2015-05-24 19:22:32 +02:00
|
|
|
QString host = m_download->url().host();
|
|
|
|
|
|
|
|
switch (m_download->state()) {
|
|
|
|
case QWebEngineDownloadItem::DownloadCompleted:
|
2015-10-04 20:59:29 +02:00
|
|
|
success = true;
|
2015-05-24 19:22:32 +02:00
|
|
|
ui->downloadInfo->setText(tr("Done - %1 (%2)").arg(host, QDateTime::currentDateTime().toString(Qt::DefaultLocaleShortDate)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QWebEngineDownloadItem::DownloadInterrupted:
|
|
|
|
ui->downloadInfo->setText(tr("Error - %1").arg(host));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QWebEngineDownloadItem::DownloadCancelled:
|
|
|
|
ui->downloadInfo->setText(tr("Cancelled - %1").arg(host));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->progressBar->hide();
|
|
|
|
ui->button->hide();
|
|
|
|
ui->frame->hide();
|
|
|
|
|
|
|
|
m_item->setSizeHint(sizeHint());
|
|
|
|
m_downloading = false;
|
2011-03-24 22:31:38 +01:00
|
|
|
|
2015-10-04 20:59:29 +02:00
|
|
|
if (success && m_openFile)
|
|
|
|
openFile();
|
|
|
|
|
2011-04-24 22:40:35 +02:00
|
|
|
emit downloadFinished(true);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadItem::downloadProgress(qint64 received, qint64 total)
|
|
|
|
{
|
2011-03-29 19:55:21 +02:00
|
|
|
#ifdef DOWNMANAGER_DEBUG
|
|
|
|
qDebug() << __FUNCTION__ << received << total;
|
|
|
|
#endif
|
2011-03-02 16:57:41 +01:00
|
|
|
qint64 currentValue = 0;
|
|
|
|
qint64 totalValue = 0;
|
|
|
|
if (total > 0) {
|
|
|
|
currentValue = received * 100 / total;
|
|
|
|
totalValue = 100;
|
|
|
|
}
|
|
|
|
ui->progressBar->setValue(currentValue);
|
|
|
|
ui->progressBar->setMaximum(totalValue);
|
2015-05-24 19:22:32 +02:00
|
|
|
m_currSpeed = received * 1000.0 / m_downTimer.elapsed();
|
2011-03-02 16:57:41 +01:00
|
|
|
m_received = received;
|
|
|
|
m_total = total;
|
|
|
|
|
2015-05-24 19:22:32 +02:00
|
|
|
updateDownloadInfo(m_currSpeed, m_received, m_total);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DownloadItem::progress()
|
|
|
|
{
|
|
|
|
return ui->progressBar->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadItem::isCancelled()
|
|
|
|
{
|
|
|
|
return ui->downloadInfo->text().startsWith(tr("Cancelled"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DownloadItem::remaingTimeToString(QTime time)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (time < QTime(0, 0, 10)) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return tr("few seconds");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (time < QTime(0, 1)) {
|
2017-09-26 01:37:46 +02:00
|
|
|
//~ singular %n second
|
|
|
|
//~ plural %n seconds
|
2013-10-10 10:36:01 +02:00
|
|
|
return tr("%n seconds", "", time.second());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (time < QTime(1, 0)) {
|
2017-09-26 01:37:46 +02:00
|
|
|
//~ singular %n minute
|
|
|
|
//~ plural %n minutes
|
2013-10-10 10:36:01 +02:00
|
|
|
return tr("%n minutes", "", time.minute());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-09-26 01:37:46 +02:00
|
|
|
//~ singular %n hour
|
|
|
|
//~ plural %n hours
|
2013-10-10 10:36:01 +02:00
|
|
|
return tr("%n hours", "", time.hour());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DownloadItem::currentSpeedToString(double speed)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (speed < 0) {
|
2011-04-21 13:28:07 +02:00
|
|
|
return tr("Unknown speed");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-21 13:28:07 +02:00
|
|
|
|
|
|
|
speed /= 1024; // kB
|
2011-11-06 17:01:23 +01:00
|
|
|
if (speed < 1000) {
|
2013-09-29 13:52:45 +02:00
|
|
|
return QString::number(speed, 'f', 0) + QLatin1String(" ") + tr("kB/s");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-21 13:28:07 +02:00
|
|
|
|
|
|
|
speed /= 1024; //MB
|
2011-11-06 17:01:23 +01:00
|
|
|
if (speed < 1000) {
|
2013-09-29 13:52:45 +02:00
|
|
|
return QString::number(speed, 'f', 2) + QLatin1String(" ") + tr("MB/s");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-21 13:28:07 +02:00
|
|
|
|
|
|
|
speed /= 1024; //GB
|
2013-09-29 13:52:45 +02:00
|
|
|
return QString::number(speed, 'f', 2) + QLatin1String(" ") + tr("GB/s");
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadItem::updateDownloadInfo(double currSpeed, qint64 received, qint64 total)
|
|
|
|
{
|
2011-03-29 19:55:21 +02:00
|
|
|
#ifdef DOWNMANAGER_DEBUG
|
|
|
|
qDebug() << __FUNCTION__ << currSpeed << received << total;
|
|
|
|
#endif
|
2011-03-02 16:57:41 +01:00
|
|
|
// QString QString QString QString
|
|
|
|
// | m_remTime | |m_currSize| |m_fileSize| |m_speed|
|
|
|
|
// Remaining 26 minutes - 339MB of 693 MB (350kB/s)
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
int estimatedTime = ((total - received) / 1024) / (currSpeed / 1024);
|
2011-03-02 16:57:41 +01:00
|
|
|
QString speed = currentSpeedToString(currSpeed);
|
|
|
|
// We have QString speed now
|
|
|
|
|
2018-01-26 17:59:08 +01:00
|
|
|
QTime time(0, 0, 0);
|
2011-03-02 16:57:41 +01:00
|
|
|
time = time.addSecs(estimatedTime);
|
|
|
|
QString remTime = remaingTimeToString(time);
|
|
|
|
m_remTime = time;
|
|
|
|
|
2013-01-22 19:04:22 +01:00
|
|
|
QString currSize = QzTools::fileSizeToString(received);
|
|
|
|
QString fileSize = QzTools::fileSizeToString(total);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (fileSize == tr("Unknown size")) {
|
2011-10-02 17:39:59 +02:00
|
|
|
ui->downloadInfo->setText(tr("%2 - unknown size (%3)").arg(currSize, speed));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-09-15 21:19:47 +02:00
|
|
|
ui->downloadInfo->setText(tr("Remaining %1 - %2 of %3 (%4)").arg(remTime, currSize, fileSize, speed));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:22:32 +02:00
|
|
|
void DownloadItem::stop()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-03-29 19:55:21 +02:00
|
|
|
#ifdef DOWNMANAGER_DEBUG
|
|
|
|
qDebug() << __FUNCTION__;
|
|
|
|
#endif
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_downloadStopped) {
|
2011-11-04 22:34:45 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-11-04 22:34:45 +01:00
|
|
|
m_downloadStopped = true;
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->progressBar->hide();
|
|
|
|
ui->button->hide();
|
|
|
|
m_item->setSizeHint(sizeHint());
|
2015-05-24 19:22:32 +02:00
|
|
|
ui->downloadInfo->setText(tr("Cancelled - %1").arg(m_download->url().host()));
|
|
|
|
m_download->cancel();
|
2011-03-29 19:55:21 +02:00
|
|
|
m_downloading = false;
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-04-24 22:40:35 +02:00
|
|
|
emit downloadFinished(false);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 17:03:04 +01:00
|
|
|
void DownloadItem::mouseDoubleClickEvent(QMouseEvent* e)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
openFile();
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
|
2011-05-25 14:26:36 +02:00
|
|
|
void DownloadItem::customContextMenuRequested(const QPoint &pos)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
menu.addAction(QIcon::fromTheme("document-open"), tr("Open File"), this, SLOT(openFile()));
|
|
|
|
|
|
|
|
menu.addAction(tr("Open Folder"), this, SLOT(openFolder()));
|
|
|
|
menu.addSeparator();
|
2011-04-08 18:52:14 +02:00
|
|
|
menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy Download Link"), this, SLOT(copyDownloadLink()));
|
|
|
|
menu.addSeparator();
|
2014-03-24 16:08:33 +01:00
|
|
|
menu.addAction(QIcon::fromTheme("process-stop"), tr("Cancel downloading"), this, SLOT(stop()))->setEnabled(m_downloading);
|
2014-01-25 21:57:05 +01:00
|
|
|
menu.addAction(QIcon::fromTheme("list-remove"), tr("Remove From List"), this, SLOT(clear()))->setEnabled(!m_downloading);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_downloading || ui->downloadInfo->text().startsWith(tr("Cancelled")) || ui->downloadInfo->text().startsWith(tr("Error"))) {
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.actions().at(0)->setEnabled(false);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.exec(mapToGlobal(pos));
|
|
|
|
}
|
|
|
|
|
2011-04-08 18:52:14 +02:00
|
|
|
void DownloadItem::copyDownloadLink()
|
|
|
|
{
|
2012-01-21 23:19:38 +01:00
|
|
|
QApplication::clipboard()->setText(m_downUrl.toString());
|
2011-04-08 18:52:14 +02:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void DownloadItem::clear()
|
|
|
|
{
|
|
|
|
emit deleteItem(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadItem::openFile()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_downloading) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2015-05-24 19:22:32 +02:00
|
|
|
QFileInfo info(m_path, m_fileName);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (info.exists()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(info.absoluteFilePath()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-29 19:55:21 +02:00
|
|
|
QMessageBox::warning(m_item->listWidget()->parentWidget(), tr("Not found"), tr("Sorry, the file \n %1 \n was not found!").arg(info.absoluteFilePath()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadItem::openFolder()
|
|
|
|
{
|
2012-09-03 22:48:52 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2015-05-24 19:22:32 +02:00
|
|
|
QString winFileName = QSL("%1/%2").arg(m_path, m_fileName);
|
2018-02-09 00:05:12 +01:00
|
|
|
|
|
|
|
if (m_downloading) {
|
|
|
|
winFileName.append(QSL(".download"));
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:55:09 +01:00
|
|
|
winFileName.replace(QLatin1Char('/'), "\\");
|
|
|
|
QString shExArg = "/e,/select,\"" + winFileName + "\"";
|
|
|
|
ShellExecute(NULL, NULL, TEXT("explorer.exe"), shExArg.toStdWString().c_str(), NULL, SW_SHOW);
|
2012-08-18 02:35:54 +02:00
|
|
|
#else
|
2011-03-02 16:57:41 +01:00
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(m_path));
|
2012-08-18 02:35:54 +02:00
|
|
|
#endif
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadItem::~DownloadItem()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
delete m_item;
|
|
|
|
}
|