19#include "ui_downloaditem.h"
32#include <QListWidgetItem>
37#include <QFileIconProvider>
38#include <QDesktopServices>
39#include <QWebEngineDownloadRequest>
40#include <QtWebEngineWidgetsVersion>
53 , m_download(downloadItem)
55 , m_fileName(fileName)
56 , m_downUrl(downloadItem->url())
57 , m_openFile(openFile)
58 , m_downloading(false)
59 , m_downloadStopped(false)
61 , m_received(downloadItem->receivedBytes())
62 , m_total(downloadItem->totalBytes())
64#ifdef DOWNMANAGER_DEBUG
65 qDebug() << __FUNCTION__ << item << reply <<
path <<
fileName;
71 ui->cancelButton->setPixmap(QIcon::fromTheme(
QSL(
"process-stop")).pixmap(20, 20));
72 ui->pauseResumeButton->setPixmap(QIcon::fromTheme(
QSL(
"media-playback-pause")).pixmap(20, 20));
73 ui->fileName->setText(m_fileName);
74 ui->downloadInfo->setText(tr(
"Remaining time unavailable"));
76 setContextMenuPolicy(Qt::CustomContextMenu);
77 connect(
this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(customContextMenuRequested(QPoint)));
85 connect(m_download, &QWebEngineDownloadRequest::isFinishedChanged,
this, &DownloadItem::finished);
86 connect(m_download, &QWebEngineDownloadRequest::receivedBytesChanged,
this, &DownloadItem::receivedOrTotalBytesChanged);
87 connect(m_download, &QWebEngineDownloadRequest::totalBytesChanged,
this, &DownloadItem::receivedOrTotalBytesChanged);
90 if (!m_downTimer.isValid()) {
94 updateDownloadInfo(0, m_download->receivedBytes(), m_download->totalBytes());
98 QFileIconProvider iconProvider;
99 QIcon fileIcon = iconProvider.icon(QFileInfo(m_fileName));
100 if (!fileIcon.isNull()) {
101 ui->fileIcon->setPixmap(fileIcon.pixmap(30));
103 ui->fileIcon->setPixmap(style()->standardIcon(QStyle::SP_FileIcon).pixmap(30));
106 ui->fileIcon->hide();
110void DownloadItem::parentResized(
const QSize &size)
112 if (size.width() < 200) {
115 setMaximumWidth(size.width());
118void DownloadItem::finished()
120#ifdef DOWNMANAGER_DEBUG
121 qDebug() << __FUNCTION__ << m_reply;
124 bool success =
false;
125 QString host = m_download->url().host();
127 switch (m_download->state()) {
128 case QWebEngineDownloadRequest::DownloadCompleted:
130 ui->downloadInfo->setText(tr(
"Done - %1 (%2)").arg(host, QLocale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat)));
133 case QWebEngineDownloadRequest::DownloadInterrupted:
134 ui->downloadInfo->setText(tr(
"Error - %1").arg(host));
137 case QWebEngineDownloadRequest::DownloadCancelled:
138 ui->downloadInfo->setText(tr(
"Cancelled - %1").arg(host));
145 ui->progressBar->hide();
146 ui->cancelButton->hide();
147 ui->pauseResumeButton->hide();
150 m_item->setSizeHint(sizeHint());
151 m_downloading =
false;
153 if (success && m_openFile)
159void DownloadItem::receivedOrTotalBytesChanged()
161 qint64 received = m_download->receivedBytes();
162 qint64 total = m_download->totalBytes();
163#ifdef DOWNMANAGER_DEBUG
164 qDebug() << __FUNCTION__ << received << total;
166 qint64 currentValue = 0;
167 qint64 totalValue = 0;
169 currentValue = received * 100 / total;
172 ui->progressBar->setValue(currentValue);
173 ui->progressBar->setMaximum(totalValue);
174 m_currSpeed = received * 1000.0 / m_downTimer.elapsed();
175 m_received = received;
178 updateDownloadInfo(m_currSpeed, m_received, m_total);
184 return ui->progressBar->value();
189 return ui->downloadInfo->text().startsWith(tr(
"Cancelled"));
194 if (time < QTime(0, 0, 10)) {
195 return tr(
"few seconds");
197 else if (time < QTime(0, 1)) {
200 return tr(
"%n seconds",
"", time.second());
202 else if (time < QTime(1, 0)) {
205 return tr(
"%n minutes",
"", time.minute());
210 return tr(
"%n hours",
"", time.hour());
217 return tr(
"Unknown speed");
223 return tr(
"%1 kB/s").arg(
locale.toString(speed,
'f', 0));
228 return tr(
"%1 MB/s").arg(
locale.toString(speed,
'f', 2));
232 return tr(
"%1 GB/s").arg(
locale.toString(speed,
'f', 2));
235void DownloadItem::updateDownloadInfo(
double currSpeed, qint64 received, qint64 total)
237#ifdef DOWNMANAGER_DEBUG
238 qDebug() << __FUNCTION__ << currSpeed << received << total;
244 if (m_download->isPaused()) {
248 int estimatedTime = ((total - received) / 1024) / (currSpeed / 1024);
253 time = time.addSecs(estimatedTime);
260 if (fileSize == tr(
"Unknown size")) {
261 ui->downloadInfo->setText(tr(
"%2 - unknown size (%3)").arg(currSize, speed));
264 ui->downloadInfo->setText(tr(
"Remaining %1 - %2 of %3 (%4)").arg(remTime, currSize, fileSize, speed));
268void DownloadItem::stop()
270#ifdef DOWNMANAGER_DEBUG
271 qDebug() << __FUNCTION__;
273 if (m_downloadStopped) {
276 m_downloadStopped =
true;
277 ui->progressBar->hide();
278 ui->cancelButton->hide();
279 ui->pauseResumeButton->hide();
280 m_item->setSizeHint(sizeHint());
281 ui->downloadInfo->setText(tr(
"Cancelled - %1").arg(m_download->url().host()));
282 m_download->cancel();
283 m_downloading =
false;
288void DownloadItem::pauseResume()
290 if (m_download->isPaused()) {
291 m_download->resume();
292 ui->pauseResumeButton->setPixmap(QIcon::fromTheme(
QSL(
"media-playback-pause")).pixmap(20, 20));
295 ui->pauseResumeButton->setPixmap(QIcon::fromTheme(
QSL(
"media-playback-start")).pixmap(20, 20));
296 ui->downloadInfo->setText(tr(
"Paused - %1").arg(m_download->url().host()));
300void DownloadItem::mouseDoubleClickEvent(QMouseEvent* e)
306void DownloadItem::customContextMenuRequested(
const QPoint &pos)
309 menu.addAction(QIcon::fromTheme(
QSL(
"document-open")), tr(
"Open File"),
this, &DownloadItem::openFile);
311 menu.addAction(tr(
"Open Folder"),
this, &DownloadItem::openFolder);
313 menu.addAction(QIcon::fromTheme(
QSL(
"edit-copy")), tr(
"Copy Download Link"),
this, &DownloadItem::copyDownloadLink);
315 menu.addAction(QIcon::fromTheme(
QSL(
"process-stop")), tr(
"Cancel downloading"),
this, &DownloadItem::stop)->setEnabled(m_downloading);
317 if (m_download->isPaused()) {
318 menu.addAction(QIcon::fromTheme(
QSL(
"media-playback-start")), tr(
"Resume downloading"),
this, &DownloadItem::pauseResume)->setEnabled(m_downloading);
320 menu.addAction(QIcon::fromTheme(
QSL(
"media-playback-pause")), tr(
"Pause downloading"),
this, &DownloadItem::pauseResume)->setEnabled(m_downloading);
323 menu.addAction(QIcon::fromTheme(
QSL(
"list-remove")), tr(
"Remove From List"),
this, &DownloadItem::clear)->setEnabled(!m_downloading);
325 if (m_downloading || ui->downloadInfo->text().startsWith(tr(
"Cancelled")) || ui->downloadInfo->text().startsWith(tr(
"Error"))) {
326 menu.actions().at(0)->setEnabled(
false);
328 menu.exec(mapToGlobal(pos));
331void DownloadItem::copyDownloadLink()
333 QApplication::clipboard()->setText(m_downUrl.toString());
336void DownloadItem::clear()
341void DownloadItem::openFile()
346 QFileInfo info(m_path, m_fileName);
348 QDesktopServices::openUrl(QUrl::fromLocalFile(info.absoluteFilePath()));
351 QMessageBox::warning(m_item->listWidget()->parentWidget(), tr(
"Not found"), tr(
"Sorry, the file \n %1 \n was not found!").arg(info.absoluteFilePath()));
355void DownloadItem::openFolder()
358 QString winFileName =
QSL(
"%1/%2").arg(m_path, m_fileName);
361 winFileName.append(
QSL(
".download"));
364 winFileName.replace(QLatin1Char(
'/'),
QSL(
"\\"));
365 QString shExArg =
QSL(
"/e,/select,\"") + winFileName +
QSL(
"\"");
366 ShellExecute(NULL, NULL, TEXT(
"explorer.exe"), shExArg.toStdWString().c_str(), NULL, SW_SHOW);
368 QDesktopServices::openUrl(QUrl::fromLocalFile(m_path));
static QString remaingTimeToString(QTime time)
DownloadItem(QListWidgetItem *item, QWebEngineDownloadRequest *downloadItem, const QString &path, const QString &fileName, bool openFile, DownloadManager *manager)
static QString currentSpeedToString(double speed)
void downloadFinished(bool success)
void deleteItem(DownloadItem *)
void progressChanged(double currSpeed, qint64 received, qint64 total)