-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwordmanagementbuttons.cpp
250 lines (206 loc) · 8.73 KB
/
passwordmanagementbuttons.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
#include "passwordmanagementbuttons.h"
#include "ui_passwordmanagementbuttons.h"
#include "passwordgenerator.h"
#include "resetpassword.h"
#include "storepassword.h"
#include "mainwindow.h" // Include MainWindow to access its methods
#include "retrievepassword.h"
#include <QCloseEvent>
#include <QJsonDocument>
#include <QFileDialog>
#include "cryptoutils.h"
#include <QComboBox>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QThread>
#include <QTimer>
#include "transferdata.h"
// =============================
// Constructor & Destructor
// =============================
passwordManagementButtons::passwordManagementButtons(QWidget *parent)
: QDialog(parent)
, ui(new Ui::passwordManagementButtons)
{
qDebug() << "passwordManagementButtons thread:" << QThread::currentThread();
ui->setupUi(this);
// Create a QComboBox for themes
QComboBox *themeComboBox = new QComboBox(ui->themeWidget);
// Set the size policy to stretch to the full width and height of themeWidget
themeComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// Add the themes to the comboBox
themeComboBox->addItem("Nightshade");
themeComboBox->addItem("Amethyst");
themeComboBox->addItem("Light");
themeComboBox->addItem("Sunset");
setWindowTitle("Falkenberg's Password Manager");
// Set a larger font to make it more prominent
QFont font("Bahnschrift Light", 14); // Adjust the font size here (16 is an example)
themeComboBox->setFont(font);
themeComboBox->setEditable(true);
themeComboBox->setCurrentText("Themes");
themeComboBox->lineEdit()->setAlignment(Qt::AlignCenter);
themeComboBox->lineEdit()->setReadOnly(true);
// Set the themeComboBox to fully cover the themeWidget
QHBoxLayout *themeLayout = new QHBoxLayout(ui->themeWidget);
themeLayout->addWidget(themeComboBox);
themeLayout->setContentsMargins(0, 0, 0, 0); // Remove margins to ensure full coverage
ui->themeWidget->setLayout(themeLayout);
connect(themeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &passwordManagementButtons::onThemeChanged);
connect(themeComboBox, &QComboBox::currentTextChanged,
this, &passwordManagementButtons::onThemeTextChanged);
}
void passwordManagementButtons::onThemeTextChanged(const QString &text)
{
if (text == "Nightshade") {
emit updateTheme(text); // Emit the signal to update the theme
}
}
passwordManagementButtons::~passwordManagementButtons()
{
if (myPassGen) {
disconnect(myPassGen.get(), nullptr, this, nullptr); // Ensure signals are disconnected
}
delete ui;
}
void passwordManagementButtons::onThemeChanged(int index)
{
QStringList themes = {"Nightshade", "Amethyst", "Light", "Sunset"};
if (index >= 0 && index < themes.size()) {
QString selectedTheme = themes.at(index);
qDebug() << selectedTheme + " theme selected";
// Emit the signal to inform MainWindow about the selected theme
emit updateTheme(selectedTheme);
QComboBox *themeComboBox = qobject_cast<QComboBox*>(sender());
if (themeComboBox) {
themeComboBox->setCurrentText(selectedTheme); // Update the combo box text
}
}
}
void passwordManagementButtons::closeEvent(QCloseEvent *event)
{
QCoreApplication::quit(); // Quit the entire application
event->accept(); // Accept the event, allowing the window to close
}
// =======================
// Slot Implementations
// =======================
void passwordManagementButtons::on_genPass_clicked()
{
if (!myPassGen) {
myPassGen = std::make_unique<PasswordGenerator>(this); // Instantiate PasswordGenerator if not already done
}
myPassGen->show(); // Display the password generator
}
void passwordManagementButtons::on_resetLoginButton_clicked()
{
if (!resetPass) {
resetPass = std::make_unique<resetPassword>(this); // Instantiate resetPassword if not already done
// Connect resetPassword's passwordUpdated signal to MainWindow's updatePassword slot
MainWindow* mainWindow = qobject_cast<MainWindow*>(parentWidget());
if (mainWindow) {
bool connection = connect(resetPass.get(), &resetPassword::passwordUpdated, mainWindow, &MainWindow::updatePassword);
qDebug() << (connection ? "Connected resetPassword::passwordUpdated to MainWindow::updatePassword."
: "Failed to connect resetPassword::passwordUpdated to MainWindow::updatePassword.");
} else {
qDebug() << "Failed to cast parentWidget to MainWindow.";
}
// Connect resetPassword's resetCanceled signal to onResetCanceled slot
connect(resetPass.get(), &resetPassword::resetCanceled, this, &passwordManagementButtons::onResetCanceled);
}
this->hide(); // Hide this dialog when the reset dialog is active
resetPass->exec(); // Block until the reset dialog is closed
this->show(); // Show this dialog again after reset
}
void passwordManagementButtons::onResetCanceled()
{
this->show(); // Show the main dialog when reset is canceled
}
void passwordManagementButtons::on_logoutButton_clicked()
{
QCoreApplication::quit(); // Quit the application
}
void passwordManagementButtons::on_storePass_clicked()
{
if (!storePass) {
storePass = std::make_unique<storePassword>(this); // Instantiate storePassword if not already done
// Connect storePassword's emitBackClicked signal to handleBackStorePassword slot
connect(storePass.get(), &storePassword::emitBackClicked, this, &passwordManagementButtons::handleBackStorePassword);
// Connect storePassword's requestGenPassword signal to handleRequestGenPassword slot
connect(storePass.get(), &storePassword::requestGenPassword, this, &passwordManagementButtons::handleRequestGenPassword);
}
this->hide(); // Hide the main dialog when storePassword is shown
storePass->show(); // Display the storePassword dialog
}
void passwordManagementButtons::handleBackStorePassword()
{
this->show(); // Show the main dialog when back is clicked in storePassword
}
void passwordManagementButtons::handleRequestGenPassword()
{
if (!myPassGen) {
myPassGen = std::make_unique<PasswordGenerator>(this); // Instantiate PasswordGenerator if not already done
}
if (!myPassGen->isVisible()) {
myPassGen->show(); // Display the password generator if not already visible
}
}
void passwordManagementButtons::on_retrievePass_clicked()
{
if (retrievePass) {
// If an instance already exists, just show it
retrievePass->show();
this->hide(); // Hide the main dialog
} else {
// Create a new instance of retrievePassword
retrievePass = new retrievePassword(this); // Use QPointer, no need for std::unique_ptr
connect(retrievePass, &retrievePassword::emitBackClicked, this, &passwordManagementButtons::handleBackRetrievePassword);
retrievePass->show();
this->hide(); // Hide the main dialog when retrievePassword is shown
}
}
void passwordManagementButtons::handleBackRetrievePassword()
{
this->show(); // Show the main dialog when back is clicked in retrievePassword
if (retrievePass) {
retrievePass->deleteLater(); // Ensure the instance is properly deleted
retrievePass = nullptr; // Reset the QPointer
}
}
void passwordManagementButtons::on_deleteAllButton_clicked()
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager";
QDir dir(dirPath);
// Confirmation dialog
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Delete Confirmation", "Are you certain you wish to delete all stored password data? This action is irreversible, and all saved passwords will be permanently removed.",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
if (dir.exists()) {
if (dir.removeRecursively()) {
qDebug() << "All data deleted successfully.";
} else {
qDebug() << "Failed to delete the folder.";
}
} else {
qDebug() << "Folder does not exist.";
}
} else {
qDebug() << "Deletion canceled by the user.";
}
}
void passwordManagementButtons::on_exportPassButton_clicked()
{
if(!tranData){
tranData = std::make_unique<transferData>(this);
// Connect back button signal to the handler
connect(tranData.get(), &transferData::backButtonPressed, this, &passwordManagementButtons::handleTransBackPressed);
}
tranData->show();
this->hide();
}
void passwordManagementButtons::handleTransBackPressed(){
tranData->hide();
this->show();
}