3#include <QContextMenuEvent>
9#include <QLoggingCategory>
10#include <QDesktopServices>
14#include "FrmDownload.h"
16#include "RabbitCommonTools.h"
17#include "ui_FrmDownload.h"
19static Q_LOGGING_CATEGORY(log,
"WebBrowser.Download")
23 , m_pDownload(downalod)
26 qDebug(log) << Q_FUNC_INFO;
28 ui->pbButton->setText(QString());
29 ui->lbFileInfo->hide();
31 setContextMenuPolicy(Qt::CustomContextMenu);
32 check = connect(
this, &QFrame::customContextMenuRequested,
33 this, &CFrmDownload::slotCustomContextMenuRequested);
37#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
38 ui->lbTitle->setText(m_pDownload->downloadFileName());
40 QFileInfo fi(m_pDownload->path());
41 ui->lbTitle->setText(fi.fileName());
43 ui->progressBar->setValue(0);
45#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
46 check = connect(m_pDownload, &QWebEngineDownloadRequest::totalBytesChanged,
this, &CFrmDownload::slotUpdateWidget);
48 check = connect(m_pDownload, &QWebEngineDownloadRequest::receivedBytesChanged,
this, &CFrmDownload::slotUpdateWidget);
51 check = connect(m_pDownload, &QWebEngineDownloadRequest::downloadProgress,
this, &CFrmDownload::slotUpdateWidget);
54 check = connect(m_pDownload, &QWebEngineDownloadRequest::stateChanged,
55 this, &CFrmDownload::slotUpdateWidget);
63CFrmDownload::~CFrmDownload()
65 qDebug(log) << Q_FUNC_INFO;
69void CFrmDownload::on_pbButton_clicked()
71 switch(m_pDownload->state()) {
72 case QWebEngineDownloadRequest::DownloadRequested:
73 case QWebEngineDownloadRequest::DownloadInProgress: {
74 m_pDownload->cancel();
77 case QWebEngineDownloadRequest::DownloadCancelled:
78 case QWebEngineDownloadRequest::DownloadInterrupted: {
79 m_pDownload->resume();
80 ui->lbFileInfo->hide();
83 case QWebEngineDownloadRequest::DownloadCompleted: {
84 RabbitCommon::CTools::LocateFileWithExplorer(
85 QDir(m_pDownload->downloadDirectory()).absoluteFilePath(m_pDownload->downloadFileName()));
89 emit sigRemoveClicked(
this);
94void CFrmDownload::slotUpdateWidget()
96 if(!m_pDownload)
return;
97 qreal totalBytes = m_pDownload->totalBytes();
98 qreal receivedBytes = m_pDownload->receivedBytes();
99 qreal bytesPerSecond = 0;
103 if (m_timeAdded.elapsed() != 0)
104 bytesPerSecond = receivedBytes / m_timeAdded.elapsed() * 1000;
106 remaining = (totalBytes - receivedBytes) / bytesPerSecond + 1;
109 szRemaining = tm.addSecs(remaining).toString(
"hh:mm:ss");
110 qDebug(log) <<
"Remaining:" << remaining << tm << szRemaining;
113 auto state = m_pDownload->state();
115 case QWebEngineDownloadRequest::DownloadRequested:
116 ui->progressBar->setDisabled(
false);
117 m_pDownload->accept();
119 case QWebEngineDownloadRequest::DownloadInProgress:
120 if (totalBytes > 0) {
121 ui->progressBar->setValue(qRound(100 * receivedBytes / totalBytes));
122 ui->progressBar->setDisabled(
false);
123 ui->progressBar->setFormat(
124 tr(
"%p% - %1 of %2 downloaded - %3/s - time left: %4")
125 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(totalBytes),
126 CStats::Convertbytes(bytesPerSecond), szRemaining));
128 ui->progressBar->setValue(0);
129 ui->progressBar->setDisabled(
false);
130 ui->progressBar->setFormat(
131 tr(
"unknown size - %1 downloaded - %2/s")
132 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
135 case QWebEngineDownloadRequest::DownloadCompleted:
136 ui->progressBar->setValue(100);
137 ui->progressBar->setDisabled(
true);
138 ui->progressBar->setFormat(
139 tr(
"completed - %1 downloaded - %2/s")
140 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
142 case QWebEngineDownloadRequest::DownloadCancelled:
143 ui->progressBar->setValue(0);
144 ui->progressBar->setDisabled(
true);
145 ui->progressBar->setFormat(
146 tr(
"cancelled - %1 downloaded - %2/s")
147 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
149 case QWebEngineDownloadRequest::DownloadInterrupted:
150 ui->progressBar->setValue(0);
151 ui->progressBar->setDisabled(
true);
152 ui->progressBar->setFormat(
153 tr(
"interrupted: %1")
154 .arg(m_pDownload->interruptReasonString()));
159 case QWebEngineDownloadRequest::DownloadRequested:
160 case QWebEngineDownloadRequest::DownloadInProgress: {
161 static QIcon cancelIcon(QIcon::fromTheme(
"media-playback-stop"));
162 ui->pbButton->setIcon(cancelIcon);
163 ui->pbButton->setToolTip(tr(
"Stop downloading"));
166 case QWebEngineDownloadRequest::DownloadCancelled:
167 case QWebEngineDownloadRequest::DownloadInterrupted: {
168 static QIcon cancelIcon(QIcon::fromTheme(
"view-refresh"));
169 ui->pbButton->setIcon(cancelIcon);
170 ui->pbButton->setToolTip(tr(
"Resumes downloading"));
171 ui->lbFileInfo->setText(m_pDownload->interruptReasonString());
172 ui->lbFileInfo->show();
175 case QWebEngineDownloadRequest::DownloadCompleted: {
176 static QIcon cancelIcon(QIcon::fromTheme(
"folder-open"));
177 ui->pbButton->setIcon(cancelIcon);
178 ui->pbButton->setToolTip(tr(
"Show in folder"));
179 ui->progressBar->hide();
180 ui->lbFileInfo->setText(tr(
"Completed") +
" - "
181 + CStats::Convertbytes(m_pDownload->totalBytes()));
182 ui->lbFileInfo->show();
184#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
185 QDir d(m_pDownload->downloadDirectory());
186 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
188 szFile = m_pDownload->path();
190 m_FileWatcher.addPath(szFile);
191 connect(&m_FileWatcher, &QFileSystemWatcher::fileChanged,
192 [&](
const QString &path) {
195 ui->lbFileInfo->setText(tr(
"The file has been deleted."));
196 ui->lbFileInfo->show();
197 ui->pbButton->hide();
205void CFrmDownload::slotCustomContextMenuRequested(
const QPoint &pos)
207 qDebug(log) << Q_FUNC_INFO;
209#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
210 QDir d(m_pDownload->downloadDirectory());
211 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
213 szFile = m_pDownload->path();
215 QFile downloadFile(szFile);
217 if(downloadFile.exists()) {
218 menu.addAction(QIcon::fromTheme(
"folder-open"), tr(
"Show in folder"),
219 this, [
this, szFile](){
220 if(!m_pDownload)
return;
221 RabbitCommon::CTools::LocateFileWithExplorer(
222 QDir(m_pDownload->downloadDirectory()).filePath(m_pDownload->downloadFileName()));
224 menu.addAction(QIcon::fromTheme(
"file-open"), tr(
"Open the file with the associated program"),
225 this, [
this, szFile](){
226 if(!m_pDownload)
return;
227 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
230 menu.addAction(QIcon::fromTheme(
"edit-copy"), tr(
"Copy url to clipboard"),
232 if(m_pDownload && QApplication::clipboard()) {
233 QApplication::clipboard()->setText(m_pDownload->url().toString());
236 menu.addAction(QIcon::fromTheme(
"list-remove"), tr(
"Remove from list"),
238 emit sigRemoveClicked(
this);
240 if(downloadFile.exists()) {
241 menu.addAction(QIcon::fromTheme(
"edit-delete"), tr(
"Delete"),
243#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
244 QDir d(m_pDownload->downloadDirectory());
245 if(!d.remove(m_pDownload->downloadFileName()))
246 qCritical(log) <<
"Remove file fail." << d.absoluteFilePath(m_pDownload->downloadFileName());
248 QFile f(m_pDownload->path());
250 qCritical(log) <<
"Remove file fail." << f.fileName();
252 emit sigRemoveClicked(
this);
256 QWidget* pW = qobject_cast<QWidget*>(sender());
258 p = pW->mapToGlobal(pos);
262void CFrmDownload::mouseDoubleClickEvent(QMouseEvent *event)
264 qDebug(log) << Q_FUNC_INFO;
265 if(!m_pDownload)
return;
267#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
268 QDir d(m_pDownload->downloadDirectory());
269 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
271 szFile = m_pDownload->path();
273 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
276void CFrmDownload::mouseReleaseEvent(QMouseEvent *event)
278 qDebug(log) << Q_FUNC_INFO;
279 emit sigSelected(
this);