From 1520f25b2ab9c4224ee5e905edcaec20f03fe487 Mon Sep 17 00:00:00 2001 From: liranbg Date: Sat, 18 Oct 2014 07:15:42 +0300 Subject: [PATCH] update std funtions to qt's --- src/jceData/page.cpp | 227 ++++++++++++------------ src/jceSettings/jcelogin.cpp | 329 +++++++++++++++++------------------ 2 files changed, 276 insertions(+), 280 deletions(-) diff --git a/src/jceData/page.cpp b/src/jceData/page.cpp index 2d9b455..8e6a2a8 100644 --- a/src/jceData/page.cpp +++ b/src/jceData/page.cpp @@ -8,14 +8,14 @@ Page::Page() { dateHeader = "";} */ QString Page::getString(QString &htmlToParse) { - makeText(htmlToParse); - return this->text; + makeText(htmlToParse); + return this->text; } void Page::makeText(QString &html) { - int index = 0; - index = html.indexOf("",0); //set index into the place where the data is - manageTableContent(html, index); + int index = 0; + index = html.indexOf("",0); //set index into the place where the data is + manageTableContent(html, index); } /** * @brief Page::manageTableContent strip html, make it string @@ -24,153 +24,150 @@ void Page::makeText(QString &html) */ void Page::manageTableContent(QString &html, int index) { - if (index == -1) - return; - QString temp; - for (int i = index; i < html.length(); i++) + if (index == -1) + return; + QString temp; + for (int i = index; i < html.length(); i++) { - if (html.at(i) == '<') + if (html.at(i) == '<') { - // / / - QString endofTable = ""; - QString tableTag = html.mid(i, 4); //legth of "tr/td" - if (tableTag == "") + // / / + QString endofTable = ""; + QString tableTag = html.mid(i, 4); //legth of "tr/td" + if (tableTag == "") { - if (!dateHeader.isEmpty()) - temp += dateHeader; - i = stitchText(html, temp, i+4); - if (i == -1) //EOF - break; - - } - else if (tableTag == " new line - i+=5; - } - else if (tableTag == "" || tableTag == "") - { - if (!dateHeader.isEmpty()) - temp += "\t"; // new cell -> tab between data - if (html.mid(i, 6) == " new line + i+=5; + } + else if (tableTag == "" || tableTag == "") + { + if (!dateHeader.isEmpty()) + temp += "\t"; // new cell -> tab between data + if (html.mid(i, 6) == "") //for gpa. year & semester title + { + break; + } + else if ((html.at(i) == '>') && (html.mid(i+4,3) != "")) //for calendar. day and hours + { + i += 1; //lenght of > + break; + } + i++; + } + i = stitchText(html, temp, i); temp += "\t"; - - if (i == -1) //EOF + } + if (html.mid(i,(endofTable).length()) == endofTable) //is end of table + { break; } - else if (tableTag == "") //for gpa. year & semester title - { - break; - } - else if ((html.at(i) == '>') && (html.mid(i+4,3) != "")) //for calendar. day and hours - { - i += 1; //lenght of > - break; - } - i++; - } - i = stitchText(html, temp, i); - temp += "\t"; - } - if (html.mid(i,(endofTable).length()) == endofTable) //is end of table - { - break; - } } } - this->text = temp; + this->text = temp; } int Page::stitchText(QString &from, QString &to, int index) { - if (from.mid(index,3) == "") + if (from.mid(index,3) == "") { - QString bTag = from.mid(index, 3); - QString dateline = from.mid(index,from.indexOf("",index+4)-index); - QString temp; - QString date; - char* tok; - int i = 0; - char* textToTok = strdup(dateline.toStdString().c_str()); - tok = strtok(textToTok,"<> :"); - while (tok != NULL) + QString bTag = from.mid(index, 3); + QString dateline = from.mid(index,from.indexOf("",index+4)-index); + QString temp; + QString date; + QStringList holder = dateline.split("<> :"); + QStringList::iterator iterator; + int i = 0; + for (iterator = holder.begin(); iterator != holder.end(); ++iterator) { - if (i == 1) + temp = (*iterator); + if (i == 0) { - temp = tok; - date += temp + "\t"; + date += temp + "\t"; } - else if (i == 3) + else if (i == 3) { - temp = tok; - date += temp; + date += temp; } - i++; - tok = strtok(NULL, "<> :"); + i++; } - dateHeader = date; - if (bTag != "") - return index-1; //go back one step - for the main function to inc i - index += dateline.length(); + dateHeader = date; + if (bTag != "") + return index-1; //go back one step - for the main function to inc i + index += dateline.length(); } - while (from.at(index) != '<' && index < (int)from.length()) + while (from.at(index) != '<' && index < (int)from.length()) { - if (from[index] == '&') + if (from[index] == '&') { - //  - QString nbspChr = from.mid(index, 6); - if (nbspChr == " ") + //  + QString nbspChr = from.mid(index, 6); + if (nbspChr == " ") { - index += 5; - from.replace(index,1,' '); + index += 5; + from.replace(index,1,' '); } } - if (endOfString(index,(int) from.length())) - return -1; //EOF + if (endOfString(index,(int) from.length())) + return -1; //EOF - else if (from.at(index) == '<') - return index - 1; //go back one step - for the main function to inc i + else if (from.at(index) == '<') + return index - 1; //go back one step - for the main function to inc i - if ((from.at(index) != '\n') && (from.at(index) != '\t')) //check the actuall data before continue - to += from.at(index); - index++; + if ((from.at(index) != '\n') && (from.at(index) != '\t')) //check the actuall data before continue + to += from.at(index); + index++; } - return index-1; + return index-1; } bool Page::endOfString(int index, int length) { - if(index < length) - return false; - return true; + if(index < length) + return false; + return true; } diff --git a/src/jceSettings/jcelogin.cpp b/src/jceSettings/jcelogin.cpp index 59118d1..dc7552b 100644 --- a/src/jceSettings/jcelogin.cpp +++ b/src/jceSettings/jcelogin.cpp @@ -6,21 +6,21 @@ */ jceLogin::jceLogin(user* username, jceStatusBar *statusBar) { - this->statusBar = statusBar; - this->recieverPage = new QString(); - this->jceA = username; - this->JceConnector = new jceSSLClient(statusBar); - QObject::connect(JceConnector,SIGNAL(serverDisconnectedbyRemote()),this,SLOT(reValidation())); - QObject::connect(JceConnector,SIGNAL(noInternetLink()),this,SLOT(reMakeConnection())); + this->statusBar = statusBar; + this->recieverPage = new QString(); + this->jceA = username; + this->JceConnector = new jceSSLClient(statusBar); + QObject::connect(JceConnector,SIGNAL(serverDisconnectedbyRemote()),this,SLOT(reValidation())); + QObject::connect(JceConnector,SIGNAL(noInternetLink()),this,SLOT(reMakeConnection())); } jceLogin::~jceLogin() { - this->jceA = NULL; - delete recieverPage; - delete JceConnector; - JceConnector = NULL; - recieverPage = NULL; + this->jceA = NULL; + delete recieverPage; + delete JceConnector; + JceConnector = NULL; + recieverPage = NULL; } /** * @brief jceLogin::makeConnection Connecting to JCE student web site with JceA (username object) and validate it. @@ -28,79 +28,79 @@ jceLogin::~jceLogin() */ int jceLogin::makeConnection() { - qDebug() << Q_FUNC_INFO << "connection to be make"; + qDebug() << Q_FUNC_INFO << "connection to be make"; - if (this->recieverPage == NULL) - this->recieverPage = new QString(); + if (this->recieverPage == NULL) + this->recieverPage = new QString(); - int returnMode; //gets status according to called function of validation step - jceStatus status = jceStatus::JCE_NOT_CONNECTED; + int returnMode; //gets status according to called function of validation step + jceStatus status = jceStatus::JCE_NOT_CONNECTED; - returnMode = checkConnection(); //checking socket status. is connected? + returnMode = checkConnection(); //checking socket status. is connected? - if (returnMode == (int)false) + if (returnMode == (int)false) { - statusBar->setIconConnectionStatus(jceStatusBar::Connecting); - if (JceConnector->makeConnect(dst_host,dst_port) == false) //couldnt make a connection - return jceStatus::ERROR_ON_OPEN_SOCKET; - else - returnMode = true; + statusBar->setIconConnectionStatus(jceStatusBar::Connecting); + if (JceConnector->makeConnect(dst_host,dst_port) == false) //couldnt make a connection + return jceStatus::ERROR_ON_OPEN_SOCKET; + else + returnMode = true; } - if (returnMode == (int)true) //connected to host + if (returnMode == (int)true) //connected to host { - statusBar->setIconConnectionStatus(jceStatusBar::Connected); - returnMode = makeFirstVisit(); - if (returnMode == (int)true) //requst and send first validation + statusBar->setIconConnectionStatus(jceStatusBar::Connected); + returnMode = makeFirstVisit(); + if (returnMode == (int)true) //requst and send first validation { - status = jceStatus::JCE_START_VALIDATING_PROGRESS; - returnMode = checkValidation(); - if (returnMode == (int)true) //check if username and password are matching + // status = jceStatus::JCE_START_VALIDATING_PROGRESS; + returnMode = checkValidation(); + if (returnMode == (int)true) //check if username and password are matching { - status = jceStatus::JCE_VALIDATION_PASSED; - returnMode = makeSecondVisit(); - if (returnMode == (int)true) //siging in the website + // status = jceStatus::JCE_VALIDATION_PASSED; + returnMode = makeSecondVisit(); + if (returnMode == (int)true) //siging in the website { - qDebug() << Q_FUNC_INFO << "Signed in succeesfully"; - status = jceStatus::JCE_YOU_ARE_IN; - setLoginFlag(true); - statusBar->setIconConnectionStatus(jceStatusBar::LoggedIn); + qDebug() << Q_FUNC_INFO << "Signed in succeesfully"; + status = jceStatus::JCE_YOU_ARE_IN; + setLoginFlag(true); + statusBar->setIconConnectionStatus(jceStatusBar::LoggedIn); } - else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) + else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) { - status = jceLogin::ERROR_ON_GETTING_INFO; + status = jceLogin::ERROR_ON_GETTING_INFO; } - else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) + else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) { - status = jceLogin::ERROR_ON_SEND_REQUEST; + status = jceLogin::ERROR_ON_SEND_REQUEST; } - else - status = jceStatus::ERROR_ON_VALIDATION; + else + status = jceStatus::ERROR_ON_VALIDATION; } - else - status = jceStatus::ERROR_ON_VALIDATION; + else + status = jceStatus::ERROR_ON_VALIDATION; } - else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) + else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) { - status = jceLogin::ERROR_ON_GETTING_INFO; + status = jceLogin::ERROR_ON_GETTING_INFO; } - else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) + else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) { - status = jceLogin::ERROR_ON_SEND_REQUEST; + status = jceLogin::ERROR_ON_SEND_REQUEST; } - else - status = jceStatus::ERROR_ON_VALIDATION_USER_BLOCKED; + else + status = jceStatus::ERROR_ON_VALIDATION_USER_BLOCKED; } - else - status = jceStatus::JCE_NOT_CONNECTED; + else + status = jceStatus::JCE_NOT_CONNECTED; - if (status != jceStatus::JCE_YOU_ARE_IN) - statusBar->setIconConnectionStatus(jceStatusBar::Error); - //we throw status even if we are IN! - qDebug() << Q_FUNC_INFO << "return status: " << status; - return status; + if (status != jceStatus::JCE_YOU_ARE_IN) + statusBar->setIconConnectionStatus(jceStatusBar::Error); + //we throw status even if we are IN! + qDebug() << Q_FUNC_INFO << "return status: " << status; + return status; } /** @@ -109,10 +109,10 @@ int jceLogin::makeConnection() */ bool jceLogin::checkConnection() const { - if (JceConnector->isConnected()) - return true; + if (JceConnector->isConnected()) + return true; - return false; + return false; } /** * @brief jceLogin::closeAll @@ -120,11 +120,11 @@ bool jceLogin::checkConnection() const void jceLogin::closeAll() { statusBar->setIconConnectionStatus(jceStatusBar::Disconnected); - this->JceConnector->makeDiconnect(); - if ((this->recieverPage != NULL) && (!this->recieverPage->isEmpty())) + this->JceConnector->makeDiconnect(); + if ((this->recieverPage != NULL) && (!this->recieverPage->isEmpty())) { - delete recieverPage; - recieverPage = NULL; + delete recieverPage; + recieverPage = NULL; } } @@ -133,17 +133,17 @@ void jceLogin::closeAll() */ void jceLogin::reMakeConnection() { - if (this->JceConnector != NULL) - delete JceConnector; - if (this->recieverPage != NULL) - delete recieverPage; - recieverPage = NULL; - JceConnector = NULL; - this->recieverPage = new QString(); - this->JceConnector = new jceSSLClient(statusBar); - QObject::connect(JceConnector,SIGNAL(serverDisconnectedbyRemote()),this,SLOT(reValidation())); - QObject::connect(JceConnector,SIGNAL(noInternetLink()),this,SLOT(reMakeConnection())); - emit connectionReadyAfterDisconnection(); + if (this->JceConnector != NULL) + delete JceConnector; + if (this->recieverPage != NULL) + delete recieverPage; + recieverPage = NULL; + JceConnector = NULL; + this->recieverPage = new QString(); + this->JceConnector = new jceSSLClient(statusBar); + QObject::connect(JceConnector,SIGNAL(serverDisconnectedbyRemote()),this,SLOT(reValidation())); + QObject::connect(JceConnector,SIGNAL(noInternetLink()),this,SLOT(reMakeConnection())); + emit connectionReadyAfterDisconnection(); } /** @@ -152,15 +152,15 @@ void jceLogin::reMakeConnection() */ int jceLogin::makeFirstVisit() { - if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA)))) + if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA)))) { - if (!JceConnector->recieveData(recieverPage)) - return jceLogin::ERROR_ON_GETTING_INFO; + if (!JceConnector->recieveData(recieverPage)) + return jceLogin::ERROR_ON_GETTING_INFO; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** * @brief jceLogin::makeSecondVisit making the second validation step of jce student portal login @@ -168,19 +168,19 @@ int jceLogin::makeFirstVisit() */ int jceLogin::makeSecondVisit() { - QString usrid=jceA->getUserID(); - QString pswid=jceA->getHashedPassword(); - if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA))))) + QString usrid=jceA->getUserID(); + QString pswid=jceA->getHashedPassword(); + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA))))) { - if (!(JceConnector->recieveData(recieverPage))) - return jceLogin::ERROR_ON_GETTING_INFO; + if (!(JceConnector->recieveData(recieverPage))) + return jceLogin::ERROR_ON_GETTING_INFO; - return true; + return true; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** * @brief jceLogin::getCalendar according to parameters, we make an HTML request and send it over socket to server @@ -190,30 +190,30 @@ int jceLogin::makeSecondVisit() */ int jceLogin::getCalendar(int year, int semester) { - if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getCalendar(*jceA,year,semester))))) + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getCalendar(*jceA,year,semester))))) { - if (!(JceConnector->recieveData(recieverPage))) - return jceLogin::ERROR_ON_GETTING_PAGE; - else - return jceLogin::JCE_PAGE_PASSED; + if (!(JceConnector->recieveData(recieverPage))) + return jceLogin::ERROR_ON_GETTING_PAGE; + else + return jceLogin::JCE_PAGE_PASSED; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } int jceLogin::getExams(int year, int semester) { if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getExamSchedule(*jceA,year,semester))))) - { + { if (!(JceConnector->recieveData(recieverPage))) - return jceLogin::ERROR_ON_GETTING_PAGE; + return jceLogin::ERROR_ON_GETTING_PAGE; else - return jceLogin::JCE_PAGE_PASSED; - } + return jceLogin::JCE_PAGE_PASSED; + } else - return jceLogin::ERROR_ON_SEND_REQUEST; + return jceLogin::ERROR_ON_SEND_REQUEST; return true; } @@ -227,17 +227,17 @@ int jceLogin::getExams(int year, int semester) */ int jceLogin::getGrades(int fromYear, int toYear, int fromSemester, int toSemester) { - if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA,fromYear, toYear, fromSemester, toSemester))))) + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA,fromYear, toYear, fromSemester, toSemester))))) { - if (!(JceConnector->recieveData(recieverPage))) - return jceLogin::ERROR_ON_GETTING_PAGE; - else - return jceLogin::JCE_PAGE_PASSED; + if (!(JceConnector->recieveData(recieverPage))) + return jceLogin::ERROR_ON_GETTING_PAGE; + else + return jceLogin::JCE_PAGE_PASSED; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** @@ -247,43 +247,42 @@ int jceLogin::getGrades(int fromYear, int toYear, int fromSemester, int toSemest bool jceLogin::checkValidation() { - //finds the hashed password - QString constUserID_TAG = "value=\"-N"; - QString constHassID_TAG = "-A,-N"; - QString hasspass,hassid; - std::size_t hasspass_position1,hasspass_position2; - std::size_t id_position1,id_position2; + //finds the hashed password + QString constUserID_TAG = "value=\"-N"; + QString constHassID_TAG = "-A,-N"; + QString hasspass,hassid; + int hasspass_position1,hasspass_position2; + int id_position1,id_position2; + hasspass_position1 = this->recieverPage->indexOf(constHassID_TAG); //looking for hasspass index + if (hasspass_position1 == -1) //didnt find the tag + return false; + else + hasspass_position1 += constHassID_TAG.length(); //skip the index of tag + hasspass_position2 = this->recieverPage->indexOf(",-A,-A", hasspass_position1); + //finds the hass pass + if (hasspass_position2 != -1) //found the hasspass! storing it + hasspass = recieverPage->mid(hasspass_position1,hasspass_position2-hasspass_position1); + else + return false; + //finds the user id + id_position1 = this->recieverPage->indexOf(constUserID_TAG, 0); //looking for hassid index + if (id_position1 == -1) //didnt find the tag + return false; + else + id_position1 += constUserID_TAG.length(); //skip the index of tag + id_position2 = this->recieverPage->indexOf(",-A", id_position1); + if (id_position2 != -1) //found the hassid! storing it + hassid = recieverPage->mid(id_position1,id_position2-id_position1); + else + return false; - hasspass_position1 = this->recieverPage->toStdString().find(constHassID_TAG.toStdString()); //looking for hasspass index - if (hasspass_position1 == std::string::npos) //didnt find the tag - return false; - else - hasspass_position1 += constHassID_TAG.length(); //skip the index of tag - hasspass_position2 = this->recieverPage->toStdString().find(",-A,-A", hasspass_position1); - //finds the hass pass - if (hasspass_position2 != std::string::npos) //found the hasspass! storing it - hasspass = recieverPage->mid(hasspass_position1,hasspass_position2-hasspass_position1); - else - return false; - //finds the user id - id_position1 = this->recieverPage->toStdString().find(constUserID_TAG.toStdString(), 0); //looking for hassid index - if (id_position1 == std::string::npos) //didnt find the tag - return false; - else - id_position1 += constUserID_TAG.length(); //skip the index of tag - id_position2 = this->recieverPage->toStdString().find(",-A", id_position1); - if (id_position2 != std::string::npos) //found the hassid! storing it - hassid = recieverPage->mid(id_position1,id_position2-id_position1); - else - return false; + //setting user information with given data hassid and hasspass + jceA->setHashedPassword(hasspass); + jceA->setUserID(hassid); - //setting user information with given data hassid and hasspass - jceA->setHashedPassword(hasspass); - jceA->setUserID(hassid); + qDebug() << "jceLogin::checkValidation(); Found Hashed: " << hasspass << "And ID: " << hassid; - qDebug() << "jceLogin::checkValidation(); Found Hashed: " << hasspass << "And ID: " << hassid; - - return true; + return true; } /** * @brief jceLogin::setLoginFlag @@ -291,7 +290,7 @@ bool jceLogin::checkValidation() */ void jceLogin::setLoginFlag(bool x) { - this->loginFlag = x; + this->loginFlag = x; } /** * @brief jceLogin::isLoginFlag checking if there is a connection, if true - > return if we signed in. otherwise, return not (not connected dough) @@ -299,9 +298,9 @@ void jceLogin::setLoginFlag(bool x) */ bool jceLogin::isLoginFlag() const { - if (checkConnection()) - return this->loginFlag; - return false; + if (checkConnection()) + return this->loginFlag; + return false; } /** @@ -310,30 +309,30 @@ bool jceLogin::isLoginFlag() const */ QString jceLogin::getPage() { - return *recieverPage; + return *recieverPage; } void jceLogin::reValidation() { - qDebug() << Q_FUNC_INFO << "Revalidating user"; + qDebug() << Q_FUNC_INFO << "Revalidating user"; - if (makeFirstVisit() == (int)true) + if (makeFirstVisit() == (int)true) { - if (checkValidation()) + if (checkValidation()) { - if (makeSecondVisit() == (int)true) - { - statusBar->setIconConnectionStatus(jceStatusBar::LoggedIn); - qDebug() << Q_FUNC_INFO << "Validated"; - } - else - qWarning() << Q_FUNC_INFO << "Second visit finished with an error"; + if (makeSecondVisit() == (int)true) + { + statusBar->setIconConnectionStatus(jceStatusBar::LoggedIn); + qDebug() << Q_FUNC_INFO << "Validated"; + } + else + qWarning() << Q_FUNC_INFO << "Second visit finished with an error"; } - else - qDebug() << Q_FUNC_INFO << "checking validation ended with an error"; + else + qDebug() << Q_FUNC_INFO << "checking validation ended with an error"; } - else + else { - qDebug() << Q_FUNC_INFO << "Couldnt Validate User"; + qDebug() << Q_FUNC_INFO << "Couldnt Validate User"; } }