- finished up event filter

- started to move the login management from qml to cpp
This commit is contained in:
Dorian Zedler 2018-12-25 19:30:42 +01:00
parent 4af8ba030e
commit 4271bec598
10 changed files with 153 additions and 104 deletions

View file

@ -5,6 +5,7 @@
#include <QDir> #include <QDir>
#include <QUrl> #include <QUrl>
#include <QtXml> #include <QtXml>
#include <QTimer>
#include <QtNetwork> #include <QtNetwork>
#include <QAuthenticator> #include <QAuthenticator>
@ -26,16 +27,28 @@ typedef struct strReturnData{
class ServerConn : public QObject class ServerConn : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString state READ getState WRITE setState NOTIFY stateChanged)
public: private:
QString state;
// can be: loggedIn ; notLoggedIn
QString username; QString username;
QString password; QString password;
QNetworkAccessManager *networkManager; QNetworkAccessManager *networkManager;
QNetworkAccessManager *refreshNetworkManager; QNetworkAccessManager *refreshNetworkManager;
QTimer *checkConnTimer;
float progress; float progress;
public: int authErrorCount;
ReturnData_t senddata(QUrl serviceUrl, QUrlQuery postData);
void updateProgress(qint64 read, qint64 total);
void setState(QString state);
public slots:
explicit ServerConn(QObject *parent = nullptr); explicit ServerConn(QObject *parent = nullptr);
~ServerConn(); ~ServerConn();
Q_INVOKABLE int 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);
@ -43,15 +56,16 @@ public:
Q_INVOKABLE float getProgress(); Q_INVOKABLE float getProgress();
Q_INVOKABLE int getFoodPlan(); Q_INVOKABLE int getFoodPlan();
Q_INVOKABLE int getEvents(QString day); Q_INVOKABLE int getEvents(QString day);
ReturnData_t senddata(QUrl serviceUrl, QUrlQuery postData);
Q_INVOKABLE QString getState();
signals:
void stateChanged();
public:
QList<QList<QString>> m_weekplan; QList<QList<QString>> m_weekplan;
QList<QStringList> m_events; QList<QStringList> m_events;
signals:
public slots:
Q_INVOKABLE void updateProgress(qint64 read, qint64 total);
}; };
extern ServerConn * pGlobalServConn; extern ServerConn * pGlobalServConn;

View file

@ -5,16 +5,18 @@ Item {
id: infoArea id: infoArea
property int alertLevel: app.getErrorInfo(infoArea.errorCode)[0] property int alertLevel: app.getErrorInfo(infoArea.errorCode)[0]
// 0 - ok
// 1 - info
// 2 - error
property int errorCode: -1 property int errorCode: -1
property var excludedCodes: []
visible: !(excludedCodes.indexOf(errorCode) >= 0)
height: childrenRect.height height: childrenRect.height
Rectangle { Rectangle {
// 0 - ok
// 1 - info
// 2 - error
radius: height * 0.5 radius: height * 0.5
width: parent.width width: parent.width
height: width height: width
@ -45,7 +47,14 @@ Item {
top: parent.bottom top: parent.bottom
margins: parent.height * 0.1 margins: parent.height * 0.1
} }
font.pixelSize: errorLongDescription.font.pixelSize * 2
width: app.width * 0.8
wrapMode: Label.Wrap
horizontalAlignment: Label.AlignHCenter
font.pixelSize: errorLongDescription.font.pixelSize * 1.8
text: app.getErrorInfo(infoArea.errorCode)[1] text: app.getErrorInfo(infoArea.errorCode)[1]
} }
@ -57,7 +66,7 @@ Item {
margins: parent.height * 0.1 margins: parent.height * 0.1
} }
width: parent.width width: app.width * 0.8
wrapMode: Label.Wrap wrapMode: Label.Wrap

View file

@ -35,7 +35,7 @@ ItemDelegate {
leftMargin: 10 leftMargin: 10
} }
width: parent.width - 10 - forwardIcon.width - 10 width: parent.width * 0.9
wrapMode: Label.Wrap wrapMode: Label.Wrap

