2019-01-19 23:03:50 +01:00
|
|
|
/*
|
|
|
|
Fannyapp - Application to view the cover plan of the Fanny-Leicht-Gymnasium ins Stuttgart Vaihingen, Germany
|
|
|
|
Copyright (C) 2019 Itsblue Development <development@itsblue.de>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
#include "headers/serverconn.h"
|
2019-02-16 21:24:50 +01:00
|
|
|
#define http https
|
2018-12-30 20:58:52 +01:00
|
|
|
|
|
|
|
ServerConn * pGlobalServConn = nullptr;
|
|
|
|
|
|
|
|
ServerConn::ServerConn(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2019-10-24 19:43:33 +02:00
|
|
|
qDebug("+----- ServerConn constructor -----+");
|
2018-12-30 20:58:52 +01:00
|
|
|
pGlobalServConn = this;
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
this->fileHelper = new FileHelper();
|
|
|
|
connect(this->fileHelper, &FileHelper::shareError, [=](){qWarning() << "share error";});
|
|
|
|
connect(this->fileHelper, &FileHelper::shareFinished, [=](){qWarning() << "share finished";});
|
|
|
|
connect(this->fileHelper, &FileHelper::shareNoAppAvailable, [=](){qWarning() << "share no app available";});
|
|
|
|
|
|
|
|
// get local work path
|
|
|
|
#if defined (Q_OS_IOS)
|
|
|
|
QString docLocationRoot = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).value(0);
|
|
|
|
qDebug() << "iOS: QStandardPaths::DocumentsLocation: " << docLocationRoot;
|
|
|
|
#elif defined(Q_OS_ANDROID)
|
|
|
|
QString docLocationRoot = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
|
|
|
|
#else
|
|
|
|
QString docLocationRoot = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
|
|
|
|
#endif
|
|
|
|
mDocumentsWorkPath = docLocationRoot.append("/tmp_pdf_files");
|
|
|
|
if (!QDir(mDocumentsWorkPath).exists()) {
|
|
|
|
if (!QDir("").mkpath(mDocumentsWorkPath)) {
|
|
|
|
pGlobalAppSettings->writeSetting("localDocPathError", "true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
// check login state
|
|
|
|
int perm = pGlobalAppSettings->loadSetting("permanent").toInt();
|
2019-01-19 22:44:48 +01:00
|
|
|
qDebug() << "+----- login state: " << perm << " -----+";
|
2018-12-30 20:58:52 +01:00
|
|
|
|
|
|
|
if(perm == 1){
|
2019-01-19 22:44:48 +01:00
|
|
|
// permanent login -> restore login
|
2019-12-07 15:38:35 +01:00
|
|
|
this->token = pGlobalAppSettings->loadSetting("token");
|
2018-12-30 20:58:52 +01:00
|
|
|
|
|
|
|
this->setState("loggedIn");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->setState("notLoggedIn");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ServerConn::login(QString username, QString password, bool permanent)
|
|
|
|
{
|
2020-01-25 11:13:32 +01:00
|
|
|
// prepare URL
|
|
|
|
|
|
|
|
QByteArray usernameByteArray = username.toUtf8();
|
|
|
|
QByteArray passwordByteArray = password.toUtf8();
|
2019-12-07 15:38:35 +01:00
|
|
|
|
2020-01-25 11:13:32 +01:00
|
|
|
QString url =
|
|
|
|
"https://www.fanny-leicht.de/j34/index.php/component/fannysubstitutionplan?task=api_login&username=" +
|
|
|
|
usernameByteArray.toBase64() + "&password=" + passwordByteArray.toBase64() + "&loginIsBase64=true";
|
|
|
|
qDebug() << url;
|
|
|
|
// send the request
|
|
|
|
QVariantMap ret = this->senddata(QUrl(url));
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
if(ret["status"].toInt() == 200){
|
2019-12-07 15:38:35 +01:00
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-12-07 15:38:35 +01:00
|
|
|
if(jsonDoc.toVariant().toMap()["result"].toInt() == 200) {
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-12-07 15:38:35 +01:00
|
|
|
this->token = jsonDoc.toVariant().toMap()["data"].toMap()["token"].toString();
|
2019-01-19 22:44:48 +01:00
|
|
|
|
2019-12-07 15:38:35 +01:00
|
|
|
if(permanent){
|
|
|
|
// if the user wants to say logged in, store the username and password to the settings file
|
|
|
|
pGlobalAppSettings->writeSetting("permanent", "1");
|
|
|
|
pGlobalAppSettings->writeSetting("token", this->token);
|
|
|
|
pGlobalAppSettings->writeSetting("password", password);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set state to loggedIn
|
|
|
|
this->setState("loggedIn");
|
|
|
|
|
|
|
|
qDebug() << "+----- logged in -----+";
|
|
|
|
|
|
|
|
// return success
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret["status"] = jsonDoc.toVariant().toMap()["result"].toInt();
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
2019-12-07 15:38:35 +01:00
|
|
|
|
|
|
|
// if not 200 was returned -> error -> return the return code
|
|
|
|
this->setState("notLoggedIn");
|
|
|
|
// -> reset the stored credentinals
|
|
|
|
pGlobalAppSettings->writeSetting("permanent", "0");
|
|
|
|
pGlobalAppSettings->writeSetting("token", "");
|
|
|
|
return ret["status"].toInt();
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ServerConn::logout()
|
|
|
|
{
|
|
|
|
// reset the data stored in the class
|
2019-12-07 15:38:35 +01:00
|
|
|
this->token = "";
|
2019-01-19 22:44:48 +01:00
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
// reset the data stored in the settings
|
|
|
|
pGlobalAppSettings->writeSetting("permanent", "0");
|
2019-12-07 15:38:35 +01:00
|
|
|
pGlobalAppSettings->writeSetting("token", "");
|
2018-12-30 20:58:52 +01:00
|
|
|
|
|
|
|
this->setState("notLoggedIn");
|
2019-01-19 22:44:48 +01:00
|
|
|
|
|
|
|
qDebug() << "+----- logout -----+";
|
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
// return success
|
|
|
|
return(200);
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
int ServerConn::getEvents(QString day)
|
2018-12-30 20:58:52 +01:00
|
|
|
{
|
2019-02-03 22:11:48 +01:00
|
|
|
// day: 0-today; 1-tomorrow
|
2019-01-19 22:44:48 +01:00
|
|
|
if(this->state != "loggedIn"){
|
|
|
|
return(401);
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// add the data to the request
|
|
|
|
QUrlQuery pdata;
|
2019-12-07 15:38:35 +01:00
|
|
|
pdata.addQueryItem("token", this->token);
|
2019-02-03 22:11:48 +01:00
|
|
|
pdata.addQueryItem("mode", pGlobalAppSettings->loadSetting("teacherMode") == "true" ? "1":"0");
|
2019-01-19 22:44:48 +01:00
|
|
|
pdata.addQueryItem("day", day);
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-01 15:21:25 +01:00
|
|
|
// send the request
|
2019-12-07 15:38:35 +01:00
|
|
|
QVariantMap ret = this->senddata(QUrl("https://www.fanny-leicht.de/j34/index.php/component/fannysubstitutionplan?task=api_getData&token=" + token
|
|
|
|
+ "&mode=" + (pGlobalAppSettings->loadSetting("teacherMode") == "true" ? "1":"0") + "&day=" + day));
|
2018-12-30 21:10:36 +01:00
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
if(ret["status"].toInt() != 200){
|
2018-12-30 20:58:52 +01:00
|
|
|
// if the request didn't result in a success, clear the old events, as they are probaply incorrect and return the error code
|
|
|
|
this->m_events.clear();
|
2019-10-24 19:43:33 +02:00
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
if(ret["status"].toInt() == 401){
|
2019-10-24 19:43:33 +02:00
|
|
|
// if the stats code is 401 -> userdata is incorrect
|
|
|
|
qDebug() << "+----- checkconn: user data is incorrect -----+";
|
|
|
|
logout();
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
return ret["status"].toInt();
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the filers list for later usage
|
|
|
|
QList<QStringList> filtersList = pGlobalAppSettings->readFilters();
|
|
|
|
|
|
|
|
// remove all elements from the filters list, that do not match the current mode ( teacher / student ) of the app
|
|
|
|
for(int i = 0; i < filtersList.length(); i++){
|
|
|
|
QStringList filterList = filtersList[i];
|
|
|
|
if( !(pGlobalAppSettings->loadSetting("teacherMode") == "true" ? filterList[2] == "t":filterList[2] == "s") ){
|
2019-01-19 22:44:48 +01:00
|
|
|
filtersList.removeAt(i);
|
|
|
|
i = i-1;
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// list to be returned
|
|
|
|
QList<QStringList> tmpEvents;
|
|
|
|
QStringList tmpEventHeader;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
//qDebug() << jsonString;
|
2019-11-06 22:02:48 +01:00
|
|
|
QJsonDocument jsonFilters = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
|
2019-02-03 22:11:48 +01:00
|
|
|
|
|
|
|
// array with tghe whole response in it
|
|
|
|
QJsonObject JsonArray = jsonFilters.object();
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// get the version of the json format
|
2019-02-03 22:11:48 +01:00
|
|
|
QString version = JsonArray.value("version").toString();
|
2019-01-27 20:47:36 +01:00
|
|
|
QStringList versionList = version.split(".");
|
|
|
|
if(versionList.length() < 3){
|
|
|
|
return(900);
|
|
|
|
}
|
|
|
|
|
|
|
|
int versionMajor = version.split(".")[0].toInt();
|
|
|
|
int versionMinor = version.split(".")[1].toInt();
|
2019-03-13 20:07:54 +01:00
|
|
|
//int versionRevision = version.split(".")[2].toInt();
|
2019-01-27 20:47:36 +01:00
|
|
|
|
|
|
|
if(versionMajor > this->apiVersion[0] || versionMinor > this->apiVersion[1]){
|
|
|
|
return(904);
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:11:48 +01:00
|
|
|
// get parse the document out of the return
|
|
|
|
QJsonObject dataArray = JsonArray.value("data").toObject();
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// get the header data
|
|
|
|
tmpEventHeader.append(dataArray.value("targetDate").toString());
|
2019-01-27 20:47:36 +01:00
|
|
|
tmpEventHeader.append(dataArray.value("refreshDate").toString());
|
2019-01-19 22:44:48 +01:00
|
|
|
tmpEventHeader.append(dataArray.value("stewardingClass").toString());
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// expand the length of the header list to seven to prevent list out of range errors
|
|
|
|
while (tmpEventHeader.length() < 7) {
|
|
|
|
tmpEventHeader.append("");
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// append the header to the temporyry event list
|
|
|
|
tmpEvents.append(tmpEventHeader);
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// get the event data
|
|
|
|
QJsonArray eventsArray = dataArray.value("events").toArray();
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
for(int i=0; i<eventsArray.count(); i++){
|
|
|
|
// get the current event-list out of the array
|
|
|
|
QJsonArray eventArray = eventsArray[i].toArray();
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// lst to store the current event
|
|
|
|
QStringList tmpEventList;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// extract values from array
|
|
|
|
foreach(const QJsonValue & key, eventArray){
|
|
|
|
// and append them to the temporyry list
|
|
|
|
tmpEventList.append(key.toString());
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
while (tmpEventList.length() < 7) {
|
|
|
|
// enshure that the list contains at least seven items (to prevent getting list out of range errors)
|
|
|
|
tmpEventList.append("");
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
if(filtersList.isEmpty()){
|
2018-12-30 20:58:52 +01:00
|
|
|
// if there are no filters append the event immideatly
|
2019-01-19 22:44:48 +01:00
|
|
|
tmpEvents.append(tmpEventList);
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if there is at least one filter, check if the event matches it
|
|
|
|
foreach(QStringList filter, filtersList){
|
|
|
|
// go through all filters and check if one of them matches the event
|
2019-01-19 22:44:48 +01:00
|
|
|
// always append the first row, as it is the legend
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
if((tmpEventList[0].contains(filter[0]) && tmpEventList[0].contains(filter[1])) || i == 0){
|
2018-12-30 20:58:52 +01:00
|
|
|
// append the eventList to the temporary event list
|
2019-01-19 22:44:48 +01:00
|
|
|
tmpEvents.append(tmpEventList);
|
2018-12-30 20:58:52 +01:00
|
|
|
// terminate the loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there is any valid data
|
2019-11-06 22:02:48 +01:00
|
|
|
if(tmpEvents.length() == 0 || tmpEvents.length() == 1) {
|
|
|
|
// no data was delivered at all -> the server encountered a parse error
|
|
|
|
tmpEvents.clear();
|
|
|
|
ret["status"].setValue(900);
|
|
|
|
}
|
|
|
|
else if(tmpEvents.length() == 2) {
|
2019-01-19 22:44:48 +01:00
|
|
|
// remove the last (in this case the second) element, as it is unnecessary (it is the legend -> not needed when there is no data)
|
|
|
|
tmpEvents.takeLast();
|
|
|
|
// set return code to 'no data' (901)
|
2019-11-06 22:02:48 +01:00
|
|
|
ret["status"].setValue(901);
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
this->m_events = tmpEvents;
|
2019-11-06 22:02:48 +01:00
|
|
|
return(ret["status"].toInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
int ServerConn::openEventPdf(QString day) {
|
|
|
|
// day: 0-today; 1-tomorrow
|
|
|
|
if(this->state != "loggedIn"){
|
|
|
|
return 401;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pGlobalAppSettings->loadSetting("localDocPathError") == "true") {
|
|
|
|
// we have no local document path to work with -> this is not going to work!
|
|
|
|
return 905;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the data to the request
|
|
|
|
QUrlQuery pdata;
|
2019-12-07 15:38:35 +01:00
|
|
|
pdata.addQueryItem("token", this->token);
|
2019-11-06 22:02:48 +01:00
|
|
|
pdata.addQueryItem("mode", pGlobalAppSettings->loadSetting("teacherMode") == "true" ? "1":"0");
|
|
|
|
pdata.addQueryItem("day", day);
|
|
|
|
pdata.addQueryItem("asPdf", "true");
|
|
|
|
|
|
|
|
// send the request
|
2019-12-07 15:38:35 +01:00
|
|
|
QVariantMap ret = this->senddata(QUrl("https://www.fanny-leicht.de/j34/index.php/component/fannysubstitutionplan?task=api_getData&token=" + token
|
|
|
|
+ "&mode=" + (pGlobalAppSettings->loadSetting("teacherMode") == "true" ? "1":"0") + "&day=" + day + "&asPdf=true"), true);
|
2019-11-06 22:02:48 +01:00
|
|
|
|
|
|
|
if(ret["status"].toInt() != 200){
|
|
|
|
// if the request didn't result in a success, clear the old events, as they are probaply incorrect and return the error code
|
|
|
|
this->m_events.clear();
|
|
|
|
|
|
|
|
if(ret["status"].toInt() == 401){
|
|
|
|
// if the stats code is 401 -> userdata is incorrect
|
|
|
|
qDebug() << "+----- checkconn: user data is incorrect -----+";
|
|
|
|
logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret["status"].toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
|
|
QString filname = (QString(pGlobalAppSettings->loadSetting("teacherMode") == "true" ? "l":"s") + QString(day == "0" ? "heute":"morgen" ));
|
|
|
|
QFile file(mDocumentsWorkPath + "/" + filname + ".pdf");
|
|
|
|
file.remove();
|
|
|
|
file.open(QIODevice::ReadWrite);
|
|
|
|
file.write(ret["data"].toByteArray());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
this->fileHelper->viewFile(mDocumentsWorkPath + "/" + filname + ".pdf", "SMorgen", "application/pdf", 1);
|
|
|
|
|
|
|
|
return 200;
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ServerConn::getFoodPlan()
|
|
|
|
{
|
2019-02-16 21:24:50 +01:00
|
|
|
// list with all data keys which need to be read from the API
|
|
|
|
QStringList foodplanDataKeys = { "cookteam", "date", "mainDish", "mainDishVeg", "garnish", "dessert" };
|
|
|
|
QString foodplanDateKey = "date";
|
|
|
|
|
|
|
|
QString url = "http://www.treffpunkt-fanny.de/images/stories/dokumente/Essensplaene/api/TFfoodplanAPI.php?dataCount=10&dataMode=days&dateFormat=U&dataFromTime=now";
|
|
|
|
// construct the URL with all requested fields
|
|
|
|
foreach(QString foodplanDataKey, foodplanDataKeys){
|
|
|
|
url.append("&fields[]="+foodplanDataKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the request to the server
|
2019-12-07 15:38:35 +01:00
|
|
|
QVariantMap ret = this->senddata(QUrl(url));
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
if(ret["status"].toInt() != 200){
|
2018-12-30 20:58:52 +01:00
|
|
|
// if the request didn't result in a success, return the error code
|
|
|
|
|
|
|
|
// if the request failed but there is still old data available
|
|
|
|
if(!this->m_weekplan.isEmpty()){
|
|
|
|
// set the status code to 902 (old data)
|
2019-11-06 22:02:48 +01:00
|
|
|
ret["status"].setValue(902);
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
return(ret["status"].toInt());
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
// list to be returned
|
2018-12-30 20:58:52 +01:00
|
|
|
// m_weekplan is a list, that contains a list for each day, which contains: cookteam, date, main dish, vagi main dish, garnish(Beilage) and Dessert.
|
2019-02-16 21:24:50 +01:00
|
|
|
QList<QStringList> tmpWeekplan;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
//qDebug() << jsonString;
|
2019-11-06 22:02:48 +01:00
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
|
|
|
|
//qDebug() << ret["text"].toString();
|
2019-02-16 21:24:50 +01:00
|
|
|
// array with the whole response in it
|
|
|
|
QJsonArray foodplanDays = jsonDoc.array();
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
foreach(QJsonValue foodplanDay, foodplanDays){
|
|
|
|
QStringList tmpFoodplanDayList;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
foreach(QString foodplanDataKey, foodplanDataKeys){
|
|
|
|
QString dataValue = foodplanDay.toObject().value(foodplanDataKey).toString();
|
|
|
|
if(foodplanDataKey == foodplanDateKey){
|
|
|
|
QDateTime date;
|
2019-11-17 21:21:52 +01:00
|
|
|
date.setSecsSinceEpoch(uint(dataValue.toInt()));
|
|
|
|
date.setTimeSpec(Qt::UTC);
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
// get the current date and time
|
|
|
|
QDateTime currentDateTime = QDateTime::currentDateTimeUtc();
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
//---------- convert the date to a readable string ----------
|
|
|
|
QString readableDateString;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
if(date.date() == currentDateTime.date()){
|
|
|
|
// the given day is today
|
|
|
|
readableDateString = "Heute";
|
|
|
|
}
|
2019-11-17 21:21:52 +01:00
|
|
|
else if (date.toTime_t() < ( currentDateTime.toTime_t() + ( 48 * 60 * 60 ) )) {
|
2019-02-16 21:24:50 +01:00
|
|
|
// the given day is tomorrow
|
|
|
|
readableDateString = "Morgen";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
readableDateString = date.toString("dddd, d.M.yy");
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
dataValue = readableDateString;
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
tmpFoodplanDayList.append( dataValue );
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-02-16 21:24:50 +01:00
|
|
|
tmpWeekplan.append(tmpFoodplanDayList);
|
2018-12-30 20:58:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// write data to global foodplan
|
2019-02-16 21:24:50 +01:00
|
|
|
this->m_weekplan = tmpWeekplan;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
|
|
|
// check if there is any valid data
|
|
|
|
if(this->m_weekplan.isEmpty()){
|
|
|
|
// no data
|
|
|
|
return(901);
|
|
|
|
}
|
|
|
|
|
|
|
|
// success
|
|
|
|
return(200);
|
|
|
|
}
|
|
|
|
|
2019-12-07 15:38:35 +01:00
|
|
|
QVariantMap ServerConn::senddata(QUrl serviceUrl, bool raw)
|
2018-12-30 20:58:52 +01:00
|
|
|
{
|
2019-03-16 23:31:07 +01:00
|
|
|
// create network manager
|
2019-03-13 20:07:54 +01:00
|
|
|
QNetworkAccessManager * networkManager = new QNetworkAccessManager();
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
QVariantMap ret; //this is a custom type to store the return-data
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// Create network request
|
2018-12-30 20:58:52 +01:00
|
|
|
QNetworkRequest request(serviceUrl);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
|
2019-03-09 23:26:58 +01:00
|
|
|
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
|
|
|
|
config.setProtocol(QSsl::TlsV1_2);
|
|
|
|
request.setSslConfiguration(config);
|
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
//send a POST request with the given url and data to the server
|
|
|
|
|
2019-03-09 23:26:58 +01:00
|
|
|
QNetworkReply *reply;
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// loop to wait until the request has finished before processing the data
|
2018-12-30 20:58:52 +01:00
|
|
|
QEventLoop loop;
|
2019-01-19 22:44:48 +01:00
|
|
|
// timer to cancel the request after 3 seconds
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
|
|
|
|
// quit the loop when the request finised
|
2019-03-13 20:07:54 +01:00
|
|
|
loop.connect(networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
|
2019-01-19 22:44:48 +01:00
|
|
|
// or the timer timed out
|
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
// start the timer
|
2019-03-13 20:07:54 +01:00
|
|
|
timer.start(10000);
|
2019-10-24 19:43:33 +02:00
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
this->updateDownloadProgress(0, 1);
|
2019-12-07 15:38:35 +01:00
|
|
|
reply = networkManager->get(request);
|
2019-10-24 19:43:33 +02:00
|
|
|
connect(reply, &QNetworkReply::sslErrors, this, [=](){ reply->ignoreSslErrors(); });
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
2019-12-07 15:38:35 +01:00
|
|
|
this, SLOT(updateDownloadProgress(qint64, qint64)));
|
2019-11-06 22:02:48 +01:00
|
|
|
|
2019-01-19 22:44:48 +01:00
|
|
|
// start the loop
|
2018-12-30 20:58:52 +01:00
|
|
|
loop.exec();
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
if(!timer.isActive()) {
|
|
|
|
// timeout
|
|
|
|
return {{"status", 0}};
|
|
|
|
}
|
2019-10-24 19:43:33 +02:00
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
//get the status code
|
|
|
|
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
ret.insert("status", status_code.toInt());
|
2019-01-19 22:44:48 +01:00
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
//get the full text response
|
2019-11-06 22:02:48 +01:00
|
|
|
if(!raw) {
|
|
|
|
ret.insert("text", QString::fromUtf8(reply->readAll()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret.insert("data", reply->readAll());
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
|
2019-03-09 23:26:58 +01:00
|
|
|
// delete the reply object
|
2019-10-24 19:43:33 +02:00
|
|
|
reply->deleteLater();
|
2019-01-19 22:44:48 +01:00
|
|
|
|
2019-03-13 20:07:54 +01:00
|
|
|
// delete the newtwork access manager object
|
2019-10-24 19:43:33 +02:00
|
|
|
networkManager->deleteLater();
|
2019-03-13 20:07:54 +01:00
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
//return the data
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
void ServerConn::updateDownloadProgress(qint64 read, qint64 total)
|
|
|
|
{
|
|
|
|
double progress;
|
|
|
|
|
|
|
|
if(total <= 0)
|
|
|
|
progress = 0;
|
|
|
|
else
|
|
|
|
progress = (double(read) / double(total));
|
|
|
|
|
|
|
|
if(progress < 0)
|
|
|
|
progress = 0;
|
|
|
|
|
|
|
|
this->downloadProgress = progress;
|
|
|
|
emit this->downloadProgressChanged();
|
|
|
|
}
|
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
QString ServerConn::getState() {
|
|
|
|
return(this->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerConn::setState(QString state) {
|
2019-01-19 22:44:48 +01:00
|
|
|
|
|
|
|
if(state != this->state){
|
2019-02-03 22:11:48 +01:00
|
|
|
qDebug() << "+----- serverconn has new state: " << state << " -----+";
|
2019-01-19 22:44:48 +01:00
|
|
|
this->state = state;
|
|
|
|
this->stateChanged(this->state);
|
|
|
|
}
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:02:48 +01:00
|
|
|
double ServerConn::getDownloadProgress() {
|
|
|
|
return this->downloadProgress;
|
|
|
|
}
|
|
|
|
|
2018-12-30 20:58:52 +01:00
|
|
|
ServerConn::~ServerConn()
|
|
|
|
{
|
2019-01-19 22:44:48 +01:00
|
|
|
qDebug("+----- ServerConn destruktor -----+");
|
2018-12-30 20:58:52 +01:00
|
|
|
}
|