Skip to content

Commit 15b5e2a

Browse files
committed
Integrate Path with Download
1 parent fc6e10a commit 15b5e2a

File tree

6 files changed

+57
-53
lines changed

6 files changed

+57
-53
lines changed

src/FileManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ bool FileManager::extractZip(
182182
QString("%1%2%3").arg(m_levelDir.absolutePath(), m_sep, zipFilename);
183183
const QString& outputPath =
184184
QString("%1%2%3").arg(m_levelDir.absolutePath(), m_sep, outputFolder);
185-
Path oPath(Path::resource);
186-
oPath << outputFolder;
185+
186+
Path oPath = (Path(Path::resource) << outputFolder);
187187

188188
qDebug() << "Unzipping file" << zipFilename << "to" << outputPath;
189189

src/Model.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ bool Model::setupDirectories(const QString& level, const QString& game) {
2222
bool status = false;
2323

2424
if (fileManager.setUpCamp(level, game) &&
25-
downloader.setUpCamp(level) &&
2625
data.initializeDatabase(level) &&
2726
m_pyRunner.setUpCamp(level)) {
2827
Path::setProgramFilesPath();
@@ -400,12 +399,12 @@ void Model::getLevel(int id) {
400399
if (id > 0) {
401400
bool status = false;
402401
ZipData zipData = data.getDownload(id);
402+
Path path(Path::resource);
403+
path << zipData.m_fileName;
403404
downloader.setUrl(zipData.m_URL);
404-
downloader.setSaveFile(zipData.m_fileName);
405+
downloader.setSaveFile(path);
405406
// this if just slips up execution but has nothing to do with the error
406407

407-
Path path(Path::resource);
408-
path << zipData.m_fileName;
409408

410409
if (path.isFile()) {
411410
qWarning() << "File exists:" << path.get();

src/Network.cpp

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
* GNU General Public License for more details.
1212
*/
1313

14-
#include "../src/Network.hpp"
1514
#include <curl/curl.h>
1615
#include <cstdio>
1716
#include <iostream>
1817
#include <vector>
1918
#include <string>
19+
#include "../src/Network.hpp"
20+
#include "../src/Path.hpp"
2021
#include <boost/asio.hpp>
2122
#include <boost/asio/ssl.hpp>
2223

@@ -79,66 +80,52 @@ std::string get_ssl_certificate(const std::string& host) {
7980
return cert_buffer;
8081
}
8182

82-
bool Downloader::setUpCamp(const QString& levelDir) {
83-
bool status = false;
84-
QFileInfo levelPathInfo(levelDir);
85-
if (levelPathInfo.isDir() == true) {
86-
m_levelDir.setPath(levelDir);
87-
status = true;
88-
}
89-
return status;
90-
}
91-
9283
void Downloader::setUrl(QUrl url) {
9384
m_url = url;
9485
}
9586

96-
void Downloader::setSaveFile(const QString& file) {
97-
m_file = file;
87+
void Downloader::setSaveFile(Path file) {
88+
m_saveFile = file;
9889
}
9990

10091
int Downloader::getStatus() {
10192
return m_status;
10293
}
10394

10495
void Downloader::run() {
105-
if (m_url.isEmpty() || m_file.isEmpty() || m_levelDir.isEmpty()) {
96+
if (m_url.isEmpty() || m_saveFile.getRoot() != Path::resource) {
10697
m_status = 3; // object error
10798
} else {
99+
m_status = 0;
108100
qDebug() << "m_url: " << m_url.toString();
109-
qDebug() << "m_file: " << m_file;
110-
qDebug() << "m_levelDir: " << m_levelDir.absolutePath();
111-
112-
QString urlString = m_url.toString();
113-
QByteArray byteArray = urlString.toUtf8();
114-
const char* url_cstring = byteArray.constData();
115-
116-
const QString filePath = QString("%1%2%3")
117-
.arg(m_levelDir.absolutePath(), QDir::separator(), m_file);
118-
119-
qDebug() << "filePath: " << filePath;
120-
121-
QFileInfo fileInfo(filePath);
122-
123-
if (fileInfo.exists() && !fileInfo.isFile()) {
124-
qDebug() << "Error: The zip path is not a regular file :"
125-
<< filePath;
101+
qDebug() << "m_filePath: " << m_saveFile.get();
102+
103+
if (m_saveFile.isSymLink()) {
104+
m_status = 5; // symlink error
105+
} else if (m_saveFile.exists() && !m_saveFile.isFile()) {
106+
qDebug()
107+
<< "Error: The zip path is not a regular file :"
108+
<< m_saveFile.get();
126109
m_status = 2; // file error
110+
127111
} else {
128-
QFile file(filePath);
112+
QFile file(m_saveFile.get());
129113
if (!file.open(QIODevice::WriteOnly)) { // flawfinder: ignore
130-
qDebug() << "Error opening file for writing:" << filePath;
131-
m_status = 2;
114+
qDebug()
115+
<< "Error opening file for writing:"
116+
<< m_saveFile.get();
117+
m_status = 4; // opening error
132118
} else {
133-
connect(&file, url_cstring);
119+
QByteArray byteArray = m_url.toString().toUtf8();
120+
const char* url_cstring = byteArray.constData();
121+
runConnect(&file, url_cstring);
122+
file.close();
134123
}
135-
136-
file.close();
137124
}
138125
}
139126
}
140127

141-
void Downloader::connect(QFile *file, const std::string& url) {
128+
void Downloader::runConnect(QFile *file, const std::string& url) {
142129
CURL* curl = curl_easy_init();
143130
if (!curl) {
144131
std::cerr << "Failed to initialize CURL\n";

src/Network.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QDebug>
2323
#include <curl/curl.h>
2424
#include <string>
25+
#include "../src/Path.hpp"
2526

2627
class Downloader : public QObject {
2728
Q_OBJECT
@@ -34,28 +35,25 @@ class Downloader : public QObject {
3435
}
3536

3637
void run();
37-
bool setUpCamp(const QString& levelDir);
3838
void setUrl(QUrl url);
3939
int getStatus();
40-
void setSaveFile(const QString& file);
40+
void setSaveFile(Path filePath);
4141

4242
signals:
4343
void networkWorkTickSignal();
4444
void networkWorkErrorSignal(int status);
4545

4646
private:
4747
void saveToFile(const QByteArray& data, const QString& filePath);
48-
void connect(QFile *file, const std::string& url);
48+
void runConnect(QFile *file, const std::string& url);
4949
QUrl m_url;
50-
QString m_file;
51-
QDir m_levelDir;
50+
Path m_saveFile;
5251
qint32 m_status;
5352
int m_lastEmittedProgress;
5453

5554
Downloader() :
5655
m_url(""),
57-
m_file(""),
58-
m_levelDir(""),
56+
m_saveFile(Path(Path::resource)),
5957
m_status(0),
6058
m_lastEmittedProgress(0) {
6159
curl_global_init(CURL_GLOBAL_DEFAULT);

src/Path.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "../src/Path.hpp"
1414
#include "../src/settings.hpp"
1515

16-
Path::Path(Root root) {
16+
Path::Path(Root root) : m_status(0), m_root(root) {
1717
if (root == programFiles) {
1818
m_path.setFile(m_programFilesPath.path());
1919
} else if (root == resource) {
@@ -112,3 +112,17 @@ bool Path::validateSymLink() {
112112

113113
return status;
114114
}
115+
116+
QString Path::getRootString() {
117+
QString path;
118+
if (m_root == programFiles) {
119+
path = m_programFilesPath.path();
120+
} else if (m_root == resource) {
121+
path = m_resourcePath.path();
122+
}
123+
return path;
124+
}
125+
126+
Path::Root Path::getRoot() {
127+
return m_root;
128+
}

src/Path.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class Path {
5353
static bool setResourcePath();
5454
QString get();
5555
QString getDir();
56+
QString getRootString();
57+
Path::Root getRoot();
5658
bool exists();
5759
bool isFile();
5860
bool isDir();
@@ -61,7 +63,8 @@ class Path {
6163

6264
Path& operator<<(const QString& subdir) {
6365
if (subdir != "..") {
64-
const QString s = QString("%1%2%3").arg(m_path.absoluteFilePath(), m_sep, subdir);
66+
const QString s = QString("%1%2%3")
67+
.arg(m_path.absoluteFilePath(), m_sep, subdir);
6568
m_path.setFile(s);
6669
}
6770
return *this;
@@ -71,6 +74,9 @@ class Path {
7174
static bool inHome(QString absoluteFilePath);
7275
static bool validate(const QFileInfo& fileInfo);
7376

77+
qint64 m_status;
78+
Root m_root;
79+
7480
QFileInfo m_path;
7581
inline static QDir m_programFilesPath;
7682
inline static QDir m_resourcePath;

0 commit comments

Comments
 (0)