This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
fanny-app/sources/serverconn.cpp

523 lines
18 KiB
C++
Raw Normal View History

2018-06-21 16:41:02 +02:00
#include "headers/serverconn.h"
2018-06-21 08:47:09 +02:00
2018-06-21 18:36:46 +02:00
2018-06-21 08:47:09 +02:00
ServerConn::ServerConn(QObject *parent) : QObject(parent)
{
qDebug("serverconn konstruktor");
this->networkManager = new QNetworkAccessManager();
2018-06-21 16:41:02 +02:00
this->refreshNetworkManager = new QNetworkAccessManager();
}
2018-06-21 08:47:09 +02:00
2018-06-21 16:41:02 +02:00
ServerConn::~ServerConn()
{
qDebug("serverconn destruktor");
delete this->networkManager;
delete this->refreshNetworkManager;
2018-06-21 08:47:09 +02:00
2018-06-21 16:41:02 +02:00
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QDir dir(path + "/.fannyapp-tmp");
dir.removeRecursively();
2018-06-21 08:47:09 +02:00
}
2018-06-21 16:41:02 +02:00
QString ServerConn::login(QString username, QString password, bool permanent)
2018-06-21 08:47:09 +02:00
{
// QUrlQuery pdata;
// ReturnData_t ret = this->senddata(QUrl("http://www.fanny-leicht.de/static15/http.intern/sheute.pdf"), pdata);
// qDebug() << ret.text;
2018-06-21 08:47:09 +02:00
// Create request
QNetworkRequest request;
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/sheute.pdf" ) );
// Pack in credentials
QString concatenatedCredentials = username + ":" + password;
QByteArray data = concatenatedCredentials.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
QUrlQuery pdata;
// Send request and connect all possible signals
QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//QNetworkReply*reply = networkManager->get( request );
QEventLoop loop;
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.exec();
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status_code == 200){
this->username = username;
this->password = password;
if(permanent){
qDebug() << "permanent";
pGlobalAppSettings->writeSetting("permanent", "1");
pGlobalAppSettings->writeSetting("username", username);
pGlobalAppSettings->writeSetting("password", password);
2018-06-21 16:41:02 +02:00
}
return("OK");
}
else if(status_code == 401){
return("Ungültige Benutzerdaten.");
}
else if(status_code == 0){
return("Keine Verbindung zum Server.");
}
else {
QString ret = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toChar();
ret = ret + reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toChar();
return(ret);
}
2018-06-21 08:47:09 +02:00
}
2018-06-21 16:41:02 +02:00
int ServerConn::logout()
{
this->username = "";
this->password = "";
pGlobalAppSettings->writeSetting("permanent", "0");
pGlobalAppSettings->writeSetting("username", "");
pGlobalAppSettings->writeSetting("password", "");
2018-06-21 17:56:36 +02:00
return(200);
2018-06-21 16:41:02 +02:00
}
QString ServerConn::getDay(QString day)
{
2018-06-30 20:59:42 +02:00
this->progress = 0;
2018-06-21 16:41:02 +02:00
// Create request
QNetworkRequest request;
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/" + day + ".pdf" ) );
// Pack in credentials
// Pack in credentials
QString concatenatedCredentials = this->username + ":" + this->password;
QByteArray data = concatenatedCredentials.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
QUrlQuery pdata;
// Send request and connect all possible signals
QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//QNetworkReply*reply = networkManager->get( request );
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(updateProgress(qint64, qint64)));
QEventLoop loop;
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.exec();
this->progress = 1;
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status_code == 200){
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QDir dir;
dir.mkdir(path + "/.fannyapp-tmp");
QFile file(path + "/.fannyapp-tmp/" + day + ".pdf");
file.remove();
file.open(QIODevice::ReadWrite);
file.write(reply->readAll());
file.close();
QUrl url = QUrl::fromLocalFile(path + "/.fannyapp-tmp/" + day + ".pdf");
// QDesktopServices::openUrl(url);
return("OK_" + url.toString());
2018-06-21 16:41:02 +02:00
}
else if(status_code == 401){
return("Ungültige Benutzerdaten.");
}
else if(status_code == 0){
return("Keine Verbindung zum Server.");
}
else {
QString ret = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toChar();
ret = ret + " (" + reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toChar() + ")";
return(ret);
2018-06-21 16:41:02 +02:00
}
}
2018-06-21 16:41:02 +02:00
int ServerConn::checkConn()
{
// Create request
QNetworkRequest request;
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/" ) );
2018-06-21 16:41:02 +02:00
// Pack in credentials
// Pack in credentials
//ZedlerDo:LxyJQB
QString concatenatedCredentials = this->username + ":" + this->password;
QByteArray data = concatenatedCredentials.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
2018-06-21 16:41:02 +02:00
QUrlQuery pdata;
// Send request and connect all possible signals
QNetworkReply*reply = this->refreshNetworkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//QNetworkReply*reply = networkManager->get( request );
2018-06-21 16:41:02 +02:00
QEventLoop loop;
loop.connect(this->refreshNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.exec();
2018-06-21 16:41:02 +02:00
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
return(status_code);
2018-06-21 16:41:02 +02:00
}
2018-06-22 09:36:20 +02:00
void ServerConn::updateProgress(qint64 read, qint64 total)
{
int read_int = read;
int total_int = total;
2018-06-22 15:33:16 +02:00
float percent = ((float)read_int / (float)total_int);
2018-06-22 09:36:20 +02:00
this->progress = percent;
2018-06-22 15:33:16 +02:00
percent = (int)percent;
// qDebug() << read << total << percent << "%";
2018-06-22 09:36:20 +02:00
}
float ServerConn::getProgress()
{
return(this->progress);
}
2018-06-21 16:41:02 +02:00
int ServerConn::getEvents(){
/*
this->progress = 0;
ReturnData_t ret; //this is a custom type to store the returned data
// Call the webservice
QNetworkRequest request(QUrl("http://api.itsblue.de/fanny/vertretung.php?uname=ZedlerDo&passwd=LxyJQB&day=smorgen&agree=true"));
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
//set ssl configuration
//send a POST request with the given url and data to the server
QNetworkReply* reply;
QUrlQuery pdata;
reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(updateProgress(qint64, qint64)));
//wait until the request has finished
QEventLoop loop;
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.exec();
//get the status code
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
this->progress = 1;
if(status_code != 200){
return(status_code.toInt());
}
QString eventString = reply->readAll();
eventString.replace(" - ", "-");
eventString.replace(" / ", "/");
eventString.replace("<pre>", "");
eventString.replace("</pre>", "");
eventString.replace("Entfall für Lehrer", "Entfall_für_Lehrer");
QStringList tmp = eventString.split("\f");
QStringList rawEvents;
QList<QStringList> tmpEvents;
for(int i=0; i<tmp.length(); i++){
QStringList dayEventList = tmp[i].split("\n");
for(int a=0; a<9; a++){
dayEventList.removeAt(0);
}
rawEvents.append(dayEventList);
}
for(int i=0; i<rawEvents.length(); i++){
tmpEvents.append(rawEvents[i].split(" "));
}
tmpEvents.takeLast();
m_eventlist = tmpEvents;
qDebug() << tmpEvents;
*/
qDebug() << "reading xml file";
QFile * xmlFile = new QFile(":/samplehtml/Download File.xml");
if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Load XML File Problem Couldn't open xmlfile.xml to load settings for download";
return 900;
}
QXmlStreamReader * xmlReader = new QXmlStreamReader(xmlFile);
//qDebug() << xmlFile->readAll();
QList<QStringList> tmpEvents;
QStringList dayList;
int currTop = 0;
//Parse the XML until we reach end of it
while(!xmlReader->atEnd()) {
if (xmlReader->readNextStartElement()) {
if (xmlReader->name().toString() == "text"){
QXmlStreamAttributes attributes = xmlReader->attributes();
QString attribute_value;
int top;
if(attributes.hasAttribute("font")){
attribute_value = attributes.value("font").toString();
}
if(attributes.hasAttribute("top")){
top = attributes.value("top").toInt();
if(abs(top - currTop) > 3){
//next line started
if(currTop > 175){
// ignore the header
tmpEvents.append(dayList);
}
dayList.clear();
currTop = top;
}
}
QString text = xmlReader->readElementText(QXmlStreamReader::IncludeChildElements);
dayList.append(text);
qDebug() << qPrintable(xmlReader->name().toString()) << text << attribute_value << dayList;
}
}
}
tmpEvents.takeFirst();
qDebug() << tmpEvents;
this->m_eventlist = tmpEvents;
if(xmlReader->hasError()) {
qDebug() << "xmlFile.xml Parse Error" << xmlReader->errorString();
//return(500);
}
//close reader and flush file
xmlReader->clear();
xmlFile->close();
return(200);
}
2018-06-25 00:15:18 +02:00
int ServerConn::getFoodPlan()
2018-06-22 15:33:16 +02:00
{
2018-06-25 08:07:05 +02:00
this->progress = 0;
2018-06-22 15:33:16 +02:00
ReturnData_t ret; //this is a custom type to store the returned data
// Call the webservice
QNetworkRequest request(QUrl("http://www.treffpunkt-fanny.de/fuer-schueler-und-lehrer/speiseplan.html"));
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
2018-06-22 15:33:16 +02:00
//set ssl configuration
//send a POST request with the given url and data to the server
QNetworkReply* reply;
QUrlQuery pdata;
reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
2018-06-25 00:15:18 +02:00
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(updateProgress(qint64, qint64)));
2018-06-22 15:33:16 +02:00
//wait until the request has finished
QEventLoop loop;
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.exec();
//get the status code
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
2018-06-25 08:07:05 +02:00
this->progress = 1;
2018-06-24 09:23:10 +02:00
if(status_code != 200){
2018-06-25 00:15:18 +02:00
return(status_code.toInt());
2018-06-24 09:23:10 +02:00
}
//initialize the weekplan to store information to it
//m_weekplan.empty(); //empty the weekplan
QList<QList<QString>> temp_weekplan;
2018-06-24 09:23:10 +02: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.
ret.text = QString::fromUtf8(reply->readAll()); //read the whole website
ret.text.replace("\n",""); //remove unnecessary stuff
2018-06-22 15:33:16 +02:00
ret.text.replace("\r","");
ret.text.replace("\t","");
ret.text.replace("style=\"width: 25%;\"", "width=\"25%\"");
2018-06-22 15:33:16 +02:00
2018-06-24 09:23:10 +02:00
QStringList stringlist_0 = ret.text.split( "<table class=\"speiseplan\">" ); //split the
//prepare the table of the first week
2018-06-22 15:33:16 +02:00
QString table1 = stringlist_0[1];
2018-06-24 09:23:10 +02:00
QStringList stringlist_1 = table1.split( "</table>" ); //remove everything after "</table>"
2018-06-22 15:33:16 +02:00
table1 = stringlist_1[0];
2018-06-24 09:23:10 +02:00
table1.remove(0,71); //remove "<tbody><tr style=\"border: 1px solid #999;\" align=\"center\" valign=\"top\">" at the beginning
table1 = table1.left(table1.length() - 13); //remove "</tr></tbody>" at the end
QStringList table1list = table1.split("<td width=\"25%\">"); //split at the days to get a list of all days
2018-06-24 09:23:10 +02:00
table1list.takeFirst(); //remove the first item, as it is empty
2018-06-22 15:33:16 +02:00
2018-06-24 09:23:10 +02:00
//prepare the table of the second week
QString table2 = stringlist_0[2];
2018-06-24 09:23:10 +02:00
QStringList stringlist_2 = table2.split( "</table>" ); //remove everything after "</table>"
table2 = stringlist_2[0];
table2.remove(0,39); //remove "<tbody><tr align=\"center\" valign=\"top\">" at the beginning
table2.remove(table2.length() - 13, table2.length()); //remove "</tr></tbody>" at the end
QStringList table2list = table2.split("<td width=\"25%\">"); //split at the days to get a list of all days
2018-06-24 09:23:10 +02:00
table2list.takeFirst(); //remove the first item, as it is empty
2018-06-22 15:33:16 +02:00
2018-06-24 09:23:10 +02:00
QStringList weeklist = table1list + table2list; //put both weeks into one big list
2018-06-24 09:23:10 +02:00
for (int i = 0; i <=7; i ++){
QString temp = weeklist[i]; //store item temporarly to edit it
weeklist[i] = temp.left(temp.length()-5); //remove "</td>" at the and of the Item
temp = weeklist[i];
//table list[i] looks now like:
//<strong>Red Hot Chili Peppers</strong>
//<br />
//<strong>26.06.2018</strong>
//<hr />Gulasch mit Kartoffeln
//<hr />Pellkartoffeln mit Quark
//<hr />Gemischter Salat
//<hr />Eaton Mess ( Erdbeer-Nachtisch )</td>
QStringList templist = temp.split("<strong>"); //split item at strong, to get the cookteam and the date
//qDebug() << templist << "\n";
temp = "";
for (int i = 0; i <=2; i ++){
2018-06-22 15:33:16 +02:00
temp += templist[i]; //convert the list to a big string
}
2018-06-24 09:23:10 +02:00
temp.replace("<br />","");
templist = temp.split("</strong>");
temp_weekplan.append({templist[0], templist[1]}); //store cookteam and date
temp = templist[2]; //store information in temp (looks like: "<hr />Gulasch mit Kartoffeln<hr />Pellkartoffeln mit Quark<hr />Gemischter Salat<hr />Eaton Mess ( Erdbeer-Nachtisch )")
templist = temp.split("<hr />"); //seperate the information
templist.takeFirst(); //remove first item
2018-06-22 15:33:16 +02:00
temp_weekplan[i].append(templist);
2018-06-22 15:33:16 +02:00
2018-06-24 09:23:10 +02:00
}
2018-06-22 15:33:16 +02:00
2018-06-25 10:04:11 +02:00
//qDebug() << temp_weekplan;
this->m_weekplan = temp_weekplan; //write changes to global foodplan
qDebug() << temp_weekplan;
2018-06-25 00:15:18 +02:00
return(200);
2018-06-24 09:23:10 +02:00
}
2018-06-22 15:33:16 +02:00
2018-06-24 09:23:10 +02:00
QVariantMap ServerConn::getFoodPlanData(int index)
{
//cookteam, date, main dish, vagi main dish, garnish(Beilage) and Dessert.
QStringList ret; //list to return
//qDebug() << index;
2018-06-24 09:23:10 +02:00
for(int i=0;i<=5;i++){
2018-06-24 09:23:10 +02:00
if(m_weekplan.size() > index){
//qDebug() << i << m_weekplan[index].size();
if(m_weekplan[index].size() > i){
2018-06-24 09:23:10 +02:00
ret.append(m_weekplan[index][i]);
//qDebug() << i << m_weekplan[index][i];
2018-06-24 09:23:10 +02:00
}
else {
ret.append(NULL);
}
}
else {
ret.append(NULL);
}
}
QString date_string_on_db = ret[1];
QDate Date = QDate::fromString(date_string_on_db," dd.MM.yyyy");
//date_string_on_db
qDebug() << Date;
qDebug() << ret;
return { {"cookteam", ret[0]}, {"date", Date}, {"main_dish", ret[2]}, {"main_dish_veg", ret[3]}, {"garnish", ret[4]}, {"dessert", ret[5]} };
2018-06-22 15:33:16 +02:00
}
QVariantMap ServerConn::getEventData(int index)
{
//cookteam, date, main dish, vagi main dish, garnish(Beilage) and Dessert.
QStringList ret; //list to return
//qDebug() << index;
for(int i=0;i<=6;i++){
if(m_eventlist.size() > index){
//qDebug() << i << m_weekplan[index].size();
if(m_eventlist[index].size() > i){
ret.append(m_eventlist[index][i]);
//qDebug() << i << m_weekplan[index][i];
}
else {
ret.append(NULL);
}
}
else {
ret.append(NULL);
}
}
if(ret.length() < 7){
ret.append("");
}
QString date_string_on_db = ret[1];
QDate Date = QDate::fromString(date_string_on_db," dd.MM.yyyy");
//date_string_on_db
qDebug() << Date;
qDebug() << ret;
return { {"grade", ret[0]}, {"hour", ret[1]}, {"replace", ret[2]}, {"subject", ret[3]}, {"room", ret[4]}, {"to", ret[5]}, {"text", ret[6]} };
}
int ServerConn::getEventCount(){
return (m_eventlist.length());
}
2018-06-21 08:47:09 +02:00
ReturnData_t ServerConn::senddata(QUrl serviceUrl, QUrlQuery pdata)
{
ReturnData_t ret; //this is a custom type to store the returned data
// Call the webservice
QNetworkRequest request(serviceUrl);
QAuthenticator authenticator;
authenticator.setUser("ZedlerDo");
authenticator.setPassword("LxyJQB");
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
2018-06-21 08:47:09 +02:00
//set ssl configuration
//send a POST request with the given url and data to the server
QNetworkReply* reply;
reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//wait until the request has finished
QEventLoop loop;
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.exec();
//get the status code
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
ret.status_code = status_code.toInt();
if(ret.status_code == 0){ //if tehstatus code is zero, the connecion to the server was not possible
ret.status_code = 444;
}
//get the full text response
ret.text = QString::fromUtf8(reply->readAll());
//return the data
return(ret);
}