Skip to content

Commit 47b5d0b

Browse files
committed
update level on http error
1 parent 8b88558 commit 47b5d0b

14 files changed

Lines changed: 203 additions & 50 deletions

database/tombll_manage_data.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,20 +598,22 @@ def update_tombll_zip_files_to_database(zip_files, level_id, con):
598598
current_set = set((z[0], z[3]) for z in database_zip_files) # (name, url)
599599
new_set = set((z['name'], z['url']) for z in zip_files)
600600

601-
# Find new files to add (using set difference)
602-
to_add = new_set - current_set
603-
for name, url in to_add:
604-
zip_file = next(z for z in zip_files if (z['name'], z['url']) == (name, url))
605-
tombll_create.database_zip_file(zip_file, level_id, con)
606-
607-
# Find files to remove (using set difference)
601+
# REMOVE
608602
to_remove = current_set - new_set
609603
for name, url in to_remove:
610604
for d in database_zip_files:
611-
if (d[1], d[4]) == (name, url):
612-
tombll_delete.database_zip_file(d[0], level_id, con)
605+
if (d[0], d[3]) == (name, url):
606+
list = tombll_read.database_zip_id_list(level_id, name, url, con)
607+
for item in list:
608+
tombll_delete.database_zip_file(item[0], level_id, con)
613609
break
614610

611+
# ADD
612+
to_add = new_set - current_set
613+
for name, url in to_add:
614+
zip_file = next(z for z in zip_files if (z['name'], z['url']) == (name, url))
615+
tombll_create.database_zip_file(zip_file, level_id, con)
616+
615617

616618
def update_tombll_json_to_database(data, level_id, con):
617619
"""Update level data and related attributes into the database.

database/tombll_read.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ def database_zip_list(level_id, con):
8383
return tombll_common.query_return_everything(query, (level_id, ), con)
8484

8585

86+
def database_zip_id_list(level_id, zip_name, zip_url, con):
87+
"""
88+
Get a list of ZIP file ids for data validation.
89+
90+
Args:
91+
level_id (int): The ID of the level for which to retrieve ZIP files.
92+
zip_name (str): Zip file name.
93+
zip_url (str): Download link.
94+
con (sqlite3.Connection): An active SQLite database connection.
95+
96+
Returns:
97+
None: List of all zip files.
98+
"""
99+
query = '''
100+
SELECT Zip.ZipID
101+
FROM Level
102+
JOIN ZipList ON Level.LevelID = ZipList.levelID
103+
JOIN Zip ON ZipList.zipID = Zip.ZipID
104+
WHERE Level.LevelID = ? AND Zip.url = ? AND Zip.name = ?
105+
'''
106+
return tombll_common.query_return_everything(query,
107+
(level_id, zip_url, zip_name), con)
108+
86109
def database_level_id(trle_id, con):
87110
"""
88111
Get level ID form TRLE lid.

src/Controller.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Controller::Controller() {
4343
this, &Controller::controllerTickSignal,
4444
Qt::QueuedConnection);
4545

46+
connect(&fileManager, &FileManager::fileWorkErrorSignal,
47+
this, &Controller::controllerFileError,
48+
Qt::QueuedConnection);
49+
4650
connect(&downloader, &Downloader::networkWorkTickSignal,
4751
this, &Controller::controllerTickSignal,
4852
Qt::QueuedConnection);

