Skip to content

Commit

Permalink
handle connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Aug 18, 2024
1 parent 51b25ec commit 994c2e3
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Controller::Controller(QObject *parent) : QObject(parent), ControllerThread(null
connect(&Model::getInstance(), SIGNAL(modelTickSignal()), this, SLOT(passTickSignal()));
connect(&FileManager::getInstance(), SIGNAL(fileWorkTickSignal()), this, SLOT(passTickSignal()));
connect(&Downloader::getInstance(), SIGNAL(networkWorkTickSignal()), this, SLOT(passTickSignal()));
connect(&Downloader::getInstance(), SIGNAL(networkWorkErrorSignal(int)), this, SLOT(passDownloadError(int)));

// The TombRaiderLinuxLauncher object have the connect and slot for last signal emit that displays
// the result, we pass back what was return before like this
Expand Down Expand Up @@ -119,3 +120,7 @@ void Controller::passTickSignal()
emit controllerTickSignal();
}

void Controller::passDownloadError(int status)
{
emit controllerDownloadError(status);
}
19 changes: 11 additions & 8 deletions src/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,26 @@ class Controller : public QObject
int getItemState(int id);
int checkGameDirectory(int id);

private:
Model& model = Model::getInstance();

explicit Controller(QObject *parent = nullptr);
~Controller();
QThread* ControllerThread;
Q_DISABLE_COPY(Controller)

public slots:
void passTickSignal();
void passDownloadError(int status);
void setupCampThread(const QString& level, const QString& game);
void setupLevelThread(int id);

signals:
void controllerTickSignal();
void controllerDownloadError(int status);
void setupCampThreadSignal(const QString& level, const QString& game);
void setupCampDone(bool status);
void setupLevelThreadSignal(int id);

private:
Model& model = Model::getInstance();

explicit Controller(QObject *parent = nullptr);
~Controller();
QThread* ControllerThread;
Q_DISABLE_COPY(Controller)
};

#endif // SRC_CONTROLLER_H_
23 changes: 16 additions & 7 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,18 @@ bool FileManager::moveFilesToDirectory(
return true;
}

