-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrievepassword.cpp
274 lines (222 loc) · 10.1 KB
/
retrievepassword.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "retrievePassword.h"
#include "ui_retrievepassword.h"
#include "CryptoUtils.h"
#include "QPushButton"
#include <QFile>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTableWidgetItem>
#include <QStandardPaths>
#include "mainwindow.h" // Include this to access globalCipherKey
#include <QMessageBox>
#include "editpassworddata.h"
// ======================
// Constructor & Destructor
// ======================
retrievePassword::retrievePassword(QWidget *parent) :
QDialog(parent),
ui(new Ui::retrievePassword)
{
ui->setupUi(this);
setWindowTitle("Falkenberg's Password Manager");
// Set up the table widget with 4 columns
ui->tableWidget->setColumnCount(5);
ui->tableWidget->setHorizontalHeaderLabels({"Password ID", "Date", "Copy Password", "Edit", "Delete"});
// Set the width of each column to equally distribute across the 800 width
int columnWidth = 154;
ui->tableWidget->setColumnWidth(0, columnWidth); // Password ID
ui->tableWidget->setColumnWidth(1, columnWidth); // Date
ui->tableWidget->setColumnWidth(2, columnWidth); // Copy Password
ui->tableWidget->setColumnWidth(3, columnWidth); // Edit Button
ui->tableWidget->setColumnWidth(4, columnWidth); // Delete Button
ui->tableWidget->setFocusPolicy(Qt::NoFocus);
loadPasswords();
}
retrievePassword::~retrievePassword()
{
delete ui;
}
// ======================
// Private Methods
// ======================
// Load and decrypt all saved passwords
void retrievePassword::loadPasswords()
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager";
QDir dir(dirPath);
int rowHeight = 50; // Set desired row height
// Iterate over each file in the directory
foreach(QFileInfo item, dir.entryInfoList(QDir::Files)) {
QString filePath = item.absoluteFilePath();
QFile file(filePath);
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file);
QByteArray encryptedData;
in >> encryptedData;
file.close();
// Decrypt the data using the globalCipherKey from MainWindow
QByteArray decryptedData = CryptoUtils::decryptData(encryptedData, globalCipherKey);
QJsonDocument doc = QJsonDocument::fromJson(decryptedData);
if (!doc.isNull()) {
QJsonObject json = doc.object();
QString passId = json["passId"].toString();
QString dateStored = json["dateStored"].toString();
QString password = json["password"].toString();
QString username = json["username"].toString();
QString thoughts = json["thoughts"].toString();
// Add a new row for this password entry
int rowCount = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(rowCount);
// Set Password ID and make it read-only
QTableWidgetItem *idItem = new QTableWidgetItem(passId);
idItem->setFlags(idItem->flags() & ~Qt::ItemIsEditable);
ui->tableWidget->setItem(rowCount, 0, idItem);
// Set Date Stored and make it read-only
QTableWidgetItem *dateItem = new QTableWidgetItem(dateStored);
dateItem->setFlags(dateItem->flags() & ~Qt::ItemIsEditable);
ui->tableWidget->setItem(rowCount, 1, dateItem);
// Add Copy button
QPushButton *copyButton = new QPushButton("Copy Password");
connect(copyButton, &QPushButton::clicked, this, [password]() {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(password); // Copy the generated password to the clipboard
});
ui->tableWidget->setCellWidget(rowCount, 2, copyButton);
// Add Edit button
QPushButton *editButton = new QPushButton("Edit");
connect(editButton, &QPushButton::clicked, this, [this, passId, password, dateStored, username, thoughts, rowCount]() {
this->hide(); // Hide the retrievePassword window
// Create and open the editPasswordData dialog
editDialog = new editPasswordData(this, passId, dateStored, password, username, thoughts);
// Connect the signal to the slot
connect(editDialog, &editPasswordData::overwriteData, this, &retrievePassword::handleOverwriteData);
editDialog->exec(); // Open the dialog modally
this->show();
});
ui->tableWidget->setCellWidget(rowCount, 3, editButton);
// Add Delete button
QPushButton *deleteButton = new QPushButton("Delete");
connect(deleteButton, &QPushButton::clicked, this, [this, passId, rowCount]() {
deletePassword(passId, rowCount);
});
ui->tableWidget->setCellWidget(rowCount, 4, deleteButton);
// Set row height
ui->tableWidget->setRowHeight(rowCount, rowHeight);
}
}
}
}
void retrievePassword::handleOverwriteData(const QString &newPassId, const QString &dateStored, const QString &password, const QString &username, const QString &thoughts)
{
// Retrieve the old pass ID from the selected row
int selectedRow = ui->tableWidget->currentRow();
if (selectedRow != -1) {
QString oldPassId = ui->tableWidget->item(selectedRow, 0)->text();
// Pass the oldPassId and newPassId to the saveUpdatedDataToFile method
saveUpdatedDataToFile(oldPassId, newPassId, password, username, thoughts);
// Optionally reload data
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
loadPasswords();
this->show(); // Show the retrievePassword window again
} else {
qWarning() << "No row selected to update.";
}
}
void retrievePassword::saveUpdatedDataToFile(const QString &oldPassId, const QString &newPassId, const QString &password, const QString &username, const QString &thoughts)
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager/";
QString oldFilePath = dirPath + oldPassId + ".bin";
QString newFilePath = dirPath + newPassId + ".bin";
// Check if the passId has been changed
if (oldPassId != newPassId) {
// Check if the new passId already exists
if (doesPassIdExist(newPassId)) {
qWarning() << "A file with the new Pass ID already exists:" << newFilePath;
return;
}
// Delete the old file
if (!QFile::remove(oldFilePath)) {
qWarning() << "Failed to delete the old file:" << oldFilePath;
return;
} else {
qDebug() << "Successfully deleted the old file:" << oldFilePath;
}
// Create a new file with the new Pass ID
QFile newFile(newFilePath);
if (newFile.open(QIODevice::WriteOnly)) {
QJsonObject json;
json["passId"] = newPassId;
json["password"] = password;
json["username"] = username;
json["thoughts"] = thoughts;
json["dateStored"] = QDate::currentDate().toString(Qt::ISODate);
QJsonDocument doc(json);
QByteArray jsonData = doc.toJson();
QByteArray encryptedData = CryptoUtils::encryptData(jsonData, globalCipherKey);
QDataStream out(&newFile);
out << encryptedData;
newFile.close();
} else {
qWarning() << "Could not open new file for writing:" << newFilePath;
}
} else {
qDebug() << "Pass ID has not changed. Overwriting the existing file.";
// If Pass ID hasn't changed, overwrite the existing file
QFile file(oldFilePath);
if (file.open(QIODevice::WriteOnly)) {
QJsonObject json;
json["passId"] = newPassId;
json["password"] = password;
json["username"] = username;
json["thoughts"] = thoughts;
json["dateStored"] = QDate::currentDate().toString(Qt::ISODate);
QJsonDocument doc(json);
QByteArray jsonData = doc.toJson();
QByteArray encryptedData = CryptoUtils::encryptData(jsonData, globalCipherKey);
QDataStream out(&file);
out << encryptedData;
file.close();
} else {
qWarning() << "Could not open existing file for writing:" << oldFilePath;
}
}
}
bool retrievePassword::doesPassIdExist(const QString &passId)
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager";
QString filePath = dirPath + "/" + passId + ".bin";
QFile file(filePath);
return file.exists();
}
// Delete password entry
void retrievePassword::deletePassword(const QString &passId, int row)
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Delete Confirmation", "Are you sure you want to delete this password entry?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager/" + passId + ".bin";
if (QFile::remove(filePath)) {
ui->tableWidget->removeRow(row); // Remove the row from the table
// Clear and reload the table to ensure consistency
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
loadPasswords();
} else {
qDebug() << "Failed to delete the file: " << filePath;
}
} else {
qDebug() << "Deletion canceled by the user.";
}
}
// ======================
// Slot Implementations
// ======================
void retrievePassword::on_backButton_clicked()
{
emit emitBackClicked(); // Emit the signal to inform the parent dialog
this->close(); // Close the dialog
deleteLater(); // Safely delete the instance
}