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()).filePath(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;
102 if (m_timeAdded.elapsed() != 0)
103 bytesPerSecond = receivedBytes / m_timeAdded.elapsed() * 1000;
105 auto state = m_pDownload->state();
107 case QWebEngineDownloadRequest::DownloadRequested:
108 ui->progressBar->setDisabled(
false);
109 m_pDownload->accept();
111 case QWebEngineDownloadRequest::DownloadInProgress:
112 if (totalBytes > 0) {
113 ui->progressBar->setValue(qRound(100 * receivedBytes / totalBytes));
114 ui->progressBar->setDisabled(
false);
115 ui->progressBar->setFormat(
116 tr(
"%p% - %1 of %2 downloaded - %3/s")
117 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(totalBytes),
118 CStats::Convertbytes(bytesPerSecond)));
120 ui->progressBar->setValue(0);
121 ui->progressBar->setDisabled(
false);
122 ui->progressBar->setFormat(
123 tr(
"unknown size - %1 downloaded - %2/s")
124 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
127 case QWebEngineDownloadRequest::DownloadCompleted:
128 ui->progressBar->setValue(100);
129 ui->progressBar->setDisabled(
true);
130 ui->progressBar->setFormat(
131 tr(
"completed - %1 downloaded - %2/s")
132 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
134 case QWebEngineDownloadRequest::DownloadCancelled:
135 ui->progressBar->setValue(0);
136 ui->progressBar->setDisabled(
true);
137 ui->progressBar->setFormat(
138 tr(
"cancelled - %1 downloaded - %2/s")
139 .arg(CStats::Convertbytes(receivedBytes), CStats::Convertbytes(bytesPerSecond)));
141 case QWebEngineDownloadRequest::DownloadInterrupted:
142 ui->progressBar->setValue(0);
143 ui->progressBar->setDisabled(
true);
144 ui->progressBar->setFormat(
145 tr(
"interrupted: %1")
146 .arg(m_pDownload->interruptReasonString()));
151 case QWebEngineDownloadRequest::DownloadRequested:
152 case QWebEngineDownloadRequest::DownloadInProgress: {
153 static QIcon cancelIcon(QIcon::fromTheme(
"media-playback-stop"));
154 ui->pbButton->setIcon(cancelIcon);
155 ui->pbButton->setToolTip(tr(
"Stop downloading"));
158 case QWebEngineDownloadRequest::DownloadCancelled:
159 case QWebEngineDownloadRequest::DownloadInterrupted: {
160 static QIcon cancelIcon(QIcon::fromTheme(
"view-refresh"));
161 ui->pbButton->setIcon(cancelIcon);
162 ui->pbButton->setToolTip(tr(
"Resumes downloading"));
163 ui->lbFileInfo->setText(m_pDownload->interruptReasonString());
164 ui->lbFileInfo->show();
167 case QWebEngineDownloadRequest::DownloadCompleted: {
168 static QIcon cancelIcon(QIcon::fromTheme(
"folder-open"));
169 ui->pbButton->setIcon(cancelIcon);
170 ui->pbButton->setToolTip(tr(
"Show in folder"));
171 ui->progressBar->hide();
172 ui->lbFileInfo->setText(tr(
"Completed") +
" - "
173 + CStats::Convertbytes(m_pDownload->totalBytes()));
174 ui->lbFileInfo->show();
176#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
177 QDir d(m_pDownload->downloadDirectory());
178 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
180 szFile = m_pDownload->path();
182 m_FileWatcher.addPath(szFile);
183 connect(&m_FileWatcher, &QFileSystemWatcher::fileChanged,
184 [&](
const QString &path) {
187 ui->lbFileInfo->setText(tr(
"The file has been deleted."));
188 ui->lbFileInfo->show();
189 ui->pbButton->hide();
197void CFrmDownload::slotCustomContextMenuRequested(
const QPoint &pos)
199 qDebug(log) << Q_FUNC_INFO;
201#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
202 QDir d(m_pDownload->downloadDirectory());
203 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
205 szFile = m_pDownload->path();
207 QFile downloadFile(szFile);
209 if(downloadFile.exists()) {
210 menu.addAction(QIcon::fromTheme(
"folder-open"), tr(
"Show in folder"),
211 this, [
this, szFile](){
212 if(!m_pDownload)
return;
213 RabbitCommon::CTools::LocateFileWithExplorer(
214 QDir(m_pDownload->downloadDirectory()).filePath(m_pDownload->downloadFileName()));
216 menu.addAction(QIcon::fromTheme(
"file-open"), tr(
"Open the file with the associated program"),
217 this, [
this, szFile](){
218 if(!m_pDownload)
return;
219 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
222 menu.addAction(QIcon::fromTheme(
"edit-copy"), tr(
"Copy url to clipboard"),
224 if(m_pDownload && QApplication::clipboard()) {
225 QApplication::clipboard()->setText(m_pDownload->url().toString());
228 menu.addAction(QIcon::fromTheme(
"list-remove"), tr(
"Remove from list"),
230 emit sigRemoveClicked(
this);
232 if(downloadFile.exists()) {
233 menu.addAction(QIcon::fromTheme(
"edit-delete"), tr(
"Delete"),
235#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
236 QDir d(m_pDownload->downloadDirectory());
237 if(!d.remove(m_pDownload->downloadFileName()))
238 qCritical(log) <<
"Remove file fail." << d.absoluteFilePath(m_pDownload->downloadFileName());
240 QFile f(m_pDownload->path());
242 qCritical(log) <<
"Remove file fail." << f.fileName();
244 emit sigRemoveClicked(
this);
248 QWidget* pW = qobject_cast<QWidget*>(sender());
250 p = pW->mapToGlobal(pos);
254void CFrmDownload::mouseDoubleClickEvent(QMouseEvent *event)
256 qDebug(log) << Q_FUNC_INFO;
257 if(!m_pDownload)
return;
259#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
260 QDir d(m_pDownload->downloadDirectory());
261 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
263 szFile = m_pDownload->path();
265 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
268void CFrmDownload::mouseReleaseEvent(QMouseEvent *event)
270 qDebug(log) << Q_FUNC_INFO;
271 emit sigSelected(
this);