View file

@ -14,8 +14,6 @@ Page {
onOpened: { onOpened: {
console.log("Filter Form opened") console.log("Filter Form opened")
var filters = _cppAppSettings.readFiltersQml()
} }
Dialog { Dialog {
@ -104,8 +102,14 @@ Page {
id: delegate id: delegate
width: contactView.width width: contactView.width
height: 0
Component.onCompleted: {
delegate.height = 50
}
text: grade + classLetter text: grade + classLetter
font.pixelSize: height * 0.4
Rectangle { Rectangle {
@ -121,6 +125,29 @@ Page {
color: "lightgrey" color: "lightgrey"
} }
Behavior on height {
NumberAnimation {
duration: 500
easing.type: Easing.InOutQuad
}
}
NumberAnimation {
id: deleteAnimation
target: delegate
property: "height"
duration: 500
from: delegate.height
to: 0
easing.type: Easing.InOutQuad
onRunningChanged: {
if(!running){
contactView.model.remove(index)
}
}
}
Button { Button {
id: deleteButton id: deleteButton
@ -136,7 +163,7 @@ Page {
scale: pressed ? 0.8:1 scale: pressed ? 0.8:1
onClicked: { onClicked: {
contactView.model.remove(index) deleteAnimation.start()
} }
background: Image { background: Image {
@ -185,38 +212,4 @@ Page {
text: "+" text: "+"
} }
} }
/*
ListView {
function getModel() {
// convert the filter list into an object
var keys = Object.keys(_cppAppSettings.readFiltersQml());
// get he lenght
var len = keys.length
// return the lenght
return(len)
}
function getDetails(index) {
var ret = _cppAppSettings.readFiltersQml()[index]
var details = ret.split("|")
return(details)
}
function refresh() {
filterList.model = filterList.getModel()
}
id: filterList
anchors.fill: parent
property string title: qsTr("connections")
property int delegateHeight: height*0.18
model: getModel()
delegate: ItemDelegate {
width: parent.width
height: filterList.delegateHeight
text: filterList.getDetails(index)[0] + filterList.getDetails(index)[1]
}
}
*/
} }

View file

@ -75,6 +75,7 @@ Page {
topMargin: parent.height/2 - height * 0.8 topMargin: parent.height/2 - height * 0.8
} }
excludedCodes: [200, 902]
errorCode: status errorCode: status
} }
} }

View file

