Rabbit Remote Control 0.1.0-bate6
Loading...
Searching...
No Matches
FrmDownload.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QContextMenuEvent>
4#include <QApplication>
5#include <QClipboard>
6#include <QMimeData>
7#include <QHBoxLayout>
8#include <QVBoxLayout>
9#include <QLoggingCategory>
10#include <QDesktopServices>
11#include <QUrl>
12#include <QFileInfo>
13#include <QDir>
14#include "FrmDownload.h"
15#include "Stats.h"
16#include "RabbitCommonTools.h"
17#include "ui_FrmDownload.h"
18
19static Q_LOGGING_CATEGORY(log, "WebBrowser.Download")
20CFrmDownload::CFrmDownload(QWebEngineDownloadRequest *downalod, QWidget *parent)
21 : QFrame(parent)
22 , ui(new Ui::CFrmDownload)
23 , m_pDownload(downalod)
24{
25 bool check = false;
26 qDebug(log) << Q_FUNC_INFO;
27 ui->setupUi(this);
28 ui->pbButton->setText(QString());
29 ui->lbFileInfo->hide();
30
31 setContextMenuPolicy(Qt::CustomContextMenu);
32 check = connect(this, &QFrame::customContextMenuRequested,
33 this, &CFrmDownload::slotCustomContextMenuRequested);
34 Q_ASSERT(check);
35
36 if(m_pDownload) {
37#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
38 ui->lbTitle->setText(m_pDownload->downloadFileName());
39#else
40 QFileInfo fi(m_pDownload->path());
41 ui->lbTitle->setText(fi.fileName());
42#endif
43 ui->progressBar->setValue(0);
44
45#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
46 check = connect(m_pDownload, &QWebEngineDownloadRequest::totalBytesChanged, this, &CFrmDownload::slotUpdateWidget);
47 Q_ASSERT(check);
48 check = connect(m_pDownload, &QWebEngineDownloadRequest::receivedBytesChanged, this, &CFrmDownload::slotUpdateWidget);
49 Q_ASSERT(check);
50#else
51 check = connect(m_pDownload, &QWebEngineDownloadRequest::downloadProgress, this, &CFrmDownload::slotUpdateWidget);
52 Q_ASSERT(check);
53#endif
54 check = connect(m_pDownload, &QWebEngineDownloadRequest::stateChanged,
55 this, &CFrmDownload::slotUpdateWidget);
56 Q_ASSERT(check);
57 }
58
59 m_timeAdded.start();
60 slotUpdateWidget();
61}
62
63CFrmDownload::~CFrmDownload()
64{
65 qDebug(log) << Q_FUNC_INFO;
66 delete ui;
67}
68
69void CFrmDownload::on_pbButton_clicked()
70{
71 switch(m_pDownload->state()) {
72 case QWebEngineDownloadRequest::DownloadRequested:
73 case QWebEngineDownloadRequest::DownloadInProgress: {
74 m_pDownload->cancel();
75 break;
76 }
77 case QWebEngineDownloadRequest::DownloadCancelled:
78 case QWebEngineDownloadRequest::DownloadInterrupted: {
79 m_pDownload->resume();
80 ui->lbFileInfo->hide();
81 break;
82 }
83 case QWebEngineDownloadRequest::DownloadCompleted: {
84 RabbitCommon::CTools::LocateFileWithExplorer(
85 QDir(m_pDownload->downloadDirectory()).absoluteFilePath(m_pDownload->downloadFileName()));
86 break;
87 }
88 default: {
89 emit sigRemoveClicked(this);
90 }
91 }
92}
93
94void CFrmDownload::slotUpdateWidget()
95{
96 if(!m_pDownload) return;
97 qreal totalBytes = m_pDownload->totalBytes();
98 qreal receivedBytes = m_pDownload->receivedBytes();
99 qreal bytesPerSecond = 0; // Initialized to 0 for a reasonable default value
100 int remaining = 0;
101 QString szRemaining;
102 // Check for division by zero
103 if (m_timeAdded.elapsed() != 0)
104 bytesPerSecond = receivedBytes / m_timeAdded.elapsed() * 1000;
105
106 remaining = (totalBytes - receivedBytes) / bytesPerSecond + 1;
107 if(remaining >= 0) {
108 QTime tm(0, 0, 0);
109 szRemaining = tm.addSecs(remaining).toString("hh:mm:ss");
110 qDebug(log) << "Remaining:" << remaining << tm << szRemaining;
111 }
112
113 auto state = m_pDownload->state();
114 switch (state) {
115 case QWebEngineDownloadRequest::DownloadRequested:
116 ui->progressBar->setDisabled(false);
117 m_pDownload->accept();
118 break;
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));
127 } else {
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)));
133 }
134 break;
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)));
141 break;
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)));
148 break;
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()));
155 break;
156 }
157
158 switch(state) {
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"));
164 break;
165 }
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();
173 break;
174 }
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();
183 QString szFile;
184#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
185 QDir d(m_pDownload->downloadDirectory());
186 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
187#else
188 szFile = m_pDownload->path();
189#endif
190 m_FileWatcher.addPath(szFile);
191 connect(&m_FileWatcher, &QFileSystemWatcher::fileChanged,
192 [&](const QString &path) {
193 QFile f(path);
194 if(!f.exists()) {
195 ui->lbFileInfo->setText(tr("The file has been deleted."));
196 ui->lbFileInfo->show();
197 ui->pbButton->hide();
198 }
199 });
200 break;
201 }
202 }
203}
204
205void CFrmDownload::slotCustomContextMenuRequested(const QPoint &pos)
206{
207 qDebug(log) << Q_FUNC_INFO;
208 QString szFile;
209#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
210 QDir d(m_pDownload->downloadDirectory());
211 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
212#else
213 szFile = m_pDownload->path();
214#endif
215 QFile downloadFile(szFile);
216 QMenu menu(this);
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()));
223 });
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));
228 });
229 }
230 menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy url to clipboard"),
231 this, [&](){
232 if(m_pDownload && QApplication::clipboard()) {
233 QApplication::clipboard()->setText(m_pDownload->url().toString());
234 }
235 });
236 menu.addAction(QIcon::fromTheme("list-remove"), tr("Remove from list"),
237 this, [&](){
238 emit sigRemoveClicked(this);
239 });
240 if(downloadFile.exists()) {
241 menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"),
242 this, [&](){
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());
247#else
248 QFile f(m_pDownload->path());
249 if(!f.remove())
250 qCritical(log) << "Remove file fail." << f.fileName();
251#endif
252 emit sigRemoveClicked(this);
253 });
254 }
255 QPoint p = pos;
256 QWidget* pW = qobject_cast<QWidget*>(sender());
257 if(pW)
258 p = pW->mapToGlobal(pos);
259 menu.exec(p);
260}
261
262void CFrmDownload::mouseDoubleClickEvent(QMouseEvent *event)
263{
264 qDebug(log) << Q_FUNC_INFO;
265 if(!m_pDownload) return;
266 QString szFile;
267#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
268 QDir d(m_pDownload->downloadDirectory());
269 szFile = d.absoluteFilePath(m_pDownload->downloadFileName());
270#else
271 szFile = m_pDownload->path();
272#endif
273 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
274}
275
276void CFrmDownload::mouseReleaseEvent(QMouseEvent *event)
277{
278 qDebug(log) << Q_FUNC_INFO;
279 emit sigSelected(this);
280}