From 320f0cc1d11f904ccbcc6605f73f48b822bd13bc Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Tue, 17 Jun 2014 22:15:38 +0300 Subject: [PATCH] Added SavedData Class under /src/data/ passed all my testing! commiting only this class with no changes within the rest of the project! --- jceGrade.pro | 4 +- src/data/savedata.cpp | 200 ++++++++++++++++++++++++++++++++++++++++++ src/data/savedata.h | 54 ++++++++++++ 3 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 src/data/savedata.cpp create mode 100644 src/data/savedata.h diff --git a/jceGrade.pro b/jceGrade.pro index 36ac194..7843e80 100644 --- a/jceGrade.pro +++ b/jceGrade.pro @@ -22,7 +22,8 @@ SOURCES += main/coursestablemanager.cpp \ src/grades/GradePage.cpp \ src/grades/Page.cpp \ src/jce/jcedate.cpp \ - src/jce/jcelogin.cpp + src/jce/jcelogin.cpp \ + src/data/savedata.cpp @@ -37,6 +38,7 @@ HEADERS += main/coursestablemanager.h \ src/jce/jcedate.h \ src/jce/jcelogin.h \ src/jce/jceLoginHtmlScripts.h \ + src/data/savedata.h diff --git a/src/data/savedata.cpp b/src/data/savedata.cpp new file mode 100644 index 0000000..89900da --- /dev/null +++ b/src/data/savedata.cpp @@ -0,0 +1,200 @@ +#include "savedata.h" + + + +/** + * @brief Checks the status tag in the file + * @return boolean the status + */ +bool SaveData::isSaved() +{ + QString status; + QFile* file = new QFile(FILE_NAME); + /* Opening file for read */ + if(file->open(QIODevice::ReadOnly|QIODevice::Text)) + { + status = getValViaTag("status", file); + if(status == "true") + { + file->close(); //close & return + return true; + } + return false; //close & return + file->close(); + } + /* If Faild to open */ + std::cout << "Faild To Accsess file: " << FILE_NAME << std::endl; + return false; +} + + +/** + * @brief This Function will modify the file, and save the data. + * @param username - QString + * @param password - QString + * @return true if saved - false if error + */ +bool SaveData::save(QString username, QString password) +{ + std::cout << "Trying to save data..." << std::endl; + QFile file(FILE_NAME); + if(!file.open(QIODevice::WriteOnly|QIODevice::Text)) + return false; /* IO Error! */ + QTextStream output(&file); + QString hash_pass = hashPassword(password); + output << "[status]true[/]\n[username]"<open(QIODevice::ReadOnly|QIODevice::Text)) + username = getValViaTag("username", file); + file->close(); + return username; + +} + +/** + * @brief getter for password + * @return QString - password + */ +QString SaveData::getPassword() +{ + QString pass = ""; + QFile* file = new QFile(FILE_NAME); + /* Opening file for read */ + if(file->open(QIODevice::ReadOnly|QIODevice::Text)) + pass = getValViaTag("password", file); + file->close(); + pass = deHashPasword(pass); + return pass; + +} + +/** + * @brief This is A functions that will init the tags file. + * if it exist - do nothing + * if there is no file, it will create it and run the deleteData function + * so tags will be set. + */ +void SaveData::init() +{ + QFile file(FILE_NAME); + if(!file.exists()) + { + file.open(QIODevice::ReadWrite | QIODevice::Text); + file.close(); + deleteData(); + } +} + +/** + * @brief This function will return the vale of a given tag name. + * NOTE: valid tag names are : "status", "username", "password" + * if tag is invalid - will return "" - an empty QString! + * @param tag - QString, the tag name + * @param file - a QFile pointer + * @return QString - the value in tag + * NOTE: if NULL value, or an invalid tag name -> return "" (empty QString!) + */ +QString SaveData::getValViaTag(QString tag, QFile* file) +{ + QString val, line , tmpTag; + QTextStream textStream( file); + while((line = textStream.readLine()) != NULL) + { + std::cout << "DEBUG: line => " << line.toStdString() << std::endl; + for(int i = 0 ; i< line.length() ;++i) + { + if(line[i] == '[' && line[i+1] != '/') //get open tag at begining of line and not end of tag ("[/") + { + i++; + tmpTag = line.mid(i, tag.length()); + if(tmpTag == tag) + { + i+= tag.length()+1; // i is now right after '[' + int j = i; + while(line[j] != '[')// put j at the end of the value + j++; + /* Then... the value is :*/ + val = line.mid(i, j-i); + std::cout << "DEBUG: ["< +#include +#include + +/* C/C++ libs */ +#include +#include +#include + +#define FILE_NAME "JAC_DB.dat" +#define DEFAULT_DATA_EMPTY "[status]false[/]\n[username][/]\n[password][/]" + +class SaveData +{ +public: + bool static isSaved(); + bool static save(QString username, QString password); + bool static deleteData(); + QString static getUsername(); + QString static getPassword(); + void static init(); +private: + QString static getValViaTag(QString tag, QFile *file); + QString static hashPassword(QString pass); + QString static deHashPasword(QString pass); +}; + +#endif // SAVEDATA_H