|
@@ -1,5 +1,6 @@
|
|
|
#include "app.h"
|
|
|
#include "filesystem.h"
|
|
|
+#include "lotromanager.h"
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
#include <QApplication>
|
|
@@ -13,6 +14,7 @@
|
|
|
DownloadManager::DownloadManager(QObject *parent)
|
|
|
: QObject(parent), downloadedCount(0), totalCount(0)
|
|
|
{
|
|
|
+ busy = false;
|
|
|
}
|
|
|
|
|
|
void DownloadManager::append(const QStringList &urlList){
|
|
@@ -24,22 +26,45 @@ void DownloadManager::append(const QStringList &urlList){
|
|
|
}
|
|
|
|
|
|
void DownloadManager::append(const QUrl &url){
|
|
|
- qDebug("%s:%i: %s%s", __FILE__, __LINE__, "Добавлен: ", url.fileName().toLocal8Bit().data());
|
|
|
- if (downloadQueue.isEmpty())
|
|
|
- QTimer::singleShot(0, this, SLOT(startNextDownload()));
|
|
|
+ qDebug("%s:%i: %s%s", __FILE__, __LINE__, "Добавлен файл для скачивания: ", url.fileName().toLocal8Bit().data());
|
|
|
|
|
|
- downloadQueue.enqueue(url);
|
|
|
- ++totalCount;
|
|
|
+ if (downloadQueue.count(url) == 0) {
|
|
|
+ downloadQueue.enqueue(url);
|
|
|
+ ++totalCount;
|
|
|
+ } else {
|
|
|
+ qDebug("%s:%i: %s %s%s", __FILE__, __LINE__, "Не добавляю закачку "
|
|
|
+ , url.fileName().toLocal8Bit().data()
|
|
|
+ , ", так как она уже находится в очереди закачек");
|
|
|
+ }
|
|
|
qDebug("%s:%i: %s%i", __FILE__, __LINE__, "Загрузок в списке: ", totalCount);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
-void DownloadManager::startNextDownload()
|
|
|
-{
|
|
|
+void DownloadManager::startDownloads() {
|
|
|
+ if (!busy)
|
|
|
+ QTimer::singleShot(0, this, SLOT(startNextDownload()));
|
|
|
+}
|
|
|
+
|
|
|
+void DownloadManager::startNextDownload() {
|
|
|
+ current_speed = "";
|
|
|
+ bytesReceivedBeforeSecond = 0;
|
|
|
+ timeElapsedBeforeSecond = 0;
|
|
|
+
|
|
|
+ if (busy) {
|
|
|
+ qWarning("%s:%i: %s", __FILE__, __LINE__, "Ошибка! Обнаружена попытка начала загрузки, когда не завершилась другая загрузка");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
App *app = &App::getInstance();
|
|
|
+ app->helper->setState("busy");
|
|
|
+ busy = true;
|
|
|
|
|
|
if (downloadQueue.isEmpty()) {
|
|
|
app->helper->setState("free");
|
|
|
qDebug("%s:%i: %s%d/%d", __FILE__, __LINE__, "Загрузка завершена. Загружено файлов: ", downloadedCount, totalCount);
|
|
|
+ busy = false;
|
|
|
emit finished();
|
|
|
return;
|
|
|
}
|
|
@@ -48,6 +73,7 @@ void DownloadManager::startNextDownload()
|
|
|
QString filename = QFileInfo(url.path()).fileName();
|
|
|
output.setFileName(QApplication::applicationDirPath() + "/data/" + filename);
|
|
|
qDebug("%s:%i: %s%s", __FILE__, __LINE__, "Начата загрузка файла: ", filename.toLocal8Bit().data());
|
|
|
+
|
|
|
|
|
|
QString hash = FileSystem::fileHash(QApplication::applicationDirPath() + "/data/" + filename, QCryptographicHash::Md5);
|
|
|
QStringList pname = output.fileName().split("/");
|
|
@@ -57,7 +83,8 @@ void DownloadManager::startNextDownload()
|
|
|
if(keyname == "loadscreens") keyname = "screens";
|
|
|
if(hash == app->config->getValue("Hashes", ptype.first()) && app->config->getValue("Editor", keyname) == "true"){
|
|
|
qDebug("%s:%i: %s%s", __FILE__, __LINE__, "Проверка хэша успешно завершена: ", filename.toLocal8Bit().data());
|
|
|
- startNextDownload();
|
|
|
+ busy = false;
|
|
|
+ QTimer::singleShot(0, this, SLOT(startNextDownload()));
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -65,38 +92,44 @@ void DownloadManager::startNextDownload()
|
|
|
QStringList parsename = filename.split("_");
|
|
|
QString name = parsename[0] + "Status";
|
|
|
download_name = parsename[0];
|
|
|
- app->writtenLabel = app->window->ui->mainbox->findChild<QLabel *>(name);
|
|
|
+
|
|
|
+
|
|
|
+ if(app->config->getValue("Editor", download_name) == "false") {
|
|
|
+ emit changePatchStatus(download_name, "Отменён");
|
|
|
+ busy = false;
|
|
|
+ QTimer::singleShot(0, this, SLOT(startNextDownload()));
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
if (!output.open(QIODevice::WriteOnly)) {
|
|
|
- qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Произошла остановка скачивания ", filename.toLocal8Bit().data());
|
|
|
- if(app->writtenLabel != nullptr) app->writtenLabel->setText("Не удалась");
|
|
|
- startNextDownload();
|
|
|
+ qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Произошла остановка скачивания (не могу открыть файл) "
|
|
|
+ , filename.toLocal8Bit().data());
|
|
|
+ emit changePatchStatus(download_name, "Ошибка записи");
|
|
|
+ busy = false;
|
|
|
+ QTimer::singleShot(0, this, SLOT(startNextDownload()));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Начинаем скачивание ", url.fileName().toLocal8Bit().data());
|
|
|
|
|
|
- app->helper->setState("busy");
|
|
|
-
|
|
|
QNetworkRequest request(url);
|
|
|
currentDownload = manager.get(request);
|
|
|
connect(this, SIGNAL(cancelDownload()), currentDownload, SLOT(abort()));
|
|
|
- connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)),
|
|
|
- SLOT(downloadProgress(qint64,qint64)));
|
|
|
- connect(currentDownload, SIGNAL(finished()),
|
|
|
- SLOT(downloadFinished()));
|
|
|
- connect(currentDownload, SIGNAL(readyRead()),
|
|
|
- SLOT(downloadReadyRead()));
|
|
|
+ connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64)));
|
|
|
+ connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished()));
|
|
|
+ connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
|
|
|
|
|
|
|
|
|
|
|
|
downloadTime.start();
|
|
|
}
|
|
|
|
|
|
-void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
-{
|
|
|
- double speed = bytesReceived * 1000.0 / downloadTime.elapsed();
|
|
|
+void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
|
|
+ App* app = &App::getInstance();
|
|
|
+
|
|
|
+ double speed = (bytesReceived - bytesReceivedBeforeSecond) * 1000.0 / (downloadTime.elapsed() - timeElapsedBeforeSecond);
|
|
|
double percent = double(std::ceil(((double)bytesReceived/ bytesTotal) * 100 * 10)) / 10.0;
|
|
|
+
|
|
|
QString unit;
|
|
|
if (speed < 1024) {
|
|
|
unit = "bytes/sec";
|
|
@@ -108,28 +141,50 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
unit = "MB/s";
|
|
|
}
|
|
|
QString speedtext = QString::fromLatin1("%1 %2").arg(speed, 3, 'f', 1).arg(unit);
|
|
|
- QString percenttext = QString::fromLatin1("%1").arg(percent, 3);
|
|
|
+ QString percenttext = QString::fromLatin1("%1").arg(percent, 3, 'f', 1);
|
|
|
|
|
|
- App *app = &App::getInstance();
|
|
|
- if(app->writtenLabel != nullptr) app->writtenLabel->setText("Загрузка ... <br/>" + percenttext+ "% (" + speedtext + ")");
|
|
|
+ if (downloadTime.elapsed() - timeElapsedBeforeSecond >= 1000) {
|
|
|
+ timeElapsedBeforeSecond = downloadTime.elapsed();
|
|
|
+ bytesReceivedBeforeSecond = bytesReceived;
|
|
|
+ current_speed = speedtext;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current_speed == "")
|
|
|
+ current_speed = speedtext;
|
|
|
+
|
|
|
+ QString elapsed_time = app->helper->countFormattedElapsedTime(bytesTotal - bytesReceived, bytesReceived * 1000.0 / downloadTime.elapsed());
|
|
|
+
|
|
|
+ emit changePatchStatus(download_name, "Загрузка ... <br/>" + percenttext + "% (" + current_speed + ")");
|
|
|
+ emit changeHint("Загрузка патча " + LotroManager::getInstance().patchTitleFromName(download_name),
|
|
|
+ "Завершено " + percenttext + "% (" + current_speed + "). Осталось примерно " + elapsed_time);
|
|
|
}
|
|
|
|
|
|
void DownloadManager::downloadFinished() {
|
|
|
+ busy = false;
|
|
|
output.close();
|
|
|
+ qDebug() << "Finished download: " << output.fileName().split('/').last().split('_')[0];
|
|
|
+
|
|
|
+ downloaded_list.append(output.fileName().split('/').last().split('_')[0]);
|
|
|
+
|
|
|
App *app = &App::getInstance();
|
|
|
app->helper->setState("free");
|
|
|
|
|
|
if (currentDownload->error()) {
|
|
|
- qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Загрузка не удалась: ", currentDownload->errorString().toLocal8Bit().data());
|
|
|
- if(app->writtenLabel != nullptr) app->writtenLabel->setText("Не удалась.");
|
|
|
- } else {
|
|
|
- if(app->writtenLabel != nullptr) app->writtenLabel->setText("Готово");
|
|
|
- qInfo("%s:%i: %s", __FILE__, __LINE__, "Все загрузки завершены. Загрузчик завершил свою работу.");
|
|
|
+ qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Загрузка не удалась: ", currentDownload->errorString().toLocal8Bit().data());
|
|
|
+ emit changePatchStatus(download_name, "Не удалась");
|
|
|
+ } else {
|
|
|
+ emit changePatchStatus(download_name, "Готово");
|
|
|
++downloadedCount;
|
|
|
}
|
|
|
|
|
|
+ if (downloadQueue.isEmpty()) {
|
|
|
+ qInfo("%s:%i: %s", __FILE__, __LINE__, "Все загрузки завершены. Загрузчик завершил свою работу.");
|
|
|
+ emit allDownloadsFinished(downloaded_list);
|
|
|
+ } else {
|
|
|
+ QTimer::singleShot(0, this, SLOT(startNextDownload()));
|
|
|
+ }
|
|
|
+
|
|
|
currentDownload->deleteLater();
|
|
|
- startNextDownload();
|
|
|
}
|
|
|
|
|
|
void DownloadManager::downloadReadyRead(){
|
|
@@ -142,8 +197,19 @@ void DownloadManager::abortDownload(QString name){
|
|
|
if(app->state != "free" && currentDownload != NULL && currentDownload->isOpen()) {
|
|
|
qDebug() << "Прерываем закачку " + download_name;
|
|
|
qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Пользователь прервал закачку файла ", download_name.toLocal8Bit().data());
|
|
|
- if(app->writtenLabel != nullptr) app->writtenLabel->setText("Не выбран");
|
|
|
+ emit changePatchStatus(name, "Отменён");
|
|
|
currentDownload->abort();
|
|
|
}
|
|
|
}
|
|
|
+ downloadQueue.removeAll(name);
|
|
|
+}
|
|
|
+
|
|
|
+int DownloadManager::getDownloadedCount() {
|
|
|
+ return downloadedCount;
|
|
|
+}
|
|
|
+
|
|
|
+void DownloadManager::resetDownloadedCount() {
|
|
|
+ totalCount = 0;
|
|
|
+ downloadedCount = 0;
|
|
|
+ downloaded_list.clear();
|
|
|
}
|