@ -1,6 +1,8 @@
import QtQuick 2.9 import QtQuick 2.9
import QtQuick.Controls 2.2 import QtQuick.Controls 2.2
import Backend 1.0
import "./Components" import "./Components"
import "./Forms" import "./Forms"
import "./Pages" import "./Pages"
@ -21,7 +23,7 @@ ApplicationWindow {
property bool initdone: false property bool initdone: false
property bool firstinitdone: false property bool firstinitdone: false
property string text_color: "black" //"#424753" property string text_color: "black" // "#424753"
property string background_color: "white" property string background_color: "white"
property string toolbar_color: "#312f38" property string toolbar_color: "#312f38"
@ -45,6 +47,10 @@ ApplicationWindow {
} }
] ]
ServerConn {
id: serverConn
}
// onBeforeRendering: { // onBeforeRendering: {
// if(!firstinitdone){ // if(!firstinitdone){
// var isinit = _cppAppSettings.loadSetting("permanent") // var isinit = _cppAppSettings.loadSetting("permanent")
@ -137,15 +143,8 @@ ApplicationWindow {
} }
function handleError(error_code){ function handleError(error_code){
if(error_code === 200){ if(error_code === 401){
app.is_error = false;
app.error = "";
}
else if(error_code === 401){
console.warn("401") console.warn("401")
_cppAppSettings.writeSetting("permanent", 0)
_cppAppSettings.writeSetting("username", "")
_cppAppSettings.writeSetting("password", "")
if(app.state !== "notLoggedIn"){ if(app.state !== "notLoggedIn"){
console.log("switching to login page") console.log("switching to login page")
app.state = "notLoggedIn" app.state = "notLoggedIn"
@ -153,31 +152,15 @@ ApplicationWindow {
app.is_error = true; app.is_error = true;
app.error = "Nicht angemeldet!!"; app.error = "Nicht angemeldet!!";
} }
else if(error_code === 500){
app.is_error = true;
app.error = "Interner Server Fehler!";
}
else if(error_code === 0){
app.is_error = true;
app.error = "Keine Verbindung zum Server!";
}
else if(error_code === 404){
//the testcon function calls a non existent file to be fast, so no error here
app.is_error = false;
}
else if(error_code === 111){
app.is_error = true;
app.error = "Unbekannter interner Fehler!";
}
else {
app.is_error = true;
app.error = "Unbekannter Fehler! ("+error_code+")";
}
} }
function getErrorInfo(errorCode) { function getErrorInfo(errorCode) {
var infoLevel var infoLevel
// 0 - ok
// 1 - info
// 2 - error
var errorString var errorString
var errorDescription var errorDescription
@ -207,6 +190,11 @@ ApplicationWindow {
errorString = "Keine Daten" errorString = "Keine Daten"
errorDescription = "Es liegen keine aktuellen Daten vor." errorDescription = "Es liegen keine aktuellen Daten vor."
break break
case 902:
infoLevel = 1
errorString = "Alte Daten"
errorDescription = "Es konnte keine Verbindung zum Server hergestellt werden, aber es sind noch alte Daten gespeichert."
break
default: default:
infoLevel = 2 infoLevel = 2
errorString = "Unerwarteter Fehler ("+errorCode+")" errorString = "Unerwarteter Fehler ("+errorCode+")"

View file

@ -1,4 +1,4 @@
5b 1 Hei D --- Entfall b5b 1 Hei D --- Entfall
5b 2 Fu D 114 Hei / Aufgaben, Arbeitsheft! 5b 2 Fu D 114 Hei / Aufgaben, Arbeitsheft!
5b 3 Oe M 114 Ei / Aufgaben 5b 3 Oe M 114 Ei / Aufgaben
5b 4 Rm M 114 Ei / Aufgaben 5b 4 Rm M 114 Ei / Aufgaben

View file

@ -7,6 +7,8 @@ AppSettings::AppSettings(QObject* parent)
{ {
qDebug("+----- AppSettings konstruktor -----"); qDebug("+----- AppSettings konstruktor -----");
pGlobalAppSettings = this;
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << "+----- Settings Path:" << path; qDebug() << "+----- Settings Path:" << path;

View file

@ -22,10 +22,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
AppSettings * pAppSettings = new AppSettings(); AppSettings * pAppSettings = new AppSettings();
pGlobalAppSettings = pAppSettings; // ServerConn * pServerConn = new ServerConn();
ServerConn * pServerConn = new ServerConn();
//pGlobalServConn = pServerConn;
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
@ -34,13 +31,14 @@ int main(int argc, char *argv[])
qmlRegisterType<FoodPlanModel>("Backend", 1, 0, "FoodPlanModel"); qmlRegisterType<FoodPlanModel>("Backend", 1, 0, "FoodPlanModel");
qmlRegisterType<EventModel>("Backend", 1, 0, "EventModel"); qmlRegisterType<EventModel>("Backend", 1, 0, "EventModel");
qmlRegisterType<FilterModel>("Backend", 1, 0, "FilterModel"); qmlRegisterType<FilterModel>("Backend", 1, 0, "FilterModel");
qmlRegisterType<ServerConn>("Backend", 1, 0, "ServerConn");
QQuickStyle::setStyle("Material"); QQuickStyle::setStyle("Material");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlContext *context = engine.rootContext(); QQmlContext *context = engine.rootContext();
context->setContextProperty("_cppServerConn", pServerConn); // context->setContextProperty("_cppServerConn", pServerConn);
context->setContextProperty("_cppAppSettings", pAppSettings); context->setContextProperty("_cppAppSettings", pAppSettings);
if (engine.rootObjects().isEmpty()) if (engine.rootObjects().isEmpty())
return -1; return -1;
@ -48,7 +46,7 @@ int main(int argc, char *argv[])
int ret; int ret;
ret = app.exec(); ret = app.exec();
delete pServerConn; // delete pServerConn;
delete pAppSettings; delete pAppSettings;
return(ret); return(ret);
} }

View file

@ -5,20 +5,17 @@ ServerConn * pGlobalServConn = nullptr;
ServerConn::ServerConn(QObject *parent) : QObject(parent) ServerConn::ServerConn(QObject *parent) : QObject(parent)
{ {
qDebug("serverconn konstruktor"); qDebug("serverconn konstruktor");
pGlobalServConn = this;
this->networkManager = new QNetworkAccessManager(); this->networkManager = new QNetworkAccessManager();
this->refreshNetworkManager = new QNetworkAccessManager(); this->refreshNetworkManager = new QNetworkAccessManager();
pGlobalServConn = this;
}
ServerConn::~ServerConn() this->checkConnTimer = new QTimer();
{ this->checkConnTimer->setInterval(1000);
qDebug("serverconn destruktor"); this->checkConnTimer->setSingleShot(true);
delete this->networkManager; this->checkConnTimer->start();
delete this->refreshNetworkManager;
connect(this->checkConnTimer, &QTimer::timeout, this, &ServerConn::checkConn);
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QDir dir(path + "/.fannyapp-tmp");
dir.removeRecursively();
} }
int ServerConn::login(QString username, QString password, bool permanent) int ServerConn::login(QString username, QString password, bool permanent)
@ -170,11 +167,25 @@ int ServerConn::checkConn()
QNetworkReply*reply = this->refreshNetworkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); QNetworkReply*reply = this->refreshNetworkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//QNetworkReply*reply = networkManager->get( request ); //QNetworkReply*reply = networkManager->get( request );
QTimer timer;
timer.start(3000);
QEventLoop loop; QEventLoop loop;
loop.connect(this->refreshNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); loop.connect(this->refreshNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
loop.connect(&timer, SLOT(timeout()), SLOT(quit()));
loop.exec(); loop.exec();
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status_code == 401){
authErrorCount ++;
if(authErrorCount > 3){
logout();
}
}
this->checkConnTimer->start();
return(status_code); return(status_code);
} }
@ -314,14 +325,20 @@ int ServerConn::getEvents(QString day){
} }
// if the event matches the filter // if the event matches the filter
foreach(QStringList filter, pGlobalAppSettings->readFilters()){ QList<QStringList> filtersList = pGlobalAppSettings->readFilters();
// go through all filters and check if one of them matches the event if(filtersList.isEmpty()){
tmpEvents.append(dayList);
}
else {
foreach(QStringList filter, filtersList){
// go through all filters and check if one of them matches the event
if(dayList[0].contains(filter[0]) && dayList[0].contains(filter[1])){ if((dayList[0].contains(filter[0]) && dayList[0].contains(filter[1]))){
// append the dayList to the temporary event list // append the dayList to the temporary event list
tmpEvents.append(dayList); tmpEvents.append(dayList);
// terminate the loop // terminate the loop
break; break;
}
} }
} }
} }
@ -492,6 +509,13 @@ int ServerConn::getFoodPlan()
this->progress = 1; this->progress = 1;
if(status_code != 200){ if(status_code != 200){
// if the request didn't result in a success, return the error code // 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)
status_code = 902;
}
return(status_code.toInt()); return(status_code.toInt());
} }
@ -712,3 +736,23 @@ ReturnData_t ServerConn::senddata(QUrl serviceUrl, QUrlQuery pdata)
//return the data //return the data
return(ret); return(ret);
} }
QString ServerConn::getState() {
return(this->state);
}
void ServerConn::setState(QString state) {
this->state = state;
}
ServerConn::~ServerConn()
{
qDebug("serverconn destruktor");
delete this->networkManager;
delete this->refreshNetworkManager;
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QDir dir(path + "/.fannyapp-tmp");
dir.removeRecursively();
}