src/Controller.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Controller : public QObject {
5454
void controllerGenerateList(const QList<int>& availableGames);
5555
void controllerTickSignal();
5656
void controllerDownloadError(int status);
57+
void controllerFileError(int status);
5758
void controllerReloadLevelList();
5859
void controllerLoadingDone();
5960
void controllerRunningDone();

src/FileManager.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,23 @@ bool FileManager::extractZip(ZipData zipData) {
203203
qWarning() << "Failed to get file info for file" << i
204204
<< "in zip file" << zipFilename.get();
205205
mz_zip_reader_end(&zip);
206+
fileWorkErrorSignal(2);
207+
cleanWorkingDir(outputFolder);
206208
break;
207209
}
208210

209-
QString filename = QString::fromUtf8(file_stat.m_filename);
210-
if (filename.endsWith('/') == true) {
211+
QString filePathName = QString::fromUtf8(file_stat.m_filename);
212+
if (filePathName.endsWith('/') == true) {
211213
continue; // Skip directories
212214
}
213215

214-
QString outFile = QString("%1%2%3").arg(outputFolder.get(), m_sep, filename);
215-
qDebug() << "Extracting" << filename;
216+
QString outFile = QString("%1%2%3").arg(outputFolder.get(), m_sep, filePathName);
217+
qDebug() << "Extracting" << filePathName;
216218

217219
if (!QDir().mkpath(QFileInfo(outFile).path())) {
218220
qWarning() << "Failed to create directory for file" << outFile;
219221
mz_zip_reader_end(&zip);
222+
fileWorkErrorSignal(3);
220223
break;
221224
}
222225

@@ -225,9 +228,10 @@ bool FileManager::extractZip(ZipData zipData) {
225228
i,
226229
outFile.toUtf8().constData(),
227230
0)) {
228-
qWarning() << "Failed to extract file" << filename
231+
qWarning() << "Failed to extract file" << filePathName
229232
<< "from zip file" << zipFilename.get();
230233
mz_zip_reader_end(&zip);
234+
fileWorkErrorSignal(4);
231235
break;
232236
}
233237

@@ -248,15 +252,16 @@ bool FileManager::extractZip(ZipData zipData) {
248252
linkToExe(outputFolder, zipData.m_type);
249253
}
250254
status = true;
255+
qDebug() << "Unzip complete";
251256
}
252257
}
253258
}
254259
} else {
260+
fileWorkErrorSignal(1);
255261
qWarning() << "Failed to open zip file" << zipFilename.get();
256262
}
257263
// Clean up
258264
mz_zip_reader_end(&zip);
259-
qDebug() << "Unzip complete";
260265
return status;
261266
}
262267

src/FileManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class FileManager : public QObject {
206206

207207
signals:
208208
void fileWorkTickSignal();
209+
void fileWorkErrorSignal(int status);
209210

210211
private:
211212
FileManager() :

src/Network.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ void Downloader::runConnect(QFile *file, const std::string& url) {
168168
status = curl_easy_perform(curl);
169169
}
170170

171+
long httpCode = 0;
172+
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
173+
if (httpCode != 200) {
174+
m_status = 7; // http error
175+
qDebug() << "HTTP error:" << httpCode;
176+
emit this->networkWorkErrorSignal(5);
177+
}
178+
171179
if (status != CURLE_OK) {
172180
m_status = 1; // curl error
173181
qDebug() << "CURL failed:" << curl_easy_strerror(status);
@@ -183,10 +191,21 @@ void Downloader::runConnect(QFile *file, const std::string& url) {
183191
emit this->networkWorkErrorSignal(3);
184192
QCoreApplication::processEvents();
185193
}
186-
} else {
187-
m_status = 0;
188-
qDebug() << "Downloaded successfully";
189194
}
195+
196+
if (m_status == 0) {
197+
file->flush();
198+
if (file->size() == 0) {
199+
m_status = 6;
200+
qDebug() << "Error: Downloaded zip is empty (0 bytes)";
201+
emit this->networkWorkErrorSignal(4);
202+
QCoreApplication::processEvents();
203+
} else {
204+
m_status = 0;
205+
qDebug() << "Downloaded successfully, size:" << file->size();
206+
}
207+
}
208+
190209
curl_easy_cleanup(curl);
191210
}
192211
}

