jce-manager/main/LoginTab/loginhandler.cpp

174 lines
4.5 KiB
C++
Raw Normal View History

2014-09-08 15:54:52 +00:00
#include "loginhandler.h"
2014-10-12 19:19:43 +00:00
loginHandler::loginHandler(user *ptr, QPushButton *loginButtonPtr, jceStatusBar *progressbarPtr): logggedInFlag(false)
2014-09-08 15:54:52 +00:00
{
this->loginButtonPtr = loginButtonPtr;
2014-10-12 19:19:43 +00:00
this->statusBar = progressbarPtr;
2014-10-04 00:54:27 +00:00
2014-10-12 19:19:43 +00:00
statusBar->setIconConnectionStatus(jceStatusBar::Disconnected);
//user settings
userPtr = ptr;
2014-10-12 19:19:43 +00:00
this->jceLog = new jceLogin(userPtr,statusBar);
QObject::connect(this->jceLog,SIGNAL(connectionReadyAfterDisconnection()),this,SLOT(readyAfterConnectionLost()));
2014-10-04 00:54:27 +00:00
2014-09-08 15:54:52 +00:00
}
bool loginHandler::login(QString username,QString password)
2014-09-08 15:54:52 +00:00
{
qDebug() << Q_FUNC_INFO << "logging in with username and password";
if (isLoggedInFlag())
{
qDebug() << Q_FUNC_INFO << "we are already connected. Logging out and return false";
logout();
return false;
}
userPtr->setUsername(username);
userPtr->setPassword(password);
if (makeConnection() == true)
{
loginButtonPtr->setText(QObject::tr("Logout"));
return isLoggedInFlag();
}
else
{
2014-10-12 22:14:26 +00:00
logout();
return false;
}
}
void loginHandler::logout()
{
loginButtonPtr->setText(QObject::tr("Login"));
2014-10-12 19:19:43 +00:00
statusBar->setIconConnectionStatus(jceStatusBar::Disconnected);
jceLog->closeAll();
logggedInFlag = false;
2014-09-08 15:54:52 +00:00
}
bool loginHandler::makeConnection()
{
if (this->jceLog == NULL)
return false;
int status = (int)jceLog->makeConnection();
switch (status)
{
case jceLogin::JCE_YOU_ARE_IN:
{
logggedInFlag = true;
return logggedInFlag;
}
case jceLogin::ERROR_ON_VALIDATION:
{
popMessage(QObject::tr("Please Check Your Username & Password"),false);
return false;
}
case jceLogin::ERROR_ON_VALIDATION_USER_BLOCKED:
{
popMessage(QObject::tr("You have been <b>BLOCKED</b> by JCE, please try in a couple of minutes."));
return false;
}
case jceLogin::ERROR_ON_OPEN_SOCKET:
{
popMessage(QObject::tr("Please Check Your Internet Connection."));
return false;
}
case jceLogin::JCE_NOT_CONNECTED:
{
break;
}
case jceLogin::ERROR_ON_GETTING_INFO:
{
popMessage(QObject::tr("Receive Request Timeout."));
return false;
}
case jceLogin::ERROR_ON_SEND_REQUEST:
{
popMessage(QObject::tr("Send Request Timeout."));
return false;
}
}
return false;
2014-09-08 15:54:52 +00:00
}
void loginHandler::readyAfterConnectionLost()
{
qWarning() << Q_FUNC_INFO;
setLoginFlag(false);
2014-10-12 22:14:26 +00:00
statusBar->setIconConnectionStatus(jceStatusBar::Disconnected);
login(userPtr->getUsername(),userPtr->getPassword());
2014-09-08 15:54:52 +00:00
}
2014-09-08 15:54:52 +00:00
bool loginHandler::isLoggedInFlag()
{
if (jceLog->isLoginFlag()) //checking connection and then if logged in
return this->logggedInFlag;
else
this->setLoginFlag(false);
return false;
2014-09-08 15:54:52 +00:00
}
void loginHandler::setLoginFlag(bool flag)
{
this->logggedInFlag = flag;
2014-09-08 15:54:52 +00:00
}
QString loginHandler::getCurrentPageContect()
{
QTextEdit parse;
if (isLoggedInFlag())
parse.setText(jceLog->getPage());
else
2014-10-12 02:52:07 +00:00
return "";
return parse.toPlainText();
2014-09-08 15:54:52 +00:00
}
int loginHandler::makeGradeRequest(int fromYear, int toYear, int fromSemester, int toSemester)
{
if (isLoggedInFlag())
2014-10-12 22:14:26 +00:00
{
statusBar->setIconConnectionStatus(jceStatusBar::Sending);
return jceLog->getGrades(fromYear, toYear, fromSemester, toSemester);
2014-10-12 22:14:26 +00:00
}
else
return jceLogin::JCE_NOT_CONNECTED;
2014-09-08 15:54:52 +00:00
}
int loginHandler::makeCalendarRequest(int year, int semester)
{
if (isLoggedInFlag())
2014-10-12 22:14:26 +00:00
{
statusBar->setIconConnectionStatus(jceStatusBar::Sending);
return jceLog->getCalendar(year,semester);
2014-10-12 22:14:26 +00:00
}
else
return jceLogin::JCE_NOT_CONNECTED;
}
int loginHandler::makeExamsScheduleRequest(int year, int semester)
{
2014-10-12 22:14:26 +00:00
if (isLoggedInFlag())
2014-10-12 22:14:26 +00:00
{
statusBar->setIconConnectionStatus(jceStatusBar::Sending);
return jceLog->getExams(year,semester);
2014-10-12 22:14:26 +00:00
}
else
return jceLogin::JCE_NOT_CONNECTED;
}
2014-09-08 15:54:52 +00:00
void loginHandler::popMessage(QString message,bool addInfo)
{
if (addInfo)
message.append(QObject::tr("\nIf this message appear without reason, please contact me at liranbg@gmail.com"));
QMessageBox msgBox;
msgBox.setWindowTitle(QObject::tr("Error"));
msgBox.setText(message);
msgBox.exec();
msgBox.setFocus();
2014-09-08 15:54:52 +00:00
}