* @brief Begins downloading the file at the given @a url. */
| 137 | * @brief Begins downloading the file at the given @a url. |
| 138 | */ |
| 139 | void Downloader::startDownload(const QUrl& url) |
| 140 | { |
| 141 | // Reset UI |
| 142 | m_ui->progressBar->setValue(0); |
| 143 | m_ui->stopButton->setText(tr("Stop")); |
| 144 | m_ui->downloadLabel->setText(tr("Downloading updates")); |
| 145 | m_ui->timeLabel->setText(tr("Time remaining") + ": " + tr("unknown")); |
| 146 | |
| 147 | // Configure the network request |
| 148 | QNetworkRequest request(url); |
| 149 | request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, |
| 150 | QNetworkRequest::NoLessSafeRedirectPolicy); |
| 151 | |
| 152 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) |
| 153 | request.setTransferTimeout(10000); |
| 154 | #endif |
| 155 | |
| 156 | if (!m_userAgentString.isEmpty()) |
| 157 | request.setRawHeader("User-Agent", m_userAgentString.toUtf8()); |
| 158 | |
| 159 | // Clean up previous reply if any |
| 160 | if (m_reply) { |
| 161 | m_reply->abort(); |
| 162 | m_reply->deleteLater(); |
| 163 | m_reply = nullptr; |
| 164 | } |
| 165 | |
| 166 | // Start download |
| 167 | m_reply = m_manager->get(request); |
| 168 | m_startTime = QDateTime::currentDateTime().toSecsSinceEpoch(); |
| 169 | |
| 170 | // Ensure that downloads directory exists |
| 171 | if (!m_downloadDir.exists()) |
| 172 | m_downloadDir.mkpath("."); |
| 173 | |
| 174 | // Remove old downloads |
| 175 | QFile::remove(m_downloadDir.filePath(m_fileName)); |
| 176 | QFile::remove(m_downloadDir.filePath(m_fileName + PARTIAL_DOWN)); |
| 177 | |
| 178 | // Update UI when download progress changes or download finishes |
| 179 | connect(m_reply, &QNetworkReply::metaDataChanged, this, &Downloader::metaDataChanged); |
| 180 | connect(m_reply, &QNetworkReply::downloadProgress, this, &Downloader::updateProgress); |
| 181 | connect(m_reply, &QNetworkReply::finished, this, &Downloader::finished); |
| 182 | |
| 183 | showNormal(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @brief Changes the name of the downloaded file. |