jce-manager/src/appDatabase/savedata.cpp

121 lines
2.1 KiB
C++
Raw Normal View History

2014-06-18 23:44:46 +00:00
#include "savedata.h"
/**
* @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.
2014-06-18 23:44:46 +00:00
*/
SaveData::SaveData()
2014-06-18 23:44:46 +00:00
{
QFile file(FILE_NAME);
if(!file.exists())
2014-06-18 23:44:46 +00:00
{
file.open(QIODevice::ReadWrite | QIODevice::Text);
file.close();
createDB();
save();
2014-06-18 23:44:46 +00:00
}
else
load();
2014-06-18 23:44:46 +00:00
}
bool SaveData::isSaved()
2014-06-18 23:44:46 +00:00
{
if(DB.value("username") == "")
return false;
2014-06-18 23:44:46 +00:00
return true;
}
void SaveData::reset()
2014-08-31 17:51:27 +00:00
{
DB.clear();
createDB();
save();
2014-08-31 17:51:27 +00:00
}
void SaveData::setUsername(QString username)
2014-06-18 23:44:46 +00:00
{
DB.insert("username", username);
save();
2014-06-18 23:44:46 +00:00
}
void SaveData::setPassword(QString password)
2014-06-18 23:44:46 +00:00
{
DB.insert("password", encrypt(password));
save();
2014-06-18 23:44:46 +00:00
}
void SaveData::setCal(QString cal)
2014-06-18 23:44:46 +00:00
{
DB.insert("calendar", cal);
save();
2014-06-18 23:44:46 +00:00
}
void SaveData::setLocal(QString local)
2014-08-31 17:51:27 +00:00
{
DB.insert("local", local);
save();
2014-08-31 17:51:27 +00:00
}
QString SaveData::getUsername()
2014-08-31 17:51:27 +00:00
{
return DB.value("username");
2014-08-31 17:51:27 +00:00
}
QString SaveData::getPassword()
2014-06-18 23:44:46 +00:00
{
return decrypte(DB.value("password"));
2014-06-18 23:44:46 +00:00
}
QString SaveData::getLocal()
2014-06-18 23:44:46 +00:00
{
return DB.value("local");
2014-06-18 23:44:46 +00:00
}
QString SaveData::getCal()
2014-06-18 23:44:46 +00:00
{
return DB.value("calendar");
2014-06-18 23:44:46 +00:00
}
void SaveData::load()
2014-06-18 23:44:46 +00:00
{
QFile file(FILE_NAME);
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> DB;
file.close();
2014-06-18 23:44:46 +00:00
}
2014-08-31 17:51:27 +00:00
void SaveData::save()
2014-08-31 17:51:27 +00:00
{
QFile file(FILE_NAME);
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << DB;
file.flush();
file.close();
}
void SaveData::createDB()
{
DB.insert("username", "");
DB.insert("password", "");
DB.insert("local", "default");
DB.insert("calendar", "");
2014-08-31 17:51:27 +00:00
}
QString SaveData::encrypt(QString pass)
{
SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f027));
return crypto.encryptToString(pass);
}
QString SaveData::decrypte(QString pass)
{
SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f027));
return crypto.decryptToString(pass);
}