Skip to content

Commit

Permalink
++ RELEASE 1.1.0-lite ++
Browse files Browse the repository at this point in the history
  • Loading branch information
pietr26 committed Mar 5, 2023
1 parent f776db1 commit b5d91c3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
59 changes: 56 additions & 3 deletions OTBackend/OTGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,29 @@ public slots:
QByteArray post(const QUrl &url, QList<QPair<QString, QString>> params = QList<QPair<QString, QString>>(), unsigned int connectionTimeout = 10000)
{
lastSuccess = 0;
return download(url, params, connectionTimeout);
return downloadPost(url, params, connectionTimeout);
}

/// [OVERLOADED] POST / Saves the download file to a local file
int post(const QUrl &url, const QString filepath, QList<QPair<QString, QString>> params = QList<QPair<QString, QString>>(), unsigned int connectionTimeout = 10000)
{
lastSuccess = 0;
saveToFile(filepath, download(url, params, connectionTimeout));
saveToFile(filepath, downloadPost(url, params, connectionTimeout));
return lastHttpCode;
}

/// [OVERLOADED] GET / Returns the downloaded file
QByteArray get(const QUrl &url, unsigned int connectionTimeout = 10000)
{
lastSuccess = 0;
return downloadGet(url, connectionTimeout);
}

/// [OVERLOADED] GET / Saves the download file to a local file
int get(const QUrl &url, const QString filepath, unsigned int connectionTimeout = 10000)
{
lastSuccess = 0;
saveToFile(filepath, downloadGet(url, connectionTimeout));
return lastHttpCode;
}

Expand All @@ -199,7 +214,7 @@ private slots:
QNetworkAccessManager manager;

/// Main part of downloading a file
QByteArray download(const QUrl &url, QList<QPair<QString, QString>> params, unsigned int connectionTimeout)
QByteArray downloadPost(const QUrl &url, QList<QPair<QString, QString>> params, unsigned int connectionTimeout)
{
qDebug().noquote().nospace() << "POST to '" << url.url() << "'";
QNetworkRequest request(url);
Expand Down Expand Up @@ -249,6 +264,44 @@ private slots:
return reply->readAll();
}

/// Main part of downloading a file
QByteArray downloadGet(const QUrl &url, unsigned int connectionTimeout)
{
qDebug().noquote().nospace() << "GET to '" << url.url() << "'";
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setTransferTimeout(connectionTimeout);

QEventLoop loop;

connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
reply = manager.get(request);

connect(reply, &QNetworkReply::downloadProgress, this, &OTNetworkConnection::downloadProgress);
loop.exec();

// Content length:
// reply->header(QNetworkRequest::ContentLengthHeader).toInt();

int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
lastHttpCode = httpCode;

if (url.url().contains("omsi-tools.de") && (httpCode == 503))
{
lastSuccess = -2;
qWarning().noquote() << QString("%1 is currently undergoing maintenance (HTTP 503). Please try again later.").arg(url.host());
}
else if ((httpCode >= 300) || (httpCode == 0))
{
lastSuccess = -1;
return "";
}
else
lastSuccess = 1;

return reply->readAll();
}

/// Saves the download file to a local file
void saveToFile(const QString filepath, const QByteArray content)
{
Expand Down
2 changes: 1 addition & 1 deletion OTModules/OTGeneric/wstartupscreen.ui
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
<font>
<family>Consolas</family>
<pointsize>14</pointsize>
<italic>true</italic>
<italic>false</italic>
<bold>true</bold>
</font>
</property>
Expand Down
1 change: 1 addition & 0 deletions data/icons/iconPlay.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ressources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@
<file>data/icons/iconSend.svg</file>
<file>data/icons/iconRule.svg</file>
<file>data/icons/iconHome.svg</file>
<file>data/icons/iconPlay.svg</file>
</qresource>
</RCC>

0 comments on commit b5d91c3

Please sign in to comment.