Skip to content

Commit

Permalink
clean up Network object
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Aug 14, 2024
1 parent 2584151 commit 78e593b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
82 changes: 50 additions & 32 deletions src/Network.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,65 @@
#include "Network.h"

struct WriteData
{
Downloader* downloader;
FILE* file;
};

bool Downloader::setUpCamp(const QString& levelDir)
{
QFileInfo levelPathInfo(levelDir);

if (levelPathInfo.isDir())
{
levelDir_m.setPath(levelDir);
return true;
}

return false;
}

//FIXME I hate to use globals like this..
qint64 oneTick=0;
qint64 remainderTick=0;
int timesSent=0;

size_t Downloader::write_callback(void* contents, size_t size, size_t nmemb, FILE* file)
size_t Downloader::write_callback(void* contents, size_t size, size_t nmemb, void* userData)
{
qint64 bars = (nmemb+remainderTick)/oneTick;
remainderTick = (nmemb+remainderTick)%oneTick;
while(bars>0)
WriteData* data = static_cast<WriteData*>(userData);
Downloader* instance = data->downloader;
FILE* file = data->file;

qint64 bars = (nmemb + instance->remainderTick) / instance->oneTick;
instance->remainderTick = (nmemb + instance->remainderTick) % instance->oneTick;

while (bars > 0)
{
emit Downloader::getInstance().networkWorkTickSignal();
timesSent+=1;
bars-=1;
emit instance->networkWorkTickSignal();
instance->timesSent += 1;
bars -= 1;
}

return fwrite(contents, size, nmemb, file);
}

void Downloader::setUrl(QUrl url)
{
url_m=url;
url_m = url;
}

void Downloader::setSaveFile(const QString& file)
{
file_m=file;
file_m = file;
}

void Downloader::saveToFile(const QByteArray& data, const QString& filePath) {
void Downloader::saveToFile(const QByteArray& data, const QString& filePath)
{
QFile file(filePath);
if (file.open(QIODevice::WriteOnly)) {

if (file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
qDebug() << "Data saved to file:" << filePath;
} else {
}
else
{
qDebug() << "Error saving data to file:" << file.errorString();
}
}
Expand All @@ -55,7 +69,7 @@ void Downloader::run()
if (url_m.isEmpty() || file_m.isEmpty() || levelDir_m.isEmpty())
return;

size_t zipFileSize=0; // I need to read filesize of the download link into this variable
size_t zipFileSize = 0; // I need to read filesize of the download link into this variable

QString urlString = url_m.toString();
QByteArray byteArray = urlString.toUtf8();
Expand Down Expand Up @@ -84,7 +98,8 @@ void Downloader::run()
else
{
qDebug() << "Failed to retrieve file size from header";
// handle ssl error
// handle SSL error, no connection, restore to previous state
// with an error on the GUI
return;
}
}
Expand All @@ -95,36 +110,39 @@ void Downloader::run()
}
}

oneTick = (zipFileSize/100)*2;
oneTick = (zipFileSize / 100) * 2;
remainderTick = 0;
int timesSent = 0;
timesSent = 0;

// Open file for writing
FILE* file = fopen(filePath.toUtf8().constData(), "wb");
if (!file) {
if (!file)
{
qDebug() << "Error opening file for writing:" << filePath;
return;
}

// Initialize libcurl easy handle
WriteData writeData = { this, file };

curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
if (curl)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Downloader::write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeData);
curl_easy_setopt(curl, CURLOPT_URL, url_cstring);

// Perform the download
CURLcode res = curl_easy_perform(curl);

if (res != CURLE_OK) {
if (res != CURLE_OK)
{
qDebug() << "curl_easy_perform() failed:" << curl_easy_strerror(res);
} else {
qDebug() << "Downloaded successful";
}
else
{
qDebug() << "Downloaded successfully";
}

// Clean up libcurl
curl_easy_cleanup(curl);
}
fclose(file);
}

9 changes: 7 additions & 2 deletions src/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Downloader : public QObject
bool setUpCamp(const QString& levelDir);
void setUrl(QUrl url);
void setSaveFile(const QString& file);
static size_t write_callback(void* contents, size_t size, size_t nmemb, FILE* file);
static size_t write_callback(void* contents, size_t size, size_t nmemb, void* userData);

signals:
void networkWorkTickSignal();
Expand All @@ -32,7 +32,12 @@ class Downloader : public QObject
QString file_m;
QDir levelDir_m;

Downloader(QObject *parent = nullptr) : QObject(parent)
qint64 oneTick = 0;
qint64 remainderTick = 0;
int timesSent = 0;

Downloader(QObject *parent = nullptr) : QObject(parent),
oneTick(0), remainderTick(0), timesSent(0)
{
// Initialize libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
Expand Down

0 comments on commit 78e593b

Please sign in to comment.