- commented code

- added settingsForm basic grade filter for the events
This commit is contained in:
Dorian Zedler 2018-12-25 00:15:25 +01:00
parent 7f127b0feb
commit 6ad57a4661
9 changed files with 309 additions and 157 deletions

View file

@ -36,7 +36,7 @@ public:
public: public:
explicit ServerConn(QObject *parent = nullptr); explicit ServerConn(QObject *parent = nullptr);
~ServerConn(); ~ServerConn();
Q_INVOKABLE QString login(QString username, QString password, bool permanent); Q_INVOKABLE int login(QString username, QString password, bool permanent);
Q_INVOKABLE int logout(); Q_INVOKABLE int logout();
Q_INVOKABLE QString getDay(QString day); Q_INVOKABLE QString getDay(QString day);
Q_INVOKABLE int checkConn(); Q_INVOKABLE int checkConn();

View file

@ -21,9 +21,6 @@ Page {
verticalCenter: parent.verticalCenter verticalCenter: parent.verticalCenter
} }
// width: childrenRect.width
// height: childrenRect.height
property int buttonHeight: app.landscape() ? root.height * (0.5*0.8):buttonWidth property int buttonHeight: app.landscape() ? root.height * (0.5*0.8):buttonWidth
property int buttonWidth: app.landscape() ? buttonHeight:root.width * (0.5*0.8) property int buttonWidth: app.landscape() ? buttonHeight:root.width * (0.5*0.8)
@ -144,6 +141,10 @@ Page {
width: smallMenu.buttonWidth width: smallMenu.buttonWidth
height: smallMenu.buttonHeight height: smallMenu.buttonHeight
onClicked: {
formStack.push(settingsForm)
}
} }
FancyButton { FancyButton {
@ -177,11 +178,10 @@ Page {
} }
onAccepted: { onAccepted: {
_cppServerConn.logout() _cppServerConn.logout()
app.state = "notLoggedInu" app.state = "notLoggedIn"
} }
} }
} }
} }
} }
} }

View file

@ -0,0 +1,75 @@
import QtQuick 2.0
import QtQuick.Controls 2.4
Page {
id: root
title: "Einstellungen"
signal opened()
onOpened: {
console.log("Home Form opened")
}
Column {
id: settingsCol
anchors.fill: parent
ItemDelegate {
width: parent.width
height: 10 + shortDescription.height + 2 + longDescription.height + 10
Label {
id: shortDescription
anchors {
top: parent.top
left: parent.left
margins: 10
}
font.pixelSize: longDescription.font.pixelSize * 1.4
text: "Klassenstufe"
}
Label {
id: longDescription
anchors {
top: shortDescription.bottom
topMargin: 2
left: parent.left
leftMargin: 10
}
width: parent.width - 10 - gradeSelect.width
wrapMode: Label.Wrap
text: "Wähle deine Klassenstufe aus, um den Vertretungsplan zu sortieren"
}
SpinBox {
id: gradeSelect
anchors {
verticalCenter: parent.verticalCenter
right: parent.right
}
from: 5
to: 12
stepSize: 1
value: _cppAppSettings.loadSetting("grade")
onValueChanged: {
_cppAppSettings.writeSetting("grade", value)
}
}
}
}
}

View file

