-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamanager.cpp
103 lines (98 loc) · 3.24 KB
/
datamanager.cpp
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
#include "datamanager.h"
#include "mainwindow.h"
#include <iostream>
#include <QMessageBox>
#include <QFileInfo>
#include "encryption.h"
#include <QFile>
#include <QTextStream>
#include <QTableWidget>
QString return_line(QString datas, QString name) {
name.append(semiclon);
QFile data(datas);
if(data.open(QIODevice::ReadWrite)) {
QTextStream in (&data);
QString line;
do {
line = in.readLine();
if(line.contains(name, Qt::CaseSensitive)) {
data.close();
return line;
}
} while(!line.isNull());
}
data.close();
return NULL;
}
bool check_if_exist(QString datas, QString name) {
if(return_line(datas, name) != NULL) return true;
return false;
}
bool add_to_file(QString file, QString line) {
QFile data(file);
if(data.open(QIODevice::ReadWrite | QIODevice::Append)) {
QTextStream stream(&data);
stream << line << endl;
stream.flush();
data.flush();
data.close();
return true;
}
else {
return false;
}
}
void load_from_file(QString user, QString pass, QTableWidget *table) {
QFile user_data(data_dir + user + extension);
QStringList encrypted_data;
if (user_data.open(QFile::ReadOnly))
{
QTextStream in (&user_data);
QString data = in.readAll();
encrypted_data = data.split("\n");
user_data.close();
for(int i = 0; i < encrypted_data.size()-1; i++) {
QString decrypted = crypt::decrypt(encrypted_data[i], pass);
QStringList splitted = decrypted.split(semiclon);
if(splitted.size() < 4) {
QMessageBox::critical(table, "Parse data", "Critical error");
exit(EXIT_FAILURE);
}
table->insertRow(table->rowCount());
QTableWidgetItem* data_item = new QTableWidgetItem();
data_item->setText(splitted[0]);
data_item->setData(Qt::UserRole, decrypted);
table->setItem(table->rowCount()-1, 0, data_item);
table->setItem(table->rowCount()-1, 1, new QTableWidgetItem(splitted[1]));
table->setItem(table->rowCount()-1, 2, new QTableWidgetItem(splitted[3]));
}
}
}
bool edit_file(QString user, QString pass, QString original,
QString replace, bool remove) {
QFile original_file(data_dir + user + extension);
QFile temp_file(data_dir + user + extension + ".temp");
original_file.open(QIODevice::ReadOnly | QIODevice::Text);
temp_file.open(QIODevice::WriteOnly | QIODevice::Text);
if(original_file.isOpen() && temp_file.isOpen()) {
QTextStream org(&original_file);
QTextStream tmp(&temp_file);
while(!org.atEnd()) {
QString actual_line = org.readLine();
if(crypt::decrypt(actual_line, pass) == original) {
if(!remove) {
tmp << crypt::encrypt(replace, pass) << endl;
}
}
else {
tmp << actual_line << endl;
}
}
original_file.close();
temp_file.close();
original_file.remove();
temp_file.rename(data_dir + user + extension);
return true;
}
return false;
}