1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <vector>
#include "User.hpp"
#include "ManageUsers.hpp"
void saveIntoFile(std::vector<User>& d, const char* file){
std::ofstream ofile(file, std::ios::out);
assert(ofile.good());
boost::archive::xml_oarchive ar(ofile);
std::cout << "before serialization" << std::endl;
ar << boost::serialization::make_nvp("Users", d);
std::cout << "after serialization" << std::endl;
ofile.close();
}
void getFromFile(std::vector<User>& d, const char* file){
std::ifstream ifile(file, std::ios::in);
assert(ifile.good());
boost::archive::xml_iarchive ar(ifile);
ar >> boost::serialization::make_nvp("Users", d);
ifile.close();
}
ManageUsers::ManageUsers(){
this->users.clear();
}
bool ManageUsers::checkLogin(User& u){
int pos = posInVector(u.getLogin());
if (pos == -1){
if (this->users.empty())
u.setId(0);
else
u.setId(this->users.back().getId() + 1);
return true;
}
return false;
}
bool ManageUsers::addUsers(User u){
if (checkLogin(u) == true){
if (u.getLogin().empty() == false && u.getLogin().empty() == false){
this->users.push_back(u);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
return false;
}
bool ManageUsers::login(std::string login, std::string password){
int pos = posInVector(login);
if (pos != -1){
if (this->users[pos].getPassword() == password)
return true;
}
return false;
}
bool ManageUsers::removeUsers(std::string login) {
int pos = posInVector(login);
if (pos != -1){
this->users[pos].setExist(false);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
bool ManageUsers::modifierInfo(User u) {
int pos = posInVector(u.getLogin());
if (u.getFirstname().empty() == false)
this->users[pos].setFirstname(u.getFirstname());
if (u.getLastname().empty() == false)
this->users[pos].setLastname(u.getLastname());
if (u.getPassword().empty() == false)
this->users[pos].setPassword(u.getPassword());
if (u.getAvatar().empty() == false)
this->users[pos].setAvatar(u.getAvatar());
if (u.getPersonalMsg().empty() == false)
this->users[pos].setPersonalMsg(u.getPersonalMsg());
saveIntoFile(this->users, "Users.xml");
return false;
}
std::vector<User> ManageUsers::getUsers() const {
return this->users;
}
bool ManageUsers::addContact(std::string owner, std::string login){
int pos = posInVector(owner);
int posContact = posInVector(login);
if (posContact != -1 && posContact != pos) {
std::string local = this->users[pos].getContacts();
if (local.find(login) == std::string::npos){
this->users[pos].setContacts(login, false);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
return false;
}
bool ManageUsers::removeContact(std::string owner, std::string login){
int pos = posInVector(owner);
int posContact = posInVector(login);
if (posContact != -1 && posContact != pos){
std::string local = this->users[pos].getContacts();
if (local.find(login) != std::string::npos){
int start = local.find(login);
local.erase(start, login.size() + 1);
this->users[pos].setContacts(local, true);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
return false;
}
int ManageUsers::posInVector(std::string login){
if (!this->users.empty()){
std::cout << "before deserialization" << std::endl;
getFromFile(this->users, "Users.xml");
std::cout << "after deserialization" << std::endl;
for (int cpt = 0; cpt < this->users.size(); cpt++){
if (this->users[cpt].getLogin() == login)
return cpt;
}
return -1;
}
return -1;
}
| |