@ -7,10 +7,6 @@ Page {
id: root id: root
objectName: "LoginPage"; objectName: "LoginPage";
onHeightChanged: {
console.log(height)
}
header: AppToolBar { header: AppToolBar {
Label { Label {
text: "Anmeldung" text: "Anmeldung"
@ -210,31 +206,43 @@ Page {
} }
function login(username, password, permanent){ function login(username, password, permanent){
// hide the keyboard
Qt.inputMethod.hide(); Qt.inputMethod.hide();
// open the busy dialog
busyDialog.open() busyDialog.open()
// disable the login button
loginButton.enabled = false loginButton.enabled = false
loginButton.text = "Loggin in.." // change the text to "Anmelden.."
loginButton.text = "Anmelden.."
console.log(username, password, permanent) console.log(username, password, permanent)
// trigger the login fucntion of the cpp backend and store the return code
var ret = _cppServerConn.login(username, password, permanent); var ret = _cppServerConn.login(username, password, permanent);
// the request has finished
// close the busy dialog
busyDialog.close() busyDialog.close()
// enable the button
loginButton.enabled = true loginButton.enabled = true
loginButton.text = "Login" // change the text of the login button back to "Anmelden"
if(ret === "OK"){ loginButton.text = "Anmelden"
// chekc if the login was successfull
if(ret === 200){
// if it was -> set the app to inited and set the state of the app to loggedIn
_cppAppSettings.writeSetting("init", 1); _cppAppSettings.writeSetting("init", 1);
app.is_error = false; app.is_error = false;
app.state = "loggedIn" app.state = "loggedIn"
} }
else{ else{
if(_cppAppSettings.loadSetting("permanent") === "1"){ // if it wasn't -> reset the stored credentinals
tiuname.text = _cppAppSettings.loadSetting("username") _cppAppSettings.writeSetting("permanent", "0")
tipasswd.text = _cppAppSettings.loadSetting("password") _cppAppSettings.writeSetting("username", "")
_cppAppSettings.writeSetting("permanent", "0") _cppAppSettings.writeSetting("password", "")
_cppAppSettings.writeSetting("username", "") // and set the error label to the error short description of the retuned error code
_cppAppSettings.writeSetting("password", "") laStatus.text = app.getErrorInfo(ret)[1]
}
laStatus.text = ret
} }
//root.qmlSignal(tiuname.text, tipasswd.text)
} }
Dialog { Dialog {
@ -242,14 +250,14 @@ Page {
modal: true modal: true
closePolicy: "NoAutoClose" closePolicy: "NoAutoClose"
focus: true focus: true
title: "Please wait..." title: "Bitte warten..."
x: (app.width - width) / 2 x: (app.width - width) / 2
y: window.height / 6 y: (app.height - height) / 2
width: Math.min(window.width, window.height) / 3 * 2 width: Math.min(window.width, window.height) / 3 * 2
height: 150 height: 150
contentHeight: aboutColumn.height contentHeight: contentColumn.height
Column { Column {
id: aboutColumn id: contentColumn
spacing: 20 spacing: 20
RowLayout { RowLayout {
width: parent.width width: parent.width
@ -262,14 +270,11 @@ Page {
Label { Label {
width: busyDialog.availableWidth width: busyDialog.availableWidth
text: "logging in..." text: "Anmelden..."
wrapMode: Label.Wrap wrapMode: Label.Wrap
font.pixelSize: 12 font.pixelSize: 12
} }
} }
} }
} }
} }

View file

@ -56,6 +56,11 @@ Page {
} }
} }
Component {
id: settingsForm
SettingsForm {}
}
popEnter: Transition { popEnter: Transition {
XAnimator { XAnimator {
from: (formStack.mirrored ? -1 : 1) * -formStack.width from: (formStack.mirrored ? -1 : 1) * -formStack.width

View file

@ -88,7 +88,7 @@ ApplicationWindow {
else { else {
ret = _cppServerConn.checkConn() ret = _cppServerConn.checkConn()
handleError(ret) app.handleError(ret)
} }
} }
else { else {
@ -187,6 +187,11 @@ ApplicationWindow {
errorString = "Keine Verbindung zum Server" errorString = "Keine Verbindung zum Server"
errorDescription = "Bitte überprüfe deine Internetverbindung und versuche es erneut." errorDescription = "Bitte überprüfe deine Internetverbindung und versuche es erneut."
break break
case 401:
infoLevel = 2
errorString = "Ungültige Zugangsdaten"
errorDescription = "Der Server hat den Zugang verweigert, bitte überprüfe deine Zugangsdaten und versuche es erneut"
break
case 500: case 500:
infoLevel = 2 infoLevel = 2
errorString = "Interner Server Fehler" errorString = "Interner Server Fehler"

View file

@ -13,5 +13,6 @@
<file>Forms/LoadingForm.qml</file> <file>Forms/LoadingForm.qml</file>
<file>Components/EventView.qml</file> <file>Components/EventView.qml</file>
<file>Components/InfoArea.qml</file> <file>Components/InfoArea.qml</file>
<file>Forms/SettingsForm.qml</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -13,6 +13,9 @@ AppSettings::AppSettings(QObject* parent)
if(loadSetting("init") == "false"){ if(loadSetting("init") == "false"){
this->writeSetting("init", 0); this->writeSetting("init", 0);
} }
if(loadSetting("grade") == "false"){
this->writeSetting("grade", 5);
}
} }
QString AppSettings::loadSetting(const QString &key) QString AppSettings::loadSetting(const QString &key)

View file

@ -21,63 +21,81 @@ ServerConn::~ServerConn()
dir.removeRecursively(); dir.removeRecursively();
} }
QString ServerConn::login(QString username, QString password, bool permanent) int ServerConn::login(QString username, QString password, bool permanent)
{ {
// QUrlQuery pdata; // QUrlQuery pdata;
// ReturnData_t ret = this->senddata(QUrl("http://www.fanny-leicht.de/static15/http.intern/sheute.pdf"), pdata); // ReturnData_t ret = this->senddata(QUrl("http://www.fanny-leicht.de/static15/http.intern/sheute.pdf"), pdata);
// qDebug() << ret.text; // qDebug() << ret.text;
// Create request // Create network request
QNetworkRequest request; QNetworkRequest request;
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/sheute.pdf" ) ); // call a non-existent file to be fast
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/logintest" ) );
// Pack in credentials // pack the credentials into a string
QString concatenatedCredentials = username + ":" + password; QString credentialsString = username + ":" + password;
QByteArray data = concatenatedCredentials.toLocal8Bit().toBase64(); // convert it to a byte array
QByteArray data = credentialsString.toLocal8Bit().toBase64();
// put it into a string
QString headerData = "Basic " + data; QString headerData = "Basic " + data;
// and finally write it into the request header
request.setRawHeader( "Authorization", headerData.toLocal8Bit() ); request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
QUrlQuery pdata; //QUrlQuery pdata;
// Send request and connect all possible signals // Send request and connect all possible signals
QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); //QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//QNetworkReply*reply = networkManager->get( request ); QNetworkReply*reply = networkManager->get( request );
// loop to wait until the request has finished before processing the data
QEventLoop loop; QEventLoop loop;
// timer to cancel the request after 3 seconds
QTimer timer;
timer.setSingleShot(true);
// quit the loop when the request finised
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
// or the timer timed out
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
// start the timer
timer.start(2000);
// start the loop
loop.exec(); loop.exec();
// get the status code from the request
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status_code == 200){ // 404 is a success because a non existent file was called and not 401 was returned -> user data was correct
if(status_code == 404){
// store username and password in the class variables
this->username = username; this->username = username;
this->password = password; this->password = password;
if(permanent){ if(permanent){
qDebug() << "permanent"; // if the user wants to say logged in, store the username and password to the settings file
pGlobalAppSettings->writeSetting("permanent", "1"); pGlobalAppSettings->writeSetting("permanent", "1");
pGlobalAppSettings->writeSetting("username", username); pGlobalAppSettings->writeSetting("username", username);
pGlobalAppSettings->writeSetting("password", password); pGlobalAppSettings->writeSetting("password", password);
} }
return("OK");
} // return success
else if(status_code == 401){ return(200);
return("Ungültige Benutzerdaten.");
}
else if(status_code == 0){
return("Keine Verbindung zum Server.");
} }
else { else {
QString ret = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toChar(); // if not 404 was returned -> error -> return the return code
ret = ret + reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toChar(); return(status_code);
return(ret);
} }
} }
int ServerConn::logout() int ServerConn::logout()
{ {
// reset the data stored in the class
this->username = ""; this->username = "";
this->password = ""; this->password = "";
// reset the data stored in the settings
pGlobalAppSettings->writeSetting("permanent", "0"); pGlobalAppSettings->writeSetting("permanent", "0");
pGlobalAppSettings->writeSetting("username", ""); pGlobalAppSettings->writeSetting("username", "");
pGlobalAppSettings->writeSetting("password", ""); pGlobalAppSettings->writeSetting("password", "");
// return success
return(200); return(200);
} }
@ -143,8 +161,7 @@ int ServerConn::checkConn()
request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/" ) ); request.setUrl( QUrl( "http://www.fanny-leicht.de/static15/http.intern/" ) );
// Pack in credentials // Pack in credentials
// Pack in credentials // e.g. ZedlerDo:LxyJQB (yes, these do actually work ;)
//ZedlerDo:LxyJQB
QString concatenatedCredentials = this->username + ":" + this->password; QString concatenatedCredentials = this->username + ":" + this->password;
QByteArray data = concatenatedCredentials.toLocal8Bit().toBase64(); QByteArray data = concatenatedCredentials.toLocal8Bit().toBase64();
QString headerData = "Basic " + data; QString headerData = "Basic " + data;
@ -161,16 +178,15 @@ int ServerConn::checkConn()
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
return(status_code); return(status_code);
} }
void ServerConn::updateProgress(qint64 read, qint64 total) void ServerConn::updateProgress(qint64 read, qint64 total)
{ {
int read_int = read; int read_int = int(read);
int total_int = total; int total_int = int(total);
float percent = ((float)read_int / (float)total_int); float percent = (float(read_int) / float(total_int));
this->progress = percent; this->progress = percent;
percent = (int)percent; percent = int(percent);
// qDebug() << read << total << percent << "%"; // qDebug() << read << total << percent << "%";
} }
@ -222,70 +238,117 @@ int ServerConn::getEvents(QString day){
} }
QString eventString = textFile->readAll(); QString eventString = textFile->readAll();
// separate all lines into a list
QStringList events = eventString.split("\n"); QStringList events = eventString.split("\n");
// a line (one element of the list) looks like this:
// class hour replace subject room to text
// 8a 1-2 Ei Ch --- Entfall KEINE KA
// [ ] <-- at least two spaces
// to OR text can be blank
// temporary list to store the events for the given day
QList<QStringList> tmpEvents; QList<QStringList> tmpEvents;
// 8a 1-2 Ei Ch --- Entfall KEINE KA // go through the list and process every single row
foreach(QString event, events){ foreach(QString event, events){
// value to count spaces between text
int spaceCount = 0; int spaceCount = 0;
// temporary list to store the data of one day
QStringList dayList; QStringList dayList;
// temporary dtring to store the data of one block
QString tmpString; QString tmpString;
// processing works like:
// go through the line char by char
for(int i = 0;i < event.length(); i++){ for(int i = 0;i < event.length(); i++){
// store the char temporarly
QCharRef tmpChar = event[i]; QCharRef tmpChar = event[i];
// check if the char is a space
if(tmpChar == " "){ if(tmpChar == " "){
// if so, increase the spaceCount by one
spaceCount ++; spaceCount ++;
} }
else { else {
// if not -> new block or part of a block started
// could be : 8a 1 - 2 OR 8a 1 - 2
// here->| OR here->|
// in the first case, the space counter is higer than one
// in the second case, the space counter is exactly one
if(spaceCount == 1){ if(spaceCount == 1){
// -> append a space to the temp string
tmpString.append(" "); tmpString.append(" ");
} }
// reset the space counter
spaceCount = 0; spaceCount = 0;
// append the current character
tmpString.append(tmpChar); tmpString.append(tmpChar);
} }
// check if the soace count is 2
if(spaceCount == 2){ if(spaceCount == 2){
// if so -> break between two blocks
// could be: 8a 1 - 2 OR 8a 1 - 2
// here ->|
// -> append the current tmpString to the dayList
dayList.append(tmpString); dayList.append(tmpString);
// and clear the tmpString
tmpString = ""; tmpString = "";
} }
qDebug() << "char= " << tmpChar << " string= " << tmpString << " list= " << dayList; qDebug() << "char= " << tmpChar << " string= " << tmpString << " list= " << dayList;
} }
// append the remaining tmpString to the dayList
dayList.append(tmpString); dayList.append(tmpString);
// fill up the dayList with blanks until it reaches the defined length
while (dayList.length() < 7) { while (dayList.length() < 7) {
dayList.append(""); dayList.append("");
} }
tmpEvents.append(dayList); // if the event matches the filter
if(dayList[0].contains(pGlobalAppSettings->loadSetting("grade"))){
// append the dayList to the temporary event list
tmpEvents.append(dayList);
}
} }
// store the new events into the class variable
this->m_events = tmpEvents; this->m_events = tmpEvents;
qDebug() << tmpEvents; qDebug() << tmpEvents;
/* // return success
qDebug() << "reading xml file"; return(200);
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);
QXmlStreamReader * xmlReader = new QXmlStreamReader(xmlFile);
//qDebug() << xmlFile->readAll();
QList<QStringList> tmpEvents;
QStringList dayList;
while (dayList.length() < 7) {
dayList.append("");
}
int currTop = 0;
/* /*
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);
QXmlStreamReader * xmlReader = new QXmlStreamReader(xmlFile);
//qDebug() << xmlFile->readAll();
QList<QStringList> tmpEvents;
QStringList dayList;
while (dayList.length() < 7) {
dayList.append("");
}
int currTop = 0;
<text top="530" left="3" width="20" height="17" font="1">8a</text> <text top="530" left="3" width="20" height="17" font="1">8a</text>
<text top="530" left="55" width="36" height="17" font="1">1 - 2</text> <text top="530" left="55" width="36" height="17" font="1">1 - 2</text>
<text top="530" left="123" width="16" height="17" font="4"><i>Ei</i></text> <text top="530" left="123" width="16" height="17" font="4"><i>Ei</i></text>
@ -294,101 +357,96 @@ int ServerConn::getEvents(QString day){
<text top="530" left="275" width="50" height="17" font="1">Entfall</text> <text top="530" left="275" width="50" height="17" font="1">Entfall</text>
<text top="530" left="391" width="83" height="17" font="1">KEINE KA</text> <text top="530" left="391" width="83" height="17" font="1">KEINE KA</text>
<text top="194" left="3" width="62" height="17" font="1">5abc 1</text> <text top="194" left="3" width="62" height="17" font="1">5abc 1</text>
<text top="194" left="102" width="27" height="17" font="4"><i>We</i></text> <text top="194" left="102" width="27" height="17" font="4"><i>We</i></text>
<text top="194" left="157" width="25" height="17" font="4"><i>KR</i></text> <text top="194" left="157" width="25" height="17" font="4"><i>KR</i></text>
<text top="194" left="209" width="18" height="17" font="1">---</text> <text top="194" left="209" width="18" height="17" font="1">---</text>
<text top="194" left="251" width="50" height="17" font="1">Entfall</text> <text top="194" left="251" width="50" height="17" font="1">Entfall</text>
#define eventXmlPosGrade 3 #define eventXmlPosGrade 3
#define eventXmlPosHour 55 #define eventXmlPosHour 55
#define eventXmlPosReplace 123 #define eventXmlPosReplace 123
#define eventXmlPosSubject 178 #define eventXmlPosSubject 178
#define eventXmlPosRoom 233 #define eventXmlPosRoom 233
#define eventXmlPosTo 275 #define eventXmlPosTo 275
#define eventXmlPosText 391 #define eventXmlPosText 391
QList<int> eventXmlPositions = {eventXmlPosGrade,eventXmlPosHour,eventXmlPosReplace,eventXmlPosSubject,eventXmlPosRoom,eventXmlPosTo,eventXmlPosText}; QList<int> eventXmlPositions = {eventXmlPosGrade,eventXmlPosHour,eventXmlPosReplace,eventXmlPosSubject,eventXmlPosRoom,eventXmlPosTo,eventXmlPosText};
qDebug() << "start xml parsing"; qDebug() << "start xml parsing";
//Parse the XML until we reach end of it //Parse the XML until we reach end of it
while(!xmlReader->atEnd()) { while(!xmlReader->atEnd()) {
if (xmlReader->readNextStartElement()) { if (xmlReader->readNextStartElement()) {
// read next element // read next element
if (xmlReader->name().toString() == "text"){ if (xmlReader->name().toString() == "text"){
// text element found // text element found
QXmlStreamAttributes attributes = xmlReader->attributes(); QXmlStreamAttributes attributes = xmlReader->attributes();
QString attribute_value; QString attribute_value;
int top; int top;
if(attributes.hasAttribute("font")){ if(attributes.hasAttribute("font")){
attribute_value = attributes.value("font").toString(); attribute_value = attributes.value("font").toString();
}
if(attributes.hasAttribute("top")){
// get the y-Position of the text
top = attributes.value("top").toInt();
if(abs(top - currTop) > 3){
// new line started
if(currTop > 175){
// if not header -> append
qDebug() << dayList;
tmpEvents.append(dayList);
}
dayList.clear();
while (dayList.length() < 7) {
dayList.append("");
}
currTop = top;
} }
if(attributes.hasAttribute("left")){ if(attributes.hasAttribute("top")){
int left = attributes.value("left").toInt(); // get the y-Position of the text
QString text = xmlReader->readElementText(QXmlStreamReader::IncludeChildElements);
qDebug() << text; top = attributes.value("top").toInt();
for(int i = 0;i<7;i++){
//qDebug() << i << left << abs(left - eventXmlPositions[i]); if(abs(top - currTop) > 3){
if(abs(left - eventXmlPositions[i]) < 30){ // new line started
dayList[i] = text;
//qDebug() << i << left << text << dayList << left << abs(left - eventXmlPositions[i]); if(currTop > 175){
// if not header -> append
qDebug() << dayList;
tmpEvents.append(dayList);
}
dayList.clear();
while (dayList.length() < 7) {
dayList.append("");
}
currTop = top;
}
if(attributes.hasAttribute("left")){
int left = attributes.value("left").toInt();
QString text = xmlReader->readElementText(QXmlStreamReader::IncludeChildElements);
qDebug() << text;
for(int i = 0;i<7;i++){
//qDebug() << i << left << abs(left - eventXmlPositions[i]);
if(abs(left - eventXmlPositions[i]) < 30){
dayList[i] = text;
//qDebug() << i << left << text << dayList << left << abs(left - eventXmlPositions[i]);
}
} }
} }
}
else {
qDebug() << " no left";
}
}
else { else {
qDebug() << " no left"; qDebug() << " no top";
} }
}
else {
qDebug() << " no top";
} }
} }
} }
}
this->m_events = tmpEvents; this->m_events = tmpEvents;
if(xmlReader->hasError()) { if(xmlReader->hasError()) {
qDebug() << "xmlFile.xml Parse Error" << xmlReader->errorString(); qDebug() << "xmlFile.xml Parse Error" << xmlReader->errorString();
//return(900); //return(900);
} }
//close reader and flush file //close reader and flush file
xmlReader->clear(); xmlReader->clear();
xmlFile->close(); xmlFile->close();
*/ */
foreach(QList<QString>day, this->m_events){
qDebug() << day;
}
return(200);
} }
int ServerConn::getFoodPlan() int ServerConn::getFoodPlan()