575 lines
19 KiB
C++
575 lines
19 KiB
C++
#include "headers/serverconn.h"
|
|
|
|
ServerConn * pGlobalServConn = nullptr;
|
|
|
|
ServerConn::ServerConn(QObject *parent) : QObject(parent)
|
|
{
|
|
qDebug("serverconn konstruktor");
|
|
this->networkManager = new QNetworkAccessManager();
|
|
this->refreshNetworkManager = new QNetworkAccessManager();
|
|
pGlobalServConn = this;
|
|
}
|
|
|
|
ServerConn::~ServerConn()
|
|
{
|
|
qDebug("serverconn destruktor");
|
|
delete this->networkManager;
|
|
delete this->refreshNetworkManager;
|
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
QDir dir(path + "/.fannyapp-tmp");
|
|
dir.removeRecursively();
|
|
}
|
|
|
|
|
|
QString ServerConn::login(QString username, QString password, bool permanent)
|
|
{
|
|
// QUrlQuery pdata;
|
|
// ReturnData_t ret = this->senddata(QUrl("http://www.fanny-leicht.de/static15/http.intern/sheute.pdf"), pdata);
|
|
// qDebug() << ret.text;
|
|
|
|
// 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);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
int ServerConn::logout()
|
|
{
|
|
this->username = "";
|
|
this->password = "";
|
|
pGlobalAppSettings->writeSetting("permanent", "0");
|
|
pGlobalAppSettings->writeSetting("username", "");
|
|
pGlobalAppSettings->writeSetting("password", "");
|
|
return(200);
|
|
}
|
|
|
|
QString ServerConn::getDay(QString day)
|
|
{
|
|
this->progress = 0;
|
|
// 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());
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
int ServerConn::checkConn()
|
|
{
|
|
// Create request
|
|
QNetworkRequest request;
|
|
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/" ) );
|
|
|
|
// 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() );
|
|
|
|
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 );
|
|
|
|
QEventLoop loop;
|
|
loop.connect(this->refreshNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
|
|
loop.exec();
|
|
|
|
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
return(status_code);
|
|
|
|
}
|
|
|
|
void ServerConn::updateProgress(qint64 read, qint64 total)
|
|
{
|
|
int read_int = read;
|
|
int total_int = total;
|
|
float percent = ((float)read_int / (float)total_int);
|
|
this->progress = percent;
|
|
percent = (int)percent;
|
|
|
|
// qDebug() << read << total << percent << "%";
|
|
}
|
|
|
|
float ServerConn::getProgress()
|
|
{
|
|
return(this->progress);
|
|
}
|
|
|
|
int ServerConn::getEvents(QString day){
|
|
|
|
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=" + this->username + "&passwd=" + this->password + "&day=" + day + "&agree=true"));
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
"application/x-www-form-urlencoded");
|
|
|
|
//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();
|
|
|
|
|
|
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(eventString);
|
|
//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);
|
|
}
|
|
|
|
int ServerConn::getFoodPlan()
|
|
{
|
|
this->progress = 0;
|
|
|
|
// 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");
|
|
|
|
// 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;
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
// quit the loop when the timer times out
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
|
|
timer.start(2000);
|
|
loop.exec();
|
|
|
|
// get the status code
|
|
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
this->progress = 1;
|
|
if(status_code != 200){
|
|
return(status_code.toInt());
|
|
}
|
|
|
|
// initialize the weekplan to store information to it
|
|
QList<QList<QString>> temp_weekplan;
|
|
|
|
// 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.
|
|
|
|
// read the whole website
|
|
QString returnedData = QString::fromUtf8(reply->readAll());
|
|
|
|
// remove unnecessary stuff
|
|
returnedData.replace("\n","");
|
|
returnedData.replace("\r","");
|
|
returnedData.replace("\t","");
|
|
|
|
// workaround for changing html syntax
|
|
returnedData.replace("style=\"width: 25%;\"", "width=\"25%\"");
|
|
|
|
// split the string at the beginning of the tables
|
|
QStringList documentList = returnedData.split( "<table class=\"speiseplan\">" );
|
|
|
|
// enshure that the data is valid
|
|
if(documentList.length() < 2){
|
|
return(900);
|
|
}
|
|
|
|
//---------- prepare the table of the first week ----------
|
|
QString table1 = documentList[1];
|
|
|
|
// enshure that the data is valid
|
|
if(table1.split( "</table>" ).length() < 1){
|
|
return(900);
|
|
}
|
|
|
|
// remove everything after "</table>"
|
|
table1 = table1.split( "</table>" )[0];
|
|
// remove "<tbody><tr style=\"border: 1px solid #999;\" align=\"center\" valign=\"top\">" at the beginning
|
|
table1.remove(0,71);
|
|
//remove "</tr></tbody>" at the end
|
|
table1 = table1.left(table1.length() - 13);
|
|
|
|
//split at the days to get a list of all days
|
|
QStringList table1list = table1.split("<td width=\"25%\">");
|
|
|
|
// enshure that the data is valid
|
|
if(table1list.length() < 5){
|
|
return(900);
|
|
}
|
|
|
|
//remove the first item, as it is empty
|
|
table1list.takeFirst();
|
|
|
|
//---------- prepare the table of the second week ----------
|
|
QString table2 = documentList[2];
|
|
|
|
// enshure that the data is valid
|
|
if(table2.split( "</table>" ).length() < 1){
|
|
return(900);
|
|
}
|
|
|
|
//remove everything after "</table>"
|
|
table2 = table2.split( "</table>" )[0];
|
|
//remove "<tbody><tr align=\"center\" valign=\"top\">" at the beginning
|
|
table2.remove(0,39);
|
|
//remove "</tr></tbody>" at the end
|
|
table2.remove(table2.length() - 13, table2.length());
|
|
|
|
//split at the days to get a list of all days
|
|
QStringList table2list = table2.split("<td width=\"25%\">");
|
|
|
|
// enshure that the data is valid
|
|
if(table2list.length() < 5){
|
|
return(900);
|
|
}
|
|
|
|
//remove the first item, as it is empty
|
|
table2list.takeFirst();
|
|
|
|
//---------- put both weeks into one big list ----------
|
|
QStringList weeklist = table1list + table2list;
|
|
|
|
//---------- go through all days and split the day-string into the different types of information ----------
|
|
|
|
for (int i = 0; i <=7; i ++){
|
|
if(i > weeklist.length()){
|
|
// if the loop exceeds the length of the wweklist some kind of eror occured
|
|
return 900;
|
|
}
|
|
|
|
// store item temporarly to edit it
|
|
QString day = weeklist[i];
|
|
// remove "</td>" at the and of the Item
|
|
day = day.left(day.length()-5);
|
|
|
|
// table list[i] looks now like: | clould be:
|
|
// <strong>cookteam</strong> | <strong>Red Hot Chili Peppers</strong>
|
|
// <br /> | <br />
|
|
// <strong>date</strong> | <strong>26.06.2018</strong>
|
|
// <hr />mainDish | <hr />Gulasch mit Kartoffeln
|
|
// <hr />mainDishVeg | <hr />Pellkartoffeln mit Quark
|
|
// <hr />garnish | <hr />Gemischter Salat
|
|
// <hr />dessert</td> | <hr />Eaton Mess ( Erdbeer-Nachtisch )</td>
|
|
|
|
// split item at strong, to get the cookteam and the date
|
|
QStringList daylist = day.split("<strong>");
|
|
day = "";
|
|
|
|
// convert the list to a big string
|
|
for (int i = 0; i <= 2; i ++){
|
|
if(i <= daylist.length()){
|
|
day += daylist[i];
|
|
}
|
|
}
|
|
|
|
day.replace("<br />","");
|
|
daylist = day.split("</strong>");
|
|
|
|
// store cookteam and date in the temp_weekplan
|
|
//temp_weekplan.append({daylist[0], daylist[1]});
|
|
|
|
// store information in day
|
|
// (looks like: "<hr />MainDish<hr />MainDishVeg<hr />Garnish<hr />Dessert")
|
|
// (could be: "<hr />Gulasch mit Kartoffeln<hr />Pellkartoffeln mit Quark<hr />Gemischter Salat<hr />Eaton Mess ( Erdbeer-Nachtisch )")
|
|
day = daylist.takeLast();
|
|
// seperate the information
|
|
daylist.append(day.split("<hr />"));
|
|
// remove the item that is emplty from the list
|
|
daylist.removeAt(2);
|
|
|
|
//---------- check if the day is aleady over ----------
|
|
|
|
// get the datestring
|
|
QString dateString = daylist[1];
|
|
// convert it to a valid QDate
|
|
QDateTime date = QDateTime::fromString(dateString,"dd.MM.yyyy");
|
|
|
|
// get the current date and time
|
|
QDateTime currentDateTime = QDateTime::currentDateTimeUtc();
|
|
|
|
// check if the given day is still in the future or today (then it is valid)
|
|
if(date.toTime_t() > currentDateTime.toTime_t() || date.date() == currentDateTime.date()){
|
|
// add the rest of the information to the temp_weekplan
|
|
qDebug() << "day valid:" << daylist;
|
|
|
|
//---------- convert the date to a readable string ----------
|
|
QString readableDateString;
|
|
|
|
if(date.date() == currentDateTime.date()){
|
|
// the given day is today
|
|
readableDateString = "Heute";
|
|
}
|
|
else if (date.toTime_t() < ( currentDateTime.toTime_t() + ( 24 * 60 * 60 ))) {
|
|
// the given day is tomorrow
|
|
readableDateString = "Morgen";
|
|
}
|
|
else {
|
|
readableDateString = date.toString("dddd, d.M.yy");
|
|
}
|
|
|
|
qDebug() << readableDateString;
|
|
|
|
// insert the redable tring into the daylist
|
|
daylist[1] = readableDateString;
|
|
|
|
// append the day to the weeklist
|
|
temp_weekplan.append({daylist[0], daylist[1], daylist[2], daylist[3], daylist[4], daylist[5]});
|
|
}
|
|
}
|
|
|
|
// write data to global foodplan
|
|
this->m_weekplan = temp_weekplan;
|
|
|
|
// check if there is any valid data
|
|
if(this->m_weekplan.isEmpty()){
|
|
// no data
|
|
return(901);
|
|
}
|
|
|
|
// success
|
|
return(200);
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
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");
|
|
|
|
//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);
|
|
}
|