xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/simplebrowser/downloadwidget.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "downloadwidget.h"
52 
53 #include <QFileInfo>
54 #include <QUrl>
55 #include <QWebEngineDownloadItem>
56 
DownloadWidget(QWebEngineDownloadItem * download,QWidget * parent)57 DownloadWidget::DownloadWidget(QWebEngineDownloadItem *download, QWidget *parent)
58     : QFrame(parent)
59     , m_download(download)
60     , m_timeAdded(QTime::currentTime())
61 {
62     setupUi(this);
63     m_dstName->setText(QFileInfo(m_download->path()).fileName());
64     m_srcUrl->setText(m_download->url().toDisplayString());
65 
66     connect(m_cancelButton, &QPushButton::clicked,
67             [this](bool) {
68         if (m_download->state() == QWebEngineDownloadItem::DownloadInProgress)
69             m_download->cancel();
70         else
71             emit removeClicked(this);
72     });
73 
74     connect(m_download, &QWebEngineDownloadItem::downloadProgress,
75             this, &DownloadWidget::updateWidget);
76 
77     connect(m_download, &QWebEngineDownloadItem::stateChanged,
78             this, &DownloadWidget::updateWidget);
79 
80     updateWidget();
81 }
82 
withUnit(qreal bytes)83 inline QString DownloadWidget::withUnit(qreal bytes)
84 {
85     if (bytes < (1 << 10))
86         return tr("%L1 B").arg(bytes);
87     else if (bytes < (1 << 20))
88         return tr("%L1 KiB").arg(bytes / (1 << 10), 0, 'f', 2);
89     else if (bytes < (1 << 30))
90         return tr("%L1 MiB").arg(bytes / (1 << 20), 0, 'f', 2);
91     else
92         return tr("%L1 GiB").arg(bytes / (1 << 30), 0, 'f', 2);
93 }
94 
updateWidget()95 void DownloadWidget::updateWidget()
96 {
97     qreal totalBytes = m_download->totalBytes();
98     qreal receivedBytes = m_download->receivedBytes();
99     qreal bytesPerSecond = receivedBytes / m_timeAdded.elapsed() * 1000;
100 
101     auto state = m_download->state();
102     switch (state) {
103     case QWebEngineDownloadItem::DownloadRequested:
104         Q_UNREACHABLE();
105         break;
106     case QWebEngineDownloadItem::DownloadInProgress:
107         if (totalBytes >= 0) {
108             m_progressBar->setValue(qRound(100 * receivedBytes / totalBytes));
109             m_progressBar->setDisabled(false);
110             m_progressBar->setFormat(
111                 tr("%p% - %1 of %2 downloaded - %3/s")
112                 .arg(withUnit(receivedBytes))
113                 .arg(withUnit(totalBytes))
114                 .arg(withUnit(bytesPerSecond)));
115         } else {
116             m_progressBar->setValue(0);
117             m_progressBar->setDisabled(false);
118             m_progressBar->setFormat(
119                 tr("unknown size - %1 downloaded - %2/s")
120                 .arg(withUnit(receivedBytes))
121                 .arg(withUnit(bytesPerSecond)));
122         }
123         break;
124     case QWebEngineDownloadItem::DownloadCompleted:
125         m_progressBar->setValue(100);
126         m_progressBar->setDisabled(true);
127         m_progressBar->setFormat(
128             tr("completed - %1 downloaded - %2/s")
129             .arg(withUnit(receivedBytes))
130             .arg(withUnit(bytesPerSecond)));
131         break;
132     case QWebEngineDownloadItem::DownloadCancelled:
133         m_progressBar->setValue(0);
134         m_progressBar->setDisabled(true);
135         m_progressBar->setFormat(
136             tr("cancelled - %1 downloaded - %2/s")
137             .arg(withUnit(receivedBytes))
138             .arg(withUnit(bytesPerSecond)));
139         break;
140     case QWebEngineDownloadItem::DownloadInterrupted:
141         m_progressBar->setValue(0);
142         m_progressBar->setDisabled(true);
143         m_progressBar->setFormat(
144             tr("interrupted: %1")
145             .arg(m_download->interruptReasonString()));
146         break;
147     }
148 
149     if (state == QWebEngineDownloadItem::DownloadInProgress) {
150         static QIcon cancelIcon(QStringLiteral(":process-stop.png"));
151         m_cancelButton->setIcon(cancelIcon);
152         m_cancelButton->setToolTip(tr("Stop downloading"));
153     } else {
154         static QIcon removeIcon(QStringLiteral(":edit-clear.png"));
155         m_cancelButton->setIcon(removeIcon);
156         m_cancelButton->setToolTip(tr("Remove from list"));
157     }
158 }
159