bool FileManager::moveFilesToParentDirectory(const QString& levelDirectory, int levelsUp)
bool FileManager::moveFilesToParentDirectory(
const QString& levelDirectory,
int levelsUp)
{
const QString& sep = QDir::separator();
QDir levelDirectoryFullPath(levelDir_m.absolutePath() + sep + levelDirectory);
QDir levelDirectoryFullPath(levelDir_m.absolutePath() + sep +
levelDirectory);

if (!levelDirectoryFullPath.exists())
{
qWarning() << "Directory does not exist:" << levelDirectoryFullPath.absolutePath();
qWarning() << "Directory does not exist:"
<< levelDirectoryFullPath.absolutePath();
return false;
}

Expand All @@ -512,7 +516,7 @@ bool FileManager::moveFilesToParentDirectory(const QString& levelDirectory, int
if (!levelDirectoryUpPath.cdUp())
{
qWarning() << "Failed to access parent directory at level "
<< i + 1 << " from:" << levelDirectoryFullPath.absolutePath();
<< i + 1 << " from:" << levelDirectoryFullPath.absolutePath();
return false;
}
}
Expand All @@ -524,7 +528,8 @@ bool FileManager::moveFilesToParentDirectory(const QString& levelDirectory, int
for (const QFileInfo& fileInfo : fileList)
{
QString srcPath = fileInfo.absoluteFilePath();
QString destPath = levelDirectoryUpPath.absolutePath() + sep + fileInfo.fileName();
QString destPath = levelDirectoryUpPath.absolutePath() + sep +
fileInfo.fileName();

if (fileInfo.isDir())
{
Expand All @@ -547,9 +552,13 @@ bool FileManager::moveFilesToParentDirectory(const QString& levelDirectory, int
}
}

if (!levelDirectoryFullPath.rmdir("."))
QDir parentDir = levelDirectoryFullPath; // Create a copy
parentDir.cdUp(); // Move to the parent directory

if (!parentDir.rmdir(levelDirectoryFullPath.dirName()))
{
qWarning() << "Failed to remove directory:" << levelDirectoryFullPath.absolutePath();
qWarning() << "Failed to remove directory:"
<< levelDirectoryFullPath.absolutePath();
return false;
}

Expand Down
30 changes: 21 additions & 9 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,29 +178,41 @@ bool Model::getGame(int id)
return setUpOg(-id);
if (id)
{
int status = 0;
ZipData zipData = data.getDownload(id);
downloader.setUrl(zipData.url);
downloader.setSaveFile(zipData.name);

if (fileManager.checkFile(zipData.name, false))
{
qDebug() << "File exists:" << zipData.name;
// send 50% signal here
for (int i=0; i < 50; i++)
const QString& sum = fileManager.calculateMD5(zipData.name, false);
if (sum != zipData.md5sum)
{
emit this->modelTickSignal();
downloader.run();
status = downloader.getStatus();
}
if (fileManager.calculateMD5(zipData.name, false) != zipData.md5sum)
else
{
downloader.run();
// send 50% signal here
for (int i=0; i < 50; i++)
{
emit this->modelTickSignal();
}
}
} else {
}
else
{
qWarning() << "File does not exist:" << zipData.name;
downloader.run();
status = downloader.getStatus();
}
if (status == 0)
{
fileManager.extractZip(zipData.name, QString::number(id)+".TRLE");
instructionManager.executeInstruction(id);
return true;
}
fileManager.extractZip(zipData.name, QString::number(id)+".TRLE");
instructionManager.executeInstruction(id);
return true;
}
return false;
}
Expand Down
23 changes: 22 additions & 1 deletion src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ void Downloader::setSaveFile(const QString& file)
file_m = file;
}

int Downloader::getStatus()
{
return status_m;
}

void Downloader::saveToFile(const QByteArray& data, const QString& filePath)
{
QFile file(filePath);
Expand Down Expand Up @@ -132,13 +137,29 @@ void Downloader::run()

if (res != CURLE_OK)
{
status_m = 1;
qDebug() << "curl_easy_perform() failed:" << curl_easy_strerror(res);
// we need to catch any of those that seem inportant here to the GUI
// and reset GUI state
// https://curl.se/libcurl/c/libcurl-errors.html
if(res == 6 || res == 7 || res == 28 || res == 35)
{
emit this->networkWorkErrorSignal(1);
}
else if(res == CURLE_PEER_FAILED_VERIFICATION)
{
emit this->networkWorkErrorSignal(2);
}
else
{
emit this->networkWorkErrorSignal(3);
}
}
else
{
status_m = 0;
qDebug() << "Downloaded successfully";
}

curl_easy_cleanup(curl);
}
fclose(file);
Expand Down
6 changes: 5 additions & 1 deletion src/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Downloader : public QObject
void run();
bool setUpCamp(const QString& levelDir);
void setUrl(QUrl url);
int getStatus();
void setSaveFile(const QString& file);
static size_t write_callback(
void* contents,
Expand All @@ -52,14 +53,17 @@ class Downloader : public QObject

signals:
void networkWorkTickSignal();
void networkWorkErrorSignal(int status);

private:
void saveToFile(const QByteArray& data, const QString& filePath);
QUrl url_m;
QString file_m;
QDir levelDir_m;
qint32 status_m = 0;

explicit Downloader(QObject* parent = nullptr) : QObject(parent)
explicit Downloader(QObject* parent = nullptr) : QObject(parent),
status_m(0)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
};
Expand Down
38 changes: 35 additions & 3 deletions src/TombRaiderLinuxLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
connect(&Controller::getInstance(), SIGNAL(setupCampDone(bool)),
this, SLOT(checkCommonFiles(bool)));

// Error signal connections
connect(&Controller::getInstance(), SIGNAL(controllerDownloadError(int)),
this, SLOT(downloadError(int)));

// Set init state
ui->pushButtonLink->setEnabled(false);
ui->pushButtonInfo->setEnabled(false);
Expand Down Expand Up @@ -96,7 +100,8 @@ int TombRaiderLinuxLauncher::testallGames(int id){
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int result = msgBox.exec();
if (result == QMessageBox::Yes) {
if (result == QMessageBox::Yes)
{
qDebug() << "User clicked Yes.";
controller.setupOg(id);
}
Expand Down Expand Up @@ -379,11 +384,38 @@ void TombRaiderLinuxLauncher::workTick()
if (ui->progressBar->value() >= 100)
{
ui->pushButtonLink->setEnabled(true);
ui->pushButtonDownload->setEnabled(false);
ui->pushButtonInfo->setEnabled(true);
ui->pushButtonDownload->setEnabled(false);
ui->stackedWidgetBar->setCurrentWidget(
ui->stackedWidgetBar->findChild<QWidget*>("navigate"));
ui->stackedWidgetBar->findChild<QWidget*>("navigate"));
}
}

void TombRaiderLinuxLauncher::downloadError(int status)
{
ui->progressBar->setValue(0);
ui->pushButtonLink->setEnabled(true);
ui->pushButtonInfo->setEnabled(true);
ui->pushButtonDownload->setEnabled(true);
ui->stackedWidgetBar->setCurrentWidget(
ui->stackedWidgetBar->findChild<QWidget*>("navigate"));
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
if (status == 1)
{
msgBox.setText("No internet");
}
else if (status == 2)
{
msgBox.setText("You seem to be missing ssl keys");
}
else
{
msgBox.setText("Could not connect");
}
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}

TombRaiderLinuxLauncher::~TombRaiderLinuxLauncher()
Expand Down
4 changes: 4 additions & 0 deletions src/TombRaiderLinuxLauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class TombRaiderLinuxLauncher : public QMainWindow
*
*/
void checkCommonFiles(bool status);
/**
*
*/
void downloadError(int status);

private:
/**
Expand Down

0 comments on commit 994c2e3

Please sign in to comment.