src/view/Levels.cpp

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "view/Levels.hpp"
2+
#include "view/Levels/Select/StackedWidgetBar.hpp"
23
#include <qapplication.h>
34
#include <qcheckbox.h>
45
#include <qlabel.h>
@@ -19,7 +20,10 @@ UiLevels::UiLevels(QWidget *parent)
1920
dialog(new Dialog(stackedWidget)),
2021
info(new Info(stackedWidget)),
2122
loading(new Loading(stackedWidget)),
22-
select(new Select(stackedWidget))
23+
select(new Select(stackedWidget)),
24+
m_listSet(false),
25+
m_wasDownloading(false),
26+
m_wasDownloadingTimes(0)
2327
{
2428
setObjectName("Levels");
2529
layout = new QGridLayout(this);
@@ -118,6 +122,9 @@ UiLevels::UiLevels(QWidget *parent)
118122
connect(&Controller::getInstance(), SIGNAL(controllerDownloadError(int)),
119123
this, SLOT(downloadError(int)));
120124

125+
connect(&Controller::getInstance(), SIGNAL(controllerFileError(int)),
126+
this, SLOT(fileError(int)));
127+
121128
// Loading done signal connections
122129
connect(&Controller::getInstance(), SIGNAL(controllerLoadingDone()),
123130
this, SLOT(updateLevelDone()));
@@ -140,28 +147,78 @@ UiLevels::UiLevels(QWidget *parent)
140147
}
141148

142149
void UiLevels::downloadError(int status) {
150+
select->downloadingState(true);
143151
select->stackedWidgetBar->progressWidgetBar->progressBar->setValue(0);
144-
select->stackedWidgetBar->navigateWidgetBar->pushButtonRun->setEnabled(true);
145-
select->stackedWidgetBar->navigateWidgetBar->pushButtonInfo->setEnabled(true);
146-
select->stackedWidgetBar->navigateWidgetBar->pushButtonDownload->setEnabled(true);
147-
select->stackedWidgetBar->setCurrentWidget(
148-
this->findChild<QWidget*>("navigateWidgetBar"));
149-
// ui->levels->select->levelViewList->setEnabled(true);
150-
// QMessageBox msgBox;
151-
// msgBox.setWindowTitle("Error");
152+
select->setCurrentWidgetBar(StackedWidgetBar::Navigate);
153+
select->levelViewList->setEnabled(true);
154+
dialog->setOptions(QStringList());
155+
152156
if (status == 1) {
153-
qDebug() << "No internet";
154-
// msgBox.setText("No internet");
157+
dialog->setMessage(QString(
158+
"No internet"
159+
));
160+
this->setStackedWidget("dialog");
155161
} else if (status == 2) {
156-
qDebug() << "You seem to be missing ssl keys";
157-
// msgBox.setText("You seem to be missing ssl keys");
162+
dialog->setMessage(QString(
163+
"The remote server's SSL certificate bad or out of date"
164+
));
165+
this->setStackedWidget("dialog");
166+
} else if (status == 3) {
167+
dialog->setMessage(QString(
168+
"Curl error"
169+
));
170+
this->setStackedWidget("dialog");
171+
} else if (status == 4) {
172+
dialog->setMessage(QString(
173+
"Downloaded zip is empty"
174+
));
175+
this->setStackedWidget("dialog");
176+
} else if (status == 5) {
177+
if (m_wasDownloadingTimes < 1) {
178+
m_wasDownloadingTimes++;
179+
m_wasDownloading = true;
180+
qint64 lid = select->getLid();
181+
controller.updateLevel(lid);
182+
loading->show();
183+
this->setStackedWidget("loading");
184+
}
158185
} else {
159-
qDebug() << "Could not connect";
160-
// msgBox.setText("Could not connect");
186+
dialog->setMessage(QString(
187+
"Could not connect or get the file"
188+
));
189+
this->setStackedWidget("dialog");
161190
}
162-
// msgBox.setStandardButtons(QMessageBox::Ok);
163-
// msgBox.setDefaultButton(QMessageBox::Ok);
164-
// msgBox.exec();
191+
}
192+
193+
void UiLevels::fileError(int status) {
194+
select->downloadingState(true);
195+
select->stackedWidgetBar->progressWidgetBar->progressBar->setValue(0);
196+
select->setCurrentWidgetBar(StackedWidgetBar::Navigate);
197+
select->levelViewList->setEnabled(true);
198+
dialog->setOptions(QStringList());
199+
200+
if (status == 1) {
201+
dialog->setMessage(QString(
202+
"Failed to open zip file for writing"
203+
));
204+
} else if (status == 2) {
205+
dialog->setMessage(QString(
206+
"Failed to get file info"
207+
));
208+
} else if (status == 3) {
209+
dialog->setMessage(QString(
210+
"Failed to create directory"
211+
));
212+
} else if (status == 4) {
213+
dialog->setMessage(QString(
214+
"Failed to extract file"
215+
));
216+
} else {
217+
dialog->setMessage(QString(
218+
"Could not handle the file"
219+
));
220+
}
221+
this->setStackedWidget("dialog");
165222
}
166223

