2019-05-11 15:21:43 +02:00
|
|
|
/*
|
|
|
|
blueROCK - for digital rock
|
|
|
|
Copyright (C) 2019 Dorian Zedler
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-11 11:49:09 +02:00
|
|
|
#include "headers/bluerockbackend.h"
|
2019-04-20 22:57:40 +02:00
|
|
|
|
2021-06-11 11:49:09 +02:00
|
|
|
BlueRockBackend::BlueRockBackend(QObject *parent) : QObject(parent)
|
2019-04-20 22:57:40 +02:00
|
|
|
{
|
2021-06-19 09:06:52 +02:00
|
|
|
this->_shareUtils = new ShareUtils(this);
|
|
|
|
this->shareResultsAsPoster("test");
|
2019-04-20 22:57:40 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:49:09 +02:00
|
|
|
QVariant BlueRockBackend::getWidgetData(QVariantMap params) {
|
2019-06-23 10:55:08 +02:00
|
|
|
QString requestUrl;
|
2021-06-05 21:18:21 +02:00
|
|
|
if(params["nation"].toString() == "ICC") {
|
|
|
|
requestUrl = "https://www.digitalrock.de/egroupware/ranking/json.php?";
|
2019-06-23 10:55:08 +02:00
|
|
|
params["nation"] = "";
|
2019-04-20 22:57:40 +02:00
|
|
|
}
|
2019-06-23 10:55:08 +02:00
|
|
|
else if (params["nation"].toString() == "GER") {
|
|
|
|
requestUrl = "https://www.digitalrock.de/egroupware/ranking/json.php?";
|
2019-04-20 22:57:40 +02:00
|
|
|
}
|
2019-06-23 10:55:08 +02:00
|
|
|
else if (params["nation"].toString() == "SUI") {
|
|
|
|
requestUrl = "https://www.digitalrock.de/egroupware/ranking/json.php?";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
params.remove("nation");
|
|
|
|
requestUrl = "https://www.digitalrock.de/egroupware/ranking/json.php?";
|
2019-05-12 21:33:22 +02:00
|
|
|
}
|
2019-05-23 23:17:27 +02:00
|
|
|
|
2021-06-05 21:18:21 +02:00
|
|
|
for(QVariantMap::const_iterator iter = params.begin(); iter != params.end(); ++iter) {
|
2019-05-23 23:17:27 +02:00
|
|
|
requestUrl += iter.key() + "=" + iter.value().toString() + "&";
|
|
|
|
}
|
|
|
|
|
2019-07-10 22:05:53 +02:00
|
|
|
requestUrl = requestUrl.left(requestUrl.length() - 1); // remove last '&'
|
2019-06-23 10:55:08 +02:00
|
|
|
|
2019-05-23 23:17:27 +02:00
|
|
|
qDebug() << requestUrl;
|
|
|
|
|
|
|
|
QVariantMap ret = this->senddata(QUrl(requestUrl));
|
|
|
|
|
2021-06-05 21:18:21 +02:00
|
|
|
if(ret["status"] != 200) {
|
2019-05-23 23:17:27 +02:00
|
|
|
// request was a failure
|
|
|
|
return QVariantMap({{"status", ret["status"]}, {"data", ""}});
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument jsonReply = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
|
|
|
|
|
|
|
|
QVariantMap data = {{"status", 200}, {"data", jsonReply.toVariant()}};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-06-11 11:49:09 +02:00
|
|
|
|
|
|
|
QVariantMap BlueRockBackend::getParamsFromUrl(QString stringUrl) {
|
|
|
|
stringUrl = stringUrl.replace("#!", "?");
|
|
|
|
QUrl url(stringUrl);
|
|
|
|
QUrlQuery query(url.query());
|
|
|
|
|
|
|
|
QVariantMap params;
|
|
|
|
|
|
|
|
for(QPair<QString, QString> pair : query.queryItems()) {
|
|
|
|
if(params.contains(pair.first))
|
|
|
|
params[pair.first] = pair.second;
|
|
|
|
else
|
|
|
|
params.insert(pair.first, pair.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:06:52 +02:00
|
|
|
void BlueRockBackend::shareResultsAsUrl(QString url) {
|
|
|
|
this->_shareUtils->shareText(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlueRockBackend::shareResultsAsPoster(QString url) {
|
|
|
|
QPdfWriter writer("/tmp/test.pdf");
|
|
|
|
writer.setPageSize(QPageSize(QPageSize::A4));
|
|
|
|
writer.setPageMargins(QMargins(0, 0, 0, 0));
|
|
|
|
writer.setResolution(600);
|
|
|
|
QPainter painter(&writer);
|
|
|
|
painter.drawText(QRect(0, 0, 1980, 100),Qt::AlignHCenter|Qt::AlignBottom,
|
|
|
|
"Children's Health Checkup Form");
|
|
|
|
QPixmap image(":/PosterTemplate.png");
|
|
|
|
painter.drawPixmap(0,0, writer.width(), writer.height(), image);
|
|
|
|
painter.end();
|
|
|
|
}
|
|
|
|
|
2019-04-20 22:57:40 +02:00
|
|
|
// ------------------------
|
|
|
|
// --- Helper functions ---
|
|
|
|
// ------------------------
|
|
|
|
|
2021-06-11 11:49:09 +02:00
|
|
|
QVariantMap BlueRockBackend::senddata(QUrl serviceUrl, QUrlQuery pdata)
|
2019-04-20 22:57:40 +02:00
|
|
|
{
|
|
|
|
// create network manager
|
|
|
|
QNetworkAccessManager * networkManager = new QNetworkAccessManager();
|
|
|
|
|
|
|
|
QVariantMap ret; //this is a custom type to store the return-data
|
|
|
|
|
|
|
|
// Create network request
|
|
|
|
QNetworkRequest request(serviceUrl);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
|
2019-05-12 21:33:22 +02:00
|
|
|
//QSslConfiguration config = QSslConfiguration::defaultConfiguration();
|
|
|
|
//config.setProtocol(QSsl::TlsV1_2);
|
|
|
|
//request.setSslConfiguration(config);
|
2019-04-20 22:57:40 +02:00
|
|
|
|
|
|
|
//send a POST request with the given url and data to the server
|
|
|
|
|
|
|
|
QNetworkReply *reply;
|
|
|
|
|
2021-06-05 21:18:21 +02:00
|
|
|
if(pdata.isEmpty()) {
|
2019-04-20 22:57:40 +02:00
|
|
|
// if no post data is given -> send a GET request
|
|
|
|
reply = networkManager->get(request);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if post data is given -> send POST request
|
|
|
|
reply = networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop to wait until the request has finished before processing the data
|
|
|
|
QEventLoop loop;
|
|
|
|
// timer to cancel the request after 3 seconds
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
|
|
|
|
// quit the loop when the request finised
|
|
|
|
loop.connect(networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
|
|
|
|
// or the timer timed out
|
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
// start the timer
|
|
|
|
timer.start(10000);
|
|
|
|
// start the loop
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
//get the status code
|
|
|
|
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
|
|
|
|
ret.insert("status", status_code.toInt());
|
|
|
|
|
|
|
|
//get the full text response
|
|
|
|
ret.insert("text", QString::fromUtf8(reply->readAll()));
|
|
|
|
|
|
|
|
// delete the reply object
|
|
|
|
delete reply;
|
|
|
|
|
|
|
|
// delete the newtwork access manager object
|
|
|
|
delete networkManager;
|
|
|
|
|
|
|
|
//return the data
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------
|
|
|
|
// --- Functions for QML ---
|
|
|
|
// -------------------------
|
|
|
|
|
2019-05-11 15:16:23 +02:00
|
|
|
|
2019-04-20 22:57:40 +02:00
|
|
|
|