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();
82 case QWebEngineDownloadRequest::DownloadCompleted: {
83 RabbitCommon::CTools::LocateFileWithExplorer(
84 QDir(m_pDownload->downloadDirectory()).filePath(m_pDownload->downloadFileName()));
88 emit sigRemoveClicked(
this);
93void CFrmDownload::slotUpdateWidget()
95 if(!m_pDownload)
return;
96 qreal totalBytes = m_pDownload->totalBytes();
97 qreal receivedBytes = m_pDownload->receivedBytes();
98 qreal bytesPerSecond = 0;
101 if (m_timeAdded.elapsed() != 0)
102 bytesPerSecond = receivedBytes / m_timeAdded.elapsed() * 1000;
104 auto state = m_pDownload->state();
106 case QWebEngineDownloadRequest::DownloadRequested:
107 m_pDownload->accept();
109 case QWebEngineDownloadRequest::DownloadInProgress:
110 if (totalBytes > 0) {
111 ui->progressBar->setValue(qRound(100 * receivedBytes / totalBytes));
112 ui->progressBar->setDisabled(
false);
113 ui->progressBar->setFormat(
114 tr(
"%p% - %1 of %2 downloaded - %3/s")
115 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(totalBytes),
116 CStats::Convertbytes(bytesPerSecond)));
118 ui->progressBar->setValue(0);
119 ui->progressBar->setDisabled(
false);
120 ui->progressBar->setFormat(
121 tr(
"unknown size - %1 downloaded - %2/s")
122 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
125 case QWebEngineDownloadRequest::DownloadCompleted:
126 ui->progressBar->setValue(100);
127 ui->progressBar->setDisabled(
true);
128 ui->progressBar->setFormat(
129 tr(
"completed - %1 downloaded - %2/s")
130 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
132 case QWebEngineDownloadRequest::DownloadCancelled:
133 ui->progressBar->setValue(0);
134 ui->progressBar->setDisabled(
true);
135 ui->progressBar->setFormat(
136 tr(
"cancelled - %1 downloaded - %2/s")
137 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
139 case QWebEngineDownloadRequest::DownloadInterrupted:
140 ui->progressBar->setValue(0);
141 ui->progressBar->setDisabled(
true);
142 ui->progressBar->setFormat(
143 tr(
"interrupted: %1")
144 .arg(m_pDownload->interruptReasonString()));
149 case QWebEngineDownloadRequest::DownloadRequested:
150 case QWebEngineDownloadRequest::DownloadInProgress: {
151 static QIcon cancelIcon(QIcon::fromTheme(
"media-playback-stop"));
152 ui->pbButton->setIcon(cancelIcon);
153 ui->pbButton->setToolTip(tr(
"Stop downloading"));
156 case QWebEngineDownloadRequest::DownloadCancelled:
157 case QWebEngineDownloadRequest::DownloadInterrupted: {
158 static QIcon cancelIcon(QIcon::fromTheme(
"view-refresh"));
159 ui->pbButton->setIcon(cancelIcon);
160 ui->pbButton->setToolTip(tr(
"Resumes downloading"));
163 case QWebEngineDownloadRequest::DownloadCompleted: {
164 static QIcon cancelIcon(QIcon::fromTheme(
"folder-open"));
165 ui->pbButton->setIcon(cancelIcon);
166 ui->pbButton->setToolTip(tr(
"Show in folder"));
167 ui->progressBar->hide();
168 ui->lbFileInfo->setText(tr(
"Completed") +
" - "
169 + CStats::Convertbytes(m_pDownload->totalBytes()));
170 ui->lbFileInfo->show();
172#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
173 QDir d(m_pDownload->downloadDirectory());
174 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
176 szFile = m_pDownload->path();
178 m_FileWatcher.addPath(szFile);
179 connect(&m_FileWatcher, &QFileSystemWatcher::fileChanged,
180 [&](
const QString &path) {
183 ui->lbFileInfo->setText(tr(
"The file has been deleted."));
184 ui->lbFileInfo->show();
185 ui->pbButton->hide();
193void CFrmDownload::slotCustomContextMenuRequested(
const QPoint &pos)
195 qDebug(log) << Q_FUNC_INFO;
197#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
198 QDir d(m_pDownload->downloadDirectory());
199 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
201 szFile = m_pDownload->path();
203 QFile downloadFile(szFile);
205 if(downloadFile.exists()) {
206 menu.addAction(QIcon::fromTheme(
"folder-open"), tr(
"Show in folder"),
207 this, [
this, szFile](){
208 if(!m_pDownload)
return;
209 RabbitCommon::CTools::LocateFileWithExplorer(
210 QDir(m_pDownload->downloadDirectory()).filePath(m_pDownload->downloadFileName()));
212 menu.addAction(QIcon::fromTheme(
"file-open"), tr(
"Open the file with the associated program"),
213 this, [
this, szFile](){
214 if(!m_pDownload)
return;
215 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
218 menu.addAction(QIcon::fromTheme(
"edit-copy"), tr(
"Copy url to clipboard"),
220 if(m_pDownload && QApplication::clipboard()) {
221 QApplication::clipboard()->setText(m_pDownload->url().toString());
224 menu.addAction(QIcon::fromTheme(
"list-remove"), tr(
"Remove from list"),
226 emit sigRemoveClicked(
this);
228 if(downloadFile.exists()) {
229 menu.addAction(QIcon::fromTheme(
"edit-delete"), tr(
"Delete"),
231#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
232 QDir d(m_pDownload->downloadDirectory());
233 if(!d.remove(m_pDownload->downloadFileName()))
234 qCritical(log) <<
"Remove file fail." << d.absoluteFilePath(m_pDownload->downloadFileName());
236 QFile f(m_pDownload->path());
238 qCritical(log) <<
"Remove file fail." << f.fileName();
240 emit sigRemoveClicked(
this);
244 QWidget* pW = qobject_cast<QWidget*>(sender());
246 p = pW->mapToGlobal(pos);
250void CFrmDownload::mouseDoubleClickEvent(QMouseEvent *event)
252 qDebug(log) << Q_FUNC_INFO;
253 if(!m_pDownload)
return;
255#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
256 QDir d(m_pDownload->downloadDirectory());
257 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
259 szFile = m_pDownload->path();
261 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
264void CFrmDownload::mouseReleaseEvent(QMouseEvent *event)
266 qDebug(log) << Q_FUNC_INFO;
267 emit sigSelected(
this);