167224
void UiLevels::backClicked() {
@@ -210,6 +267,7 @@ void UiLevels::callbackDialog(QString selected) {
210267
select->setRemovedLevel();
211268
}
212269
}
270+
213271
this->setStackedWidget("select");
214272
}
215273

@@ -295,6 +353,7 @@ void UiLevels::setSortMode(LevelListProxy::SortMode mode) {
295353
}
296354

297355
void UiLevels::setList() {
356+
m_listSet = true;
298357
QVector<QSharedPointer<ListItemData>> list;
299358
controller.getList(&list);
300359
InstalledStatus installedStatus = getInstalled();
@@ -418,18 +477,22 @@ void UiLevels::walkthroughClicked() {
418477
void UiLevels::updateLevelDone() {
419478
loading->hide();
420479
if (m_loadingDoneGoTo == "select") {
421-
setList();
422-
this->stackedWidget->setCurrentWidget(
423-
this->stackedWidget->findChild<QWidget*>("select"));
480+
if (!m_listSet) {
481+
setList();
482+
}
483+
if (m_wasDownloading) {
484+
emit downloadOrRemoveClickedSignal();
485+
m_wasDownloading = false;
486+
}
487+
setStackedWidget("select");
424488
} else if (m_loadingDoneGoTo == "info") {
425489
qint64 id = select->getLid();
426490
if (id != 0) {
427491
InfoData info = controller.getInfo(id);
428492
if (!(info.m_body == "" && info.m_imageList.size() == 0)) {
429493
infoClicked();
430494
} else {
431-
this->stackedWidget->setCurrentWidget(
432-
this->stackedWidget->findChild<QWidget*>("select"));
495+
setStackedWidget("select");
433496
}
434497
}
435498
} else {
@@ -459,8 +522,7 @@ void UiLevels::removeClicked(qint64 id) {
459522
<< "Remove just Level files");
460523
}
461524

462-
stackedWidget->setCurrentWidget(
463-
stackedWidget->findChild<QWidget*>("dialog"));
525+
this->setStackedWidget("dialog");
464526
}
465527

466528
void UiLevels::downloadClicked(qint64 id) {
@@ -472,8 +534,7 @@ void UiLevels::downloadClicked(qint64 id) {
472534
//
473535
select->downloadingState(false);
474536
select->stackedWidgetBar->progressWidgetBar->progressBar->setValue(0);
475-
select->stackedWidgetBar->setCurrentWidget(
476-
select->stackedWidgetBar->findChild<QWidget*>("progressWidgetBar"));
537+
select->setCurrentWidgetBar(StackedWidgetBar::Progress);
477538

478539
}
479540

@@ -510,9 +571,9 @@ void UiLevels::workTick() {
510571
QString("installed/level%1").arg(id),
511572
"true");
512573
}
513-
this->select->stackedWidgetBar->setCurrentWidget(
514-
this->select->stackedWidgetBar->findChild<QWidget*>("navigateWidgetBar"));
515-
this->select->downloadingState(true);
574+
575+
select->setCurrentWidgetBar(StackedWidgetBar::Navigate);
576+
select->downloadingState(true);
516577
levelDirSelected(id);
517578
}
518579
}

0 commit comments

Comments
 (0)